Source code of Windows XP (NT5)
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.

418 lines
13 KiB

  1. VERSION 1.0 CLASS
  2. BEGIN
  3. MultiUse = -1 'True
  4. Persistable = 0 'NotPersistable
  5. DataBindingBehavior = 0 'vbNone
  6. DataSourceBehavior = 0 'vbNone
  7. MTSTransactionMode = 0 'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "HssExt"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. Private m_oFs As Scripting.FileSystemObject
  16. Private m_oDocNode As IXMLDOMNode
  17. Private m_ocollValidSkus As Scripting.Dictionary
  18. Private m_iValidFlag As Integer
  19. Const V_DISPLAY_NAME As Integer = 2 ^ 0
  20. Const V_APPLICABLE_SKUS As Integer = 2 ^ 1
  21. Const V_EXECUTABLE_NAME As Integer = 2 ^ 2
  22. Const V_OWNER As Integer = 2 ^ 3
  23. Const V_EXTENSION_FOLDER As Integer = 2 ^ 4
  24. Const V_VALID_EXTENSION As Integer = (V_DISPLAY_NAME Or _
  25. V_APPLICABLE_SKUS Or _
  26. V_EXECUTABLE_NAME Or _
  27. V_OWNER Or _
  28. V_EXTENSION_FOLDER)
  29. Private Sub Class_Initialize()
  30. Set m_oFs = New Scripting.FileSystemObject
  31. Dim oDom As DOMDocument: Set oDom = New DOMDocument
  32. Dim oElem As IXMLDOMElement, oNode As IXMLDOMNode
  33. Set oElem = oDom.createElement("hss-tools-extension")
  34. Set m_oDocNode = oDom.appendChild(oElem)
  35. Set m_ocollValidSkus = New Scripting.Dictionary
  36. m_ocollValidSkus.Add "STD", True ' 0
  37. m_ocollValidSkus.Add "PRO", True ' 1
  38. m_ocollValidSkus.Add "SRV", True ' 2
  39. m_ocollValidSkus.Add "ADV", True ' 3
  40. m_ocollValidSkus.Add "DAT", True ' 4
  41. m_ocollValidSkus.Add "PRO64", True ' 5
  42. m_ocollValidSkus.Add "ADV64", True ' 6
  43. m_ocollValidSkus.Add "DAT64", True ' 7
  44. m_ocollValidSkus.Add "WINME", True ' 8
  45. End Sub
  46. Function IsValid(Optional ByRef strMsg As String) As Boolean
  47. IsValid = ((m_iValidFlag And V_VALID_EXTENSION) = V_VALID_EXTENSION)
  48. If (IsValid) Then
  49. strMsg = "HSS Extension is valid"
  50. Else
  51. strMsg = "HSS Extension information is invalid for the following items:" + vbCrLf + vbCrLf
  52. If ((m_iValidFlag And V_DISPLAY_NAME) <> V_DISPLAY_NAME) Then
  53. strMsg = strMsg + "Display Name" + vbCrLf
  54. End If
  55. If ((m_iValidFlag And V_APPLICABLE_SKUS) <> V_APPLICABLE_SKUS) Then
  56. strMsg = strMsg + "Applicable skus" + vbCrLf
  57. End If
  58. If ((m_iValidFlag And V_EXECUTABLE_NAME) <> V_EXECUTABLE_NAME) Then
  59. strMsg = strMsg + "Extension executable name" + vbCrLf
  60. End If
  61. If ((m_iValidFlag And V_OWNER) <> V_OWNER) Then
  62. strMsg = strMsg + "Owner name" + vbCrLf
  63. End If
  64. If ((m_iValidFlag And V_EXTENSION_FOLDER) <> V_EXTENSION_FOLDER) Then
  65. strMsg = strMsg + "Extension folder" + vbCrLf
  66. End If
  67. End If
  68. End Function
  69. Function InitFromDisk(ByVal strExtPath As String) As DOMDocument
  70. Set InitFromDisk = Nothing
  71. strExtPath = Trim$(strExtPath)
  72. If (Len(strExtPath) = 0) Then GoTo Common_Exit
  73. If (Not (m_oFs.FileExists(strExtPath))) Then GoTo Common_Exit
  74. Dim oDomExt As DOMDocument: Set oDomExt = New DOMDocument
  75. oDomExt.async = False
  76. oDomExt.Load (strExtPath)
  77. If (oDomExt.parseError <> 0) Then GoTo Common_Exit
  78. Dim oEl2 As IXMLDOMElement
  79. ' Set oEl2 = oDomExt.selectSingleNode("hss-tools-extension/executable-name")
  80. ' oEl2.Text = oSubF.Path + "\" + oEl2.Text
  81. ' Now we need to recreate the in-core only information of the Extension.
  82. Set oEl2 = oDomExt.createElement("extension-folder")
  83. oEl2.Text = m_oFs.GetParentFolderName(strExtPath)
  84. oDomExt.documentElement.appendChild oEl2
  85. Set oEl2 = oDomExt.createElement("run-this-extension")
  86. oEl2.Text = "no"
  87. oDomExt.documentElement.appendChild oEl2
  88. Set m_oDocNode = oDomExt.documentElement
  89. Set InitFromDisk = oDomExt
  90. Common_Exit:
  91. Exit Function
  92. End Function
  93. Function SaveToDisk(Optional strDestFolder As String = "") As Boolean
  94. SaveToDisk = False
  95. Dim strMsg As String
  96. If (Not IsValid(strMsg)) Then
  97. Err.Raise vbObjectError + 9999, "HssExt::SaveTodisk", _
  98. strMsg
  99. End If
  100. If (Len(strDestFolder) <> 0 And (strDestFolder <> ExtensionFolder)) Then
  101. If (Not m_oFs.FolderExists(strDestFolder)) Then
  102. m_oFs.CreateFolder strDestFolder
  103. Else
  104. m_oFs.DeleteFolder strDestFolder, True
  105. End If
  106. ' We first need to copy the Extension to the Extension Folder
  107. m_oFs.CopyFolder ExtensionFolder, strDestFolder, True
  108. ExtensionFolder = strDestFolder
  109. End If
  110. PersistableExtensionDom.save ExtensionFolder + "\ExtensionDescription.xml"
  111. SaveToDisk = True
  112. Common_Exit:
  113. Exit Function
  114. End Function
  115. Private Function PersistableExtensionDom() As DOMDocument
  116. Set PersistableExtensionDom = Nothing
  117. ' We need to filter out al the In-Core only information
  118. ' then we have a Disk Good Image.
  119. Dim oDom As DOMDocument: Set oDom = New DOMDocument
  120. oDom.loadXML m_oDocNode.ownerDocument.xml
  121. If (oDom.parseError <> 0) Then
  122. Err.Raise vbObjectError + 9999, "HssExt::PersistableExtensionDom", _
  123. "Unexpected parsing error while creating Persistable DOM Extension Image"
  124. End If
  125. ' This are the in-core items we want to filter.
  126. Dim oNode As IXMLDOMNode, oDocEl As IXMLDOMNode
  127. Set oDocEl = oDom.documentElement
  128. Set oNode = oDocEl.selectSingleNode("run-this-extension")
  129. If (Not oNode Is Nothing) Then oDom.removeChild oNode
  130. Set oNode = oDocEl.selectSingleNode("extension-folder")
  131. If (Not oNode Is Nothing) Then oDocEl.removeChild oNode
  132. Set PersistableExtensionDom = oDom
  133. End Function
  134. ' This function Creates an Extension that is Good for
  135. ' saving in the root directory of the Extension itself
  136. ' This means that all elements/attributes that live
  137. ' only in-memory or in the Summary ExtensionsList are
  138. ' not created here. Those should be set upon extension
  139. ' discovery.
  140. Function CreateExtension(ByVal strDisplayName As String, _
  141. ByVal strComment As String, _
  142. ByVal strOwner As String, _
  143. ByVal strExecutable As String, _
  144. ByVal bModifiesCab As Boolean, _
  145. ByRef ocollSkuList As Scripting.Dictionary _
  146. ) As IXMLDOMNode
  147. Set CreateExtension = Nothing
  148. If (Not m_oDocNode.childNodes Is Nothing) Then
  149. Err.Raise vbObjectError + 9999, _
  150. "HssExt::CreateExtension", _
  151. "This function can only be called as a Constructor"
  152. End If
  153. DisplayName = strDisplayName
  154. Comment = strComment
  155. ExecutableName = strExecutable
  156. ModifiesCab = bModifiesCab
  157. ApplicableSkus = ocollSkuList
  158. End Function
  159. ' ============= Properties ==================
  160. Private Function GetSimpleElement( _
  161. ByVal strElement As String, _
  162. Optional ByRef oNode As IXMLDOMNode _
  163. ) As String
  164. Set oNode = m_oDocNode.selectSingleNode(strElement)
  165. If (oNode Is Nothing) Then
  166. GetSimpleElement = ""
  167. Else
  168. GetSimpleElement = oNode.Text
  169. End If
  170. End Function
  171. Private Sub SetSimpleElement( _
  172. ByVal strElement As String, _
  173. strNewValue As String _
  174. )
  175. Dim oEl As IXMLDOMElement
  176. GetSimpleElement strElement, oEl
  177. If (oEl Is Nothing) Then
  178. Set oEl = m_oDocNode.ownerDocument.createElement(strElement)
  179. m_oDocNode.appendChild oEl
  180. End If
  181. oEl.Text = strNewValue
  182. End Sub
  183. Public Property Get DisplayName() As String
  184. DisplayName = GetSimpleElement("display-name")
  185. End Property
  186. Public Property Let DisplayName(ByVal strNewValue As String)
  187. strNewValue = Trim$(strNewValue)
  188. If (Len(strNewValue) = 0) Then
  189. Err.Raise vbObjectError + 9999, _
  190. "HssExt::Let DisplayName", _
  191. "Display Name must contain something"
  192. End If
  193. SetSimpleElement "display-name", strNewValue
  194. m_iValidFlag = (m_iValidFlag Or V_DISPLAY_NAME)
  195. End Property
  196. Public Property Get Comment() As String
  197. Comment = GetSimpleElement("comment")
  198. End Property
  199. Public Property Let Comment(ByVal strNewValue As String)
  200. strNewValue = Trim$(strNewValue)
  201. If (Len(strNewValue) = 0) Then GoTo Common_Exit
  202. SetSimpleElement "comment", strNewValue
  203. Common_Exit:
  204. Exit Property
  205. End Property
  206. Public Property Get ExecutableName() As String
  207. ExecutableName = GetSimpleElement("executable-name")
  208. End Property
  209. Public Property Let ExecutableName(ByVal strNewValue As String)
  210. strNewValue = Trim$(strNewValue)
  211. If ((Len(strNewValue) = 0) Or _
  212. (Not m_oFs.FileExists(ExtensionFolder + "\" + strNewValue)) Or _
  213. (Not IsExecutableExtension(strNewValue))) Then
  214. Err.Raise vbObjectError + 9999, _
  215. "HssExt::Let ExecutableName", _
  216. "Executable Name must contain a valid executable file"
  217. End If
  218. SetSimpleElement "executable-name", strNewValue
  219. m_iValidFlag = (m_iValidFlag Or V_EXECUTABLE_NAME)
  220. End Property
  221. Public Property Get ExtensionFolder() As String
  222. ExtensionFolder = GetSimpleElement("extension-folder")
  223. End Property
  224. Public Property Let ExtensionFolder(ByVal strNewValue As String)
  225. strNewValue = Trim$(strNewValue)
  226. If ((Len(strNewValue) = 0) Or (Not m_oFs.FolderExists(strNewValue))) Then
  227. Err.Raise vbObjectError + 9999, _
  228. "HssExt::Let ExtensionFolder", _
  229. "Extension Folder must contain a valid and accessible Folder"
  230. End If
  231. SetSimpleElement "extension-folder", strNewValue
  232. m_iValidFlag = (m_iValidFlag Or V_EXTENSION_FOLDER)
  233. End Property
  234. 'Public Property Get CopyFromFolder() As String
  235. ' CopyFromFolder = GetSimpleElement("copy-from-folder")
  236. 'End Property
  237. '
  238. 'Public Property Let CopyFromFolder(ByVal strNewValue As String)
  239. ' strNewValue = Trim$(strNewValue)
  240. ' If ((Len(strNewValue) = 0) Or (Not m_oFs.FolderExists(strNewValue))) Then
  241. ' Err.Raise vbObjectError + 9999, _
  242. ' "HssExt::Let CopyFromFolder", _
  243. ' "Copy From Folder Folder must contain a valid and accessible Folder"
  244. ' End If
  245. '
  246. ' SetSimpleElement "copy-from-folder", strNewValue
  247. ' ' m_iValidFlag = (m_iValidFlag Or V_EXTENSION_FOLDER)
  248. '
  249. 'End Property
  250. Public Property Get Owner() As String
  251. Owner = GetSimpleElement("owner")
  252. End Property
  253. Public Property Let Owner(ByVal strNewValue As String)
  254. strNewValue = Trim$(strNewValue)
  255. If (Len(strNewValue) = 0) Then
  256. Err.Raise vbObjectError + 9999, _
  257. "HssExt::Let Owner", _
  258. "Owner Name must contain a valid Name for OEM"
  259. End If
  260. SetSimpleElement "owner", strNewValue
  261. m_iValidFlag = (m_iValidFlag Or V_OWNER)
  262. End Property
  263. Public Property Get ModifiesCab() As Boolean
  264. If (GetSimpleElement("modifies-cab") = "yes") Then
  265. ModifiesCab = True
  266. Else
  267. ModifiesCab = False
  268. End If
  269. End Property
  270. Public Property Let ModifiesCab(ByVal bNewValue As Boolean)
  271. SetSimpleElement "modifies-cab", IIf(bNewValue, "yes", "no")
  272. End Property
  273. Public Property Get RunThisExtension() As Boolean
  274. If (GetSimpleElement("run-this-extension") = "yes") Then
  275. RunThisExtension = True
  276. Else
  277. RunThisExtension = False
  278. End If
  279. End Property
  280. Public Property Let RunThisExtension(ByVal bNewValue As Boolean)
  281. SetSimpleElement "run-this-extension", IIf(bNewValue, "yes", "no")
  282. End Property
  283. Public Property Let ApplicableSkus(ByRef oCollSkus As Scripting.Dictionary)
  284. If (oCollSkus Is Nothing) Then GoTo Error_NoSku
  285. If (oCollSkus.Count = 0) Then
  286. Error_NoSku:
  287. Err.Raise vbObjectError + 9999, _
  288. "HssExt Let ApplicableSkus", _
  289. "You must include at least one SKU"
  290. End If
  291. Dim oDom As DOMDocument: Set oDom = m_oDocNode.ownerDocument
  292. Dim oElem As IXMLDOMElement, oNode As IXMLDOMNode
  293. Dim oDomFrag As IXMLDOMDocumentFragment
  294. Set oDomFrag = oDom.createDocumentFragment
  295. Set oElem = oDom.createElement("applicable-skus")
  296. Set oNode = oDomFrag.appendChild(oElem)
  297. Dim vSku As Variant
  298. For Each vSku In oCollSkus.Keys
  299. If (Not IsValidSku(vSku)) Then
  300. Err.Raise vbObjectError + 9999, _
  301. "HssExt Let ApplicableSkus", _
  302. "Sku Value " + vSku + " is not a valid SKU Value"
  303. End If
  304. Set oElem = oDom.createElement("sku")
  305. oElem.Text = vSku
  306. oNode.appendChild oElem
  307. Next
  308. Dim oOldApplicableSkus As IXMLDOMNode
  309. Set oOldApplicableSkus = m_oDocNode.selectSingleNode("applicable-skus")
  310. If (Not oOldApplicableSkus Is Nothing) Then
  311. m_oDocNode.removeChild oOldApplicableSkus
  312. End If
  313. m_oDocNode.appendChild oDomFrag
  314. m_iValidFlag = (m_iValidFlag Or V_APPLICABLE_SKUS)
  315. End Property
  316. Public Property Get ApplicableSkus() As Scripting.Dictionary
  317. Dim oNodeList As IXMLDOMNodeList
  318. Set oNodeList = m_oDocNode.selectNodes("applicable-skus/sku")
  319. If (oNodeList Is Nothing) Then GoTo Common_Exit
  320. Dim oNode As IXMLDOMNode, strSku As String
  321. For Each oNode In oNodeList
  322. strSku = oNode.Text
  323. If (ApplicableSkus.Exists(strSku)) Then
  324. ApplicableSkus.Add strSku, strSku
  325. End If
  326. Next
  327. Common_Exit:
  328. Exit Property
  329. End Property
  330. Private Function IsExecutableExtension(ByVal strExe As String) As Boolean
  331. IsExecutableExtension = False
  332. Select Case UCase$(m_oFs.GetExtensionName(strExe))
  333. Case "EXE", "VBS", "JS", "BAT", "PL"
  334. IsExecutableExtension = True
  335. End Select
  336. End Function
  337. Function IsValidSku(ByVal strSku As String) As Boolean
  338. IsValidSku = m_ocollValidSkus.Exists(strSku)
  339. End Function