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