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  static std::shared_ptr<void> platformHandle() { return std::make_shared<XercesPlatform>(); }
47  private:
49  public:
51  ~XercesPlatform();
52 
53  private:
54  // do not make any kind of copies
55  XercesPlatform(const XercesPlatform &orig) = delete;
56  XercesPlatform &operator = (const XercesPlatform &orig) = delete;
57 
58  static unsigned int instances;
59  };
60 
61  void init(Handler &handler);
62 
63  std::auto_ptr<XercesPlatform> platform;
64 
65  std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER InputSource> source;
66  std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader> parser;
67 
69 
70  bool done;
71 };
72 
73 class XMLSimpleStr {
74  public:
75  XMLSimpleStr(const XMLCh *str) :
76  string(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str))
77  {}
78 
81 
82  inline operator const char *() const
83  { return string; }
84 
85  inline static bool isAllSpaces(const XMLCh *str, unsigned int length)
87  XMLChar1_0::isAllSpaces(str, length); }
88 
89  inline static bool isSpace(XMLCh ch)
91  XMLChar1_0::isWhitespace(ch); }
92 
93  private:
94  char *string;
95 };
96 
97 class XMLUniStr {
98  public:
99  XMLUniStr(const char *str) :
100  unicode(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str))
101  {}
102 
105 
106  operator const XMLCh *() const
107  { return unicode; }
108 
109  private:
110  XMLCh *unicode;
111 };
112 
113 template<typename T>
116  public:
117  typedef typename T::Stream_t Stream_t;
118 
119  XMLInputSourceWrapper(std::auto_ptr<Stream_t> &obj) : obj(obj) {}
121 
122  XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream* makeStream() const override
123  { return new T(*obj); }
124 
125  private:
126  std::auto_ptr<Stream_t> obj;
127 };
128 
129 class CBInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
130  public:
131  class Reader {
132  public:
133  virtual ~Reader();
134  virtual const std::string &data()= 0;
135  };
136 
137  typedef Reader Stream_t;
138 
140  ~CBInputStream() override;
141 
142  XMLFilePos curPos() const override { return pos; }
143 
144  XMLSize_t readBytes(XMLByte *const buf,
145  const XMLSize_t size) override;
146 
147  const XMLCh* getContentType() const override { return nullptr; }
148 
149  private:
152  unsigned int pos;
153 };
154 
155 class STLInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
156  public:
157  typedef std::istream Stream_t;
158 
159  STLInputStream(std::istream &in);
160  ~STLInputStream() override;
161 
162  XMLFilePos curPos() const override { return pos; }
163 
164  XMLSize_t readBytes(XMLByte *const buf,
165  const XMLSize_t size) override;
166 
167  const XMLCh* getContentType() const override { return nullptr; }
168 
169  private:
170  std::istream &in;
171  unsigned int pos;
172 };
173 
175  public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
176  public:
178 
180  ~StorageInputStream() override;
181 
182  XMLFilePos curPos() const override { return pos; }
183 
184  XMLSize_t readBytes(XMLByte *const buf,
185  const XMLSize_t size) override;
186 
187  const XMLCh* getContentType() const override { return nullptr; }
188 
189  private:
191  unsigned int pos;
192  lzma_stream lstr;
194  unsigned int lasttotal_;
195 
196  unsigned int buffLoc_ = 0,buffTotal_ = 0;
197  std::vector<uint8_t> buffer_;
198  static constexpr unsigned bufferSize_ = 16*1024*1024;
199 };
200 
204 
205 } // namespace lhef
206 
207 #endif // GeneratorInterface_LHEInterface_XMLUtils_h
size
Write out results.
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader > parser
Definition: XMLUtils.h:66
XMLFilePos curPos() const override
Definition: XMLUtils.h:182
unsigned int pos
Definition: XMLUtils.h:171
XMLInputSourceWrapper< StorageInputStream > StorageInputSource
Definition: XMLUtils.h:203
XMLInputSourceWrapper< STLInputStream > STLInputSource
Definition: XMLUtils.h:202
XMLInputSourceWrapper(std::auto_ptr< Stream_t > &obj)
Definition: XMLUtils.h:119
std::istream Stream_t
Definition: XMLUtils.h:157
const XMLCh * getContentType() const override
Definition: XMLUtils.h:147
XMLFilePos curPos() const override
Definition: XMLUtils.h:142
StorageWrap(std::unique_ptr< Storage > storage)
Definition: XMLUtils.cc:25
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER InputSource > source
Definition: XMLUtils.h:65
std::string buffer
Definition: XMLUtils.h:151
XMLInputSourceWrapper< CBInputStream > CBInputSource
Definition: XMLUtils.h:201
std::auto_ptr< XercesPlatform > platform
Definition: XMLUtils.h:63
int init
Definition: HydjetWrapper.h:67
static bool isSpace(XMLCh ch)
Definition: XMLUtils.h:89
#define XERCES_CPP_NAMESPACE_QUALIFIER
Definition: LHERunInfo.h:16
#define constexpr
unsigned int lasttotal_
Definition: XMLUtils.h:194
std::istream & in
Definition: XMLUtils.h:170
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token
Definition: XMLUtils.h:68
XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream * makeStream() const override
Definition: XMLUtils.h:122
Storage * operator->()
Definition: XMLUtils.h:29
static bool isAllSpaces(const XMLCh *str, unsigned int length)
Definition: XMLUtils.h:85
static std::shared_ptr< void > platformHandle()
Definition: XMLUtils.h:46
std::auto_ptr< Stream_t > obj
Definition: XMLUtils.h:126
unsigned int pos
Definition: XMLUtils.h:152
~XMLInputSourceWrapper() override
Definition: XMLUtils.h:120
XMLCh * unicode
Definition: XMLUtils.h:110
StorageWrap & in
Definition: XMLUtils.h:190
StorageWrap Stream_t
Definition: XMLUtils.h:177
const XMLCh * getContentType() const override
Definition: XMLUtils.h:187
def parse(path, config)
Definition: dumpparser.py:13
const XMLCh * getContentType() const override
Definition: XMLUtils.h:167
XMLSimpleStr(const XMLCh *str)
Definition: XMLUtils.h:75
XMLUniStr(const char *str)
Definition: XMLUtils.h:99
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::unique_ptr< Storage > storage
Definition: XMLUtils.h:33
XMLFilePos curPos() const override
Definition: XMLUtils.h:162
static unsigned int instances
Definition: XMLUtils.h:58
std::vector< uint8_t > buffer_
Definition: XMLUtils.h:197
long double T
Helper class to handle FWLite file input sources.