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.

364 lines
9.6 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. WMIParser_InstanceName.cpp
  5. Abstract:
  6. This file contains the implementation of the WMIParser::InstanceName class,
  7. which is used to hold the data of an name for an instance inside a CIM schema.
  8. Revision History:
  9. Davide Massarenti (Dmassare) 10/03/99
  10. created
  11. ******************************************************************************/
  12. #include "stdafx.h"
  13. #define ATTRIB_CLASSNAME L"CLASSNAME"
  14. #define ATTRIB_NAME L"NAME"
  15. #define TAG_KEYVALUE L"KEYVALUE"
  16. #define TAG_VALUE_REFERENCE L"VALUE.REFERENCE"
  17. #define TAG_KEYBINDING L"KEYBINDING"
  18. #define TAG_KEYBINDING_NAME_DEFAULT L"<DEFAULT>"
  19. const LPCWSTR l_Namespace [] = { L"INSTANCEPATH/NAMESPACEPATH/LOCALNAMESPACEPATH/NAMESPACE", L"LOCALINSTANCEPATH/LOCALNAMESPACEPATH/NAMESPACE" };
  20. const LPCWSTR l_InstanceName[] = { L"INSTANCEPATH/INSTANCENAME" , L"LOCALINSTANCEPATH/INSTANCENAME" };
  21. WMIParser::InstanceName::InstanceName()
  22. {
  23. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::InstanceName" );
  24. // MPC::wstring m_szNamespace;
  25. // MPC::wstring m_szClass;
  26. //
  27. // KeyMap m_mapKeyBinding;
  28. }
  29. WMIParser::InstanceName::~InstanceName()
  30. {
  31. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::~InstanceName" );
  32. }
  33. bool WMIParser::InstanceName::operator==( /*[in]*/ InstanceName const &wmipin ) const
  34. {
  35. MPC::NocaseCompare strCmp;
  36. bool fRes = false;
  37. if(strCmp( m_szNamespace, wmipin.m_szNamespace ) == true &&
  38. strCmp( m_szClass , wmipin.m_szClass ) == true )
  39. {
  40. fRes = (m_mapKeyBinding == wmipin.m_mapKeyBinding);
  41. }
  42. return fRes;
  43. }
  44. bool WMIParser::InstanceName::operator< ( /*[in]*/ InstanceName const &wmipin ) const
  45. {
  46. MPC::NocaseLess strLess;
  47. MPC::NocaseCompare strCmp;
  48. const MPC::wstring& leftNamespace = m_szNamespace;
  49. const MPC::wstring& leftClass = m_szClass;
  50. const MPC::wstring& rightNamespace = wmipin.m_szNamespace;
  51. const MPC::wstring& rightClass = wmipin.m_szClass;
  52. bool fRes;
  53. if(strCmp( leftNamespace, rightNamespace ) == true)
  54. {
  55. if(strCmp( leftClass, rightClass ) == true)
  56. {
  57. fRes = (m_mapKeyBinding < wmipin.m_mapKeyBinding);
  58. }
  59. else
  60. {
  61. fRes = strLess( leftClass, rightClass );
  62. }
  63. }
  64. else
  65. {
  66. fRes = strLess( leftNamespace, rightNamespace );
  67. }
  68. return fRes;
  69. }
  70. ////////////////////////////////////////////////
  71. HRESULT WMIParser::InstanceName::ParseNamespace()
  72. {
  73. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::ParseNamespace" );
  74. HRESULT hr;
  75. CComPtr<IXMLDOMNodeList> xdnlList;
  76. CComPtr<IXMLDOMNode> xdnNode;
  77. int iPass;
  78. bool fBuilt = false;
  79. for(iPass=0; iPass<ARRAYSIZE(l_Namespace); iPass++,xdnlList=NULL,xdnNode=NULL)
  80. {
  81. //
  82. // Get all the elements of type "NAMESPACE".
  83. //
  84. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNodes( l_Namespace[iPass], &xdnlList ));
  85. for(;SUCCEEDED(hr = xdnlList->nextNode( &xdnNode )) && xdnNode != NULL; xdnNode = NULL)
  86. {
  87. MPC::wstring szNamespace;
  88. bool fFound;
  89. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetAttribute( NULL, ATTRIB_NAME, szNamespace, fFound, xdnNode ));
  90. if(fFound)
  91. {
  92. if(fBuilt == false)
  93. {
  94. fBuilt = true;
  95. }
  96. else
  97. {
  98. m_szNamespace += L'/';
  99. }
  100. m_szNamespace += szNamespace;
  101. }
  102. }
  103. if(fBuilt) break;
  104. }
  105. if(fBuilt == false)
  106. {
  107. //
  108. // Initialize the namespace to a meaningful default.
  109. //
  110. m_szNamespace = L"<UNKNOWN>";
  111. }
  112. hr = S_OK;
  113. __HCP_FUNC_CLEANUP;
  114. __HCP_FUNC_EXIT(hr);
  115. }
  116. HRESULT WMIParser::InstanceName::ParseKey( /*[in] */ IXMLDOMNode* pxdnNode ,
  117. /*[out]*/ InstanceNameItem& wmipini ,
  118. /*[out]*/ bool& fFound )
  119. {
  120. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::ParseKey" );
  121. _ASSERT(pxdnNode != NULL);
  122. HRESULT hr;
  123. MPC::XmlUtil xmlNodeSub( pxdnNode );
  124. CComPtr<IXMLDOMNode> xdnNode;
  125. CComVariant vValue;
  126. bool fFoundValue;
  127. fFound = false;
  128. //
  129. // Try to parse KEYVALUE element.
  130. //
  131. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlNodeSub.GetValue( TAG_KEYVALUE, vValue, fFoundValue ));
  132. if(fFoundValue)
  133. {
  134. if(SUCCEEDED(vValue.ChangeType( VT_BSTR )))
  135. {
  136. wmipini.m_szValue = OLE2W( vValue.bstrVal );
  137. fFound = true;
  138. }
  139. }
  140. //
  141. // Try to parse VALUE.REFERENCE element.
  142. //
  143. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlNodeSub.GetNode( TAG_VALUE_REFERENCE, &xdnNode ));
  144. if(xdnNode)
  145. {
  146. __MPC_EXIT_IF_ALLOC_FAILS(hr, wmipini.m_wmipvrValue, new ValueReference());
  147. __MPC_EXIT_IF_METHOD_FAILS(hr, wmipini.m_wmipvrValue->Parse( xdnNode ));
  148. fFound = true;
  149. }
  150. hr = S_OK;
  151. __HCP_FUNC_CLEANUP;
  152. __HCP_FUNC_EXIT(hr);
  153. }
  154. HRESULT WMIParser::InstanceName::ParseKeys()
  155. {
  156. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::ParseNamespace" );
  157. HRESULT hr;
  158. CComPtr<IXMLDOMNodeList> xdnlList;
  159. CComPtr<IXMLDOMNode> xdnNode;
  160. CComPtr<IXMLDOMNode> xdnNodeSub;
  161. int iPass;
  162. MPC::wstring szKey;
  163. bool fFound;
  164. m_szClass = L"";
  165. for(iPass=0; iPass<ARRAYSIZE(l_InstanceName); iPass++,xdnlList=NULL,xdnNode=NULL)
  166. {
  167. //
  168. // Look for an element of type "INSTANCENAME".
  169. //
  170. __MPC_EXIT_IF_METHOD_FAILS(hr, m_xmlNode.GetNode( l_InstanceName[iPass], &xdnNode ));
  171. if(xdnNode)
  172. {
  173. MPC::XmlUtil xmlNodeSub( xdnNode );
  174. //
  175. // Get the attribute named "CLASSNAME".
  176. //
  177. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlNodeSub.GetAttribute( NULL, ATTRIB_CLASSNAME, m_szClass, fFound ));
  178. //
  179. // Get all the elements of type "KEYBINDING".
  180. //
  181. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlNodeSub.GetNodes( TAG_KEYBINDING, &xdnlList ));
  182. for(;SUCCEEDED(hr = xdnlList->nextNode( &xdnNodeSub )) && xdnNodeSub != NULL; xdnNodeSub = NULL)
  183. {
  184. InstanceNameItem wmipini;
  185. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlNodeSub.GetAttribute( NULL, ATTRIB_NAME , szKey , fFound, xdnNodeSub )); if(fFound == false) continue;
  186. __MPC_EXIT_IF_METHOD_FAILS(hr, ParseKey( xdnNodeSub, wmipini, fFound ));
  187. if(fFound)
  188. {
  189. m_mapKeyBinding[ szKey.c_str() ] = wmipini;
  190. }
  191. }
  192. //
  193. // Also look for KEYVALUE or VALUE.REFERENCE alone.
  194. //
  195. {
  196. InstanceNameItem wmipini;
  197. __MPC_EXIT_IF_METHOD_FAILS(hr, ParseKey( xdnNode, wmipini, fFound ));
  198. if(fFound)
  199. {
  200. m_mapKeyBinding[ TAG_KEYBINDING_NAME_DEFAULT ] = wmipini;
  201. }
  202. }
  203. }
  204. }
  205. hr = S_OK;
  206. __HCP_FUNC_CLEANUP;
  207. __HCP_FUNC_EXIT(hr);
  208. }
  209. ////////////////////////////////////////////////
  210. HRESULT WMIParser::InstanceName::put_Node( /*[in] */ IXMLDOMNode* pxdnNode ,
  211. /*[out]*/ bool& fEmpty )
  212. {
  213. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::put_Node" );
  214. HRESULT hr;
  215. __MPC_PARAMCHECK_BEGIN(hr)
  216. __MPC_PARAMCHECK_NOTNULL(pxdnNode);
  217. __MPC_PARAMCHECK_END();
  218. m_xmlNode = pxdnNode;
  219. fEmpty = true;
  220. //
  221. // Analize the node...
  222. //
  223. __MPC_EXIT_IF_METHOD_FAILS(hr, ParseNamespace());
  224. __MPC_EXIT_IF_METHOD_FAILS(hr, ParseKeys ());
  225. //
  226. // If any of these fields is empty, probably the InstanceName is malformed, so it's best to skip it...
  227. //
  228. if(m_szNamespace.length() != 0 &&
  229. m_szClass .length() != 0 )
  230. {
  231. fEmpty = false;
  232. }
  233. hr = S_OK;
  234. __HCP_FUNC_CLEANUP;
  235. __HCP_FUNC_EXIT(hr);
  236. }
  237. ////////////////////////////////////////////////
  238. HRESULT WMIParser::InstanceName::get_Namespace( /*[out]*/ MPC::wstring& szNamespace )
  239. {
  240. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::get_Namespace" );
  241. HRESULT hr;
  242. szNamespace = m_szNamespace;
  243. hr = S_OK;
  244. __HCP_FUNC_EXIT(hr);
  245. }
  246. HRESULT WMIParser::InstanceName::get_Class( /*[out]*/ MPC::wstring& szClass )
  247. {
  248. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::get_Class" );
  249. HRESULT hr;
  250. szClass = m_szClass;
  251. hr = S_OK;
  252. __HCP_FUNC_EXIT(hr);
  253. }
  254. HRESULT WMIParser::InstanceName::get_KeyBinding( /*[out]*/ KeyIterConst& itBegin ,
  255. /*[out]*/ KeyIterConst& itEnd )
  256. {
  257. __HCP_FUNC_ENTRY( "WMIParser::InstanceName::get_KeyBinding" );
  258. HRESULT hr;
  259. itBegin = m_mapKeyBinding.begin();
  260. itEnd = m_mapKeyBinding.end ();
  261. hr = S_OK;
  262. __HCP_FUNC_EXIT(hr);
  263. }