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 namespace edm::storage {
20  class Storage;
21 }
22 
23 namespace lhef {
24 
25  class StorageWrap {
26  public:
28 
29  StorageWrap(std::unique_ptr<Storage> storage);
30  ~StorageWrap();
31 
32  Storage *operator->() { return storage.get(); }
33  const Storage *operator->() const { return storage.get(); }
34 
35  private:
36  std::unique_ptr<Storage> storage;
37  };
38 
39  class XMLDocument {
40  public:
41  class Handler : public XERCES_CPP_NAMESPACE_QUALIFIER DefaultHandler {};
42 
43  XMLDocument(std::unique_ptr<std::istream> &in, Handler &handler);
44  XMLDocument(std::unique_ptr<StorageWrap> &in, Handler &handler);
45  virtual ~XMLDocument();
46 
47  bool parse();
48 
49  static std::shared_ptr<void> platformHandle() { return std::make_shared<XercesPlatform>(); }
50 
51  private:
53  public:
55  // do not make any kind of copies
56  XercesPlatform(const XercesPlatform &orig) = delete;
57  XercesPlatform &operator=(const XercesPlatform &orig) = delete;
59 
60  private:
61  static unsigned int instances;
62  };
63 
64  void init(Handler &handler);
65 
66  std::unique_ptr<XercesPlatform> platform;
67 
68  std::unique_ptr<XERCES_CPP_NAMESPACE_QUALIFIER InputSource> source;
69  std::unique_ptr<XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader> parser;
70 
72 
73  bool done;
74  };
75 
76  class XMLSimpleStr {
77  public:
78  XMLSimpleStr(const XMLCh *str) : string(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str)) {}
79 
81 
82  inline operator const char *() const { return string; }
83 
84  inline static bool isAllSpaces(const XMLCh *str, unsigned int length) {
85  return XERCES_CPP_NAMESPACE_QUALIFIER XMLChar1_0::isAllSpaces(str, length);
86  }
87 
88  inline static bool isSpace(XMLCh ch) { return XERCES_CPP_NAMESPACE_QUALIFIER XMLChar1_0::isWhitespace(ch); }
89 
90  private:
91  char *string;
92  };
93 
94  class XMLUniStr {
95  public:
96  XMLUniStr(const char *str) : unicode(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(str)) {}
97 
99 
100  operator const XMLCh *() const { return unicode; }
101 
102  private:
103  XMLCh *unicode;
104  };
105 
106  template <typename T>
108  public:
109  typedef typename T::Stream_t Stream_t;
110 
111  XMLInputSourceWrapper(std::unique_ptr<Stream_t> &obj) : obj(std::move(obj)) {}
113 
114  XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream *makeStream() const override { return new T(*obj); }
115 
116  private:
117  std::unique_ptr<Stream_t> obj;
118  };
119 
120  class CBInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
121  public:
122  class Reader {
123  public:
124  virtual ~Reader();
125  virtual const std::string &data() = 0;
126  };
127 
128  typedef Reader Stream_t;
129 
131  ~CBInputStream() override;
132 
133  XMLFilePos curPos() const override { return pos; }
134 
135  XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override;
136 
137  const XMLCh *getContentType() const override { return nullptr; }
138 
139  private:
142  unsigned int pos;
143  };
144 
145  class STLInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
146  public:
147  typedef std::istream Stream_t;
148 
149  STLInputStream(std::istream &in);
150  ~STLInputStream() override;
151 
152  XMLFilePos curPos() const override { return pos; }
153 
154  XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override;
155 
156  const XMLCh *getContentType() const override { return nullptr; }
157 
158  private:
159  std::istream &in;
160  unsigned int pos;
161  };
162 
163  class StorageInputStream : public XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream {
164  public:
166 
168  ~StorageInputStream() override;
169 
170  XMLFilePos curPos() const override { return pos; }
171 
172  XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override;
173 
174  const XMLCh *getContentType() const override { return nullptr; }
175 
176  private:
178  unsigned int pos;
179  lzma_stream lstr;
181  unsigned int lasttotal_;
182 
183  unsigned int buffLoc_ = 0, buffTotal_ = 0;
184  std::vector<uint8_t> buffer_;
185  static constexpr unsigned bufferSize_ = 16 * 1024 * 1024;
186  };
187 
191 
192 } // namespace lhef
193 
194 #endif // GeneratorInterface_LHEInterface_XMLUtils_h
size
Write out results.
std::unique_ptr< Stream_t > obj
Definition: XMLUtils.h:117
const XMLCh * getContentType() const override
Definition: XMLUtils.h:156
XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:112
XMLFilePos curPos() const override
Definition: XMLUtils.h:133
XERCES_CPP_NAMESPACE_QUALIFIER BinInputStream * makeStream() const override
Definition: XMLUtils.h:114
unsigned int pos
Definition: XMLUtils.h:160
XMLInputSourceWrapper< StorageInputStream > StorageInputSource
Definition: XMLUtils.h:190
XMLInputSourceWrapper< STLInputStream > STLInputSource
Definition: XMLUtils.h:189
std::istream Stream_t
Definition: XMLUtils.h:147
void init(Handler &handler)
Definition: XMLUtils.cc:65
StorageWrap(std::unique_ptr< Storage > storage)
Definition: XMLUtils.cc:26
StorageInputStream(StorageWrap &in)
Definition: XMLUtils.cc:169
std::string buffer
Definition: XMLUtils.h:141
XMLInputSourceWrapper< CBInputStream > CBInputSource
Definition: XMLUtils.h:188
virtual ~XMLDocument()
Definition: XMLUtils.cc:87
virtual const std::string & data()=0
CBInputStream(Reader &in)
Definition: XMLUtils.cc:108
static bool isSpace(XMLCh ch)
Definition: XMLUtils.h:88
static unsigned int instances
Definition: XMLUtils.h:61
std::unique_ptr< XercesPlatform > platform
Definition: XMLUtils.h:66
XercesPlatform & operator=(const XercesPlatform &orig)=delete
#define XERCES_CPP_NAMESPACE_QUALIFIER
Definition: LHERunInfo.h:16
STLInputStream(std::istream &in)
Definition: XMLUtils.cc:144
unsigned int lasttotal_
Definition: XMLUtils.h:181
const Storage * operator->() const
Definition: XMLUtils.h:33
std::istream & in
Definition: XMLUtils.h:159
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token
Definition: XMLUtils.h:71
static constexpr unsigned bufferSize_
Definition: XMLUtils.h:185
Storage * operator->()
Definition: XMLUtils.h:32
static bool isAllSpaces(const XMLCh *str, unsigned int length)
Definition: XMLUtils.h:84
std::unique_ptr< XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader > parser
Definition: XMLUtils.h:69
static std::shared_ptr< void > platformHandle()
Definition: XMLUtils.h:49
XMLInputSourceWrapper(std::unique_ptr< Stream_t > &obj)
Definition: XMLUtils.h:111
unsigned int pos
Definition: XMLUtils.h:142
~XMLInputSourceWrapper() override
Definition: XMLUtils.h:112
XMLCh * unicode
Definition: XMLUtils.h:103
std::unique_ptr< XERCES_CPP_NAMESPACE_QUALIFIER InputSource > source
Definition: XMLUtils.h:68
XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:198
StorageWrap & in
Definition: XMLUtils.h:177
unsigned int buffLoc_
Definition: XMLUtils.h:183
StorageWrap Stream_t
Definition: XMLUtils.h:165
XMLFilePos curPos() const override
Definition: XMLUtils.h:170
XMLDocument(std::unique_ptr< std::istream > &in, Handler &handler)
Definition: XMLUtils.cc:49
XMLSimpleStr(const XMLCh *str)
Definition: XMLUtils.h:78
unsigned int buffTotal_
Definition: XMLUtils.h:183
XMLUniStr(const char *str)
Definition: XMLUtils.h:96
~StorageInputStream() override
Definition: XMLUtils.cc:196
~STLInputStream() override
Definition: XMLUtils.cc:149
~CBInputStream() override
Definition: XMLUtils.cc:110
std::unique_ptr< Storage > storage
Definition: XMLUtils.h:36
XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:151
std::vector< uint8_t > buffer_
Definition: XMLUtils.h:184
const XMLCh * getContentType() const override
Definition: XMLUtils.h:174
#define str(s)
long double T
const XMLCh * getContentType() const override
Definition: XMLUtils.h:137
XMLFilePos curPos() const override
Definition: XMLUtils.h:152
def move(src, dest)
Definition: eostools.py:511
Helper class to handle FWLite file input sources.