Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     ' Macro for localizing the columns of a grid.
  2.    ' Input Selection : The "<COLUMNS>....</COLUMNS>" part of the grid.
  3.    ' Output -  There are 2 sections in the output. The first section should be
  4.    '           used to replace the part selected in the input. The second section
  5.    '           is an XML to be copied to the local resource file.
  6.    Sub GenerateGridColumnHeadingResource()
  7.         GenerateGridColumnHeadingResource_Helper(String.Empty)
  8.     End Sub
  9.  
  10.     Sub GenerateGridColumnHeadingResourceWithPrefix()
  11.         Dim resourcePrefix As String = InputBox("Enter Resource Prefix")
  12.         GenerateGridColumnHeadingResource_Helper(resourcePrefix)
  13.     End Sub
  14.  
  15.     Sub GenerateGridColumnHeadingResource_Helper(ByVal resourcePrefix As String)
  16.         DTE.UndoContext.Open("MakeGridColumnHeadingResource")
  17.  
  18.         Dim selectedText As TextSelection
  19.         selectedText = DTE.ActiveDocument.Selection
  20.  
  21.         Dim line, originalCode As String
  22.         originalCode = selectedText.Text
  23.  
  24.         Dim lines() As String
  25.         lines = Split(originalCode, vbLf)
  26.  
  27.         Dim heading As String
  28.  
  29.         Dim regResourceName As New System.Text.RegularExpressions.Regex("(\s|(<BR>)|('s)|/|&)*", Text.RegularExpressions.RegexOptions.Compiled)
  30.         Dim regHeading As New System.Text.RegularExpressions.Regex("(?<pre>headertext\s*=\s*"")(?<heading>[^""]*)""", Text.RegularExpressions.RegexOptions.Compiled)
  31.  
  32.         Dim regValueAmp As New System.Text.RegularExpressions.Regex("&", Text.RegularExpressions.RegexOptions.Compiled)
  33.         Dim regValueLT As New System.Text.RegularExpressions.Regex("<", Text.RegularExpressions.RegexOptions.Compiled)
  34.         Dim regValueGT As New System.Text.RegularExpressions.Regex(">", Text.RegularExpressions.RegexOptions.Compiled)
  35.  
  36.         Dim xmlOutFormat As String = "<data name=""{0}"" xml:space=""preserve"">" _
  37.                 & vbCrLf & vbTab & "<value>{1}</value>" _
  38.                 & vbCrLf & "</data>"
  39.  
  40.         Dim resAssignFormat As String = "<%$ Resources:{0}{1}Columns_{2} %>"
  41.         Dim resNameFormat As String = "{0}{1}Columns_{2}"
  42.  
  43.         Dim markupOutput As String = ""
  44.         Dim resourceOutput As String = ""
  45.  
  46.         For Each line In lines
  47.             If line.Length > 0 Then
  48.                 Dim mtch As System.Text.RegularExpressions.Match
  49.                 mtch = regHeading.Match(line)
  50.  
  51.                 If mtch.Success Then
  52.                     heading = mtch.Groups("heading").Value.Trim
  53.                     Dim resName As String = regResourceName.Replace(heading, "")
  54.                     Dim fullResName As String = String.Format(resNameFormat, resourcePrefix, IIf(resourcePrefix.Length > 0, "_", ""), resName)
  55.                     Dim assignResource As String = String.Format(resAssignFormat, resourcePrefix, IIf(resourcePrefix.Length > 0, "_", ""), resName)
  56.  
  57.                     Dim resValue As String = regValueAmp.Replace(heading, "&")
  58.                     resValue = regValueLT.Replace(resValue, "<")
  59.                     resValue = regValueGT.Replace(resValue, ">")
  60.  
  61.                     line = regHeading.Replace(line, "${pre}" & assignResource & """")
  62.  
  63.                     resourceOutput = resourceOutput & vbCrLf & String.Format(xmlOutFormat, fullResName, resValue)
  64.  
  65.                 End If
  66.  
  67.                 markupOutput = markupOutput & line
  68.             End If
  69.         Next
  70.  
  71.         ReplaceSelectedtextWith(selectedText, markupOutput)
  72.         AddToResourceFile(resourceOutput)
  73.  
  74.         DTE.UndoContext.Close()
  75.  
  76.     End Sub
  77.  
  78.     Private Sub ReplaceSelectedtextWith(ByVal selectedText As TextSelection, ByVal replacementText As String)
  79.         Dim startPoint As EnvDTE.EditPoint
  80.         startPoint = selectedText.TopPoint.CreateEditPoint()
  81.         selectedText.Delete()
  82.         startPoint.Insert(replacementText)
  83.  
  84.     End Sub
  85.     Private Sub AddToResourceFile(ByVal resourceData As String)
  86.         Dim targetResxDocument As EnvDTE.Document = GetTargetResourceDocument()
  87.         Dim textEditor As TextDocument = targetResxDocument.Object("TextDocument")
  88.         Dim editPoint As EnvDTE.EditPoint = textEditor.CreateEditPoint(textEditor.EndPoint) 'should skip to the end of the document at the closing '</root>' element
  89.        editPoint.LineUp(1) 'jump up one line from the </root>
  90.        editPoint.EndOfLine() 'go the end of the line
  91.        editPoint.Insert(Environment.NewLine)
  92.         editPoint.Insert(resourceData)
  93.         targetResxDocument.Save()
  94.  
  95.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement