FFSM++ 1.1.0
French Forest Sector Model ++
Loading...
Searching...
No Matches
InputNode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2015 by Laboratoire d'Economie Forestière *
3 * http://ffsm-project.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version, given the compliance with the *
9 * exceptions listed in the file COPYING that is distribued together *
10 * with this file. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22//#include <QtGui> // Qt4
23#include <QtWidgets> // Qt5
24#include <QtXml>
25
26#include "InputNode.h"
27//#include "InputDocument.h"
28
29
32
35
36bool
37InputNode::setWorkingFile(std::string filename_h){
38
39 QString errorStr;
40 int errorLine;
41 int errorColumn;
42
43 QFile file(filename_h.c_str());
44 QIODevice* device;
45 device = &file;
46
47 QDomDocument doc;
48 if (!doc.setContent(device, true, &errorStr, &errorLine, &errorColumn)) {
49 string message = "XML error on file "+ filename_h + " at line ";
50 message.append(i2s(errorLine));
51 message.append(" column ");
52 message = message.c_str() + i2s(errorColumn);
53 message = message + ": ";
54 message = message + errorStr.toStdString();
55 msgOut(MSG_WARNING, message.c_str());
56 return false;
57 }
58 domElement = doc.documentElement();
59 return true;
60}
61
62// *******************************************************************************
63int
65 return domElement.text().toInt();
66}
67
68double
70 return domElement.text().toDouble(); // This is a Qt function that works both with dot and comma separators !
71}
72
73std::string
75 return domElement.text().toStdString();
76}
77
78bool
80 string content = domElement.text().toStdString();
81 if (content == "false" || content == "falso" || content == "FALSE" || content == "0")
82 return false;
83 else if (content == "true" || content == "vero" || content == "TRUE" || content == "1")
84 return true;
85 msgOut(MSG_WARNING, "Sorry, I don't know how to convert "+content+" to a bool value. I return true... hope for the best");
86 return true;
87}
88
89int
90InputNode::getIntAttributeByName(std::string attributeName_h){
91 if (domElement.hasAttribute(attributeName_h.c_str())){
92 return domElement.attribute(attributeName_h.c_str()).toInt();
93 }else{
94 msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
95 return 0;
96 }
97}
98
99double
100InputNode::getDoubleAttributeByName(std::string attributeName_h){
101 if (domElement.hasAttribute(attributeName_h.c_str())){
102 return domElement.attribute(attributeName_h.c_str()).toDouble();
103 }else{
104 msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
105 return 0;
106 }
107}
108
109string
110InputNode::getStringAttributeByName(std::string attributeName_h){
111 if (domElement.hasAttribute(attributeName_h.c_str())){
112 return domElement.attribute(attributeName_h.c_str()).toStdString();
113 }else{
114 msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
115 return "";
116 }
117}
118
119bool
120InputNode::hasAttributeByName(std::string attributeName_h){
121 if (domElement.hasAttribute(attributeName_h.c_str())){
122 return 1;
123 }else{
124 return 0;
125 }
126}
127
129InputNode::getNodeByName(string nodeName_h, int debugLevel, bool childFlag){
130 /*
131 QDomNodeList myElementList = domElement.elementsByTagName ( nodeName_h.c_str() );
132 if (myElementList.size()>1){
133 msgOut(debugLevel, "Too many elements. Expected only one of type "+nodeName_h);
134 }
135 if (myElementList.isEmpty()){
136 msgOut(debugLevel, "No elements in the XML file. Expected 1 of type "+nodeName_h);
137 }
138 QDomElement myElement = myElementList.item(0).toElement() ;
139 InputNode myInputNode(myElement);
140 return myInputNode; */
141 vector<InputNode> myNodes = getNodesByName(nodeName_h, debugLevel, childFlag);
142 if (myNodes.size()>1){
143 msgOut(debugLevel, "Too many elements. Expected only one of type "+nodeName_h);
144 return myNodes[0];
145 }
146 if (myNodes.size() == 0){
147 msgOut(debugLevel, "No elements in the XML file. Expected 1 of type "+nodeName_h+". Returning emty node!!");
148 InputNode toReturn;
149 return toReturn;
150 }
151 return myNodes[0];
152}
153
154vector <InputNode>
155InputNode::getNodesByName(string nodeName_h, int debugLevel, bool childFlag){
156 vector <InputNode> myNodeVector;
157 if (!childFlag){
158 QDomNodeList myElementList = domElement.elementsByTagName ( nodeName_h.c_str() );
159 for (int i=0;i<myElementList.size();i++){
160 InputNode myInputNode(myElementList.item(i).toElement());
161 myNodeVector.push_back(myInputNode);
162 }
163
164 }
165 else {
166 QDomNodeList myElementList = domElement.childNodes();
167 for (int i=0;i<myElementList.size();i++){
168 if ( myElementList.item(i).nodeType() == QDomNode::ElementNode
169 && myElementList.item(i).toElement().tagName().toStdString() == nodeName_h){
170 InputNode myInputNode(myElementList.item(i).toElement());
171 myNodeVector.push_back(myInputNode);
172 }
173 }
174 }
175 if (myNodeVector.size()==0){
176 msgOut(debugLevel, "No elements in the XML file. Expected at least one of type "+nodeName_h);
177 }
178 //for (int i=0;i<myElementList.size();i++){
179 // InputNode myInputNode(myElementList.item(i).toElement());
180 // myNodeVector.push_back(myInputNode);
181
182 /*InputNode myInputNode(myElementList.item(i).toElement());
183 string firstNodeContent= myInputNode.getStringContent();
184 // checking that the setting is not an empy line nor a comment (e.g. starting with "#")..
185 if(firstNodeContent=="") continue;
186 unsigned int z;
187 z = firstNodeContent.find("#");
188 if( z!=string::npos && z == 0) continue;
189 // chacking also the "childs" as in the XMLs deriving from csv I want delete the whole "<record>" tree, including his childs (fields)
190 vector <InputNode> childs = myInputNode.getChildNodes();
191 if(childs.size()>0){
192 string firstChildContent= childs[0].getStringContent();
193 // checking that the setting is not an empy line nor a comment (e.g. starting with "#")..
194 if(firstChildContent=="") continue;
195 unsigned int y;
196 y = firstChildContent.find("#");
197 if( y!=string::npos && y == 0) continue;
198 }
199 myNodeVector.push_back(myInputNode);
200 */
201
202
203 //}
204 return myNodeVector;
205}
206
207
208/*
209InputNode
210InputNode::getNode(string nodeName_h, string attributeName_h, string attributeValue_h, int debugLevel){
211 vector <InputNode> nodes = getNodes(nodeName_h, attributeName_h, attributeValue_h, debugLevel);
212 if (nodes.size()>1){
213 msgOut(debugLevel,"I got more than one node with specified carhacteristics. Returned the first one or aborting.");
214 return nodes[0];
215 } else if (nodes.size() == 0) {
216 msgOut(debugLevel,"I don't have any node with the requested parameters. Returning an empty node.");
217 InputNode toReturn;
218 return toReturn;
219 } else {
220 return nodes[0];
221 }
222}
223
224vector <InputNode>
225InputNode::getNodes(string nodeName_h, string attributeName_h, string attributeValue_h, int debugLevel){
226 vector <InputNode> nodes;
227
228 return nodes;
229
230}
231*/
232
233
234vector <InputNode>
236 vector <InputNode> myNodeVector;
237 QDomNodeList myElementList = domElement.childNodes();
238 for (int i=0;i<myElementList.size();i++){
239 if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
240 InputNode myInputNode(myElementList.item(i).toElement());
241 myNodeVector.push_back(myInputNode);
242 }
243 }
244 return myNodeVector;
245}
246
247bool
249 bool toReturn = false;
250 QDomNodeList myElementList = domElement.childNodes();
251 for (int i=0;i<myElementList.size();i++){
252 if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
253 if(myElementList.item(i).toElement().tagName().toStdString() == name_h) return true;
254 }
255 }
256 return toReturn;
257}
258
259int
261 int myElementListCountInt = 0;
262 QDomNodeList myElementList = domElement.childNodes();
263 for (int i=0;i<myElementList.size();i++){
264 if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
265 myElementListCountInt++ ;
266 }
267 }
268 return myElementListCountInt;
269}
270
271string
273 return domElement.tagName().toStdString();
274}
275
@ MSG_ERROR
Print an ERROR message, but don't stop the model.
Definition BaseClass.h:61
@ MSG_WARNING
Print a WARNING message.
Definition BaseClass.h:60
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
Definition BaseClass.cpp:50
string i2s(const int &int_h) const
integer to string conversion
Wrapper around the underlying library for reading DOM elements (nodes).
Definition InputNode.h:51
bool hasChildNode(string name_h)
True if it has specified child node.
bool getBoolContent()
Get the content between its tagName as bool.
Definition InputNode.cpp:79
double getDoubleContent()
Get the content between its tagName as double.
Definition InputNode.cpp:69
int getIntAttributeByName(string attributeName_h)
Get an attribute by name as integer.
Definition InputNode.cpp:90
bool setWorkingFile(std::string filename_h)
Load the file on memory. Return false if no success.
Definition InputNode.cpp:37
string getStringAttributeByName(string attributeName_h)
Get an attribute by name as string.
string getNodeName()
vector< InputNode > getNodesByName(string nodeName_h, int debugLevel=MSG_WARNING, bool childFlag=false)
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition InputNode.h:80
int getChildNodesCount()
Only Elements
InputNode getNodeByName(string nodeName_h, int debugLevel=MSG_CRITICAL_ERROR, bool childFlag=false)
return 0-or-1 nodes by name.
int getIntContent()
Get the content between its tagName as integer.
Definition InputNode.cpp:64
double getDoubleAttributeByName(string attributeName_h)
Get an attribute by name as double.
bool hasAttributeByName(string attributeName_h)
Check if an attribute with a certain name exist.
vector< InputNode > getChildNodes()
Retrieve a child node with gived name and optionally with gived attribute or gived pair attribute/val...
string getStringContent()
Get the content between its tagName as std::string.
Definition InputNode.cpp:74