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.

343 lines
7.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: U H X M L . C P P
  7. //
  8. // Contents: Helper routines for the XML DOM
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 8 Oct 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include <msxml2.h>
  18. #include "uhxml.h"
  19. #include "uhcommon.h"
  20. #include "upnphost.h"
  21. HRESULT HrLoadDocument(BSTR bstrTemplate, IXMLDOMDocumentPtr & pDoc)
  22. {
  23. HRESULT hr = S_OK;
  24. hr = pDoc.HrCreateInstanceInproc(CLSID_DOMDocument30);
  25. if(SUCCEEDED(hr))
  26. {
  27. VARIANT_BOOL vb;
  28. pDoc->put_resolveExternals(VARIANT_FALSE);
  29. hr = pDoc->loadXML(bstrTemplate, &vb);
  30. if(SUCCEEDED(hr) && !vb)
  31. {
  32. hr = UPNP_E_INVALID_XML;
  33. }
  34. }
  35. TraceHr(ttidError, FAL, hr, FALSE, "HrLoadDocument");
  36. return hr;
  37. }
  38. HRESULT HrLoadDocumentFromFile(BSTR bstrUrl, IXMLDOMDocumentPtr & pDoc)
  39. {
  40. HRESULT hr = S_OK;
  41. hr = pDoc.HrCreateInstanceInproc(CLSID_DOMDocument30);
  42. if(SUCCEEDED(hr))
  43. {
  44. VARIANT_BOOL vb;
  45. VARIANT varPath;
  46. V_VT(&varPath) = VT_BSTR;
  47. V_BSTR(&varPath) = bstrUrl;
  48. hr = pDoc->load(varPath, &vb);
  49. if(SUCCEEDED(hr) && !vb)
  50. {
  51. // Map variant bool error indicator to general COM failure
  52. hr = E_FAIL;
  53. }
  54. }
  55. TraceHr(ttidError, FAL, hr, FALSE, "HrLoadDocumentFromFile");
  56. return hr;
  57. }
  58. HRESULT HrSelectNodes(
  59. const wchar_t * szPattern,
  60. IXMLDOMNodePtr & pNode,
  61. IXMLDOMNodeListPtr & pNodeList)
  62. {
  63. HRESULT hr = S_OK;
  64. // Do BSTR conversion
  65. BSTR bstr = NULL;
  66. hr = HrSysAllocString(szPattern, &bstr);
  67. if(SUCCEEDED(hr))
  68. {
  69. pNodeList.Release();
  70. hr = pNode->selectNodes(bstr, pNodeList.AddressOf());
  71. if(S_FALSE == hr)
  72. {
  73. hr = E_FAIL;
  74. }
  75. SysFreeString(bstr);
  76. }
  77. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrSelectNodes");
  78. return hr;
  79. }
  80. HRESULT HrSelectNode(
  81. const wchar_t * szPattern,
  82. IXMLDOMNodePtr & pNode,
  83. IXMLDOMNodePtr & pNodeMatch)
  84. {
  85. HRESULT hr = S_OK;
  86. // Do BSTR conversion
  87. BSTR bstr = NULL;
  88. hr = HrSysAllocString(szPattern, &bstr);
  89. if(SUCCEEDED(hr))
  90. {
  91. pNodeMatch.Release();
  92. hr = pNode->selectSingleNode(bstr, pNodeMatch.AddressOf());
  93. if(S_FALSE == hr)
  94. {
  95. hr = E_FAIL;
  96. }
  97. SysFreeString(bstr);
  98. }
  99. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrSelectNode");
  100. return hr;
  101. }
  102. HRESULT HrSelectNodeText(
  103. const wchar_t * szPattern,
  104. IXMLDOMNodePtr & pNode,
  105. CUString & strText)
  106. {
  107. HRESULT hr = S_OK;
  108. IXMLDOMNodePtr pNodeMatch;
  109. hr = HrSelectNode(szPattern, pNode, pNodeMatch);
  110. if(SUCCEEDED(hr))
  111. {
  112. BSTR bstr = NULL;
  113. hr = pNodeMatch->get_text(&bstr);
  114. if(SUCCEEDED(hr))
  115. {
  116. hr = strText.HrAssign(bstr);
  117. SysFreeString(bstr);
  118. }
  119. }
  120. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrSelectNodeText");
  121. return hr;
  122. }
  123. HRESULT HrSelectAndSetNodeText(
  124. const wchar_t * szPattern,
  125. IXMLDOMNodePtr & pNode,
  126. const CUString & strText)
  127. {
  128. HRESULT hr = S_OK;
  129. IXMLDOMNodePtr pNodeMatch;
  130. hr = HrSelectNode(szPattern, pNode, pNodeMatch);
  131. if(SUCCEEDED(hr))
  132. {
  133. BSTR bstr = NULL;
  134. hr = strText.HrGetBSTR(&bstr);
  135. if(SUCCEEDED(hr))
  136. {
  137. hr = pNodeMatch->put_text(bstr);
  138. SysFreeString(bstr);
  139. }
  140. }
  141. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrSelectAndSetNodeText");
  142. return hr;
  143. }
  144. HRESULT HrGetNodeText(
  145. IXMLDOMNodePtr & pNode,
  146. CUString & strText)
  147. {
  148. HRESULT hr = S_OK;
  149. BSTR bstr = NULL;
  150. hr = pNode->get_text(&bstr);
  151. if(SUCCEEDED(hr))
  152. {
  153. hr = strText.HrAssign(bstr);
  154. SysFreeString(bstr);
  155. }
  156. TraceHr(ttidError, FAL, hr, FALSE, "HrGetNodeText");
  157. return hr;
  158. }
  159. HRESULT HrSetNodeText(
  160. IXMLDOMNodePtr & pNode,
  161. const CUString & strText)
  162. {
  163. HRESULT hr = S_OK;
  164. BSTR bstr = NULL;
  165. hr = strText.HrGetBSTR(&bstr);
  166. if(SUCCEEDED(hr))
  167. {
  168. hr = pNode->put_text(bstr);
  169. SysFreeString(bstr);
  170. }
  171. TraceHr(ttidError, FAL, hr, FALSE, "HrSetNodeText");
  172. return hr;
  173. }
  174. HRESULT HrIsNodeEmpty(
  175. IXMLDOMNodePtr & pNode)
  176. {
  177. HRESULT hr = S_OK;
  178. BSTR bstr = NULL;
  179. hr = pNode->get_text(&bstr);
  180. if(S_OK == hr)
  181. {
  182. if(SysStringLen(bstr))
  183. {
  184. hr = S_FALSE;
  185. }
  186. SysFreeString(bstr);
  187. }
  188. else
  189. {
  190. hr = S_OK;
  191. }
  192. return hr;
  193. }
  194. HRESULT HrIsPresentOnceHelper(
  195. const wchar_t * szPattern,
  196. IXMLDOMNodePtr & pNode,
  197. IXMLDOMNodePtr & pNodeInstance)
  198. {
  199. HRESULT hr = S_OK;
  200. IXMLDOMNodeListPtr pNodeList;
  201. hr = HrSelectNodes(szPattern, pNode, pNodeList);
  202. if(SUCCEEDED(hr))
  203. {
  204. long nLength = 0;
  205. hr = pNodeList->get_length(&nLength);
  206. if(SUCCEEDED(hr))
  207. {
  208. if(1 == nLength)
  209. {
  210. hr = pNodeList->get_item(0, pNodeInstance.AddressOf());
  211. }
  212. else if (nLength > 1)
  213. {
  214. hr = UPNP_E_DUPLICATE_NOT_ALLOWED;
  215. }
  216. else
  217. {
  218. hr = S_FALSE;
  219. }
  220. }
  221. }
  222. else
  223. {
  224. hr = S_FALSE;
  225. }
  226. return hr;
  227. }
  228. HRESULT HrIsNodeOfValidLength(
  229. const wchar_t * szPattern,
  230. IXMLDOMNodePtr & pNode,
  231. LONG cchMax)
  232. {
  233. HRESULT hr = S_OK;
  234. IXMLDOMNodePtr pNodeMatch;
  235. hr = HrSelectNode(szPattern, pNode, pNodeMatch);
  236. if(SUCCEEDED(hr))
  237. {
  238. BSTR bstr = NULL;
  239. hr = pNodeMatch->get_text(&bstr);
  240. if(S_OK == hr)
  241. {
  242. if (SysStringLen(bstr) > (ULONG)cchMax)
  243. {
  244. hr = S_FALSE;
  245. }
  246. SysFreeString(bstr);
  247. }
  248. }
  249. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrSelectNodeText");
  250. return hr;
  251. }
  252. HRESULT HrIsNodePresentOnce(
  253. const wchar_t * szPattern,
  254. IXMLDOMNodePtr & pNode)
  255. {
  256. HRESULT hr = S_OK;
  257. IXMLDOMNodePtr pNodeInstance;
  258. hr = HrIsPresentOnceHelper(szPattern, pNode, pNodeInstance);
  259. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrIsPresentOnce(%S)", szPattern);
  260. return hr;
  261. }
  262. HRESULT HrIsNodePresentOnceAndEmpty(
  263. const wchar_t * szPattern,
  264. IXMLDOMNodePtr & pNode)
  265. {
  266. HRESULT hr = S_OK;
  267. IXMLDOMNodePtr pNodeInstance;
  268. hr = HrIsPresentOnceHelper(szPattern, pNode, pNodeInstance);
  269. if(S_OK == hr)
  270. {
  271. hr = HrIsNodeEmpty(pNodeInstance);
  272. }
  273. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrIsPresentOnceAndEmpty(%S)", szPattern);
  274. return hr;
  275. }
  276. HRESULT HrIsNodePresentOnceAndNotEmpty(
  277. const wchar_t * szPattern,
  278. IXMLDOMNodePtr & pNode)
  279. {
  280. HRESULT hr = S_OK;
  281. IXMLDOMNodePtr pNodeInstance;
  282. hr = HrIsPresentOnceHelper(szPattern, pNode, pNodeInstance);
  283. if(S_OK == hr)
  284. {
  285. hr = HrIsNodeEmpty(pNodeInstance);
  286. if(S_OK == hr)
  287. {
  288. hr = S_FALSE;
  289. }
  290. else
  291. {
  292. hr = S_OK;
  293. }
  294. }
  295. TraceHr(ttidError, FAL, hr, (hr == S_FALSE), "HrIsNodePresentOnceAndNotEmpty(%S)", szPattern);
  296. return hr;
  297. }