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.

350 lines
11 KiB

  1. #include "stdinc.h"
  2. #include "fusioneventlog.h"
  3. #include "xmlparsertest.hxx"
  4. #include "stdio.h"
  5. #include "FusionEventLog.h"
  6. #include "xmlparser.hxx"
  7. VOID PrintTreeFromRoot(SXS_XMLTreeNode * Root)
  8. {
  9. SXS_XMLTreeNode * pChild;
  10. SXS_XMLTreeNode * pNext;
  11. if (Root == NULL)
  12. return;
  13. Root->PrintSelf();
  14. pChild = Root->m_pFirstChild;
  15. if (pChild == NULL)
  16. return;
  17. FusionpDbgPrint("BeginChildren\n");
  18. while (pChild)
  19. {
  20. pNext = pChild->m_pSiblingNode;
  21. PrintTreeFromRoot(pChild);
  22. pChild = pNext;
  23. }
  24. FusionpDbgPrint("EndChildren\n");
  25. return;
  26. }
  27. HRESULT XMLParserTestFactory::Initialize()
  28. {
  29. HRESULT hr = NOERROR;
  30. if ((m_Tree != NULL) || (m_pNamespaceManager != NULL))
  31. {
  32. hr = E_FAIL;
  33. goto Exit;
  34. }
  35. if ((m_Tree != NULL) || (m_pNamespaceManager != NULL))
  36. {
  37. hr = E_UNEXPECTED;
  38. goto Exit;
  39. }
  40. m_Tree = new SXS_XMLDOMTree;
  41. if (m_Tree == NULL)
  42. {
  43. hr = E_OUTOFMEMORY;
  44. goto Exit;
  45. }
  46. m_pNamespaceManager = new CXMLNamespaceManager;
  47. if (m_pNamespaceManager == NULL)
  48. {
  49. hr = E_OUTOFMEMORY;
  50. goto Exit;
  51. }
  52. if (!m_pNamespaceManager->Initialize())
  53. {
  54. hr = HRESULT_FROM_WIN32(::GetLastError());
  55. goto Exit;
  56. }
  57. hr = NOERROR;
  58. Exit:
  59. return hr;
  60. }
  61. //---------------------------------------------------------------------------
  62. HRESULT
  63. STDMETHODCALLTYPE
  64. XMLParserTestFactory::NotifyEvent(
  65. /* [in] */ IXMLNodeSource *pSource,
  66. /* [in] */ XML_NODEFACTORY_EVENT iEvt)
  67. {
  68. UNUSED(pSource);
  69. UNUSED(iEvt);
  70. switch (iEvt)
  71. {
  72. case XMLNF_STARTDTDSUBSET:
  73. FusionpDbgPrint(" [");
  74. //_fNLPending = true;
  75. break;
  76. case XMLNF_ENDDTDSUBSET:
  77. FusionpDbgPrint("]");
  78. //_fNLPending = true;
  79. break;
  80. }
  81. return S_OK;
  82. }
  83. //---------------------------------------------------------------------------
  84. HRESULT
  85. STDMETHODCALLTYPE
  86. XMLParserTestFactory::BeginChildren(
  87. /* [in] */ IXMLNodeSource *pSource,
  88. /* [in] */ XML_NODE_INFO *pNodeInfo)
  89. {
  90. UNUSED(pSource);
  91. UNUSED(pNodeInfo);
  92. HRESULT hr = NOERROR;
  93. FusionpDbgPrint("BeginChildren\n");
  94. hr = m_pNamespaceManager->OnBeginChildren(pSource,pNodeInfo);
  95. m_Tree->SetChildCreation();
  96. return hr;
  97. }
  98. //---------------------------------------------------------------------------
  99. HRESULT
  100. STDMETHODCALLTYPE
  101. XMLParserTestFactory::EndChildren(
  102. /* [in] */ IXMLNodeSource *pSource,
  103. /* [in] */ BOOL fEmptyNode,
  104. /* [in] */ XML_NODE_INFO *pNodeInfo)
  105. {
  106. UNUSED(pSource);
  107. UNUSED(fEmptyNode);
  108. UNUSED(pNodeInfo);
  109. HRESULT hr = NOERROR;
  110. FusionpDbgPrint("EndChildren");
  111. hr= m_pNamespaceManager->OnEndChildren(pSource, fEmptyNode, pNodeInfo);
  112. if ( fEmptyNode ) {
  113. FusionpDbgPrint("(fEmpty=TRUE)\n");
  114. }else{
  115. m_Tree->ReturnToParent();
  116. //m_Tree->TurnOffFirstChildFlag();
  117. FusionpDbgPrint("\n");
  118. }
  119. return hr;
  120. }
  121. //---------------------------------------------------------------------------
  122. HRESULT
  123. STDMETHODCALLTYPE
  124. XMLParserTestFactory::CreateNode(
  125. /* [in] */ IXMLNodeSource *pSource,
  126. /* [in] */ PVOID pNode,
  127. /* [in] */ USHORT cNumRecs,
  128. /* [in] */ XML_NODE_INFO* * apNodeInfo)
  129. {
  130. HRESULT hr = NOERROR;
  131. FN_TRACE_HR(hr);
  132. // XML_NODE_INFO* pNodeInfo = *apNodeInfo; // generates c4189: 'pNodeInfo' : local variable is initialized but not referenced
  133. DWORD i;
  134. // use of namespace
  135. CSmallStringBuffer buffNamespace;
  136. SIZE_T cchNamespacePrefix;
  137. UNUSED(pSource);
  138. UNUSED(pNode);
  139. UNUSED(apNodeInfo);
  140. UNUSED(cNumRecs);
  141. if ( apNodeInfo[0]->dwType == XML_ELEMENT || apNodeInfo[0]->dwType == XML_PCDATA)
  142. m_Tree->AddNode(cNumRecs, apNodeInfo);
  143. FusionpDbgPrint("CreateNode\n");
  144. for( i = 0; i < cNumRecs; i++) {
  145. switch(apNodeInfo[i]->dwType) {
  146. case XML_CDATA:
  147. ::FusionpDbgPrint("\t\t XML_CDATA: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  148. break;
  149. case XML_COMMENT :
  150. ::FusionpDbgPrint("\t\t XML_COMMENT: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  151. break;
  152. case XML_WHITESPACE :
  153. ::FusionpDbgPrint("\t\t XML_WHITESPACE: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  154. break;
  155. case XML_ELEMENT :
  156. ::FusionpDbgPrint("\t\t XML_ELEMENT: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  157. break;
  158. case XML_ATTRIBUTE :
  159. ::FusionpDbgPrint("\t\t XML_ATTRIBUTE: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  160. break;
  161. case XML_PCDATA :
  162. ::FusionpDbgPrint("\t\t XML_PCDATA: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  163. break;
  164. case XML_PI:
  165. ::FusionpDbgPrint("\t\t XML_PI: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  166. break;
  167. case XML_XMLDECL :
  168. ::FusionpDbgPrint("\t\t XML_XMLDECL: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  169. break;
  170. case XML_DOCTYPE :
  171. ::FusionpDbgPrint("\t\t XML_DOCTYPE: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  172. break;
  173. case XML_ENTITYDECL :
  174. ::FusionpDbgPrint("\t\t XML_ENTITYDECL: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  175. break;
  176. case XML_ELEMENTDECL :
  177. ::FusionpDbgPrint("\t\t XML_ELEMENTDECL: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  178. break;
  179. case XML_ATTLISTDECL :
  180. ::FusionpDbgPrint("\t\t XML_ATTLISTDECL: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  181. break;
  182. case XML_NOTATION :
  183. ::FusionpDbgPrint("\t\t XML_NOTATION: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  184. break;
  185. case XML_ENTITYREF :
  186. ::FusionpDbgPrint("\t\t XML_ENTITYREF: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  187. break;
  188. case XML_DTDATTRIBUTE:
  189. ::FusionpDbgPrint("\t\t XML_DTDATTRIBUTE: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  190. break;
  191. case XML_GROUP :
  192. ::FusionpDbgPrint("\t\t XML_GROUP: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  193. break;
  194. case XML_INCLUDESECT :
  195. ::FusionpDbgPrint("\t\t XML_INCLUDESECT: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  196. break;
  197. case XML_NAME :
  198. ::FusionpDbgPrint("\t\t XML_NAME: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  199. break;
  200. case XML_NMTOKEN :
  201. ::FusionpDbgPrint("\t\t XML_NMTOKEN: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  202. break;
  203. case XML_STRING :
  204. ::FusionpDbgPrint("\t\t XML_STRING: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  205. break;
  206. case XML_PEREF :
  207. ::FusionpDbgPrint("\t\t XML_PEREF: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  208. break;
  209. case XML_MODEL :
  210. ::FusionpDbgPrint("\t\t XML_MODEL: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  211. break;
  212. case XML_ATTDEF :
  213. ::FusionpDbgPrint("\t\t XML_ATTDEF: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  214. break;
  215. case XML_ATTTYPE :
  216. ::FusionpDbgPrint("\t\t XML_ATTTYPE: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  217. break;
  218. case XML_ATTPRESENCE :
  219. ::FusionpDbgPrint("\t\t XML_ATTPRESENCE: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  220. break;
  221. case XML_DTDSUBSET :
  222. ::FusionpDbgPrint("\t\t XML_DTDSUBSET: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  223. break;
  224. case XML_LASTNODETYPE :
  225. ::FusionpDbgPrint("\t\t XML_LASTNODETYPE: [%.*ls]\n", apNodeInfo[i]->ulLen, apNodeInfo[i]->pwcText);
  226. break;
  227. default :
  228. ::FusionpDbgPrint("UNKNOWN TYPE! ERROR!!\n");
  229. } // end of switch
  230. }
  231. if (apNodeInfo[0]->dwType != XML_ELEMENT)
  232. {
  233. hr = NOERROR;
  234. goto Exit;
  235. }
  236. hr = m_pNamespaceManager->OnCreateNode(
  237. pSource, pNode, cNumRecs, apNodeInfo);
  238. if ( FAILED(hr))
  239. goto Exit;
  240. for( i=0; i<cNumRecs; i++)
  241. {
  242. if ((apNodeInfo[i]->dwType == XML_ELEMENT) || (apNodeInfo[i]->dwType == XML_ATTRIBUTE ))
  243. {
  244. IFCOMFAILED_EXIT(m_pNamespaceManager->Map(0, apNodeInfo[i], &buffNamespace, &cchNamespacePrefix));
  245. //FusionpDbgPrint("Namespace is %ls with length=%Id\n", static_cast<PCWSTR>(buffNamespace), cchNamespace);
  246. buffNamespace.Clear();
  247. }
  248. }
  249. Exit:
  250. return hr;
  251. }
  252. //---------------------------------------------------------------------------
  253. HRESULT XMLParserTest(PCWSTR filename)
  254. {
  255. HRESULT hr = S_OK;
  256. CSmartRef<IXMLParser> pIXMLParser;
  257. CSmartRef<XMLParserTestFactory> factory;
  258. CSmartRef<XMLParserTestFileStream> filestream;
  259. filestream = NEW (XMLParserTestFileStream());
  260. if (filestream == NULL)
  261. {
  262. ::FusionpDbgPrintEx(
  263. FUSION_DBG_LEVEL_INFO,
  264. "SxsDebug:: fail to new XMLParserTestFileStream, out of memory\n");
  265. hr = E_OUTOFMEMORY;
  266. goto Exit;
  267. }
  268. filestream->AddRef(); // refCount = 1;
  269. if (!filestream->open(filename))
  270. {
  271. ::FusionpDbgPrintEx(
  272. FUSION_DBG_LEVEL_INFO,
  273. "SxsDebug:: fail to call XMLParserTestFileStream::open\n");
  274. hr = E_UNEXPECTED;
  275. goto Exit;
  276. }
  277. factory = new XMLParserTestFactory;
  278. if (factory == NULL)
  279. {
  280. ::FusionpDbgPrintEx(
  281. FUSION_DBG_LEVEL_INFO,
  282. "SxsDebug:: fail to new XMLParserTestFactory, out of memory\n");
  283. hr = E_OUTOFMEMORY;
  284. goto Exit;
  285. }
  286. factory->AddRef(); // RefCount = 1
  287. hr = factory->Initialize();
  288. if (FAILED(hr))
  289. goto Exit;
  290. pIXMLParser = NEW(XMLParser);
  291. if (pIXMLParser == NULL)
  292. {
  293. ::FusionpDbgPrintEx(
  294. FUSION_DBG_LEVEL_ERROR,
  295. "SXS.DLL: Attempt to instantiate XML parser failed\n");
  296. goto Exit;
  297. }
  298. pIXMLParser->AddRef(); // refCount = 1 ;
  299. hr = pIXMLParser->SetInput(filestream); // filestream's RefCount=2
  300. if (!SUCCEEDED(hr))
  301. goto Exit;
  302. hr = pIXMLParser->SetFactory(factory); // factory's RefCount=2
  303. if (!SUCCEEDED(hr))
  304. goto Exit;
  305. hr = pIXMLParser->Run(-1);
  306. if (FAILED(hr))
  307. goto Exit;
  308. PrintTreeFromRoot(factory->GetTreeRoot());
  309. Exit:
  310. // at this point, pIXMLParser's RefCount = 1 ;
  311. // factory's RefCount = 2;
  312. // filestream's RefCount = 2 ;
  313. return hr;
  314. }