Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

XMLHandler.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_FORMAT_HANDLERS_XMLHANDLER_H
00028 #define OPENMS_FORMAT_HANDLERS_XMLHANDLER_H
00029 
00030 #include <iostream>
00031 
00032 #include <OpenMS/CONCEPT/Types.h>
00033 #include <OpenMS/CONCEPT/Macros.h>
00034 
00035 #include <OpenMS/DATASTRUCTURES/DateTime.h>
00036 #include <OpenMS/METADATA/MetaInfoInterface.h>
00037 
00038 #include <xercesc/sax2/DefaultHandler.hpp>
00039 #include <xercesc/sax/Locator.hpp>
00040 #include <xercesc/sax2/Attributes.hpp>
00041 
00042 namespace OpenMS
00043 {
00044   namespace Internal
00045   {
00046   
00048     class StringManager 
00049     {
00050       public:
00052         StringManager();
00053       
00055         ~StringManager();
00056             
00058         void clear();
00059       
00061         XMLCh* convert(const char* str) const;
00062       
00064         XMLCh* convert(const std::string& str) const;
00065 
00067         XMLCh* convert(const String& str) const;
00068       
00070         char* convert(const XMLCh* str) const;
00071       private:
00072         mutable std::vector<XMLCh*> xml_strings_ ;
00073         mutable std::vector<char*> c_strings_ ;     
00074     };
00075 
00079   class XMLHandler
00080     : public xercesc::DefaultHandler
00081   {
00082     public:
00084       class EndParsingSoftly
00085         : public Exception::Base
00086       {
00087         public:
00088           EndParsingSoftly(const char* file, int line, const char* function) throw()
00089             :Exception::Base(file,line,function)
00090           {
00091           }
00092       };
00093 
00095       XMLHandler(const String& filename, const String& version);
00097       virtual ~XMLHandler();
00098 
00105       void fatalError(const xercesc::SAXParseException& exception);
00106       void error(const xercesc::SAXParseException& exception);
00107       void warning(const xercesc::SAXParseException& exception);
00109       
00111       void fatalError(const String& msg, UInt line=0, UInt column=0) const;
00113       void error(const String& msg, UInt line=0, UInt column=0) const;
00115       void warning(const String& msg, UInt line=0, UInt column=0) const;
00116       
00118       virtual void characters(const XMLCh* const chars, unsigned int length);
00120       virtual void startElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname, const xercesc::Attributes& attrs);
00122       virtual void endElement( const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname);
00123 
00125       virtual void writeTo(std::ostream& /*os*/) {};
00126       
00128       String errorString();
00129 
00130     protected:
00132       mutable String error_message_;
00133       
00135       String file_;
00136       
00138       String version_;
00139       
00141       StringManager sm_;
00142       
00148       std::vector<String> open_tags_;
00149       
00151       inline bool equal_(const XMLCh* a, const XMLCh* b)
00152       {
00153         return xercesc::XMLString::compareString(a,b)==0;
00154       }
00155 
00157 
00158 
00169       inline void writeCVS_(std::ostream& os, DoubleReal value, const String& acc, const String& name, int indent=4) const
00170       {
00171         if (value!=0.0)
00172         {
00173           os << String(indent,'\t') << "<cvParam cvLabel=\"psi\" accession=\"PSI:" << acc << "\" name=\"" << name << "\" value=\"" << value << "\"/>\n";
00174         }
00175       }
00187       inline void writeCVS_(std::ostream& os, const String& value, const String& acc, const String& name, int indent=4) const
00188       {
00189         if (value!="")
00190         {
00191           os << String(indent,'\t') << "<cvParam cvLabel=\"psi\" accession=\"PSI:" << acc << "\" name=\"" << name << "\" value=\"" << value << "\"/>\n";
00192         }
00193       }
00194 
00208       void writeCVS_(std::ostream& os, int value, int map, const String& acc, const String& name, int indent=4);
00209 
00211       inline void writeUserParam_(std::ostream& os, const MetaInfoInterface& meta, int indent=4)
00212       {
00213         std::vector<String> keys;
00214         meta.getKeys(keys);
00215         for (std::vector<String>::const_iterator it = keys.begin(); it!=keys.end(); ++it)
00216         {
00217           if ( (*it)[0] != '#')  // internally used meta info start with '#'
00218           {
00219             os << String(indent,'\t') << "<userParam name=\"" << *it << "\" value=\"" << meta.getMetaValue(*it) << "\"/>\n";
00220           }
00221         }
00222       }
00224 
00226 
00227 
00228       void writeUserParam_(const String& tag_name, std::ostream& os, const MetaInfoInterface& meta, UInt indent) const;
00230 
00232 
00233 
00234       std::vector< std::vector<String> > cv_terms_;
00235       
00237       inline UInt cvStringToEnum_(UInt section, const String& term, const char* message)
00238       {
00239         OPENMS_PRECONDITION(section<cv_terms_.size(),"cvStringToEnum_: Index overflow (secion number too large)");
00240         //std::cout << "looking up key \"" << value << "\" in map nr. " << index << "..." << std::endl;
00241     
00242         std::vector<String>::const_iterator it = std::find(cv_terms_[section].begin(), cv_terms_[section].end(), term);
00243         if (it == cv_terms_[section].end())
00244         {
00245           std::cout << "Warning: Unexpected CV entry '" << message << "'='" << term << "' parsed in " << file_ << std::endl;
00246         }
00247         else
00248         {
00249           return  (it - cv_terms_[section].begin());
00250         }
00251         
00252         return 0;
00253       }
00254 
00256       
00258 
00259 
00260       inline Int asInt_(const String& in)
00261       {
00262         Int res = 0;
00263         try
00264         {
00265           res = in.toInt();
00266         }
00267         catch (Exception::ConversionError)
00268         {
00269           error(String("Int conversion error of \"") + in + "\"");
00270         }
00271         return res;
00272       }
00274       inline Int asInt_(const XMLCh* in)
00275       {
00276         return xercesc::XMLString::parseInt(in);
00277       }
00279       inline UInt asUInt_(const String& in)
00280       {
00281         UInt res = 0;
00282         try
00283         {
00284           Int tmp = in.toInt();
00285           if (tmp<0)
00286           {
00287             Exception::ConversionError(__FILE__, __LINE__, __PRETTY_FUNCTION__,"");
00288           }
00289           res = UInt(tmp);
00290         }
00291         catch (Exception::ConversionError)
00292         {
00293           error(String("UInt conversion error of \"") + in + "\"");
00294         }
00295         return res;
00296       }
00298       inline double asDouble_(const String& in)
00299       {
00300         double res = 0.0;
00301         try
00302         {
00303           res = in.toDouble();
00304         }
00305         catch (Exception::ConversionError)
00306         {
00307           error(String("Double conversion error of \"") + in + "\"");
00308         }
00309         return res;
00310       }
00312       inline float asFloat_(const String& in)
00313       {
00314         float res = 0.0;
00315         try
00316         {
00317           res = in.toFloat();
00318         }
00319         catch (Exception::ConversionError)
00320         {
00321           error(String("Float conversion error of \"") + in + "\"");
00322         }
00323         return res;
00324       }
00326       inline bool asBool_(const String& in)
00327       {
00328         if (in == "true" || in == "TRUE" || in == "True" || in == "1") 
00329         {
00330           return true;
00331         }
00332         else if (in == "false" || in == "FALSE" || in == "False" || in == "0")
00333         {
00334            return false;
00335         }
00336         else 
00337         {
00338           error(String("Boolean conversion error of \"") + in + "\"");
00339         }
00340         return false;
00341       }
00343       inline DateTime asDateTime_(const String& in)
00344       {
00345         //std::cout << "IN: " << in << std::endl;
00346         DateTime res;
00347         if (in!="")
00348         { 
00349           try
00350           {
00351             // xs::DateTime to OpenMS::DateTime
00352             String tmp(in);
00353             tmp.substitute('T', ' ');
00354             tmp = tmp.substr(0,19);
00355             res.set(tmp);
00356           }
00357           catch(Exception::ParseError err)
00358           {
00359             error(String("DateTime conversion error of \"") + in + "\"");
00360           }
00361         }
00362         return res;
00363       }
00365     
00367 
00368 
00369       inline char* attributeAsString_(const xercesc::Attributes& a, const char* name) const
00370       {
00371         const XMLCh* val = a.getValue(sm_.convert(name));
00372         if (val==0) fatalError(String("Required attribute '") + name + "' not present!");
00373         return sm_.convert(val);
00374       }
00376       inline Int attributeAsInt_(const xercesc::Attributes& a, const char* name) const
00377       {
00378         const XMLCh* val = a.getValue(sm_.convert(name));
00379         if (val==0) fatalError(String("Required attribute '") + name + "' not present!");
00380         return xercesc::XMLString::parseInt(val);
00381       }
00383       inline DoubleReal attributeAsDouble_(const xercesc::Attributes& a, const char* name) const
00384       {
00385         const XMLCh* val = a.getValue(sm_.convert(name));
00386         if (val==0) fatalError(String("Required attribute '") + name + "' not present!");
00387         return atof(sm_.convert(val));
00388       }
00390       inline void optionalAttributeAsString_(String& value, const xercesc::Attributes& a, const char* name) const
00391       {
00392         const XMLCh* val = a.getValue(sm_.convert(name));
00393         if (val!=0)
00394         {
00395           char* tmp2 = sm_.convert(val);
00396           if (String(tmp2) != "")
00397           {
00398             value = tmp2;
00399           }
00400         }
00401       }
00403       inline void optionalAttributeAsInt_(Int& value, const xercesc::Attributes& a, const char* name) const
00404       {
00405         const XMLCh* val = a.getValue(sm_.convert(name));
00406         if (val!=0)
00407         {
00408           value = xercesc::XMLString::parseInt(val);
00409         }
00410       }
00412       inline void optionalAttributeAsUInt_(UInt& value, const xercesc::Attributes& a, const char* name) const
00413       {
00414         const XMLCh* val = a.getValue(sm_.convert(name));
00415         if (val!=0)
00416         {
00417           value = xercesc::XMLString::parseInt(val);
00418         }
00419       }
00421       inline void optionalAttributeAsDouble_(DoubleReal& value, const xercesc::Attributes& a, const char* name) const
00422       {
00423         const XMLCh* val = a.getValue(sm_.convert(name));
00424         if (val!=0)
00425         {
00426           value = atof(sm_.convert(val));
00427         }
00428       }
00430       inline char* attributeAsString_(const xercesc::Attributes& a, const XMLCh* name) const
00431       {
00432         const XMLCh* val = a.getValue(name);
00433         if (val==0) fatalError(String("Required attribute '") + sm_.convert(name) + "' not present!");
00434         return sm_.convert(val);
00435       }
00437       inline Int attributeAsInt_(const xercesc::Attributes& a, const XMLCh* name) const
00438       {
00439         const XMLCh* val = a.getValue(name);
00440         if (val==0) fatalError(String("Required attribute '") + sm_.convert(name) + "' not present!");
00441         return xercesc::XMLString::parseInt(val);
00442       }
00444       inline DoubleReal attributeAsDouble_(const xercesc::Attributes& a, const XMLCh* name) const
00445       {
00446         const XMLCh* val = a.getValue(name);
00447         if (val==0) fatalError(String("Required attribute '") + sm_.convert(name) + "' not present!");
00448         return atof(sm_.convert(val));
00449       }
00451       inline void optionalAttributeAsString_(String& value, const xercesc::Attributes& a, const XMLCh* name) const
00452       {
00453         const XMLCh* val = a.getValue(name);
00454         if (val!=0)
00455         {
00456           char* tmp2 = sm_.convert(val);
00457           if (String(tmp2) != "")
00458           {
00459             value = tmp2;
00460           }
00461         }
00462       }
00464       inline void optionalAttributeAsInt_(Int& value, const xercesc::Attributes& a, const XMLCh* name) const
00465       {
00466         const XMLCh* val = a.getValue(name);
00467         if (val!=0)
00468         {
00469           value = xercesc::XMLString::parseInt(val);
00470         }
00471       }
00473       inline void optionalAttributeAsUInt_(UInt& value, const xercesc::Attributes& a, const XMLCh* name) const
00474       {
00475         const XMLCh* val = a.getValue(name);
00476         if (val!=0)
00477         {
00478           value = xercesc::XMLString::parseInt(val);
00479         }
00480       }
00482       inline void optionalAttributeAsDouble_(DoubleReal& value, const xercesc::Attributes& a, const XMLCh* name) const
00483       {
00484         const XMLCh* val = a.getValue(name);
00485         if (val!=0)
00486         {
00487           value = atof(sm_.convert(val));
00488         }
00489       }
00491     
00492     private:
00494       XMLHandler();
00495   };
00496   
00497   } // namespace Internal
00498 } // namespace OpenMS
00499 
00500 #endif // OPENMS_FORMAT_HANDLERS_XMLHANDLER_H

Generated Tue Apr 1 15:36:39 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1