ConfigSchemaHelper.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2015 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jptree.hpp"
  14. #include "XMLTags.h"
  15. #include <cstring>
  16. #include "jfile.hpp"
  17. #include "ConfigSchemaHelper.hpp"
  18. #include "SchemaAttributes.hpp"
  19. #include "SchemaElement.hpp"
  20. #include "SchemaEnumeration.hpp"
  21. #include "ExceptionStrings.hpp"
  22. #include "BuildSet.hpp"
  23. #include "SchemaMapManager.hpp"
  24. #include "ConfigSchemaHelper.hpp"
  25. #include "ConfigFileUtils.hpp"
  26. #include "JSONMarkUp.hpp"
  27. #define SUCCESS 0
  28. #define FAILURE 1
  29. #define LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET \
  30. int nComponentCount = CBuildSetManager::getInstance()->getBuildSetComponentCount(); \
  31. \
  32. for (int idx = 0; idx < nComponentCount; idx++)
  33. using namespace CONFIGURATOR;
  34. CConfigSchemaHelper* CConfigSchemaHelper::s_pCConfigSchemaHelper = NULL;
  35. CConfigSchemaHelper* CConfigSchemaHelper::getInstance(const char* pDefaultDirOverride)
  36. {
  37. // not thread safe!!!
  38. if (s_pCConfigSchemaHelper == NULL)
  39. {
  40. s_pCConfigSchemaHelper = new CConfigSchemaHelper();
  41. s_pCConfigSchemaHelper->m_nTables = 0;
  42. if (pDefaultDirOverride != NULL && pDefaultDirOverride[0] != 0)
  43. s_pCConfigSchemaHelper->setBasePath(pDefaultDirOverride);
  44. }
  45. return s_pCConfigSchemaHelper;
  46. }
  47. CConfigSchemaHelper* CConfigSchemaHelper::getInstance(const char* pBuildSetFileName, const char *pBaseDirectory, const char *pDefaultDirOverride)
  48. {
  49. assert(pBuildSetFileName != NULL);
  50. assert(pBaseDirectory != NULL);
  51. if (s_pCConfigSchemaHelper == NULL && pBuildSetFileName != NULL && pBaseDirectory != NULL)
  52. {
  53. s_pCConfigSchemaHelper = new CConfigSchemaHelper(pBuildSetFileName, pBaseDirectory, pDefaultDirOverride);
  54. s_pCConfigSchemaHelper->m_nTables = 0;
  55. }
  56. return s_pCConfigSchemaHelper;
  57. }
  58. CConfigSchemaHelper::CConfigSchemaHelper(const char* pBuildSetFile, const char* pBuildSetDir, const char* pDefaultDirOverride) : m_pBasePath(NULL), m_nTables(0),\
  59. m_pEnvPropertyTree(NULL), m_pSchemaMapManager(NULL)
  60. {
  61. assert(pBuildSetFile != NULL);
  62. assert(pBuildSetDir != NULL);
  63. CBuildSetManager::getInstance(pBuildSetFile, pBuildSetDir);
  64. m_pSchemaMapManager = new CSchemaMapManager();
  65. }
  66. CConfigSchemaHelper::~CConfigSchemaHelper()
  67. {
  68. delete[] m_pBasePath;
  69. delete CConfigSchemaHelper::m_pSchemaMapManager;
  70. CConfigSchemaHelper::m_pSchemaMapManager = NULL;
  71. CConfigSchemaHelper::s_pCConfigSchemaHelper = NULL;
  72. }
  73. bool CConfigSchemaHelper::populateSchema()
  74. {
  75. assert(m_pSchemaMapManager != NULL);
  76. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  77. {
  78. const char *pSchemaName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
  79. if (pSchemaName != NULL)
  80. {
  81. CXSDNodeBase *pNull = NULL;
  82. CSchema *pSchema = CSchema::load(pSchemaName, pNull);
  83. assert(pSchema->getLinkCount() == 1);
  84. m_pSchemaMapManager->setSchemaForXSD(pSchemaName, pSchema);
  85. }
  86. }
  87. populateEnvXPath();
  88. return true;
  89. }
  90. void CConfigSchemaHelper::printConfigSchema(StringBuffer &strXML) const
  91. {
  92. assert(m_pSchemaMapManager != NULL);
  93. const char *pComponent = NULL;
  94. CSchema* pSchema = NULL;
  95. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  96. {
  97. const char *pSchemaName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
  98. if (pComponent == NULL || strcmp(pComponent, pSchemaName) == 0)
  99. {
  100. const char* pXSDSchema = pSchemaName;
  101. if (pXSDSchema == NULL)
  102. continue;
  103. pSchema = m_pSchemaMapManager->getSchemaForXSD(pSchemaName);
  104. if (pSchema != NULL)
  105. {
  106. if (strXML.length() > 0 ? strcmp(strXML.str(), pXSDSchema) == 0 : true)
  107. pSchema->dump(::std::cout);
  108. }
  109. }
  110. }
  111. }
  112. const char* CConfigSchemaHelper::printDocumentation(const char* comp)
  113. {
  114. assert(comp != NULL && *comp != 0);
  115. assert(m_pSchemaMapManager != NULL);
  116. if (comp == NULL || *comp == 0)
  117. return NULL;
  118. CSchema* pSchema = NULL;
  119. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  120. {
  121. const char *pSchemaName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
  122. if (pSchemaName != NULL && strcmp(comp, pSchemaName) == 0)
  123. {
  124. pSchema = m_pSchemaMapManager->getSchemaForXSD(pSchemaName);
  125. assert(pSchema != NULL);
  126. if (pSchema != NULL)
  127. {
  128. static StringBuffer strDoc;
  129. strDoc.clear(); // needed when printing more than 1 component
  130. pSchema->getDocumentation(strDoc);
  131. return strDoc.str();
  132. }
  133. }
  134. }
  135. return NULL;
  136. }
  137. void CConfigSchemaHelper::printJSON(const char* comp, char **pOutput, int nIdx, bool bCleanUp) const
  138. {
  139. if (! (comp != NULL && *comp != 0) )
  140. {
  141. DBGLOG("no component selected for JSON, index = %d", nIdx);
  142. return;
  143. }
  144. assert(m_pSchemaMapManager != NULL);
  145. StringBuffer strJSON;
  146. strJSON.clear();
  147. resetTables();
  148. CSchema* pSchema = NULL;
  149. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  150. {
  151. const char *pSchemaName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
  152. //const char *pSchemaName = CBuildSetManager::getInstance()->getBuildSetComponentName(idx);
  153. if (pSchemaName != NULL && strcmp(comp, pSchemaName) == 0)
  154. {
  155. pSchema = m_pSchemaMapManager->getSchemaForXSD(pSchemaName);
  156. assert(pSchema != NULL);
  157. if (pSchema != NULL)
  158. {
  159. pSchema->getJSON(strJSON, 0, nIdx);
  160. *pOutput = (char*)malloc((sizeof(char))* (strJSON.length())+1);
  161. if (bCleanUp == true)
  162. {
  163. this->clearLF(strJSON);
  164. strJSON.replaceString("\\","\\\\");
  165. }
  166. sprintf(*pOutput,"%s",strJSON.str());
  167. return;
  168. }
  169. else
  170. {
  171. *pOutput = NULL;
  172. }
  173. }
  174. }
  175. }
  176. void CConfigSchemaHelper::printJSONByKey(const char* key, char **pOutput, bool bCleanUp) const
  177. {
  178. if (! (key != NULL && *key != 0) )
  179. {
  180. DBGLOG("no component key provided for to generate JSON");
  181. return;
  182. }
  183. assert(m_pSchemaMapManager != NULL);
  184. if (key[0] == '#')
  185. key = &(key[1]);
  186. StringBuffer strKey(key);
  187. StringBuffer strJSON;
  188. strJSON.clear();
  189. resetTables();
  190. const char *pChar = strrchr(key,'[');
  191. assert(pChar != NULL && strlen(pChar) >= 3);
  192. int length = strlen(pChar);
  193. StringBuffer strIdx;
  194. pChar++;
  195. int nTemp = 0;
  196. do
  197. {
  198. strIdx.append(*(pChar+nTemp));
  199. nTemp++;
  200. }while (*(pChar+nTemp) != ']');
  201. int nIndexForJSON = atoi(strIdx.str());
  202. strKey.setLength(strKey.length()-length); // remove [N] from XPath;
  203. CSchema* pSchema = NULL;
  204. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  205. {
  206. const char *pProcessName = CBuildSetManager::getInstance()->getBuildSetProcessName(idx);
  207. if (pProcessName != NULL && strcmp(strKey.str(), pProcessName) == 0)
  208. {
  209. pSchema = m_pSchemaMapManager->getSchemaForXSD(CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx));
  210. assert(pSchema != NULL);
  211. if (pSchema != NULL)
  212. {
  213. pSchema->getJSON(strJSON, 0, nIndexForJSON-1);
  214. *pOutput = (char*)malloc((sizeof(char))* (strJSON.length())+1);
  215. if (bCleanUp == true)
  216. {
  217. this->clearLF(strJSON);
  218. strJSON.replaceString("\\","\\\\");
  219. }
  220. sprintf(*pOutput,"%s",strJSON.str());
  221. return;
  222. }
  223. else
  224. {
  225. *pOutput = NULL;
  226. }
  227. }
  228. }
  229. }
  230. void CConfigSchemaHelper::printNavigatorJSON(char **pOutput, bool bCleanUp) const
  231. {
  232. StringBuffer strJSON;
  233. CJSONMarkUpHelper::getNavigatorJSON(strJSON);
  234. if (strJSON.length() == 0)
  235. *pOutput = NULL;
  236. *pOutput = (char*)malloc((sizeof(char))* (strJSON.length())+1);
  237. if (bCleanUp == true)
  238. {
  239. this->clearLF(strJSON);
  240. strJSON.replaceString("\\","\\\\");
  241. }
  242. sprintf(*pOutput,"%s",strJSON.str());
  243. return;
  244. }
  245. void CConfigSchemaHelper::printDump(const char* comp) const
  246. {
  247. assert(comp != NULL && *comp != 0);
  248. assert(m_pSchemaMapManager != NULL);
  249. if (comp == NULL || *comp == 0)
  250. return;
  251. CSchema* pSchema = NULL;
  252. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  253. {
  254. const char *pSchemaName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
  255. if (pSchemaName != NULL && strcmp(comp, pSchemaName) == 0)
  256. {
  257. pSchema = m_pSchemaMapManager->getSchemaForXSD(pSchemaName);
  258. assert(pSchema != NULL);
  259. if (pSchema != NULL)
  260. pSchema->dump(::std::cout);
  261. }
  262. }
  263. }
  264. //test purposes
  265. bool CConfigSchemaHelper::getXMLFromSchema(StringBuffer& strXML, const char* pComponent)
  266. {
  267. assert (m_pSchemaMapManager != NULL);
  268. CAttributeArray *pAttributeArray = NULL;
  269. CElementArray *pElementArray = NULL;
  270. CSchema* pSchema = NULL;
  271. strXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Environment>\n\t<Software>");
  272. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  273. {
  274. const char *pSchemaName = CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx);
  275. if (pComponent == NULL || strcmp(pComponent, pSchemaName) == 0)
  276. {
  277. if (pSchemaName == NULL)
  278. continue;
  279. pSchema = m_pSchemaMapManager->getSchemaForXSD(pSchemaName);
  280. if (pSchema != NULL)
  281. strXML.append(pSchema->getXML(NULL));
  282. }
  283. }
  284. strXML.append("\t</Software>\n</Environment>\n");
  285. return true;
  286. }
  287. void CConfigSchemaHelper::addExtensionToBeProcessed(CExtension *pExtension)
  288. {
  289. assert(pExtension != NULL);
  290. if (pExtension != NULL)
  291. m_extensionArr.append(*pExtension);
  292. }
  293. void CConfigSchemaHelper::addAttributeGroupToBeProcessed(CAttributeGroup *pAttributeGroup)
  294. {
  295. assert(pAttributeGroup != NULL);
  296. if (pAttributeGroup != NULL)
  297. m_attributeGroupArr.append(*pAttributeGroup);
  298. }
  299. void CConfigSchemaHelper::addNodeForTypeProcessing(CXSDNodeWithType *pNode)
  300. {
  301. assert(pNode != NULL);
  302. if (pNode != NULL)
  303. m_nodeWithTypeArr.append(*pNode);
  304. }
  305. void CConfigSchemaHelper::addNodeForBaseProcessing(CXSDNodeWithBase *pNode)
  306. {
  307. assert(pNode != NULL);
  308. if (pNode != NULL)
  309. m_nodeWithBaseArr.append(*pNode);
  310. }
  311. void CConfigSchemaHelper::processExtensionArr()
  312. {
  313. int length = m_extensionArr.length();
  314. for (int idx = 0; idx < length; idx++)
  315. {
  316. CExtension &Extension = (m_extensionArr.item(idx));
  317. const char *pName = Extension.getBase();
  318. assert(pName != NULL);
  319. if (pName != NULL)
  320. {
  321. CXSDNode *pNodeBase = NULL;
  322. pNodeBase = m_pSchemaMapManager->getSimpleTypeWithName(pName) != NULL ? dynamic_cast<CSimpleType*>(m_pSchemaMapManager->getSimpleTypeWithName(pName)) : NULL;
  323. if (pNodeBase == NULL)
  324. pNodeBase = m_pSchemaMapManager->getComplexTypeWithName(pName) != NULL ? dynamic_cast<CComplexType*>(m_pSchemaMapManager->getComplexTypeWithName(pName)) : NULL ;
  325. assert(pNodeBase != NULL);
  326. if (pNodeBase != NULL)
  327. Extension.setBaseNode(pNodeBase);
  328. }
  329. }
  330. m_extensionArr.popAll(false);
  331. }
  332. void CConfigSchemaHelper::processAttributeGroupArr()
  333. {
  334. aindex_t length = m_attributeGroupArr.length();
  335. for (aindex_t idx = 0; idx < length; idx++)
  336. {
  337. CAttributeGroup &AttributeGroup = (m_attributeGroupArr.item(idx));
  338. const char *pRef = AttributeGroup.getRef();
  339. assert(pRef != NULL && pRef[0] != 0);
  340. if (pRef != NULL && pRef[0] != 0)
  341. {
  342. assert(m_pSchemaMapManager != NULL);
  343. CAttributeGroup *pAttributeGroup = m_pSchemaMapManager->getAttributeGroupFromXPath(pRef);
  344. assert(pAttributeGroup != NULL);
  345. if (pAttributeGroup != NULL)
  346. AttributeGroup.setRefNode(pAttributeGroup);
  347. }
  348. }
  349. m_attributeGroupArr.popAll(true);
  350. }
  351. void CConfigSchemaHelper::processNodeWithTypeArr(CXSDNodeBase *pParentNode)
  352. {
  353. int length = m_nodeWithTypeArr.length();
  354. for (int idx = 0; idx < length; idx++)
  355. {
  356. CXSDNodeWithType *pNodeWithType = &(m_nodeWithTypeArr.item(idx));
  357. const char *pTypeName = pNodeWithType->getType();
  358. assert(pTypeName != NULL);
  359. if (pTypeName != NULL)
  360. {
  361. CXSDNode *pNode = NULL;
  362. pNode = m_pSchemaMapManager->getSimpleTypeWithName(pTypeName) != NULL ? dynamic_cast<CSimpleType*>(m_pSchemaMapManager->getSimpleTypeWithName(pTypeName)) : NULL;
  363. if (pNode == NULL)
  364. pNode = m_pSchemaMapManager->getComplexTypeWithName(pTypeName) != NULL ? dynamic_cast<CComplexType*>(m_pSchemaMapManager->getComplexTypeWithName(pTypeName)) : NULL;
  365. if (pNode == NULL)
  366. pNode = CXSDBuiltInDataType::create(pNodeWithType, pTypeName);
  367. if (pNode != NULL)
  368. pNodeWithType->setTypeNode(pNode);
  369. else
  370. PROGLOG("Unsupported type '%s'", pTypeName);
  371. }
  372. }
  373. m_nodeWithTypeArr.popAll(true);
  374. }
  375. void CConfigSchemaHelper::processNodeWithBaseArr()
  376. {
  377. int length = m_nodeWithBaseArr.length();
  378. for (int idx = 0; idx < length; idx++)
  379. {
  380. CXSDNodeWithBase *pNodeWithBase = &(this->m_nodeWithBaseArr.item(idx));
  381. const char *pBaseName = pNodeWithBase->getBase();
  382. assert(pBaseName != NULL);
  383. if (pBaseName != NULL)
  384. {
  385. CXSDNode *pNode = NULL;
  386. pNode = m_pSchemaMapManager->getSimpleTypeWithName(pBaseName) != NULL ? dynamic_cast<CSimpleType*>(m_pSchemaMapManager->getSimpleTypeWithName(pBaseName)) : NULL;
  387. if (pNode == NULL)
  388. pNode = m_pSchemaMapManager->getComplexTypeWithName(pBaseName) != NULL ? dynamic_cast<CComplexType*>(m_pSchemaMapManager->getComplexTypeWithName(pBaseName)) : NULL;
  389. if (pNode == NULL)
  390. pNode = CXSDBuiltInDataType::create(pNode, pBaseName);
  391. assert(pNode != NULL);
  392. if (pNode != NULL)
  393. pNodeWithBase->setBaseNode(pNode);
  394. else
  395. PROGLOG("Unsupported type '%s'", pBaseName);
  396. }
  397. }
  398. m_nodeWithBaseArr.popAll(false);
  399. }
  400. void CConfigSchemaHelper::addElementForRefProcessing(CElement *pElement)
  401. {
  402. assert (pElement != NULL);
  403. if (pElement != NULL)
  404. m_ElementArr.append(*pElement);
  405. }
  406. void CConfigSchemaHelper::processElementArr(CElement *pElement)
  407. {
  408. int length = m_nodeWithBaseArr.length();
  409. for (int idx = 0; idx < length; idx++)
  410. {
  411. CElement *pElement = &(this->m_ElementArr.item(idx));
  412. const char *pRef = pElement->getRef();
  413. assert(pRef != NULL);
  414. if (pRef != NULL)
  415. {
  416. CElement *pRefElementNode = NULL;
  417. pRefElementNode = m_pSchemaMapManager->getElementWithName(pRef);
  418. if (pRefElementNode != NULL)
  419. pElement->setRefElementNode(pRefElementNode);
  420. else
  421. //TODO: throw exception
  422. assert(!"Unknown element referenced");
  423. }
  424. }
  425. m_ElementArr.popAll(false);
  426. }
  427. void CConfigSchemaHelper::populateEnvXPath()
  428. {
  429. CSchema* pSchema = NULL;
  430. StringBuffer strXPath;
  431. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  432. {
  433. pSchema = m_pSchemaMapManager->getSchemaForXSD(CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx));
  434. if (pSchema != NULL)
  435. pSchema->populateEnvXPath(strXPath);
  436. }
  437. }
  438. void CConfigSchemaHelper::loadEnvFromConfig(const char *pEnvFile)
  439. {
  440. assert(pEnvFile != NULL);
  441. typedef ::IPropertyTree PT;
  442. Linked<PT> pEnvXMLRoot;
  443. try
  444. {
  445. pEnvXMLRoot.setown(createPTreeFromXMLFile(pEnvFile));
  446. }
  447. catch (...)
  448. {
  449. CONFIGURATOR::MakeExceptionFromMap(EX_STR_CAN_NOT_PROCESS_ENV_XML);
  450. }
  451. CSchema* pSchema = NULL;
  452. this->setEnvPropertyTree(pEnvXMLRoot.getLink());
  453. this->setEnvFilePath(pEnvFile);
  454. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  455. {
  456. pSchema = m_pSchemaMapManager->getSchemaForXSD(CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx));
  457. if (pSchema != NULL)
  458. pSchema->loadXMLFromEnvXml(pEnvXMLRoot);
  459. }
  460. }
  461. void CConfigSchemaHelper::addToolTip(const char *js)
  462. {
  463. assert (js != NULL);
  464. assert (js[0] != 0);
  465. if (js == NULL || js[0] == 0)
  466. return;
  467. m_strToolTipsJS.append(js);
  468. }
  469. const char* CConfigSchemaHelper::getToolTipJS() const
  470. {
  471. static StringBuffer strJS;
  472. strJS.clear();
  473. for (int idx = 0; idx < m_strToolTipsJS.length(); idx++)
  474. {
  475. strJS.append(m_strToolTipsJS.item(idx));
  476. }
  477. return strJS.str();
  478. }
  479. void CConfigSchemaHelper::setEnvTreeProp(const char *pXPath, const char* pValue)
  480. {
  481. assert(pXPath != NULL && pXPath[0] != 0);
  482. assert(m_pSchemaMapManager != NULL);
  483. CAttribute *pAttribute = m_pSchemaMapManager->getAttributeFromXPath(pXPath);
  484. assert(pAttribute != NULL);
  485. StringBuffer strPropName("@");
  486. strPropName.append(pAttribute->getName());
  487. if (this->getEnvPropertyTree()->queryPropTree(pAttribute->getConstAncestorNode(1)->getEnvXPath())->queryProp(strPropName.str()) == NULL)
  488. //should check if this attribute is optional for validation
  489. this->getEnvPropertyTree()->queryPropTree(pAttribute->getConstAncestorNode(1)->getEnvXPath())->setProp(strPropName.str(), pValue);
  490. else if (strcmp (this->getEnvPropertyTree()->queryPropTree(pAttribute->getConstAncestorNode(1)->getEnvXPath())->queryProp(strPropName.str()), pValue) == 0)
  491. return; // nothing changed
  492. else
  493. this->getEnvPropertyTree()->queryPropTree(pAttribute->getConstAncestorNode(1)->getEnvXPath())->setProp(strPropName.str(), pValue);
  494. }
  495. const char* CConfigSchemaHelper::getTableValue(const char* pXPath, int nRow) const
  496. {
  497. assert(pXPath != NULL);
  498. assert(m_pSchemaMapManager != NULL);
  499. CAttribute *pAttribute = m_pSchemaMapManager->getAttributeFromXPath(pXPath);
  500. CElement *pElement = NULL;
  501. if (pAttribute == NULL)
  502. {
  503. pElement = m_pSchemaMapManager->getElementFromXPath(pXPath);
  504. assert(pElement != NULL);
  505. return pElement->getEnvValueFromXML();
  506. }
  507. else
  508. {
  509. assert(pAttribute != NULL);
  510. if (nRow == 1)
  511. return pAttribute->getEnvValueFromXML();
  512. else
  513. {
  514. StringBuffer strXPath(pXPath);
  515. StringBuffer strXPathOrignal(pXPath);
  516. CConfigSchemaHelper::stripXPathIndex(strXPath);
  517. strXPath.appendf("[%d]", nRow);
  518. char pTemp[64];
  519. int offset = strXPath.length() - (strlen(itoa(nRow, pTemp, 10)) - 1);
  520. strXPath.append(strXPathOrignal, strXPath.length(), strXPathOrignal.length()-offset);
  521. pAttribute = m_pSchemaMapManager->getAttributeFromXPath(strXPath.str());
  522. if (pAttribute == NULL)
  523. return NULL;
  524. return pAttribute->getEnvValueFromXML();
  525. }
  526. }
  527. }
  528. int CConfigSchemaHelper::getElementArraySize(const char *pXPath) const
  529. {
  530. assert(pXPath != NULL);
  531. assert(m_pSchemaMapManager != NULL);
  532. //CElementArray *pElementArray = m_pSchemaMapManager->getElementArrayFromXSDXPath(pXPath);
  533. CElementArray *pElementArray = m_pSchemaMapManager->getElementArrayFromXPath(pXPath);
  534. if (pElementArray == NULL)
  535. return 0;
  536. //return pElementArray->getCountOfSiblingElements(pXPath);
  537. VStringBuffer strXPath("%s[1]",pElementArray->getXSDXPath());
  538. //return pElementArray->getCountOfSiblingElements(pElementArray->getXSDXPath());
  539. return pElementArray->getCountOfSiblingElements(strXPath.str());
  540. }
  541. const char* CConfigSchemaHelper::getAttributeXSDXPathFromEnvXPath(const char* pEnvXPath) const
  542. {
  543. assert(pEnvXPath != NULL && *pEnvXPath != 0);
  544. assert(m_pSchemaMapManager != NULL);
  545. CAttribute *pAttribute = m_pSchemaMapManager->getAttributeFromXPath(pEnvXPath);
  546. assert(pAttribute != NULL);
  547. return pAttribute->getXSDXPath();
  548. }
  549. const char* CConfigSchemaHelper::getElementArrayXSDXPathFromEnvXPath(const char* pXSDXPath) const
  550. {
  551. assert(pXSDXPath != NULL);
  552. assert(m_pSchemaMapManager != NULL);
  553. CElementArray *pElementArray = m_pSchemaMapManager->getElementArrayFromXSDXPath(pXSDXPath);
  554. assert(pElementArray != NULL);
  555. return pElementArray->getXSDXPath();
  556. }
  557. void CConfigSchemaHelper::appendAttributeXPath(const char* pXPath)
  558. {
  559. m_strArrayEnvXPaths.append(pXPath);
  560. }
  561. void CConfigSchemaHelper::appendElementXPath(const char* pXPath)
  562. {
  563. m_strArrayEnvXPaths.append(pXPath);
  564. }
  565. int CConfigSchemaHelper::stripXPathIndex(StringBuffer &strXPath)
  566. {
  567. int nLen = strXPath.length()-3;
  568. int nLengthOfStringInBracket = 3;
  569. while (nLen > 0)
  570. {
  571. if (strXPath[nLen] == '[')
  572. {
  573. strXPath.reverse().remove(0,strXPath.length()-nLen).reverse();
  574. return nLengthOfStringInBracket;
  575. }
  576. nLen--;
  577. nLengthOfStringInBracket++;
  578. }
  579. return nLengthOfStringInBracket;
  580. }
  581. bool CConfigSchemaHelper::isXPathTailAttribute(const StringBuffer &strXPath)
  582. {
  583. int nLen = strXPath.length()-3;
  584. while (nLen > 0)
  585. {
  586. if (strXPath[nLen] == '[')
  587. {
  588. if (strXPath[nLen+1] == '@')
  589. return true;
  590. else
  591. return false;
  592. }
  593. nLen--;
  594. }
  595. assert(!"Control should not reach here");
  596. return false;
  597. }
  598. void CConfigSchemaHelper::setBasePath(const char *pBasePath)
  599. {
  600. assert(m_pBasePath == NULL);
  601. int nLength = strlen(pBasePath);
  602. m_pBasePath = new char[nLength+1];
  603. strcpy(m_pBasePath, pBasePath);
  604. }
  605. bool CConfigSchemaHelper::saveConfigurationFile() const
  606. {
  607. assert(m_strEnvFilePath.length() != 0);
  608. if (m_strEnvFilePath.length() == 0)
  609. return false;
  610. if (this->getConstEnvPropertyTree() == NULL)
  611. return false;
  612. StringBuffer strXML;
  613. strXML.appendf("<" XML_HEADER ">\n<!-- Edited with THE CONFIGURATOR -->\n");
  614. ::toXML(this->getConstEnvPropertyTree(), strXML, 0, XML_SortTags | XML_Format);
  615. if (CConfigFileUtils::getInstance()->writeConfigurationToFile(m_strEnvFilePath.str(), strXML.str(), strXML.length()) == CConfigFileUtils::CF_NO_ERROR)
  616. return true;
  617. else
  618. return false;
  619. }
  620. bool CConfigSchemaHelper::saveConfigurationFileAs(const char *pFilePath)
  621. {
  622. assert(pFilePath && *pFilePath);
  623. if (pFilePath == NULL || *pFilePath == 0)
  624. return false;
  625. if (this->getConstEnvPropertyTree() == NULL)
  626. return false;
  627. StringBuffer strXML;
  628. strXML.appendf("<" XML_HEADER ">\n<!-- Edited with THE CONFIGURATOR -->\n");
  629. ::toXML(this->getConstEnvPropertyTree(), strXML, 0, XML_SortTags | XML_Format);
  630. if (CConfigFileUtils::getInstance()->writeConfigurationToFile(pFilePath, strXML.str(), strXML.length()) == CConfigFileUtils::CF_NO_ERROR)
  631. {
  632. m_strEnvFilePath.set(pFilePath);
  633. return true;
  634. }
  635. else
  636. return false;
  637. }
  638. void CConfigSchemaHelper::addKeyRefForReverseAssociation(const CKeyRef *pKeyRef) const
  639. {
  640. }
  641. void CConfigSchemaHelper::processKeyRefReverseAssociation() const
  642. {
  643. }
  644. void CConfigSchemaHelper::addKeyForReverseAssociation(const CKey *pKeyRef) const
  645. {
  646. }
  647. void CConfigSchemaHelper::processKeyReverseAssociation() const
  648. {
  649. }
  650. int CConfigSchemaHelper::getInstancesOfComponentType(const char *pCompType) const
  651. {
  652. assert(pCompType != NULL && *pCompType != 0);
  653. LOOP_THRU_BUILD_SET_MANAGER_BUILD_SET
  654. {
  655. const char *pCompName = CBuildSetManager::getInstance()->getBuildSetComponentTypeName(idx);// ->getBuildSetProcessName(idx);
  656. const char *pProcessName = CBuildSetManager::getInstance()->getBuildSetProcessName(idx);
  657. if (pCompName != NULL && strcmp(pCompName, pCompType) == 0)
  658. {
  659. CSchema *pSchema = m_pSchemaMapManager->getSchemaForXSD(CBuildSetManager::getInstance()->getBuildSetComponentFileName(idx));
  660. assert(pSchema != NULL);
  661. if (pSchema == NULL)
  662. return FAILURE;
  663. int nCount = 0;
  664. ::VStringBuffer strXPath("./%s/%s[%d]", XML_TAG_SOFTWARE, pProcessName, 1);
  665. while (true)
  666. {
  667. ::IPropertyTree *pTree = CConfigSchemaHelper::getInstance()->getEnvPropertyTree();
  668. if (pTree == NULL)
  669. return FAILURE;
  670. if (pTree->queryPropTree(strXPath.str()) == NULL)
  671. return nCount;
  672. nCount++;
  673. strXPath.setf("./%s/%s[%d]", XML_TAG_SOFTWARE, pProcessName, nCount+1);
  674. }
  675. }
  676. }
  677. return SUCCESS;
  678. }
  679. const char* CConfigSchemaHelper::getInstanceNameOfComponentType(const char *pCompType, int idx)
  680. {
  681. if (pCompType == NULL || *pCompType == 0)
  682. return NULL; // throw exception?
  683. if (this->getEnvPropertyTree() == NULL)
  684. return NULL; // throw exception?
  685. ::VStringBuffer strXPath("./%s/%s[%d]", XML_TAG_SOFTWARE, pCompType, idx+1);
  686. typedef ::IPropertyTree jlibIPropertyTree;
  687. const ::IPropertyTree *pTree = const_cast<const jlibIPropertyTree*>(this->getEnvPropertyTree()->queryPropTree(strXPath.str()));
  688. return pTree->queryProp(XML_ATTR_NAME);
  689. }
  690. void CConfigSchemaHelper::clearLF(::StringBuffer& strToClear)
  691. {
  692. strToClear.replaceString("\n","");
  693. }
  694. CConfigSchemaHelper* CConfigSchemaHelper::getNewInstance(const char* pDefaultDirOverride)
  695. {
  696. if (CConfigSchemaHelper::s_pCConfigSchemaHelper != NULL)
  697. {
  698. delete CConfigSchemaHelper::s_pCConfigSchemaHelper;
  699. CConfigSchemaHelper::s_pCConfigSchemaHelper = NULL;
  700. }
  701. CConfigSchemaHelper::getInstance(pDefaultDirOverride);
  702. CConfigSchemaHelper::getInstance()->populateSchema();
  703. return CConfigSchemaHelper::getInstance();
  704. }