CMS 3D CMS Logo

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