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.

318 lines
7.4 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. WMIParser_Snapshot.cpp
  5. Abstract:
  6. This file contains the implementation of the WMIParser::Snapshot class,
  7. which is used to hold the data of an snapshot inside a CIM schema.
  8. Revision History:
  9. Davide Massarenti (Dmassare) 07/25/99
  10. created
  11. ******************************************************************************/
  12. #include "stdafx.h"
  13. #define TAG_DECLARATION L"DECLARATION"
  14. #define TAG_DECLGROUP L"DECLGROUP.WITHPATH"
  15. //const LPCWSTR l_Instances[] = { L"DECLARATION/DECLGROUP/VALUE.OBJECT" ,
  16. // L"DECLARATION/DECLGROUP.WITHNAME/VALUE.NAMEDOBJECT" ,
  17. // L"DECLARATION/DECLGROUP.WITHPATH/VALUE.OBJECTWITHPATH" ,
  18. // L"DECLARATION/DECLGROUP.WITHPATH/VALUE.OBJECTWITHLOCALPATH" };
  19. static const WCHAR l_InstancesRoot[] = L"DECLARATION/DECLGROUP.WITHPATH";
  20. static const LPCWSTR l_Instances[] = { L"DECLARATION/DECLGROUP.WITHPATH/VALUE.OBJECTWITHPATH" ,
  21. L"DECLARATION/DECLGROUP.WITHPATH/VALUE.OBJECTWITHLOCALPATH" };
  22. static CComBSTR l_EmptyCIM( L"<?xml version=\"1.0\" encoding=\"unicode\"?><CIM CIMVERSION=\"2.0\" DTDVERSION=\"2.0\"><DECLARATION><DECLGROUP.WITHPATH></DECLGROUP.WITHPATH></DECLARATION></CIM>" );
  23. WMIParser::Snapshot::Snapshot()
  24. {
  25. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::Snapshot" );
  26. // MPC_XmlUtil m_xmlNode;
  27. //
  28. // InstList m_lstInstances;
  29. }
  30. WMIParser::Snapshot::~Snapshot()
  31. {
  32. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::~Snapshot" );
  33. }
  34. HRESULT WMIParser::Snapshot::Parse()
  35. {
  36. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::Parse" );
  37. HRESULT hr;
  38. CComPtr<IXMLDOMNodeList> xdnlList;
  39. CComPtr<IXMLDOMNode> xdnNode;
  40. bool fEmpty;
  41. int iPass;
  42. m_lstInstances.clear();
  43. for(iPass=0; iPass<sizeof(l_Instances)/sizeof(*l_Instances); iPass++,xdnlList=NULL,xdnNode=NULL)
  44. {
  45. //
  46. // Get all the elements of type "INSTANCE".
  47. //
  48. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNodes( l_Instances[iPass], &xdnlList ));
  49. for(;SUCCEEDED(hr = xdnlList->nextNode( &xdnNode )) && xdnNode != NULL; xdnNode = NULL)
  50. {
  51. InstIter wmipiNew = m_lstInstances.insert( m_lstInstances.end() );
  52. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipiNew->put_Node( xdnNode, fEmpty ));
  53. if(fEmpty == true)
  54. {
  55. //
  56. // The instance appears to be empty, so don't use it.
  57. //
  58. m_lstInstances.erase( wmipiNew );
  59. }
  60. }
  61. }
  62. hr = S_OK;
  63. __HCP_FUNC_CLEANUP;
  64. __HCP_FUNC_EXIT(hr);
  65. }
  66. ////////////////////////////////////////////////
  67. HRESULT WMIParser::Snapshot::put_Node( /*[in]*/ IXMLDOMNode* pxdnNode )
  68. {
  69. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::put_Node" );
  70. _ASSERT(pxdnNode != NULL);
  71. HRESULT hr;
  72. m_xmlNode = pxdnNode;
  73. __MPC_EXIT_IF_METHOD_FAILS(hr, Parse());
  74. hr = S_OK;
  75. __HCP_FUNC_CLEANUP;
  76. __HCP_FUNC_EXIT(hr);
  77. }
  78. HRESULT WMIParser::Snapshot::get_Node( /*[out]*/ IXMLDOMNode* *pxdnNode )
  79. {
  80. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::get_Node" );
  81. HRESULT hr;
  82. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetRoot( pxdnNode ));
  83. hr = S_OK;
  84. __HCP_FUNC_CLEANUP;
  85. __HCP_FUNC_EXIT(hr);
  86. }
  87. HRESULT WMIParser::Snapshot::get_NodeForInstances( /*[out]*/ IXMLDOMNode* *pxdnNode )
  88. {
  89. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::get_NodeForInstances" );
  90. HRESULT hr;
  91. if(m_xdnInstances == NULL)
  92. {
  93. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNode( l_InstancesRoot, &m_xdnInstances ));
  94. if(m_xdnInstances == NULL)
  95. {
  96. __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  97. }
  98. }
  99. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xdnInstances->QueryInterface( IID_IXMLDOMNode, (void **)pxdnNode ));
  100. hr = S_OK;
  101. __HCP_FUNC_CLEANUP;
  102. __HCP_FUNC_EXIT(hr);
  103. }
  104. ////////////////////////////////////////////////
  105. HRESULT WMIParser::Snapshot::get_Instances( /*[out]*/ InstIterConst& itBegin ,
  106. /*[out]*/ InstIterConst& itEnd )
  107. {
  108. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::get_Instances" );
  109. HRESULT hr;
  110. itBegin = m_lstInstances.begin();
  111. itEnd = m_lstInstances.end ();
  112. hr = S_OK;
  113. __HCP_FUNC_EXIT(hr);
  114. }
  115. ////////////////////////////////////////////////
  116. HRESULT WMIParser::Snapshot::clone_Instance( /*[in] */ Instance* pwmipiOld ,
  117. /*[out]*/ Instance*& pwmipiNew )
  118. {
  119. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::clone" );
  120. HRESULT hr;
  121. CComPtr<IXMLDOMNode> xdnNode;
  122. CComPtr<IXMLDOMNode> xdnNodeCloned;
  123. CComPtr<IXMLDOMNode> xdnNodeParent;
  124. CComPtr<IXMLDOMNode> xdnNodeReplaced;
  125. pwmipiNew = NULL;
  126. //
  127. // Get the XML node of old instance.
  128. //
  129. __MPC_EXIT_IF_METHOD_FAILS(hr, pwmipiOld->get_Node( &xdnNode ));
  130. //
  131. // Make a copy of it.
  132. //
  133. __MPC_EXIT_IF_METHOD_FAILS(hr, xdnNode->cloneNode( VARIANT_TRUE, &xdnNodeCloned ));
  134. //
  135. // Get the root of our document.
  136. //
  137. __MPC_EXIT_IF_METHOD_FAILS(hr, get_NodeForInstances( &xdnNodeParent ));
  138. __MPC_EXIT_IF_METHOD_FAILS(hr, xdnNodeParent->appendChild( xdnNodeCloned, &xdnNodeReplaced ));
  139. //
  140. // Create a new INSTANCE object, attach it to the XML node and insert it in the list of instances.
  141. //
  142. {
  143. InstIter wmipiNew = m_lstInstances.insert( m_lstInstances.end() );
  144. bool fEmpty;
  145. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipiNew->put_Node( xdnNodeReplaced, fEmpty ));
  146. pwmipiNew = &(*wmipiNew);
  147. }
  148. hr = S_OK;
  149. __HCP_FUNC_CLEANUP;
  150. __HCP_FUNC_EXIT(hr);
  151. }
  152. ////////////////////////////////////////////////
  153. ////////////////////////////////////////////////
  154. ////////////////////////////////////////////////
  155. HRESULT WMIParser::Snapshot::New()
  156. {
  157. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::New" );
  158. HRESULT hr;
  159. CComPtr<IXMLDOMDocument> xddDoc;
  160. VARIANT_BOOL fLoaded;
  161. //
  162. // Create the DOM object.
  163. //
  164. __MPC_EXIT_IF_METHOD_FAILS(hr, ::CoCreateInstance( CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&xddDoc ));
  165. __MPC_EXIT_IF_METHOD_FAILS(hr, xddDoc->loadXML( l_EmptyCIM, &fLoaded ));
  166. if(fLoaded == VARIANT_FALSE)
  167. {
  168. __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  169. }
  170. else
  171. {
  172. MPC::XmlUtil xml( xddDoc );
  173. CComPtr<IXMLDOMNode> xdnRoot;
  174. __MPC_EXIT_IF_METHOD_FAILS(hr, xml.GetRoot( &xdnRoot ));
  175. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.New( xdnRoot, TRUE ));
  176. }
  177. hr = S_OK;
  178. __HCP_FUNC_CLEANUP;
  179. __HCP_FUNC_EXIT(hr);
  180. }
  181. HRESULT WMIParser::Snapshot::Load( /*[in]*/ LPCWSTR szFile ,
  182. /*[in]*/ LPCWSTR szRootTag )
  183. {
  184. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::Load" );
  185. HRESULT hr;
  186. bool fLoaded;
  187. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.Load( szFile, szRootTag, fLoaded ));
  188. if(fLoaded == false)
  189. {
  190. __MPC_SET_ERROR_AND_EXIT(hr, E_INVALIDARG);
  191. }
  192. __MPC_EXIT_IF_METHOD_FAILS(hr, Parse());
  193. hr = S_OK;
  194. __HCP_FUNC_CLEANUP;
  195. __HCP_FUNC_EXIT(hr);
  196. }
  197. HRESULT WMIParser::Snapshot::Save( /*[in]*/ LPCWSTR szFile )
  198. {
  199. __HCP_FUNC_ENTRY( "WMIParser::Snapshot::Save" );
  200. HRESULT hr;
  201. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.Save( szFile ));
  202. hr = S_OK;
  203. __HCP_FUNC_CLEANUP;
  204. __HCP_FUNC_EXIT(hr);
  205. }