InputNode.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006-2008 by Antonello Lobianco                         *
00003  *   http://regmas.org                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 3 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <QtGui>
00021 #include <QtXml>
00022 
00023 #include "InputNode.h"
00024 //#include "InputDocument.h"
00025 
00026 
00027 InputNode::InputNode(){
00028 }
00029 
00030 InputNode::~InputNode(){
00031 }
00032 
00033 bool
00034 InputNode::setWorkingFile(std::string filename_h){
00035 
00036     QString errorStr;
00037     int errorLine;
00038     int errorColumn;
00039     
00040     QFile file(filename_h.c_str());
00041     QIODevice* device;
00042     device = &file;
00043         
00044     QDomDocument doc;
00045     if (!doc.setContent(device, true, &errorStr, &errorLine, &errorColumn)) {
00046         string message = "XML error on file "+ filename_h + " at line ";
00047         message.append(i2s(errorLine));
00048         message.append(" column ");
00049         message = message.c_str() + i2s(errorColumn);
00050         message = message + ": ";
00051         message = message + errorStr.toStdString();
00052         msgOut(MSG_WARNING, message.c_str());
00053         return false;
00054     }
00055     domElement = doc.documentElement();
00056     return true;
00057 };
00058 
00059 // *******************************************************************************
00060 int
00061 InputNode::getIntContent(){
00062     return domElement.text().toInt();
00063 };
00064 
00065 double
00066 InputNode::getDoubleContent(){
00067     return domElement.text().toDouble(); // This is a Qt function that works both with dot and comma separators !
00068 };
00069 
00070 std::string
00071 InputNode::getStringContent(){
00072     return domElement.text().toStdString();
00073 };
00074 
00075 bool
00076 InputNode::getBoolContent(){
00077     string content = domElement.text().toStdString();
00078     if (content == "false" || content == "falso" || content == "FALSE" || content == "0")
00079         return false;
00080     else if (content == "true" || content == "vero" || content == "TRUE" || content == "1")
00081         return true;
00082     msgOut(MSG_WARNING, "Sorry, I don't know how to convert "+content+" to a bool value. I return true... hope for the best");
00083     return true;
00084 };
00085 
00086 int 
00087 InputNode::getIntAttributeByName(std::string attributeName_h){
00088     if (domElement.hasAttribute(attributeName_h.c_str())){
00089         return domElement.attribute(attributeName_h.c_str()).toInt();
00090     }else{
00091         msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
00092         return 0;
00093     }
00094 };
00095 
00096 double 
00097 InputNode::getDoubleAttributeByName(std::string attributeName_h){
00098     if (domElement.hasAttribute(attributeName_h.c_str())){
00099         return domElement.attribute(attributeName_h.c_str()).toDouble();
00100     }else{
00101         msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
00102         return 0;
00103     }
00104 };
00105 
00106 string 
00107 InputNode::getStringAttributeByName(std::string attributeName_h){
00108     if (domElement.hasAttribute(attributeName_h.c_str())){
00109         return domElement.attribute(attributeName_h.c_str()).toStdString();
00110     }else{
00111         msgOut(MSG_ERROR, "Element doens't have attribute " + attributeName_h );
00112         return "";
00113     }
00114 };
00115 
00116 bool 
00117 InputNode::hasAttributeByName(std::string attributeName_h){
00118     if (domElement.hasAttribute(attributeName_h.c_str())){
00119         return 1;
00120     }else{
00121         return 0;
00122     }
00123 };
00124 
00125 InputNode
00126 InputNode::getNodeByName(string nodeName_h, int debugLevel, bool childFlag){
00127     /*
00128     QDomNodeList myElementList = domElement.elementsByTagName ( nodeName_h.c_str() );
00129     if (myElementList.size()>1){
00130         msgOut(debugLevel, "Too many elements. Expected only one of type "+nodeName_h);
00131     }
00132     if (myElementList.isEmpty()){
00133         msgOut(debugLevel, "No elements in the XML file. Expected 1 of type "+nodeName_h);
00134     }
00135     QDomElement myElement = myElementList.item(0).toElement() ;
00136     InputNode myInputNode(myElement);
00137     return myInputNode; */
00138     vector<InputNode> myNodes = getNodesByName(nodeName_h, debugLevel, childFlag);
00139     if (myNodes.size()>1){
00140         msgOut(debugLevel, "Too many elements. Expected only one of type "+nodeName_h);
00141         return myNodes[0];
00142     }
00143     if (myNodes.size() == 0){
00144         msgOut(debugLevel, "No elements in the XML file. Expected 1 of type "+nodeName_h+". Returning emty node!!");
00145         InputNode toReturn;
00146         return toReturn;
00147     }
00148     return myNodes[0];
00149 }
00150 
00151 vector <InputNode>
00152 InputNode::getNodesByName(string nodeName_h, int debugLevel, bool childFlag){
00153     vector <InputNode> myNodeVector;
00154     if (!childFlag){
00155         QDomNodeList myElementList = domElement.elementsByTagName ( nodeName_h.c_str() );
00156         for (int i=0;i<myElementList.size();i++){
00157             InputNode myInputNode(myElementList.item(i).toElement());
00158             myNodeVector.push_back(myInputNode);
00159         }
00160         
00161     }
00162     else {
00163         QDomNodeList myElementList = domElement.childNodes();
00164         for (int i=0;i<myElementList.size();i++){
00165             if (   myElementList.item(i).nodeType() == QDomNode::ElementNode
00166                 && myElementList.item(i).toElement().tagName().toStdString() == nodeName_h){
00167                 InputNode myInputNode(myElementList.item(i).toElement());
00168                 myNodeVector.push_back(myInputNode);
00169             }
00170         }
00171     }
00172     if (myNodeVector.size()==0){
00173         msgOut(debugLevel, "No elements in the XML file. Expected at least one of type "+nodeName_h);
00174     }
00175     //for (int i=0;i<myElementList.size();i++){
00176     //  InputNode myInputNode(myElementList.item(i).toElement());
00177     //  myNodeVector.push_back(myInputNode);
00178 
00179         /*InputNode myInputNode(myElementList.item(i).toElement());
00180         string firstNodeContent= myInputNode.getStringContent();
00181         // checking that the setting is not an empy line nor a comment (e.g. starting with "#")..
00182         if(firstNodeContent=="") continue;
00183         unsigned int z;
00184         z = firstNodeContent.find("#");
00185         if( z!=string::npos && z == 0) continue;
00186         // chacking also the "childs" as in the XMLs deriving from csv I want delete the whole "<record>" tree, including his childs (fields)
00187         vector <InputNode> childs = myInputNode.getChildNodes();
00188         if(childs.size()>0){
00189             string firstChildContent= childs[0].getStringContent();
00190             // checking that the setting is not an empy line nor a comment (e.g. starting with "#")..
00191             if(firstChildContent=="") continue;
00192             unsigned int y;
00193             y = firstChildContent.find("#");
00194             if( y!=string::npos && y == 0) continue;
00195         }
00196         myNodeVector.push_back(myInputNode);
00197         */
00198 
00199 
00200     //}
00201     return myNodeVector;
00202 };
00203 
00204 
00205 /*
00206 InputNode
00207 InputNode::getNode(string nodeName_h, string attributeName_h, string attributeValue_h, int debugLevel){
00208     vector <InputNode> nodes = getNodes(nodeName_h, attributeName_h, attributeValue_h, debugLevel);
00209     if (nodes.size()>1){
00210         msgOut(debugLevel,"I got more than one node with specified carhacteristics. Returned the first one or aborting.");
00211         return nodes[0];
00212     } else if (nodes.size() == 0) {
00213         msgOut(debugLevel,"I don't have any node with the requested parameters. Returning an empty node.");
00214         InputNode toReturn;
00215         return toReturn;
00216     } else {
00217         return nodes[0];
00218     }
00219 }
00220 
00221 vector <InputNode>
00222 InputNode::getNodes(string nodeName_h, string attributeName_h, string attributeValue_h, int debugLevel){
00223     vector <InputNode> nodes;
00224 
00225     return nodes;
00226 
00227 }
00228 */
00229 
00230 
00231 vector <InputNode>
00232 InputNode::getChildNodes(){
00233     vector <InputNode> myNodeVector;
00234     QDomNodeList myElementList = domElement.childNodes();
00235     for (int i=0;i<myElementList.size();i++){
00236         if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
00237             InputNode myInputNode(myElementList.item(i).toElement());
00238             myNodeVector.push_back(myInputNode);
00239         }
00240     }
00241     return myNodeVector;
00242 };
00243 
00244 bool
00245 InputNode::hasChildNode(string name_h){
00246     bool toReturn = false;
00247     QDomNodeList myElementList = domElement.childNodes();
00248     for (int i=0;i<myElementList.size();i++){
00249         if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
00250             if(myElementList.item(i).toElement().tagName().toStdString() == name_h) return true;
00251         }
00252     }
00253     return toReturn;
00254 }
00255 
00256 int
00257 InputNode::getChildNodesCount(){
00258     int myElementListCountInt = 0;
00259     QDomNodeList myElementList = domElement.childNodes();
00260     for (int i=0;i<myElementList.size();i++){
00261         if (myElementList.item(i).nodeType() == QDomNode::ElementNode ){
00262             myElementListCountInt++ ;
00263         }
00264     }
00265     return myElementListCountInt;
00266 }
00267 
00268 string
00269 InputNode::getNodeName(){
00270     return domElement.tagName().toStdString();
00271 };