XMLSettingsReader.cpp 7.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/** @file XMLSettingsReader.cpp
 *  @brief Implementation of XMLSettingsReader.
 *
 *  Function definitions for XMLSettingsReader are provided.
 *  This class is the main  class to read xml configuration files for ZDCs and RPD.
 *  DOM logic is chosen for the xml implementation
 *  @author Riccardo Longo
 *  @bug No known bugs.
 */


#include "XMLSettingsReader.h"

using namespace xercesc;

/** @brief Default Constructor for XMLSettingsReader.
 */
XMLSettingsReader::XMLSettingsReader( void ){

    try {
        XMLPlatformUtils::Initialize();
      } catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());

        std::cout << "Fatal error during the parser initialization! " << std::endl;
        XMLString::release(&message);
      }

      //Initial number of root nodes = 0
      rootNode = 0;

      //Initializing the DOM parser and the error handles
      myDOMParser = new XercesDOMParser();
      myDOMParser->setValidationScheme(XercesDOMParser::Val_Always);
      myDOMParser->setDoNamespaces(false);
      ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
      myDOMParser->setErrorHandler(errHandler);
      myDOMParser->setDoSchema(false);
      myDOMParser->setValidationConstraintFatal(false);

}

/** @brief Destructor for XMLSettingsReader.
 */
XMLSettingsReader::~XMLSettingsReader( void ){

    if (myDOMParser != NULL) delete myDOMParser;
    XMLPlatformUtils::Terminate();

}

/*! @brief This function returns the number of baseNodes with the specified name
 *  @param _nodeName the name of the baseNodes to count
 *
 * Assume:
 * <root>
 *  <detector>
 *   <channel></channel>
 *  </detector>
 *  <detector>
 *   <channel></channel>
 *   <channel></channel>
 *  </detector>
 * </root>
 * getBaseNodeCount("detector") returns 2
 *
 */

int XMLSettingsReader::getBaseNodeCount(std::string _nodeName){

    return rootNode->getElementsByTagName(XMLString::transcode(_nodeName.c_str()))->getLength();

}


/*! @brief returns the number of child Nodes in a give parent Node
 *  @param _nodeName parent node name
 *  @param _nodeNumber number of the parentNode
 *  @param _childName the name of the childNode to count
 *
 * Assume:
 * <root>
 *  <detector>
 *   <channel></channel>
 *  </detector>
 *  <detector>
 *   <channel></channel>
 *   <channel></channel>
 *  </detector>
 * </root>
 * getNodeChildCount("detector",0,"channel") returns 1
 * getNodeChildCount("detector",1,"channel") returns 2
 */

int XMLSettingsReader::getNodeChildCount(std::string _nodeName, int _nodeNumber, std::string _childName){

      XMLCh* bufferNode = XMLString::transcode(_nodeName.c_str());
      DOMNodeList* list = myDOMParser->getDocument()->getElementsByTagName(bufferNode);
      XMLString::release(&bufferNode);

      if (list->getLength() <= (unsigned int) _nodeNumber)
        return 0;

      DOMElement* parent = dynamic_cast<DOMElement*>(list->item(_nodeNumber));
      //Now we create a child list
      DOMNodeList* childList = parent->getElementsByTagName(XMLString::transcode(_childName.c_str()));
      //And we return its size
      return (int) childList->getLength();

}

/*! @brief This method returns a string the value of a child
 *  @param _baseNode the name of the baseNode
 *  @param _baseNumber the number of the baseNode to choose
 *  @param _childNode the name of the childNode to choose
 *
 * Usage example:
 * Assume:
 * <root>
 *  <detector>
 *   <channel>C1</channel>
 *   <delay>100</delay>
 *  </detector>
 *  <detector>
 *   <channel>C2</channel>
 *   <delay>200</delay>
 *  </detector>
 * </root>
 *
 * getChildValue("detector",0,"channel"); will return "C1";
 * and
 * getChildValue("detector",1,"delay"); will return "200";
 *
 * The idea is to then cast the string into the type desired for the child
 */

std::string XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber, std::string _childNode){

      XMLCh* bufferNode = XMLString::transcode(_baseNode.c_str());
      DOMNodeList* list = myDOMParser->getDocument()->getElementsByTagName(bufferNode);
      XMLString::release(&bufferNode);

      if (getBaseNodeCount(_baseNode) <= _baseNumber)
        return "";

      DOMElement* parent = dynamic_cast<DOMElement*>(list->item(_baseNumber));
      DOMElement* child = dynamic_cast<DOMElement*>(parent->getElementsByTagName(XMLString::transcode(_childNode.c_str()))->item(0));
      std::string value;
      if (child) {
        char* bufferChild = XMLString::transcode(child->getTextContent());
        value = bufferChild;
        XMLString::release(&bufferChild);
      } else {
        //Not found? then empty
        value = "";
      }
      return value;
}

/**
 * @brief This methods perform the same as XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber, std::string _childNod) but returns the output into a string passed as argument
 * @param _baseNode the name of the baseNode
 * @param _baseNumber the number of the baseNode to choose
 * @param _childNode the name of the childNode to choose
 * @param _retVal the string containing the result
 */
void  XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber,
    std::string _childNode, std::string &_retVal)
{
  _retVal = getChildValue(_baseNode, _baseNumber, _childNode);
}

/**
 * @brief This methods perform the same as XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber, std::string _childNod) but cast the output into a boolean passed as argument
 * @param _baseNode the name of the baseNode
 * @param _baseNumber the number of the baseNode to choose
 * @param _childNode the name of the childNode to choose
 * @param _retVal the boolean containing the result
 */
void  XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber,
    std::string _childNode, bool& _retVal)
{
    _retVal = (getChildValue(_baseNode, _baseNumber, _childNode)!= "false");
}

/**
 * @brief This methods perform the same as XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber, std::string _childNod) but cast the output into a double passed as argument
 * @param _baseNode the name of the baseNode
 * @param _baseNumber the number of the baseNode to choose
 * @param _childNode the name of the childNode to choose
 * @param _retVal the double containing the result
 */
void  XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber,
    std::string _childNode, double& _retVal)
{
    std::stringstream buffer;
    buffer << getChildValue(_baseNode, _baseNumber, _childNode);
    buffer >> _retVal;
}

/**
 * @brief This methods perform the same as XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber, std::string _childNod) but cast the output into a integer passed as argument
 * @param _baseNode the name of the baseNode
 * @param _baseNumber the number of the baseNode to choose
 * @param _childNode the name of the childNode to choose
 * @param _retVal the double containing the result
 */
void  XMLSettingsReader::getChildValue(std::string _baseNode, int _baseNumber,
    std::string _childNode, int& _retVal)
{
    std::stringstream buffer;
    buffer << getChildValue(_baseNode, _baseNumber, _childNode);
    buffer >> _retVal;
}