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.

567 lines
15 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. WMIParser_Instance.cpp
  5. Abstract:
  6. This file contains the implementation of the WMIParser::Instance class,
  7. which is used to hold the data of an instance inside a CIM schema.
  8. Revision History:
  9. Davide Massarenti (Dmassare) 07/25/99
  10. created
  11. ******************************************************************************/
  12. #include "stdafx.h"
  13. #define XQL_PROPERTY L"INSTANCE/PROPERTY"
  14. #define XQL_PROPERTY_ARRAY L"INSTANCE/PROPERTY.ARRAY"
  15. #define XQL_PROPERTY_REFERENCE L"INSTANCE/PROPERTY.REFERENCE"
  16. #define TAG_INSTANCE L"INSTANCE"
  17. #define TAG_PROPERTY L"PROPERTY"
  18. #define TAG_VALUE L"VALUE"
  19. #define ATTRIB_NAME L"NAME"
  20. #define ATTRIB_TYPE L"TYPE"
  21. #define PROPERTY_TIMESTAMP L"TimeStamp"
  22. #define PROPERTY_CHANGE L"Change"
  23. #define PROPERTY_CHANGE_TYPE L"string"
  24. WMIParser::Instance::Instance()
  25. {
  26. __HCP_FUNC_ENTRY( "WMIParser::Instance::Instance" );
  27. // MPC::XmlUtil m_xmlNode;
  28. //
  29. // Property_Scalar m_wmippTimeStamp;
  30. m_fTimeStamp = false; // bool m_fTimeStamp;
  31. //
  32. // Property_Scalar m_wmippChange;
  33. m_fChange = false; // bool m_fChange;
  34. //
  35. // InstanceName m_wmipinIdentity;
  36. //
  37. m_fPropertiesParsed = false; // bool m_fPropertiesParsed;
  38. // PropMap m_mapPropertiesScalar;
  39. // ArrayMap m_mapPropertiesArray;
  40. // ReferenceMap m_mapPropertiesReference;
  41. }
  42. WMIParser::Instance::~Instance()
  43. {
  44. __HCP_FUNC_ENTRY( "WMIParser::Instance::~Instance" );
  45. }
  46. bool WMIParser::Instance::operator==( /*[in]*/ Instance const &wmipi ) const
  47. {
  48. __HCP_FUNC_ENTRY( "WMIParser::Instance::operator==" );
  49. bool fRes = false;
  50. //
  51. // We don't parse the properties until the last moment...
  52. //
  53. if( m_fPropertiesParsed == false) (void)const_cast<Instance*>( this )->ParseProperties();
  54. if(wmipi.m_fPropertiesParsed == false) (void)const_cast<Instance*>(&wmipi)->ParseProperties();
  55. if(m_mapPropertiesScalar == wmipi.m_mapPropertiesScalar &&
  56. m_mapPropertiesArray == wmipi.m_mapPropertiesArray &&
  57. m_mapPropertiesReference == wmipi.m_mapPropertiesReference )
  58. {
  59. fRes = true;
  60. }
  61. __HCP_FUNC_EXIT(fRes);
  62. }
  63. ////////////////////////////////////////////////
  64. HRESULT WMIParser::Instance::ParseIdentity( /*[in] */ IXMLDOMNode* pxdnNode ,
  65. /*[out]*/ bool& fEmpty )
  66. {
  67. __HCP_FUNC_ENTRY( "WMIParser::Instance::ParseIdentity" );
  68. HRESULT hr;
  69. __MPC_EXIT_IF_METHOD_FAILS(hr, m_wmipinIdentity.put_Node( pxdnNode, fEmpty ));
  70. hr = S_OK;
  71. __HCP_FUNC_CLEANUP;
  72. __HCP_FUNC_EXIT(hr);
  73. }
  74. HRESULT WMIParser::Instance::ParseProperties()
  75. {
  76. __HCP_FUNC_ENTRY( "WMIParser::Instance::ParseProperties" );
  77. HRESULT hr;
  78. if(m_fPropertiesParsed == false)
  79. {
  80. __MPC_EXIT_IF_METHOD_FAILS(hr, ParsePropertiesScalar ());
  81. __MPC_EXIT_IF_METHOD_FAILS(hr, ParsePropertiesArray ());
  82. __MPC_EXIT_IF_METHOD_FAILS(hr, ParsePropertiesReference());
  83. m_fPropertiesParsed = true;
  84. }
  85. hr = S_OK;
  86. __HCP_FUNC_CLEANUP;
  87. __HCP_FUNC_EXIT(hr);
  88. }
  89. HRESULT WMIParser::Instance::ParsePropertiesScalar()
  90. {
  91. __HCP_FUNC_ENTRY( "WMIParser::Instance::ParsePropertiesScalar" );
  92. HRESULT hr;
  93. CComPtr<IXMLDOMNodeList> xdnlList;
  94. CComPtr<IXMLDOMNode> xdnNode;
  95. //
  96. // Get all the elements of type "PROPERTY".
  97. //
  98. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNodes( XQL_PROPERTY, &xdnlList ));
  99. for(;SUCCEEDED(hr = xdnlList->nextNode( &xdnNode )) && xdnNode != NULL; xdnNode = NULL)
  100. {
  101. Property wmipp;
  102. MPC::wstring szName;
  103. //
  104. // Get the name of the property.
  105. //
  106. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipp.put_Node( xdnNode ));
  107. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipp.get_Name( szName ));
  108. ////////////////////////////////////////////////////////////
  109. //
  110. // Filter out "Timestamp" and "Change" properties!
  111. //
  112. if(wmipp == PROPERTY_TIMESTAMP && m_fTimeStamp == false)
  113. {
  114. __MPC_EXIT_IF_METHOD_FAILS(hr, m_wmippTimeStamp.put_Node( xdnNode ));
  115. m_fTimeStamp = true;
  116. continue;
  117. }
  118. if(wmipp == PROPERTY_CHANGE && m_fChange == false)
  119. {
  120. __MPC_EXIT_IF_METHOD_FAILS(hr, m_wmippChange.put_Node( xdnNode ));
  121. m_fChange = true;
  122. continue;
  123. }
  124. ////////////////////////////////////////////////////////////
  125. //
  126. // Parse the whole property.
  127. //
  128. {
  129. Property_Scalar& wmipps = m_mapPropertiesScalar[ szName ];
  130. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipps.put_Node( xdnNode ));
  131. }
  132. }
  133. hr = S_OK;
  134. __HCP_FUNC_CLEANUP;
  135. __HCP_FUNC_EXIT(hr);
  136. }
  137. HRESULT WMIParser::Instance::ParsePropertiesArray()
  138. {
  139. __HCP_FUNC_ENTRY( "WMIParser::Instance::ParsePropertiesArray" );
  140. HRESULT hr;
  141. CComPtr<IXMLDOMNodeList> xdnlList;
  142. CComPtr<IXMLDOMNode> xdnNode;
  143. //
  144. // Get all the elements of type "PROPERTY.ARRAY".
  145. //
  146. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNodes( XQL_PROPERTY_ARRAY, &xdnlList ));
  147. for(;SUCCEEDED(hr = xdnlList->nextNode( &xdnNode )) && xdnNode != NULL; xdnNode = NULL)
  148. {
  149. Property wmipp;
  150. MPC::wstring szName;
  151. //
  152. // Get the name of the property.
  153. //
  154. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipp.put_Node( xdnNode ));
  155. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipp.get_Name( szName ));
  156. //
  157. // Parse the whole property.
  158. //
  159. {
  160. Property_Array& wmippa = m_mapPropertiesArray[ szName ];
  161. __MPC_EXIT_IF_METHOD_FAILS(hr, wmippa.put_Node( xdnNode ));
  162. }
  163. }
  164. hr = S_OK;
  165. __HCP_FUNC_CLEANUP;
  166. __HCP_FUNC_EXIT(hr);
  167. }
  168. HRESULT WMIParser::Instance::ParsePropertiesReference()
  169. {
  170. __HCP_FUNC_ENTRY( "WMIParser::Instance::ParsePropertiesReference" );
  171. HRESULT hr;
  172. CComPtr<IXMLDOMNodeList> xdnlList;
  173. CComPtr<IXMLDOMNode> xdnNode;
  174. //
  175. // Get all the elements of type "PROPERTY.REFERENCE".
  176. //
  177. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNodes( XQL_PROPERTY_REFERENCE, &xdnlList ));
  178. for(;SUCCEEDED(hr = xdnlList->nextNode( &xdnNode )) && xdnNode != NULL; xdnNode = NULL)
  179. {
  180. Property wmipp;
  181. MPC::wstring szName;
  182. //
  183. // Get the name of the property.
  184. //
  185. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipp.put_Node( xdnNode ));
  186. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipp.get_Name( szName ));
  187. //
  188. // Parse the whole property.
  189. //
  190. {
  191. Property_Reference& wmippr = m_mapPropertiesReference[ szName ];
  192. __MPC_EXIT_IF_METHOD_FAILS(hr, wmippr.put_Node( xdnNode ));
  193. }
  194. }
  195. hr = S_OK;
  196. __HCP_FUNC_CLEANUP;
  197. __HCP_FUNC_EXIT(hr);
  198. }
  199. ////////////////////////////////////////////////
  200. HRESULT WMIParser::Instance::put_Node( /*[in] */ IXMLDOMNode* pxdnNode ,
  201. /*[out]*/ bool& fEmpty )
  202. {
  203. __HCP_FUNC_ENTRY( "WMIParser::Instance::put_Node" );
  204. HRESULT hr;
  205. __MPC_PARAMCHECK_BEGIN(hr)
  206. __MPC_PARAMCHECK_NOTNULL(pxdnNode);
  207. __MPC_PARAMCHECK_END();
  208. m_xmlNode = pxdnNode;
  209. fEmpty = true;
  210. //
  211. // Analize the node...
  212. //
  213. __MPC_EXIT_IF_METHOD_FAILS(hr, ParseIdentity( pxdnNode, fEmpty ));
  214. hr = S_OK;
  215. __HCP_FUNC_CLEANUP;
  216. __HCP_FUNC_EXIT(hr);
  217. }
  218. HRESULT WMIParser::Instance::get_Node( /*[out]*/ IXMLDOMNode* *pxdnNode )
  219. {
  220. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_Node" );
  221. HRESULT hr;
  222. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetRoot( pxdnNode ));
  223. hr = S_OK;
  224. __HCP_FUNC_CLEANUP;
  225. __HCP_FUNC_EXIT(hr);
  226. }
  227. ////////////////////////////////////////////////
  228. HRESULT WMIParser::Instance::get_Namespace( /*[out]*/ MPC::wstring& szNamespace )
  229. {
  230. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_Namespace" );
  231. HRESULT hr;
  232. hr = m_wmipinIdentity.get_Namespace( szNamespace );
  233. __HCP_FUNC_EXIT(hr);
  234. }
  235. HRESULT WMIParser::Instance::get_Class( /*[out]*/ MPC::wstring& szClass )
  236. {
  237. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_Class" );
  238. HRESULT hr;
  239. hr = m_wmipinIdentity.get_Class( szClass );
  240. __HCP_FUNC_EXIT(hr);
  241. }
  242. HRESULT WMIParser::Instance::get_TimeStamp( /*[out]*/ Property_Scalar*& wmippTimeStamp ,
  243. /*[out]*/ bool& fFound )
  244. {
  245. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_TimeStamp" );
  246. HRESULT hr;
  247. //
  248. // We don't parse the properties until the last moment...
  249. //
  250. if(m_fPropertiesParsed == false)
  251. {
  252. __MPC_EXIT_IF_METHOD_FAILS(hr, ParseProperties());
  253. }
  254. fFound = m_fTimeStamp;
  255. wmippTimeStamp = m_fTimeStamp ? &m_wmippTimeStamp : NULL;
  256. hr = S_OK;
  257. __HCP_FUNC_CLEANUP;
  258. __HCP_FUNC_EXIT(hr);
  259. }
  260. HRESULT WMIParser::Instance::get_Change( /*[out]*/ Property_Scalar*& wmippChange )
  261. {
  262. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_Change" );
  263. HRESULT hr;
  264. //
  265. // We don't parse the properties until the last moment...
  266. //
  267. if(m_fPropertiesParsed == false) (void)ParseProperties();
  268. //
  269. // If the "CHANGE" property is not present, create a fake one.
  270. //
  271. if(m_fChange == false)
  272. {
  273. CComPtr<IXMLDOMNode> xdnNodeInstance;
  274. CComPtr<IXMLDOMNode> xdnNodeProperty;
  275. CComPtr<IXMLDOMNode> xdnNodeValue;
  276. bool fFound;
  277. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNode( TAG_INSTANCE, &xdnNodeInstance ));
  278. if(xdnNodeInstance)
  279. {
  280. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.CreateNode ( TAG_PROPERTY, &xdnNodeProperty, xdnNodeInstance ));
  281. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.PutAttribute( NULL, ATTRIB_NAME, PROPERTY_CHANGE , fFound, xdnNodeProperty ));
  282. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.PutAttribute( NULL, ATTRIB_TYPE, PROPERTY_CHANGE_TYPE, fFound, xdnNodeProperty ));
  283. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.CreateNode ( TAG_VALUE, &xdnNodeValue , xdnNodeProperty ));
  284. __MPC_EXIT_IF_METHOD_FAILS(hr, m_wmippChange.put_Node( xdnNodeProperty ));
  285. m_fChange = true;
  286. }
  287. else
  288. {
  289. __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ERROR_BAD_FORMAT);
  290. }
  291. }
  292. wmippChange = m_fChange ? &m_wmippChange : NULL;
  293. hr = S_OK;
  294. __HCP_FUNC_CLEANUP;
  295. __HCP_FUNC_EXIT(hr);
  296. }
  297. HRESULT WMIParser::Instance::get_Identity( /*[out]*/ InstanceName*& wmipin )
  298. {
  299. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_Identity" );
  300. HRESULT hr;
  301. wmipin = &m_wmipinIdentity;
  302. hr = S_OK;
  303. __HCP_FUNC_EXIT(hr);
  304. }
  305. HRESULT WMIParser::Instance::get_Properties( /*[out]*/ PropIterConst& itBegin ,
  306. /*[out]*/ PropIterConst& itEnd )
  307. {
  308. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_Properties" );
  309. HRESULT hr;
  310. //
  311. // We don't parse the properties until the last moment...
  312. //
  313. if(m_fPropertiesParsed == false) (void)ParseProperties();
  314. itBegin = m_mapPropertiesScalar.begin();
  315. itEnd = m_mapPropertiesScalar.end ();
  316. hr = S_OK;
  317. __HCP_FUNC_EXIT(hr);
  318. }
  319. HRESULT WMIParser::Instance::get_PropertiesArray( /*[out]*/ ArrayIterConst& itBegin ,
  320. /*[out]*/ ArrayIterConst& itEnd )
  321. {
  322. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_PropertiesArray" );
  323. HRESULT hr;
  324. //
  325. // We don't parse the properties until the last moment...
  326. //
  327. if(m_fPropertiesParsed == false) (void)ParseProperties();
  328. itBegin = m_mapPropertiesArray.begin();
  329. itEnd = m_mapPropertiesArray.end ();
  330. hr = S_OK;
  331. __HCP_FUNC_EXIT(hr);
  332. }
  333. HRESULT WMIParser::Instance::get_PropertiesReference( /*[out]*/ ReferenceIterConst& itBegin ,
  334. /*[out]*/ ReferenceIterConst& itEnd )
  335. {
  336. __HCP_FUNC_ENTRY( "WMIParser::Instance::get_PropertiesReference" );
  337. HRESULT hr;
  338. //
  339. // We don't parse the properties until the last moment...
  340. //
  341. if(m_fPropertiesParsed == false) (void)ParseProperties();
  342. itBegin = m_mapPropertiesReference.begin();
  343. itEnd = m_mapPropertiesReference.end ();
  344. hr = S_OK;
  345. __HCP_FUNC_EXIT(hr);
  346. }
  347. bool WMIParser::Instance::CompareByClass( /*[in]*/ Instance const &wmipi ) const
  348. {
  349. MPC::NocaseLess strLess;
  350. MPC::NocaseCompare strCmp;
  351. const MPC::wstring& leftNamespace = m_wmipinIdentity.m_szNamespace;
  352. const MPC::wstring& leftClass = m_wmipinIdentity.m_szClass;
  353. const MPC::wstring& rightNamespace = wmipi.m_wmipinIdentity.m_szNamespace;
  354. const MPC::wstring& rightClass = wmipi.m_wmipinIdentity.m_szClass;
  355. bool fRes;
  356. if(strCmp( leftNamespace, rightNamespace ) == true)
  357. {
  358. fRes = strLess( leftClass, rightClass );
  359. }
  360. else
  361. {
  362. fRes = strLess( leftNamespace, rightNamespace );
  363. }
  364. return fRes;
  365. }
  366. bool WMIParser::Instance::CompareByKey( /*[in]*/ Instance const &wmipi ) const
  367. {
  368. bool fRes = false;
  369. if(m_wmipinIdentity < wmipi.m_wmipinIdentity) fRes = true;
  370. return fRes;
  371. }
  372. /////////////////////////////////////////////////////////////////////////////
  373. /////////////////////////////////////////////////////////////////////////////
  374. /////////////////////////////////////////////////////////////////////////////
  375. bool WMIParser::Instance_Less_ByClass::operator()( /*[in]*/ Instance* const & left ,
  376. /*[in]*/ Instance* const & right ) const
  377. {
  378. return left->CompareByClass( *right );
  379. }
  380. bool WMIParser::Instance_Less_ByKey::operator()( /*[in]*/ Instance* const & left ,
  381. /*[in]*/ Instance* const & right ) const
  382. {
  383. return left->CompareByKey( *right );
  384. }