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