Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
6.5 KiB

  1. Attribute VB_Name = "XML"
  2. Option Explicit
  3. Public Function XMLFindFirstNode( _
  4. ByVal i_DOMNode As MSXML2.IXMLDOMNode, _
  5. ByVal i_strName As String _
  6. ) As MSXML2.IXMLDOMNode
  7. Dim DOMNode As MSXML2.IXMLDOMNode
  8. Set XMLFindFirstNode = Nothing
  9. If (i_DOMNode.nodeName = i_strName) Then
  10. Set XMLFindFirstNode = i_DOMNode
  11. Exit Function
  12. ElseIf (Not (i_DOMNode.firstChild Is Nothing)) Then
  13. For Each DOMNode In i_DOMNode.childNodes
  14. Set XMLFindFirstNode = XMLFindFirstNode(DOMNode, i_strName)
  15. If (Not (XMLFindFirstNode Is Nothing)) Then
  16. Exit Function
  17. End If
  18. Next
  19. End If
  20. End Function
  21. Public Function XMLGetAttribute( _
  22. ByVal i_DOMNode As MSXML2.IXMLDOMNode, _
  23. ByVal i_strAttributeName As String _
  24. ) As String
  25. On Error Resume Next
  26. XMLGetAttribute = i_DOMNode.Attributes.getNamedItem(i_strAttributeName).nodeValue
  27. End Function
  28. Public Sub XMLSetAttribute( _
  29. ByVal u_DOMNode As MSXML2.IXMLDOMNode, _
  30. ByVal i_strAttributeName As String, _
  31. ByVal i_strAttributeValue As String _
  32. )
  33. Dim Element As MSXML2.IXMLDOMElement
  34. Set Element = u_DOMNode
  35. Element.setAttribute i_strAttributeName, i_strAttributeValue
  36. End Sub
  37. Public Function XMLCreateChildElement( _
  38. ByVal u_DOMNodeParent As MSXML2.IXMLDOMNode, _
  39. ByVal i_strElementName As String, _
  40. ByVal i_strText As String, _
  41. ByVal i_blnNameValuePairsExist As Boolean, _
  42. ByRef i_arrNameValuePairs() As String _
  43. ) As MSXML2.IXMLDOMNode
  44. Dim DOMDoc As MSXML2.DOMDocument
  45. Dim Element As MSXML2.IXMLDOMElement
  46. Dim intIndex As Long
  47. Set DOMDoc = u_DOMNodeParent.ownerDocument
  48. Set Element = DOMDoc.createElement(i_strElementName)
  49. If (i_strText <> "") Then
  50. Element.Text = i_strText
  51. End If
  52. Set XMLCreateChildElement = u_DOMNodeParent.appendChild(Element)
  53. If (i_blnNameValuePairsExist) Then
  54. For intIndex = LBound(i_arrNameValuePairs) To UBound(i_arrNameValuePairs)
  55. Element.setAttribute i_arrNameValuePairs(intIndex, 0), _
  56. i_arrNameValuePairs(intIndex, 1)
  57. Next
  58. End If
  59. End Function
  60. Public Sub XMLCopyAttributes( _
  61. ByRef i_DOMNodeSrc As MSXML2.IXMLDOMNode, _
  62. ByRef u_DOMNodeDest As MSXML2.IXMLDOMNode _
  63. )
  64. Dim Attr As MSXML2.IXMLDOMAttribute
  65. Dim Element As MSXML2.IXMLDOMElement
  66. Set Element = u_DOMNodeDest
  67. For Each Attr In i_DOMNodeSrc.Attributes
  68. Element.setAttribute Attr.Name, Attr.Value
  69. Next
  70. End Sub
  71. Public Function XMLCopyDOMTree( _
  72. ByRef i_DOMNodeSrc As MSXML2.IXMLDOMNode, _
  73. ByRef u_DOMNodeParent As MSXML2.IXMLDOMNode _
  74. ) As MSXML2.IXMLDOMNode
  75. Dim DOMNode As MSXML2.IXMLDOMNode
  76. Dim DOMElement As MSXML2.IXMLDOMElement
  77. Dim DOMText As MSXML2.IXMLDOMText
  78. Dim DOMAttr As MSXML2.IXMLDOMAttribute
  79. Dim DOMNodeChild As MSXML2.IXMLDOMNode
  80. If (i_DOMNodeSrc.ownerDocument Is u_DOMNodeParent.ownerDocument) Then
  81. Set DOMNode = i_DOMNodeSrc.cloneNode(True)
  82. u_DOMNodeParent.appendChild DOMNode
  83. Else
  84. ' Different DOM Nodes, so we really have to copy and
  85. ' recreate the node from one DOM Tree to another.
  86. Select Case i_DOMNodeSrc.nodeType
  87. Case NODE_TEXT
  88. Set DOMText = u_DOMNodeParent.ownerDocument.createTextNode(i_DOMNodeSrc.Text)
  89. Set DOMNode = u_DOMNodeParent.appendChild(DOMText)
  90. Case Else
  91. Set DOMElement = u_DOMNodeParent.ownerDocument.createElement(i_DOMNodeSrc.nodeName)
  92. Set DOMNode = u_DOMNodeParent.appendChild(DOMElement)
  93. For Each DOMAttr In i_DOMNodeSrc.Attributes
  94. DOMElement.setAttribute DOMAttr.nodeName, DOMAttr.nodeValue
  95. Next
  96. For Each DOMNodeChild In i_DOMNodeSrc.childNodes
  97. XMLCopyDOMTree DOMNodeChild, DOMNode
  98. Next
  99. End Select
  100. End If
  101. Set XMLCopyDOMTree = DOMNode
  102. End Function
  103. Private Function p_XMLValidChar( _
  104. ByRef i_char As String _
  105. ) As Boolean
  106. Dim intAscW As Long
  107. intAscW = AscW(i_char)
  108. ' Sometimes AscW returns a negative number. Eg 0x8021 -> 0xFFFF8021
  109. intAscW = intAscW And &HFFFF&
  110. Select Case intAscW
  111. Case &H9&, &HA&, &HD&, &H20& To &HD7FF&, &HE000& To &HFFFD&
  112. p_XMLValidChar = True
  113. Case Else
  114. p_XMLValidChar = False
  115. End Select
  116. End Function
  117. Public Function XMLValidString( _
  118. ByRef i_str As String _
  119. ) As Boolean
  120. Dim intIndex As Long
  121. Dim intLength As Long
  122. intLength = Len(i_str)
  123. For intIndex = 1 To intLength
  124. If (Not p_XMLValidChar(Mid$(i_str, intIndex, 1))) Then
  125. XMLValidString = False
  126. Exit Function
  127. End If
  128. Next
  129. XMLValidString = True
  130. End Function
  131. Public Function XMLMakeValidString( _
  132. ByRef i_str As String _
  133. ) As String
  134. Dim intIndex As Long
  135. Dim intLength As Long
  136. Dim str As String
  137. XMLMakeValidString = i_str
  138. intLength = Len(i_str)
  139. For intIndex = 1 To intLength
  140. If (Not p_XMLValidChar(Mid$(i_str, intIndex, 1))) Then
  141. XMLMakeValidString = Mid$(XMLMakeValidString, 1, intIndex - 1) & " " & _
  142. Mid$(XMLMakeValidString, intIndex + 1)
  143. End If
  144. Next
  145. End Function
  146. Public Function XMLEscape( _
  147. ByVal i_str As String _
  148. ) As String
  149. XMLEscape = XMLMakeValidString(i_str)
  150. XMLEscape = Replace$(XMLEscape, "&", "&amp;")
  151. XMLEscape = Replace$(XMLEscape, "<", "&lt;")
  152. XMLEscape = Replace$(XMLEscape, ">", "&gt;")
  153. XMLEscape = Replace$(XMLEscape, "'", "&apos;")
  154. XMLEscape = Replace$(XMLEscape, """", "&quot;")
  155. End Function
  156. Public Function XMLUnEscape( _
  157. ByVal i_str As String _
  158. ) As String
  159. XMLUnEscape = i_str
  160. XMLUnEscape = Replace$(XMLUnEscape, "&quot;", """")
  161. XMLUnEscape = Replace$(XMLUnEscape, "&apos;", "'")
  162. XMLUnEscape = Replace$(XMLUnEscape, "&gt;", ">")
  163. XMLUnEscape = Replace$(XMLUnEscape, "&lt;", "<")
  164. XMLUnEscape = Replace$(XMLUnEscape, "&amp;", "&")
  165. End Function
  166. Public Function XMLSpecialCharacter( _
  167. ByVal i_chr As String _
  168. ) As Boolean
  169. Select Case i_chr
  170. Case """", "'", ">", "<", "&"
  171. XMLSpecialCharacter = True
  172. Case Else
  173. XMLSpecialCharacter = False
  174. End Select
  175. End Function