CMS 3D CMS Logo

XMLUtils.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 #include <string>
4 #include <vector>
5 #include <cstring>
6 
8 #include <xercesc/util/XMLString.hpp>
9 #include <xercesc/util/XMLUni.hpp>
10 #include <xercesc/sax2/SAX2XMLReader.hpp>
11 #include <xercesc/sax2/XMLReaderFactory.hpp>
12 
15 
18 
19 #include "XMLUtils.h"
20 
22 
23 namespace lhef {
24 
25  StorageWrap::StorageWrap(std::unique_ptr<Storage> storage) : storage(std::move(storage)) {}
26 
28 
30 
32  if (!instances++) {
33  try {
35  } catch (const XMLException &e) {
36  throw cms::Exception("XMLDocument") << "cms::concurrency::xercesInitialize failed "
37  "because of: "
38  << XMLSimpleStr(e.getMessage()) << std::endl;
39  }
40  }
41  }
42 
44  if (!--instances)
46  }
47 
48  XMLDocument::XMLDocument(std::unique_ptr<std::istream> &in, Handler &handler)
49  : platform(new XercesPlatform()),
50  source(new STLInputSource(in)),
51  parser(XMLReaderFactory::createXMLReader()),
52  done(false) {
53  init(handler);
54  }
55 
56  XMLDocument::XMLDocument(std::unique_ptr<StorageWrap> &in, Handler &handler)
57  : platform(new XercesPlatform()),
58  source(new StorageInputSource(in)),
59  parser(XMLReaderFactory::createXMLReader()),
60  done(false) {
61  init(handler);
62  }
63 
64  void XMLDocument::init(Handler &handler) {
65  try {
66  parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
67  parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
68  parser->setFeature(XMLUni::fgXercesSchema, false);
69  parser->setFeature(XMLUni::fgXercesSchemaFullChecking, false);
70 
71  parser->setContentHandler(&handler);
72  parser->setLexicalHandler(&handler);
73  parser->setErrorHandler(&handler);
74 
75  if (!parser->parseFirst(*source, token))
76  throw cms::Exception("XMLParseError") << "SAXParser::parseFirst failed" << std::endl;
77  } catch (const XMLException &e) {
78  throw cms::Exception("XMLDocument")
79  << "cms::concurrency::xercesInitialize failed because of " << XMLSimpleStr(e.getMessage()) << std::endl;
80  } catch (const SAXException &e) {
81  throw cms::Exception("XMLDocument")
82  << "XML parser reported: " << XMLSimpleStr(e.getMessage()) << "." << std::endl;
83  }
84  }
85 
87 
89  try {
90  if (done || parser->getErrorCount())
91  return false;
92 
93  done = !parser->parseNext(token);
94  } catch (const XMLException &e) {
95  throw cms::Exception("XMLDocument")
96  << "cms::concurrency::xercesInitialize failed because of " << XMLSimpleStr(e.getMessage()) << std::endl;
97  } catch (const SAXException &e) {
98  throw cms::Exception("XMLDocument")
99  << "XML parser reported: " << XMLSimpleStr(e.getMessage()) << "." << std::endl;
100  }
101 
102  return !done;
103  }
104 
106 
108 
110 
111  XMLSize_t CBInputStream::readBytes(XMLByte *const buf, const XMLSize_t size) {
112  char *rawBuf = reinterpret_cast<char *>(buf);
113  unsigned int bytes = size * sizeof(XMLByte);
114  unsigned int read = 0;
115 
116  while (read < bytes) {
117  if (buffer.empty()) {
118  buffer = reader.data();
119  if (buffer.empty())
120  break;
121  }
122 
123  unsigned int len = buffer.length();
124  unsigned int rem = bytes - read;
125  if (rem < len) {
126  std::memcpy(rawBuf + read, buffer.c_str(), rem);
127  buffer.erase(0, rem);
128  read += rem;
129  break;
130  }
131 
132  std::memcpy(rawBuf + read, buffer.c_str(), len);
133  buffer.clear();
134  read += len;
135  }
136 
137  read /= sizeof(XMLByte);
138  pos += read;
139 
140  return read;
141  }
142 
143  STLInputStream::STLInputStream(std::istream &in) : in(in) {
144  if (in.bad())
145  throw cms::Exception("FileStreamError") << "I/O stream bad in STLInputStream::STLInputStream()" << std::endl;
146  }
147 
149 
150  XMLSize_t STLInputStream::readBytes(XMLByte *const buf, const XMLSize_t size) {
151  char *rawBuf = reinterpret_cast<char *>(buf);
152  unsigned int bytes = size * sizeof(XMLByte);
153  in.read(rawBuf, bytes);
154  unsigned int readBytes = in.gcount();
155 
156  if (in.bad())
157  throw cms::Exception("FileStreamError") << "I/O stream bad in STLInputStream::readBytes()" << std::endl;
158 
159  unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
160  unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
161  for (unsigned int i = 1; i <= rest; i++)
162  in.putback(rawBuf[readBytes - i]);
163 
164  pos += read;
165  return read;
166  }
167 
169  : in(in), lstr(LZMA_STREAM_INIT), compression_(false), lasttotal_(0) {
170  buffer_.reserve(bufferSize_);
171  // Check the kind of file.
172  char header[6];
173  /*unsigned int s = */ in->read(header, 6);
174  in->position(0, Storage::SET);
175  // Let's use lzma to start with.
176  if (header[1] == '7' && header[2] == 'z' && header[3] == 'X' && header[4] == 'Z') {
177  compression_ = true;
178  lstr = LZMA_STREAM_INIT;
179  // We store the beginning of the outBuffer to make sure
180  // we can always update previous results.
181 
182 #if LZMA_VERSION <= UINT32_C(49990030)
183  int ret = lzma_auto_decoder(&lstr, NULL, NULL);
184 #else
185  int ret = lzma_auto_decoder(&lstr, -1, 0);
186 #endif
187 
188  if (ret != LZMA_OK) {
189  lzma_end(&lstr);
190  throw cms::Exception("IO") << "Error while reading compressed LHE file";
191  }
192  }
193  }
194 
196 
197  XMLSize_t StorageInputStream::readBytes(XMLByte *const buf, const XMLSize_t size) {
198  // Compression code is not able to handle sizeof(XMLByte) > 1.
199  assert(sizeof(XMLByte) == sizeof(unsigned char));
200 
201  if (!(buffLoc_ < buffTotal_)) {
202  int rd = in->read((void *)&buffer_[0], buffer_.capacity());
203  // Storage layer is supposed to throw exceptions instead of returning errors; just-in-case
204  if (rd < 0) {
206  ex << "Error while reading buffered LHE file";
207  throw ex;
208  }
209  buffLoc_ = 0;
210  buffTotal_ = rd;
211  if (buffTotal_ == 0) {
212  return 0;
213  }
214  }
215  unsigned int dataRead;
216  if (!compression_) {
217  dataRead = std::min(buffTotal_ - buffLoc_, static_cast<unsigned int>(size));
218  memcpy(buf, &buffer_[buffLoc_], dataRead);
219  buffLoc_ += dataRead;
220  } else {
221  dataRead = buffTotal_ - buffLoc_;
222  lstr.next_in = &buffer_[buffLoc_];
223  lstr.avail_in = dataRead;
224  lstr.next_out = buf;
225  lstr.avail_out = size;
226  int ret = lzma_code(&lstr, LZMA_RUN);
227  if (ret != LZMA_OK && ret != LZMA_STREAM_END) { /* decompression error */
228  lzma_end(&lstr);
229  throw cms::Exception("IO") << "Error while reading compressed LHE file (error code " << ret << ")";
230  }
231  dataRead -= lstr.avail_in;
232  buffLoc_ += dataRead;
233  // Decoder was unable to make progress; reset stream and try again.
234  // If this becomes problematic, we can make the buffer circular.
235  if (!dataRead) {
236  // NOTE: lstr.avail_in == buffTotal-buffLoc_
237  in->position(-(IOOffset)(lstr.avail_in), Storage::CURRENT);
238  buffLoc_ = 0;
239  buffTotal_ = 0;
240  return readBytes(buf, size);
241  }
242  dataRead = (size - lstr.avail_out);
243  }
244  return dataRead;
245  }
246 
247 } // namespace lhef
size
Write out results.
XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:111
void init(Handler &handler)
Definition: XMLUtils.cc:64
virtual IOSize read(void *into, IOSize n, IOOffset pos)
Definition: Storage.cc:12
StorageWrap(std::unique_ptr< Storage > storage)
Definition: XMLUtils.cc:25
StorageInputStream(StorageWrap &in)
Definition: XMLUtils.cc:168
std::string buffer
Definition: XMLUtils.h:138
ret
prodAgent to be discontinued
void xercesTerminate()
Definition: Xerces.cc:23
virtual ~XMLDocument()
Definition: XMLUtils.cc:86
virtual const std::string & data()=0
CBInputStream(Reader &in)
Definition: XMLUtils.cc:107
static unsigned int instances
Definition: XMLUtils.h:58
std::unique_ptr< XercesPlatform > platform
Definition: XMLUtils.h:63
#define NULL
Definition: scimark2.h:8
reader
Definition: DQM.py:105
void xercesInitialize()
Definition: Xerces.cc:18
STLInputStream(std::istream &in)
Definition: XMLUtils.cc:143
std::istream & in
Definition: XMLUtils.h:156
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token
Definition: XMLUtils.h:68
virtual IOOffset position(void) const
Definition: Storage.cc:72
static constexpr unsigned bufferSize_
Definition: XMLUtils.h:182
std::unique_ptr< XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader > parser
Definition: XMLUtils.h:66
T min(T a, T b)
Definition: MathUtil.h:58
std::unique_ptr< XERCES_CPP_NAMESPACE_QUALIFIER InputSource > source
Definition: XMLUtils.h:65
XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:197
StorageWrap & in
Definition: XMLUtils.h:174
unsigned int buffLoc_
Definition: XMLUtils.h:180
XMLDocument(std::unique_ptr< std::istream > &in, Handler &handler)
Definition: XMLUtils.cc:48
int64_t IOOffset
Definition: IOTypes.h:19
unsigned int buffTotal_
Definition: XMLUtils.h:180
~StorageInputStream() override
Definition: XMLUtils.cc:195
~STLInputStream() override
Definition: XMLUtils.cc:148
~CBInputStream() override
Definition: XMLUtils.cc:109
std::unique_ptr< Storage > storage
Definition: XMLUtils.h:32
XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:150
std::vector< uint8_t > buffer_
Definition: XMLUtils.h:181
static std::string const source
Definition: EdmProvDump.cc:47
def move(src, dest)
Definition: eostools.py:511