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.

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