CMS 3D CMS Logo

XMLUtils.h
Go to the documentation of this file.
1 #ifndef GeneratorInterface_LHEInterface_XMLUtils_h
2 #define GeneratorInterface_LHEInterface_XMLUtils_h
3 
4 #include <iostream>
5 #include <string>
6 #include <memory>
7 #include <vector>
8 
9 #include <xercesc/util/XMLString.hpp>
10 #include <xercesc/util/XMLUni.hpp>
11 #include <xercesc/util/XMLChar.hpp>
12 #include <xercesc/util/BinInputStream.hpp>
13 #include <xercesc/framework/XMLPScanToken.hpp>
14 #include <xercesc/sax/InputSource.hpp>
15 #include <xercesc/sax2/DefaultHandler.hpp>
16 #include <xercesc/sax2/SAX2XMLReader.hpp>
17 #include <lzma.h>
18 
19 
20 class Storage;
21 
22 namespace lhef {
23 
24 class StorageWrap {
25  public:
26  StorageWrap(std::unique_ptr<Storage> storage);
27  ~StorageWrap();
28 
29  Storage *operator -> () { return storage.get(); }
30  const Storage *operator -> () const { return storage.get(); }
31 
32  private:
33  std::unique_ptr<Storage> storage;
34 };
35 
36 class XMLDocument {
37  public:
38  class Handler : public XERCES_CPP_NAMESPACE_QUALIFIER DefaultHandler {};
39 
40  XMLDocument(std::auto_ptr<std::istream> &in, Handler &handler);
41  XMLDocument(std::auto_ptr<StorageWrap> &in, Handler &handler);
42  virtual ~XMLDocument();
43 
44  bool parse();
45 
46  private:
48  public:
50  ~XercesPlatform();
51 
52  private:
53  // do not make any kind of copies
54  XercesPlatform(const XercesPlatform &orig);
55  XercesPlatform &operator = (const XercesPlatform &orig);
56 
57  static unsigned int instances;
58  };
59 
60  void init(Handler &handler);
61 
62  std::auto_ptr<XercesPlatform> platform;
63 
64  std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER InputSource> source;
65  std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader> parser;
66 
68 
69  bool done;
70 };
71 
72 class XMLSimpleStr {
73  public:
74  XMLSimpleStr(const XMLCh *str) :
75  string(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str))
76  {}
77 
80 
81  inline operator const char *() const
82  { return string; }
83 
84  inline static bool isAllSpaces(const XMLCh *str, unsigned int length)
86  XMLChar1_0::isAllSpaces(str, length); }
87 
88  inline static bool isSpace(XMLCh ch)
90  XMLChar1_0::isWhitespace(ch); }
91 
92  private:
93  char *string;
94 };
95 
96 class XMLUniStr {
97  public:
98  XMLUniStr(const char *str) :
99  unicode(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str))
100  {}
101 
104 
105  operator const XMLCh *() const
106  { return unicode; }
107 
108  private:
109  XMLCh *unicode;
110 };
111 
112 template<typename T>
115  public:
116  typedef typename T::Stream_t Stream_t;
117 
118  XMLInputSourceWrapper(std::auto_ptr<Stream_t> &obj) : obj(obj) {}
120 
121  virtual XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream* makeStream() const
122  { return new T(*obj); }
123 
124  private:
125  std::auto_ptr<Stream_t> obj;
126 };
127 
128 class CBInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
129  public:
130  class Reader {
131  public:
132  virtual ~Reader();
133  virtual const std::string &data()= 0;
134  };
135 
136  typedef Reader Stream_t;
137 
139  virtual ~CBInputStream();
140 
141  virtual XMLFilePos curPos() const override { return pos; }
142 
143  virtual XMLSize_t readBytes(XMLByte *const buf,
144  const XMLSize_t size) override;
145 
146  virtual const XMLCh* getContentType() const override { return 0; }
147 
148  private:
151  unsigned int pos;
152 };
153 
154 class STLInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
155  public:
156  typedef std::istream Stream_t;
157 
158  STLInputStream(std::istream &in);
159  virtual ~STLInputStream();
160 
161  virtual XMLFilePos curPos() const override { return pos; }
162 
163  virtual XMLSize_t readBytes(XMLByte *const buf,
164  const XMLSize_t size) override;
165 
166  virtual const XMLCh* getContentType() const override { return 0; }
167 
168  private:
169  std::istream &in;
170  unsigned int pos;
171 };
172 
174  public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
175  public:
177 
179  virtual ~StorageInputStream();
180 
181  virtual XMLFilePos curPos() const override { return pos; }
182 
183  virtual XMLSize_t readBytes(XMLByte *const buf,
184  const XMLSize_t size) override;
185 
186  virtual const XMLCh* getContentType() const override { return 0; }
187 
188  private:
190  unsigned int pos;
191  lzma_stream lstr;
193  unsigned int lasttotal_;
194 
195  unsigned int buffLoc_ = 0,buffTotal_ = 0;
196  std::vector<uint8_t> buffer_;
197  static constexpr unsigned bufferSize_ = 16*1024*1024;
198 };
199 
203 
204 } // namespace lhef
205 
206 #endif // GeneratorInterface_LHEInterface_XMLUtils_h
size
Write out results.
virtual const XMLCh * getContentType() const override
Definition: XMLUtils.h:146
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader > parser
Definition: XMLUtils.h:65
unsigned int pos
Definition: XMLUtils.h:170
XMLInputSourceWrapper< StorageInputStream > StorageInputSource
Definition: XMLUtils.h:202
XMLInputSourceWrapper< STLInputStream > STLInputSource
Definition: XMLUtils.h:201
XMLInputSourceWrapper(std::auto_ptr< Stream_t > &obj)
Definition: XMLUtils.h:118
std::istream Stream_t
Definition: XMLUtils.h:156
virtual const XMLCh * getContentType() const override
Definition: XMLUtils.h:186
StorageWrap(std::unique_ptr< Storage > storage)
Definition: XMLUtils.cc:25
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER InputSource > source
Definition: XMLUtils.h:64
std::string buffer
Definition: XMLUtils.h:150
XMLInputSourceWrapper< CBInputStream > CBInputSource
Definition: XMLUtils.h:200
std::auto_ptr< XercesPlatform > platform
Definition: XMLUtils.h:62
int init
Definition: HydjetWrapper.h:67
static bool isSpace(XMLCh ch)
Definition: XMLUtils.h:88
#define XERCES_CPP_NAMESPACE_QUALIFIER
Definition: LHERunInfo.h:16
virtual XMLFilePos curPos() const override
Definition: XMLUtils.h:141
#define constexpr
unsigned int lasttotal_
Definition: XMLUtils.h:193
std::istream & in
Definition: XMLUtils.h:169
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token
Definition: XMLUtils.h:67
Storage * operator->()
Definition: XMLUtils.h:29
static bool isAllSpaces(const XMLCh *str, unsigned int length)
Definition: XMLUtils.h:84
std::auto_ptr< Stream_t > obj
Definition: XMLUtils.h:125
unsigned int pos
Definition: XMLUtils.h:151
XMLCh * unicode
Definition: XMLUtils.h:109
StorageWrap & in
Definition: XMLUtils.h:189
StorageWrap Stream_t
Definition: XMLUtils.h:176
virtual ~XMLInputSourceWrapper()
Definition: XMLUtils.h:119
def parse(path, config)
Definition: dumpparser.py:13
XMLSimpleStr(const XMLCh *str)
Definition: XMLUtils.h:74
virtual XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream * makeStream() const
Definition: XMLUtils.h:121
XMLUniStr(const char *str)
Definition: XMLUtils.h:98
virtual XMLFilePos curPos() const override
Definition: XMLUtils.h:181
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::unique_ptr< Storage > storage
Definition: XMLUtils.h:33
static unsigned int instances
Definition: XMLUtils.h:57
virtual XMLFilePos curPos() const override
Definition: XMLUtils.h:161
std::vector< uint8_t > buffer_
Definition: XMLUtils.h:196
virtual const XMLCh * getContentType() const override
Definition: XMLUtils.h:166
long double T
Helper class to handle FWLite file input sources.