CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/GeneratorInterface/LHEInterface/src/XMLUtils.h

Go to the documentation of this file.
00001 #ifndef GeneratorInterface_LHEInterface_XMLUtils_h
00002 #define GeneratorInterface_LHEInterface_XMLUtils_h
00003 
00004 #include <iostream>
00005 #include <string>
00006 #include <memory>
00007 
00008 #include <xercesc/util/XMLString.hpp>
00009 #include <xercesc/util/XMLUni.hpp>
00010 #include <xercesc/util/XMLChar.hpp>
00011 #include <xercesc/util/BinInputStream.hpp>
00012 #include <xercesc/framework/XMLPScanToken.hpp>
00013 #include <xercesc/sax/InputSource.hpp>
00014 #include <xercesc/sax2/DefaultHandler.hpp>
00015 #include <xercesc/sax2/SAX2XMLReader.hpp>
00016 
00017 class Storage;
00018 
00019 namespace lhef {
00020 
00021 class StorageWrap {
00022     public:
00023         StorageWrap(Storage *storage);
00024         ~StorageWrap();
00025 
00026         Storage *operator -> () { return storage.get(); }
00027         const Storage *operator -> () const { return storage.get(); }
00028 
00029     private:
00030         std::auto_ptr<Storage>  storage;
00031 };
00032 
00033 class XMLDocument {
00034     public:
00035         class Handler : public XERCES_CPP_NAMESPACE_QUALIFIER DefaultHandler {};
00036 
00037         XMLDocument(std::auto_ptr<std::istream> &in, Handler &handler);
00038         XMLDocument(std::auto_ptr<StorageWrap> &in, Handler &handler);
00039         virtual ~XMLDocument();
00040 
00041         bool parse();
00042 
00043     private:
00044         class XercesPlatform {
00045             public:
00046                 XercesPlatform();
00047                 ~XercesPlatform();
00048 
00049             private:
00050                 // do not make any kind of copies
00051                 XercesPlatform(const XercesPlatform &orig);
00052                 XercesPlatform &operator = (const XercesPlatform &orig);
00053 
00054                 static unsigned int instances;
00055         };
00056 
00057         void init(Handler &handler);
00058 
00059         std::auto_ptr<XercesPlatform>                                   platform;
00060 
00061         std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER InputSource>       source;
00062         std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader>     parser;
00063 
00064         XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken                    token;
00065 
00066         bool                                                            done;
00067 };
00068 
00069 class XMLSimpleStr {
00070     public:
00071         XMLSimpleStr(const XMLCh *str) :
00072                 string(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str))
00073         {}
00074 
00075         ~XMLSimpleStr()
00076         { XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&string); }
00077 
00078         inline operator const char *() const
00079         { return string; }
00080 
00081         inline static bool isAllSpaces(const XMLCh *str, unsigned int length)
00082         { return XERCES_CPP_NAMESPACE_QUALIFIER
00083                                 XMLChar1_0::isAllSpaces(str, length); }
00084 
00085         inline static bool isSpace(XMLCh ch)
00086         { return XERCES_CPP_NAMESPACE_QUALIFIER
00087                                 XMLChar1_0::isWhitespace(ch); }
00088 
00089     private:
00090         char    *string;
00091 };
00092 
00093 class XMLUniStr {
00094     public:
00095         XMLUniStr(const char *str) :
00096                 unicode(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str))
00097         {}
00098 
00099         ~XMLUniStr()
00100         { XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&unicode); }
00101 
00102         operator const XMLCh *() const
00103         { return unicode; }
00104 
00105     private:
00106         XMLCh   *unicode;
00107 };
00108 
00109 template<typename T>
00110 class XMLInputSourceWrapper :
00111                         public XERCES_CPP_NAMESPACE_QUALIFIER InputSource {
00112     public:
00113         typedef typename T::Stream_t Stream_t;
00114 
00115         XMLInputSourceWrapper(std::auto_ptr<Stream_t> &obj) : obj(obj) {}
00116         virtual ~XMLInputSourceWrapper() {}
00117 
00118         virtual XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream* makeStream() const
00119         { return new T(*obj); }
00120 
00121     private:
00122         std::auto_ptr<Stream_t> obj;
00123 };
00124 
00125 class CBInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
00126     public:
00127         class Reader {
00128             public:
00129                 virtual ~Reader();
00130                 virtual const std::string &data()= 0;
00131         };
00132 
00133         typedef Reader Stream_t;
00134 
00135         CBInputStream(Reader &in);
00136         virtual ~CBInputStream();
00137 
00138         virtual unsigned int curPos() const { return pos; }
00139 
00140         virtual unsigned int readBytes(XMLByte *const buf,
00141                                        const unsigned int size);
00142 
00143     private:
00144         Reader          &reader;
00145         std::string     buffer;
00146         unsigned int    pos;
00147 };
00148 
00149 class STLInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
00150     public:
00151         typedef std::istream Stream_t;
00152 
00153         STLInputStream(std::istream &in);
00154         virtual ~STLInputStream();
00155 
00156         virtual unsigned int curPos() const { return pos; }
00157 
00158         virtual unsigned int readBytes(XMLByte *const buf,
00159                                        const unsigned int size);
00160 
00161     private:
00162         std::istream    &in;
00163         unsigned int    pos;
00164 };
00165 
00166 class StorageInputStream :
00167                 public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
00168     public:
00169         typedef StorageWrap Stream_t;
00170 
00171         StorageInputStream(StorageWrap &in);
00172         virtual ~StorageInputStream();
00173 
00174         virtual unsigned int curPos() const { return pos; }
00175 
00176         virtual unsigned int readBytes(XMLByte *const buf,
00177                                        const unsigned int size);
00178 
00179     private:
00180         StorageWrap     &in;
00181         unsigned int    pos;
00182 };
00183 
00184 typedef XMLInputSourceWrapper<CBInputStream> CBInputSource;
00185 typedef XMLInputSourceWrapper<STLInputStream> STLInputSource;
00186 typedef XMLInputSourceWrapper<StorageInputStream> StorageInputSource;
00187 
00188 } // namespace lhef
00189 
00190 #endif // GeneratorInterface_LHEInterface_XMLUtils_h