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.

459 lines
14 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. ParamConfig.cpp
  5. Abstract:
  6. Implements the class CParamList that contains methods for traversing the elements
  7. of XML file that contains the parameters required by the search engine. A sample parameter list
  8. (also known as config file) XML file if shown here -
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <CONFIG_DATA
  11. SERVER_URL = "http://gsadevnet/GSASearch/search.asmx/Search"
  12. REMOTECONFIG_SERVER_URL = "http://gsadevnet/GSASearch/search.asmx/"
  13. UPDATE_FREQUENCY = "3">
  14. <PARAM_ITEM NAME="ProdID" TYPE="CONFIG_DATA">
  15. <DESCRIPTION>Choose one of the following products:</DESCRIPTION>
  16. <PARAM_VALUE VALUE="enable">
  17. <DISPLAYSTRING>Accessibility</DISPLAYSTRING>
  18. </PARAM_VALUE>
  19. <PARAM_VALUE VALUE="drx" DEFAULT="true">
  20. <DISPLAYSTRING>DirectX (Home User)</DISPLAYSTRING>
  21. </PARAM_VALUE>
  22. </PARAM_ITEM>
  23. </CONFIG_DATA>
  24. Revision History:
  25. a-prakac created 12/05/2000
  26. ********************************************************************/
  27. #include "stdafx.h"
  28. static const WCHAR g_wszMutexName[] = L"PCH_PARAMCONFIG";
  29. /////////////////////////////////////////////////////////////////////
  30. CFG_BEGIN_FIELDS_MAP(CParamList::CParamValue)
  31. CFG_ATTRIBUTE( L"VALUE" , BSTR, m_bstrValue ),
  32. CFG_ELEMENT ( L"DISPLAYSTRING", BSTR, m_bstrDisplayString ),
  33. CFG_ATTRIBUTE( L"DEFAULT" , bool, m_bDefault ),
  34. CFG_END_FIELDS_MAP()
  35. CFG_BEGIN_CHILD_MAP(CParamList::CParamValue)
  36. CFG_END_CHILD_MAP()
  37. DEFINE_CFG_OBJECT(CParamList::CParamValue, L"PARAM_VALUE")
  38. DEFINE_CONFIG_METHODS__NOCHILD(CParamList::CParamValue)
  39. /////////////////////////////////////////////////////////////////////
  40. /////////////////////////////////////////////////////////////////////
  41. CFG_BEGIN_FIELDS_MAP(CParamList::CParamItem)
  42. CFG_ATTRIBUTE( L"NAME" , BSTR, m_bstrName ),
  43. CFG_ATTRIBUTE( L"TYPE" , BSTR, m_bstrType ),
  44. CFG_ELEMENT ( L"DESCRIPTION", BSTR, m_bstrDescription ),
  45. CFG_ATTRIBUTE( L"REQUIRED" , bool, m_bRequired ),
  46. CFG_ATTRIBUTE( L"VISIBLE" , bool, m_bVisible ),
  47. CFG_END_FIELDS_MAP()
  48. CFG_BEGIN_CHILD_MAP(CParamList::CParamItem)
  49. CFG_CHILD(CParamList::CParamValue)
  50. CFG_END_CHILD_MAP()
  51. DEFINE_CFG_OBJECT(CParamList::CParamItem, L"PARAM_ITEM")
  52. DEFINE_CONFIG_METHODS_CREATEINSTANCE_SECTION(CParamList::CParamItem,tag,defSubType)
  53. if(tag == _cfg_table_tags[0])
  54. {
  55. defSubType = &(*(m_lstParamValue.insert( m_lstParamValue.end() )));
  56. return S_OK;
  57. }
  58. DEFINE_CONFIG_METHODS_SAVENODE_SECTION(CParamList::CParamItem,xdn)
  59. hr = MPC::Config::SaveList( m_lstParamValue, xdn );
  60. DEFINE_CONFIG_METHODS_END(CParamList::CParamItem)
  61. /////////////////////////////////////////////////////////////////////
  62. CFG_BEGIN_FIELDS_MAP(CParamList)
  63. CFG_ATTRIBUTE ( L"SERVER_URL" , BSTR, m_bstrServerURL ),
  64. CFG_ELEMENT ( L"SEARCHENGINE_NAME" , BSTR, m_bstrSearchEngineName ),
  65. CFG_ELEMENT ( L"SEARCHENGINE_DESCRIPTION", BSTR, m_bstrSearchEngineDescription ),
  66. CFG_ELEMENT ( L"SEARCHENGINE_OWNER" , BSTR, m_bstrSearchEngineOwner ),
  67. CFG_ATTRIBUTE ( L"UPDATE_FREQUENCY" , long, m_lUpdateFrequency ),
  68. CFG_ATTRIBUTE__TRISTATE( L"REMOTECONFIG_SERVER_URL" , BSTR, m_bstrRemoteConfigServerURL , m_bRemoteServerUrlPresent ),
  69. CFG_ATTRIBUTE__TRISTATE( L"ERROR_INFO" , BSTR, m_bstrErrorInfo , m_bError ),
  70. CFG_ATTRIBUTE__TRISTATE( L"STANDARD_SEARCH" , bool, m_bStandardSearch , m_bSearchTypePresent ),
  71. CFG_END_FIELDS_MAP()
  72. CFG_BEGIN_CHILD_MAP(CParamList)
  73. CFG_CHILD(CParamList::CParamItem)
  74. CFG_END_CHILD_MAP()
  75. DEFINE_CFG_OBJECT(CParamList, L"CONFIG_DATA")
  76. DEFINE_CONFIG_METHODS_CREATEINSTANCE_SECTION(CParamList,tag,defSubType)
  77. if(tag == _cfg_table_tags[0])
  78. {
  79. defSubType = &(*(m_lstParamItem.insert( m_lstParamItem.end() )));
  80. return S_OK;
  81. }
  82. DEFINE_CONFIG_METHODS_SAVENODE_SECTION(CParamList,xdn)
  83. hr = MPC::Config::SaveList( m_lstParamItem, xdn );
  84. DEFINE_CONFIG_METHODS_END(CParamList)
  85. /////////////////////////////////////////////////////////////////////
  86. /////////////////////////////////////////////////////////////////////
  87. bool CParamList::CParamItem::FindDefaultValue( /*[out]*/ ParamValueIter& it )
  88. {
  89. for(it = m_lstParamValue.begin(); it != m_lstParamValue.end(); it++)
  90. {
  91. if(it->m_bDefault == true) return true;
  92. }
  93. return false;
  94. }
  95. /////////////////////////////////////////////////////////////////////
  96. /////////////////////////////////////////////////////////////////////
  97. // Commenting out MPC:: is a workaround for a compiler bug.
  98. CParamList::CParamList() : /*MPC::*/NamedMutex( g_wszMutexName )
  99. {
  100. // Initialize the Update Frequency to -1 so that in case the server hasnt provided an update frequency
  101. // then the default frequency can be used instead
  102. m_lUpdateFrequency = -1;
  103. m_bStandardSearch = true;
  104. }
  105. CParamList::~CParamList()
  106. {
  107. }
  108. bool CParamList::IsStandardSearch()
  109. {
  110. return (m_bSearchTypePresent ? m_bStandardSearch : true);
  111. }
  112. /************
  113. Method - CParamList::Load(BSTR bstrConfigFilePath)
  114. Description - This method loads the XML file (whose location is given bstrConfigFilePath)
  115. into a list and sets the iterator of the list to the first element in the list. It then loads the XML file
  116. into a DOM tree and retrieves a collection of nodes with tag name PARAM_ITEM.
  117. ************/
  118. HRESULT CParamList::Load( /*[in]*/ BSTR bstrLCID, /*[in]*/ BSTR bstrID, /*[in]*/ BSTR bstrXMLConfigData )
  119. {
  120. __HCP_FUNC_ENTRY( "CParamList::Load" );
  121. HRESULT hr;
  122. bool fLoaded;
  123. bool fFound;
  124. MPC::XmlUtil xmlConfigData;
  125. CComPtr<IStream> pStream;
  126. MPC::wstring strFileName;
  127. CComPtr<IXMLDOMNode> ptrDOMNode;
  128. CComBSTR bstrXML;
  129. //
  130. // First try to load the file from the user setting path - if that fails then load the ConfigData
  131. // The file, if present, is located in user settings directory and is named bstrID_bstrLCID.xml
  132. //
  133. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::GetUserWritablePath( strFileName, HC_ROOT_HELPCTR ));
  134. m_bstrConfigFilePath.Append( strFileName.c_str() );
  135. m_bstrConfigFilePath.Append( L"\\" );
  136. m_bstrConfigFilePath.Append( bstrID );
  137. m_bstrConfigFilePath.Append( L"_" );
  138. m_bstrConfigFilePath.Append( bstrLCID );
  139. m_bstrConfigFilePath.Append( L".xml" );
  140. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlConfigData.Load( m_bstrConfigFilePath, NSW_TAG_CONFIGDATA, fLoaded, &fFound ));
  141. if(!fFound)
  142. {
  143. // The file could not be loaded for some reason - try loading the package_description.xml data
  144. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlConfigData.LoadAsString( bstrXMLConfigData, NSW_TAG_DATA, fLoaded, &fFound ));
  145. if(!fFound)
  146. {
  147. // Even if this cant be loaded then exit
  148. __MPC_SET_ERROR_AND_EXIT(hr, S_OK);
  149. }
  150. // Now load the CONFIG_DATA section
  151. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlConfigData.GetNode( NSW_TAG_CONFIGDATA, &ptrDOMNode ) );
  152. __MPC_EXIT_IF_METHOD_FAILS(hr, ptrDOMNode->get_xml( &bstrXML ));
  153. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlConfigData.LoadAsString( bstrXML, NSW_TAG_CONFIGDATA, fLoaded, &fFound ));
  154. if(!fFound)
  155. {
  156. // Cant be loaded - exit
  157. __MPC_SET_ERROR_AND_EXIT(hr, S_OK);
  158. }
  159. }
  160. // At this point the XML data has been loaded
  161. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlConfigData.SaveAsStream( (IUnknown**)&pStream ));
  162. __MPC_EXIT_IF_METHOD_FAILS(hr, MPC::Config::LoadStream ( this, pStream ));
  163. //
  164. // For each parameter, copy the XML blob.
  165. //
  166. {
  167. CComPtr<IXMLDOMNodeList> xdnl;
  168. CComPtr<IXMLDOMNode> xdn;
  169. __MPC_EXIT_IF_METHOD_FAILS(hr, xmlConfigData.GetNodes( NSW_TAG_PARAMITEM, &xdnl ));
  170. if(xdnl)
  171. {
  172. for(ParamItemIter it = m_lstParamItem.begin(); it != m_lstParamItem.end() && SUCCEEDED(xdnl->nextNode( &xdn )) && xdn; it++, xdn.Release())
  173. {
  174. __MPC_EXIT_IF_METHOD_FAILS(hr, xdn->get_xml( &it->m_bstrXML ));
  175. }
  176. }
  177. }
  178. __MPC_EXIT_IF_METHOD_FAILS(hr, MoveFirst());
  179. hr = S_OK;
  180. __HCP_FUNC_CLEANUP;
  181. __HCP_FUNC_EXIT(hr);
  182. }
  183. /************
  184. Method - CParamList::IsCursorValid(), MoveFirst(), MoveNext()
  185. Description - These methods are used to traverse the list that contains the various XML elements of the
  186. loaded file.
  187. ************/
  188. HRESULT CParamList::ClearResults()
  189. {
  190. __HCP_FUNC_ENTRY( "CParamList::ClearResult" );
  191. m_lstParamItem.clear();
  192. return S_OK;
  193. }
  194. bool CParamList::IsCursorValid()
  195. {
  196. return (m_itCurrentParam != m_lstParamItem.end());
  197. }
  198. HRESULT CParamList::MoveFirst()
  199. {
  200. m_itCurrentParam = m_lstParamItem.begin();
  201. return S_OK;
  202. }
  203. HRESULT CParamList::MoveNext()
  204. {
  205. if(IsCursorValid())
  206. {
  207. m_itCurrentParam++;
  208. }
  209. return S_OK;
  210. }
  211. /************
  212. Method - CParamList::get_Name, get_ServerUrl, get_ConfigFilePath, get_Type
  213. Description - Properties for getting the corresponding items.
  214. ************/
  215. HRESULT CParamList::get_Name( /*[out]*/ CComBSTR& bstrName )
  216. {
  217. if(IsCursorValid()) bstrName = m_itCurrentParam->m_bstrName;
  218. return S_OK;
  219. }
  220. HRESULT CParamList::get_ServerUrl( /*[out]*/ CComBSTR& bstrServerURL )
  221. {
  222. bstrServerURL = m_bstrServerURL;
  223. return S_OK;
  224. }
  225. HRESULT CParamList::get_RemoteServerUrl( /*[out]*/ CComBSTR& bstrRemoteServerURL )
  226. {
  227. bstrRemoteServerURL = m_bstrRemoteConfigServerURL;
  228. return S_OK;
  229. }
  230. bool CParamList::RemoteConfig()
  231. {
  232. return m_bRemoteServerUrlPresent;
  233. }
  234. HRESULT CParamList::get_UpdateFrequency( /*[out]*/ long& lUpdateFrequency )
  235. {
  236. lUpdateFrequency = m_lUpdateFrequency;
  237. return S_OK;
  238. }
  239. HRESULT CParamList::get_SearchEngineName(/*[out]*/ CComBSTR& bstrSEName )
  240. {
  241. bstrSEName = m_bstrSearchEngineName;
  242. return S_OK;
  243. }
  244. HRESULT CParamList::get_SearchEngineDescription( /*[out]*/CComBSTR& bstrSEDescription )
  245. {
  246. bstrSEDescription = m_bstrSearchEngineDescription;
  247. return S_OK;
  248. }
  249. HRESULT CParamList::get_SearchEngineOwner( /*[out]*/ CComBSTR& bstrSEOwner )
  250. {
  251. bstrSEOwner = m_bstrSearchEngineOwner;
  252. return S_OK;
  253. }
  254. HRESULT CParamList::get_ConfigFilePath( /*[out]*/CComBSTR& bstrFilePath )
  255. {
  256. bstrFilePath = m_bstrConfigFilePath;
  257. return S_OK;
  258. }
  259. HRESULT CParamList::get_Type( /*[in]*/ BSTR bstrType, /*[out]*/ ParamTypeEnum& enmParamType)
  260. {
  261. if (MPC::StrICmp( bstrType, L"PARAM_UI1" ) == 0) enmParamType = PARAM_UI1;
  262. else if(MPC::StrICmp( bstrType, L"PARAM_I2" ) == 0) enmParamType = PARAM_I2;
  263. else if(MPC::StrICmp( bstrType, L"PARAM_I4" ) == 0) enmParamType = PARAM_I4;
  264. else if(MPC::StrICmp( bstrType, L"PARAM_R4" ) == 0) enmParamType = PARAM_R4;
  265. else if(MPC::StrICmp( bstrType, L"PARAM_R8" ) == 0) enmParamType = PARAM_R8;
  266. else if(MPC::StrICmp( bstrType, L"PARAM_BOOL" ) == 0) enmParamType = PARAM_BOOL;
  267. else if(MPC::StrICmp( bstrType, L"PARAM_DATE" ) == 0) enmParamType = PARAM_DATE;
  268. else if(MPC::StrICmp( bstrType, L"PARAM_BSTR" ) == 0) enmParamType = PARAM_BSTR;
  269. else if(MPC::StrICmp( bstrType, L"PARAM_I1" ) == 0) enmParamType = PARAM_I1;
  270. else if(MPC::StrICmp( bstrType, L"PARAM_UI2" ) == 0) enmParamType = PARAM_UI2;
  271. else if(MPC::StrICmp( bstrType, L"PARAM_UI4" ) == 0) enmParamType = PARAM_UI4;
  272. else if(MPC::StrICmp( bstrType, L"PARAM_INT" ) == 0) enmParamType = PARAM_INT;
  273. else if(MPC::StrICmp( bstrType, L"PARAM_UINT" ) == 0) enmParamType = PARAM_UINT;
  274. else if(MPC::StrICmp( bstrType, L"PARAM_LIST" ) == 0) enmParamType = PARAM_LIST;
  275. return S_OK;
  276. }
  277. /************
  278. Method - CParamList::InitializeParamObject( SearchEngine::ParamItem_Definition2& def )
  279. Description - This method is called to initialize a parameter item object. Initializes
  280. with the current parameter item.
  281. ************/
  282. HRESULT CParamList::InitializeParamObject( /*[out]*/ SearchEngine::ParamItem_Definition2& def )
  283. {
  284. __HCP_FUNC_ENTRY( "CParamList::InitializeParamObject" );
  285. HRESULT hr;
  286. if(IsCursorValid())
  287. {
  288. CParamItem& item = *m_itCurrentParam;
  289. BSTR bstrData = NULL;
  290. __MPC_EXIT_IF_METHOD_FAILS(hr, get_Type( item.m_bstrType, def.m_pteParamType ));
  291. if(def.m_pteParamType == PARAM_LIST)
  292. {
  293. bstrData = item.m_bstrXML;
  294. }
  295. else
  296. {
  297. ParamValueIter itValue;
  298. if(m_itCurrentParam->FindDefaultValue( itValue ))
  299. {
  300. bstrData = itValue->m_bstrValue;
  301. }
  302. }
  303. if(item.m_bstrName .Length()) { def.m_strName = item.m_bstrName ; def.m_szName = def.m_strName .c_str(); }
  304. if(item.m_bstrDescription.Length()) { def.m_strDisplayString = item.m_bstrDescription; def.m_szDisplayString = def.m_strDisplayString.c_str(); }
  305. if(STRINGISPRESENT(bstrData) ) { def.m_strData = bstrData ; def.m_szData = def.m_strData .c_str(); }
  306. def.m_bRequired = item.m_bRequired;
  307. def.m_bVisible = item.m_bVisible;
  308. }
  309. hr = S_OK;
  310. __HCP_FUNC_CLEANUP;
  311. __HCP_FUNC_EXIT(hr);
  312. }
  313. /************
  314. Method - CParamList::GetDefaultValue (CComBSTR bstrParamName, MPC::wstring& wszValue)
  315. Description - This method is called to get the default value for a parameter.
  316. ************/
  317. HRESULT CParamList::GetDefaultValue( /*[in]*/ BSTR bstrParamName, /*[in,out]*/ MPC::wstring& strValue )
  318. {
  319. __HCP_FUNC_ENTRY("CParamList::GetDefaultValue");
  320. HRESULT hr;
  321. __MPC_EXIT_IF_METHOD_FAILS(hr, MoveFirst());
  322. while(IsCursorValid())
  323. {
  324. if(MPC::StrCmp( m_itCurrentParam->m_bstrName, bstrParamName ) == 0)
  325. {
  326. ParamValueIter itValue;
  327. if(m_itCurrentParam->FindDefaultValue( itValue ))
  328. {
  329. strValue = SAFEBSTR(itValue->m_bstrValue);
  330. }
  331. break;
  332. }
  333. __MPC_EXIT_IF_METHOD_FAILS(hr, MoveNext());
  334. }
  335. hr = S_OK;
  336. __HCP_FUNC_CLEANUP;
  337. __HCP_FUNC_EXIT(hr);
  338. }