FFSM++  1.1.0
French Forest Sector Model ++
InputNode Class Reference

Wrapper around the underlying library for reading DOM elements (nodes). More...

#include <InputNode.h>

Inheritance diagram for InputNode:
Collaboration diagram for InputNode:

Public Member Functions

 InputNode ()
 
 InputNode (QDomElement domElement_h)
 
 ~InputNode ()
 
bool setWorkingFile (std::string filename_h)
 Load the file on memory. Return false if no success. More...
 
int getIntContent ()
 Get the content between its tagName as integer. More...
 
double getDoubleContent ()
 Get the content between its tagName as double. More...
 
string getStringContent ()
 Get the content between its tagName as std::string. More...
 
bool getBoolContent ()
 Get the content between its tagName as bool. More...
 
int getIntAttributeByName (string attributeName_h)
 Get an attribute by name as integer. More...
 
double getDoubleAttributeByName (string attributeName_h)
 Get an attribute by name as double. More...
 
string getStringAttributeByName (string attributeName_h)
 Get an attribute by name as string. More...
 
bool hasAttributeByName (string attributeName_h)
 Check if an attribute with a certain name exist. More...
 
InputNode getNodeByName (string nodeName_h, int debugLevel=MSG_CRITICAL_ERROR, bool childFlag=false)
 return 0-or-1 nodes by name. More...
 
vector< InputNodegetNodesByName (string nodeName_h, int debugLevel=MSG_WARNING, bool childFlag=false)
 
vector< InputNodegetChildNodes ()
 Retrieve a child node with gived name and optionally with gived attribute or gived pair attribute/value. It raises an error if more than one. More...
 
bool hasChildNode (string name_h)
 True if it has specified child node. More...
 
int getChildNodesCount ()
 Only Elements More...
 
string getNodeName ()
 
- Public Member Functions inherited from BaseClass
 BaseClass ()
 
 ~BaseClass ()
 
void msgOut (const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
 Overloaded function to print the output log. More...
 
void msgOut (const int &msgCode_h, const int &msg_h, const bool &refreshGUI_h=true) const
 Overloaded function to print the output log. More...
 
void msgOut (const int &msgCode_h, const double &msg_h, const bool &refreshGUI_h=true) const
 Overloaded function to print the output log. More...
 
int s2i (const string &string_h) const
 string to integer conversion More...
 
double s2d (const string &string_h) const
 string to double conversion More...
 
double s2d (const string &string_h, const bool &replaceComma) const
 string to double conversion More...
 
bool s2b (const string &string_h) const
 string to bool conversion More...
 
string i2s (const int &int_h) const
 integer to string conversion More...
 
string d2s (const double &double_h) const
 double to string conversion More...
 
string b2s (const bool &bool_h) const
 bool to string conversion More...
 
vector< int > s2i (const vector< string > &string_h) const
 string to integer conversion (vector) More...
 
vector< double > s2d (const vector< string > &string_h, const bool &replaceComma=false) const
 string to double conversion (vector) More...
 
vector< bool > s2b (const vector< string > &string_h) const
 string to bool conversion (vector) More...
 
vector< string > i2s (const vector< int > &int_h) const
 integer to string conversion (vector) More...
 
vector< string > d2s (const vector< double > &double_h) const
 double to string conversion (vector) More...
 
vector< string > b2s (const vector< bool > &bool_h) const
 bool to string conversion (vector) More...
 
int getType (const string &type_h) const
 Return a type according to enum TYPE_* from a string (eg: "string" -> TYPE_STRING (2)) More...
 
void refreshGUI () const
 Ping to periodically return the control to the GUI. More...
 
template<typename T >
string toString (const T &x) const
 
template<typename T >
stringTo (const std::string &s) const
 
int vSum (const vector< int > &vector_h) const
 
double vSum (const vector< double > &vector_h) const
 
int vSum (const vector< vector< int > > &vector_h) const
 
double vSum (const vector< vector< double > > &vector_h) const
 
void tokenize (const string &str, vector< string > &tokens, const string &delimiter=" ") const
 Tokenize a string using a delimiter (default is space) More...
 
void untokenize (string &str, vector< string > &tokens, const string &delimiter=" ") const
 
template<typename K , typename V >
findMap (const map< K, V > &mymap, const K &key, const int &error_level=MSG_CRITICAL_ERROR, const V &notFoundValue=numeric_limits< V >::min()) const
 Lookup a map for a value. Return the value starting from the key. More...
 
template<typename K , typename V >
void changeMapValue (map< K, V > &mymap, const K &key, const V &value, const int &error_level=MSG_CRITICAL_ERROR)
 Change the value stored in a map given the key and the new value. More...
 
template<typename K , typename V >
void incrMapValue (map< K, V > &mymap, const K &key, const V &value, const int &error_level=MSG_CRITICAL_ERROR)
 Increments a value stored in a map of the specified value, given the key. More...
 
template<typename K , typename V >
void incrOrAddMapValue (map< K, V > &mymap, const K &key, const V &value)
 Increments a value stored in a map of the specified value, given the key. More...
 
template<typename K , typename V >
void resetMapValues (map< K, V > &mymap, const V &value)
 Reset all values stored in a map to the specified one. More...
 
template<typename K , typename V >
map< K, V > vectorToMap (const vector< K > &keys, const V &value=0.0)
 Returns a map built using the given vector and the given (scalar) value as keys/values pairs. More...
 
template<typename T >
vector< T > positionsToContent (const vector< T > &vector_h, const vector< int > &positions)
 Return a vector of content from a vector and a vector of positions (int) More...
 
template<typename V >
void debugMap (const map< iisskey, V > &mymap)
 Debug a map. More...
 
template<typename K , typename V >
void debugMap (const map< K, V > &mymap, const K &key)
 
template<typename K >
int getMaxPos (const vector< K > &v)
 Returns the position of the maximum element in the vector (the last one in case of multiple equivalent maxima) More...
 
template<typename K >
int getMinPos (const vector< K > &v)
 Returns the position of the minimum element in the vector (the first one in case of multiple equivalent minima) More...
 
template<typename K >
getMax (const vector< K > &v)
 Returns the value of the maximum element in the vector (the last one in case of multiple equivalent maxima) More...
 
template<typename K >
getMin (const vector< K > &v)
 Returns the value of the minimum element in the vector (the first one in case of multiple equivalent minima) More...
 
template<typename K >
double getAvg (const vector< K > &v)
 Returns the average of the elements in the vector. More...
 
template<typename K >
double getSd (const vector< K > &v, bool sample=true)
 
template<typename K >
int getPos (const K &element, const vector< K > &v, const int &msgCode_h=MSG_CRITICAL_ERROR)
 
template<typename K >
bool inVector (const K &element, const vector< K > &v)
 
double normSample (const double &avg, const double &stdev, const double &minval=NULL, const double &maxval=NULL) const
 Sample from a normal distribution with bounds. Slower (double time, but still you see the diff only after milion of loops). More...
 
template<typename K >
normSample (normal_distribution< K > &d, std::mt19937 &gen, const K &minval=NULL, const K &maxval=NULL) const
 Sample from a normal distribution with bounds. Faster (half time) as the normal_distribution is made only once. More...
 
template<typename T >
std::string toString (const T &x) const
 

Private Attributes

QDomElement domElement
 The underlying library-depending DOM rappresentation of the element. More...
 

Additional Inherited Members

- Protected Attributes inherited from BaseClass
ThreadManagerMTHREAD
 Pointer to the Thread manager. More...
 

Detailed Description

Wrapper around the underlying library for reading DOM elements (nodes).

A small wrapper class using an underlying library (currently QtXml) to read DOM nodes.
This class works with the individual nodes (DOM Elements), while the companion class InputDocument wrapper the whole document (DOM Document).
Note: In the DOM terminology "Elements" are a subset of the more general "nodes" (that include comments and other typologies..)

Author
Antonello Lobianco

Definition at line 51 of file InputNode.h.

Constructor & Destructor Documentation

InputNode ( )

Definition at line 30 of file InputNode.cpp.

30  {
31 }
InputNode ( QDomElement  domElement_h)
inline

Definition at line 55 of file InputNode.h.

55 {domElement=domElement_h;}; //<Constructor
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
~InputNode ( )

Definition at line 33 of file InputNode.cpp.

33  {
34 }

Member Function Documentation

bool getBoolContent ( )

Get the content between its tagName as bool.

Definition at line 79 of file InputNode.cpp.

79  {
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 }
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
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
Print a WARNING message.
Definition: BaseClass.h:60

Here is the call graph for this function:

vector< InputNode > getChildNodes ( )

Retrieve a child node with gived name and optionally with gived attribute or gived pair attribute/value. It raises an error if more than one.

Retrieve all child nodes with gived name and optionally with gived attribute or gived pair attribute/value. It raises an error if more than one. Filtered to return only child Elements

Definition at line 235 of file InputNode.cpp.

235  {
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 }
Wrapper around the underlying library for reading DOM elements (nodes).
Definition: InputNode.h:51
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
int getChildNodesCount ( )

Only Elements

Definition at line 260 of file InputNode.cpp.

260  {
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 }
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
double getDoubleAttributeByName ( std::string  attributeName_h)

Get an attribute by name as double.

Definition at line 100 of file InputNode.cpp.

100  {
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 }
Print an ERROR message, but don&#39;t stop the model.
Definition: BaseClass.h:61
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
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80

Here is the call graph for this function:

double getDoubleContent ( )

Get the content between its tagName as double.

Definition at line 69 of file InputNode.cpp.

69  {
70  return domElement.text().toDouble(); // This is a Qt function that works both with dot and comma separators !
71 }
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
int getIntAttributeByName ( std::string  attributeName_h)

Get an attribute by name as integer.

Definition at line 90 of file InputNode.cpp.

90  {
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 }
Print an ERROR message, but don&#39;t stop the model.
Definition: BaseClass.h:61
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
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80

Here is the call graph for this function:

int getIntContent ( )

Get the content between its tagName as integer.

Definition at line 64 of file InputNode.cpp.

64  {
65  return domElement.text().toInt();
66 }
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
InputNode getNodeByName ( string  nodeName_h,
int  debugLevel = MSG_CRITICAL_ERROR,
bool  childFlag = false 
)

return 0-or-1 nodes by name.

Definition at line 129 of file InputNode.cpp.

129  {
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 }
Wrapper around the underlying library for reading DOM elements (nodes).
Definition: InputNode.h:51
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
vector< InputNode > getNodesByName(string nodeName_h, int debugLevel=MSG_WARNING, bool childFlag=false)
Definition: InputNode.cpp:155

Here is the call graph for this function:

string getNodeName ( )

Definition at line 272 of file InputNode.cpp.

272  {
273  return domElement.tagName().toStdString();
274 }
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
vector< InputNode > getNodesByName ( string  nodeName_h,
int  debugLevel = MSG_WARNING,
bool  childFlag = false 
)

return 0-to-n nodes by name

Definition at line 155 of file InputNode.cpp.

Referenced by getNodeByName(), Gis::initLayers(), and ModelData::loadInput().

155  {
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 }
Wrapper around the underlying library for reading DOM elements (nodes).
Definition: InputNode.h:51
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
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80

Here is the call graph for this function:

Here is the caller graph for this function:

string getStringAttributeByName ( std::string  attributeName_h)

Get an attribute by name as string.

Definition at line 110 of file InputNode.cpp.

110  {
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 }
Print an ERROR message, but don&#39;t stop the model.
Definition: BaseClass.h:61
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
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80

Here is the call graph for this function:

std::string getStringContent ( )

Get the content between its tagName as std::string.

Definition at line 74 of file InputNode.cpp.

74  {
75  return domElement.text().toStdString();
76 }
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
bool hasAttributeByName ( std::string  attributeName_h)

Check if an attribute with a certain name exist.

Definition at line 120 of file InputNode.cpp.

120  {
121  if (domElement.hasAttribute(attributeName_h.c_str())){
122  return 1;
123  }else{
124  return 0;
125  }
126 }
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
bool hasChildNode ( string  name_h)

True if it has specified child node.

Definition at line 248 of file InputNode.cpp.

248  {
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 }
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
bool setWorkingFile ( std::string  filename_h)

Load the file on memory. Return false if no success.

Definition at line 37 of file InputNode.cpp.

Referenced by Gis::initLayers(), and ModelData::loadInput().

37  {
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 }
string i2s(const int &int_h) const
integer to string conversion
Definition: BaseClass.cpp:219
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
QDomElement domElement
The underlying library-depending DOM rappresentation of the element.
Definition: InputNode.h:80
Print a WARNING message.
Definition: BaseClass.h:60

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation


The documentation for this class was generated from the following files: