CMS 3D CMS Logo

IgXMLReader.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
00002 
00003 #include "Iguana/Studio/interface/IgXMLReader.h"
00004 
00005 #include <xercesc/parsers/XercesDOMParser.hpp>
00006 #include <xercesc/dom/DOMDocument.hpp>
00007 #include <xercesc/sax/HandlerBase.hpp>
00008 #include <xercesc/util/XMLString.hpp>
00009 #include <xercesc/util/PlatformUtils.hpp>
00010 // #include <classlib/utils/DebugAids.h>
00011 
00012 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00013 
00014 #if defined(XERCES_NEW_IOSTREAMS)
00015 #include <iostream>
00016 #else
00017 #include <iostream.h>
00018 #endif
00019 
00020 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00021 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00022 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00023 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00024 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00025 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00026 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00027 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
00028 
00029 XERCES_CPP_NAMESPACE_USE
00030 
00031 IgXMLReader::IgXMLReader ()
00032 {
00033         try
00034         {
00035                 XMLPlatformUtils::Initialize ();
00036                 //XERCES_CPP_NAMESPACE_QUALIFIER XercesDOMParser* m_parser = new XercesDOMParser ();
00037                 //parser->setValidationScheme (XercesDOMParser::Val_Always);    
00038                 //parser->setDoNamespaces (true);
00039         
00040                 //      ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
00041                 //      parser->setErrorHandler(errHandler);
00042         }
00043         catch (const XMLException& toCatch) 
00044         {
00045                 char* message = XMLString::transcode(toCatch.getMessage());
00046                 std::cout << "Error during initialization! :\n"
00047                         << message << "\n";
00048                 XMLString::release(&message);
00049         }
00050 }
00051 
00052 IgXMLReader::~IgXMLReader ()
00053 {
00054 }
00055 
00056 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument*
00057 IgXMLReader::read (const std::string &fileName)
00058 {
00059         XercesDOMParser* parser = new XercesDOMParser ();
00060         try 
00061         {
00062                 parser->parse (fileName.c_str ());
00063                 if (parser->getErrorCount())
00064                 {
00065                         return 0;
00066                 }
00067                 return parser->getDocument ();
00068         }
00069         catch (const XMLException& toCatch) 
00070         {
00071                 char* message = XMLString::transcode (toCatch.getMessage ());
00072                 std::cerr << "Exception message is: \n"
00073                                 << message << "\n";
00074                 XMLString::release(&message);
00075                 return 0;
00076         }
00077         catch (const DOMException& toCatch) 
00078         {
00079                 char* message = XMLString::transcode(toCatch.msg);
00080                 std::cerr << "Exception message is: \n"
00081                                 << message << "\n";
00082                 XMLString::release(&message);
00083                 return 0;
00084         }
00085         catch (...) 
00086         {
00087                 std::cerr << "Unexpected Exception \n" ;
00088                 return 0;
00089         }
00090 }
00091 
00092 void
00093 IgXMLReader::retrieveTexts (DOMNode* node, std::vector<std::string>& textList, bool isOnlyRoot)
00094 {
00095         // if 'isonlyroot' is chosen, the passed node is treated as a root node, 
00096         // so only it's children are examined and not also its neighbours
00097         if (isOnlyRoot && node)
00098         {
00099                 retrieveTexts (node->getFirstChild (), textList);
00100         }
00101         else
00102         {
00103                 retrieveTexts (node, textList);
00104         }
00105 }
00106 
00107 void
00108 IgXMLReader::retrieveTexts (DOMNode* node, std::vector<std::string>& textList)
00109 {
00110         // recursively collect the information in the tags and return it in a vector 
00111         // of strings ordered by their appearence. depth-first/preorder traversal
00112         if (node)
00113         {
00114                 if (node->getNodeType () == DOMNode::TEXT_NODE)
00115                 {
00116                         // values should not start with tab, whitespace or a new line
00117                         std::string data = XMLString::transcode (node->getNodeValue ());
00118                         if (!data.empty () && data[0] != '\t' && data[0] != '\n' && data[0] != ' ')
00119                         {
00120                                 textList.push_back (data);
00121                         }
00122                 }
00123                 
00124                 if (node->hasChildNodes ())
00125                 {
00126                         retrieveTexts (node->getFirstChild (), textList);
00127                 }
00128                 
00129                 node = node->getNextSibling ();
00130                 if (node)
00131                 {
00132                         retrieveTexts (node, textList);
00133                 }
00134         }
00135 }
00136 
00137 DOMNode*
00138 IgXMLReader::getOneElementByTagName (DOMNode* root, XMLCh* string)
00139 {
00140         // this method only returns a DOMNode if its tag appears _once_
00141         DOMNodeList* list = 0;
00142         DOMNode* result = 0;
00143         
00144         if (root)
00145         {
00146                 list = dynamic_cast<DOMElement *> (root)->getElementsByTagName (string);
00147                 
00148                 
00149                 if (list && list->getLength () == 1)
00150                 {
00151                         result = list->item (0);
00152                 }
00153                 else
00154                 {
00155                         std::cerr << "There should only be one node! No node returned!" << std::endl;
00156                 }
00157         }
00158         return result;
00159 }
00160 
00161 // copied from Alignment/CocoaUtilities/ALIUtils
00162 int
00163 IgXMLReader::isNumber (const std::string& str)
00164 {
00165         int isnum = 1;
00166         int numE = 0;
00167         for (uint ii = 0; ii < str.length(); ii++)
00168         {
00169                 if (!isdigit(str[ii]) && str[ii]!='.' && str[ii]!='-' && str[ii]!='+')
00170                 {
00171                         //--- check for E(xponential)
00172                         if (str[ii] == 'E' || str[ii] == 'e' )
00173                         {
00174                                 if (numE != 0 || ii == str.length() - 1)
00175                                 {
00176                                         isnum = 0;
00177                                         break;
00178                                 }
00179                                 numE++;
00180                         } 
00181                         else
00182                         {
00183                                 isnum = 0; 
00184                                 break;
00185                         }
00186                 }
00187         }       
00188         return isnum;
00189 }
00190 
00191 // copied from Alignment/CocoaUtilities/ALIUtils
00192 float 
00193 IgXMLReader::getFloat (const std::string& str) 
00194 {
00195         //----------- first check that it is a number
00196         
00197         if (!isNumber (str))
00198         {
00199                 std::cerr << "!!!! EXITING: trying to get the float from a string that is not a number " << str << std::endl;
00200                 exit (1);
00201         }
00202         return atof (str.c_str ());
00203 }
00204 
00205 // copied from PhysicsTools/MVATrainer/XMLDocument
00206 bool 
00207 IgXMLReader::getBool (std::string value)
00208 {
00209         for (unsigned int i = 0; i < value.size(); i++)
00210         {
00211                 if (value[i] >= 'A' && value[i] <= 'Z')
00212                 {
00213                         value[i] += 'a' - 'A';
00214                 }
00215         }
00216 
00217         if (value == "1" || value == "y" || value == "yes" ||
00218             value == "true" || value == "ok")
00219         {
00220                 return true;
00221         }
00222         else if (value == "0" || value == "n" || value == "no" || value == "false")
00223         {
00224                 return false;
00225         }
00226         else
00227         {
00228                 std::cerr << "!!!! EXITING: provided string was not a boolean representation: " << value << std::endl;
00229                 exit (1);
00230         }
00231 }

Generated on Tue Jun 9 17:38:50 2009 for CMSSW by  doxygen 1.5.4