]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_XMLReader.cpp
Salome HOME
Process validators on the first feature's creation using the Config_ValidatorReader
[modules/shaper.git] / src / Config / Config_XMLReader.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * Config_XMLReader.cpp
5  *
6  *  Created on: Mar 14, 2014
7  *      Author: sbh
8  */
9
10 #include <Config_XMLReader.h>
11 #include <Config_Keywords.h>
12 #include <Config_Common.h>
13 #include <Config_PropManager.h>
14
15 #include <Events_Loop.h>
16 #include <Events_Error.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19
20 #include <fstream>
21
22 #ifdef WIN32
23 #pragma warning(disable : 4996) // for getenv
24 #endif
25
26 #ifdef _DEBUG
27 #include <iostream>
28 #endif
29
30 Config_XMLReader::Config_XMLReader(const std::string& theXmlFileName)
31     : myXmlDoc(NULL)
32 {
33   std::string prefix = ""; 
34   Config_Prop* aProp = Config_PropManager::findProp("Plugins", "default_path");
35   if (aProp)
36     prefix = aProp->value();
37   /*
38    * Get path to *.xml files (typically ./bin/../plugins/)
39
40    * the problem: application may be launched using python executable,
41    * to use environment variable (at least for the current moment)
42    */
43   if (prefix.empty()) {
44     char* anEnv = getenv("NEW_GEOM_CONFIG_FILE");
45     if (anEnv) {
46       prefix = std::string(anEnv);
47     }
48   }
49 #ifdef WIN32
50     prefix += "\\";
51 #else
52     prefix += "/";
53 #endif
54   myDocumentPath = prefix + theXmlFileName;
55   std::ifstream aTestFile(myDocumentPath);
56   if (!aTestFile) Events_Error::send("Unable to open " + myDocumentPath);
57   aTestFile.close();
58 }
59
60 Config_XMLReader::~Config_XMLReader()
61 {
62   xmlFreeDoc(myXmlDoc);
63 }
64
65 void Config_XMLReader::readAll()
66 {
67   xmlNodePtr aRoot = findRoot();
68   readRecursively(aRoot);
69 }
70
71 void Config_XMLReader::processNode(xmlNodePtr theNode)
72 {
73   if (isNode(theNode, NODE_SOURCE, NULL)) {
74     std::string aSourceFile = getProperty(theNode, SOURCE_FILE);
75     Config_XMLReader aSourceReader = Config_XMLReader(aSourceFile);
76     readRecursively(aSourceReader.findRoot());
77 #ifdef _DEBUG
78     //std::cout << "Config_XMLReader::sourced node: " << aSourceFile << std::endl;
79 #endif
80   }
81 }
82
83 bool Config_XMLReader::processChildren(xmlNodePtr aNode)
84 {
85   return true;
86 }
87
88 xmlNodePtr Config_XMLReader::findRoot()
89 {
90   if (myXmlDoc == NULL) {
91     myXmlDoc = xmlParseFile(myDocumentPath.c_str());
92   }
93   if (myXmlDoc == NULL) {
94 #ifdef _DEBUG
95     std::cout << "Config_XMLReader::import: " << "Document " << myDocumentPath
96     << " is not parsed successfully." << std::endl;
97 #endif
98     return NULL;
99   }
100   xmlNodePtr aRoot = xmlDocGetRootElement(myXmlDoc);
101 #ifdef _DEBUG
102   if(aRoot == NULL) {
103     std::cout << "Config_XMLReader::import: " << "Error: empty document";
104   }
105 #endif
106   return aRoot;
107 }
108
109 void Config_XMLReader::readRecursively(xmlNodePtr theParent)
110 {
111   if (!theParent)
112     return;
113   xmlNodePtr aNode = theParent->xmlChildrenNode;
114   for (; aNode; aNode = aNode->next) {
115     //Still no text processing in features...
116     if (!isElementNode(aNode)) {
117       continue;
118     }
119     processNode(aNode);
120     if (processChildren(aNode)) {
121       readRecursively(aNode);
122     }
123   }
124 }
125
126 xmlNodePtr Config_XMLReader::node(void* theNode)
127 {
128   return static_cast<xmlNodePtr>(theNode);
129 }
130
131 std::string Config_XMLReader::getNodeName(xmlNodePtr theNode)
132 {
133   std::string result = "";
134   char* aPropChars = (char*) theNode->name;
135   if (!aPropChars || aPropChars[0] == 0)
136     return result;
137   result = std::string(aPropChars);
138   return result;
139 }
140
141 void Config_XMLReader::storeAttribute(xmlNodePtr theNode, const char* theAttribute)
142 {
143   std::string aKey = getNodeName(theNode) + ":" + std::string(theAttribute);
144   std::string aValue = getProperty(theNode, theAttribute);
145   if(!aValue.empty()) {
146     myCachedAttributes[aKey] = aValue;
147   }
148 }
149
150 std::string Config_XMLReader::restoreAttribute(xmlNodePtr theNode, const char* theAttribute)
151 {
152   return restoreAttribute(getNodeName(theNode).c_str(), theAttribute);
153 }
154
155 std::string Config_XMLReader::restoreAttribute(const char* theNodeName, const char* theAttribute)
156 {
157   std::string aKey = std::string(theNodeName) + ":" + std::string(theAttribute);
158   std::string result = "";
159   if(myCachedAttributes.find(aKey) != myCachedAttributes.end()) {
160     result = myCachedAttributes[aKey];
161   }
162   return result;
163 }