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()),
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 
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
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
lhef::XMLDocument::platform
std::unique_ptr< XercesPlatform > platform
Definition: XMLUtils.h:63
lhef::StorageWrap::StorageWrap
StorageWrap(std::unique_ptr< Storage > storage)
Definition: XMLUtils.cc:25
mps_fire.i
i
Definition: mps_fire.py:428
submitPVValidationJobs.instances
list instances
Definition: submitPVValidationJobs.py:855
funct::false
false
Definition: Factorize.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
min
T min(T a, T b)
Definition: MathUtil.h:58
lhef::CBInputStream::Reader
Definition: XMLUtils.h:119
lhef::XMLDocument::init
void init(Handler &handler)
Definition: XMLUtils.cc:64
pos
Definition: PixelAliasList.h:18
lhef::StorageInputStream::buffTotal_
unsigned int buffTotal_
Definition: XMLUtils.h:180
lhef::XMLDocument::XercesPlatform::~XercesPlatform
~XercesPlatform()
Definition: XMLUtils.cc:43
Storage::SET
Definition: Storage.h:22
lhef::STLInputStream::readBytes
XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:150
cms::cuda::assert
assert(be >=bs)
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
lhef::StorageInputStream::buffer_
std::vector< uint8_t > buffer_
Definition: XMLUtils.h:181
XERCES_CPP_NAMESPACE_USE
Definition: XMLConfigWriter.cc:40
lhef::STLInputStream::~STLInputStream
~STLInputStream() override
Definition: XMLUtils.cc:148
Storage.h
lhef::XMLDocument::XercesPlatform
Definition: XMLUtils.h:48
edm::Exception
Definition: EDMException.h:77
EDMException.h
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
XMLUtils.h
lhef::XMLDocument::done
bool done
Definition: XMLUtils.h:70
lhef::StorageInputStream::bufferSize_
static constexpr unsigned bufferSize_
Definition: XMLUtils.h:182
fileCollector.done
done
Definition: fileCollector.py:123
lhef::XMLDocument::parse
bool parse()
Definition: XMLUtils.cc:88
source
static const std::string source
Definition: EdmProvDump.cc:47
lhef::StorageInputStream::~StorageInputStream
~StorageInputStream() override
Definition: XMLUtils.cc:195
lhef::CBInputStream::Reader::data
virtual const std::string & data()=0
lhef::CBInputStream::~CBInputStream
~CBInputStream() override
Definition: XMLUtils.cc:109
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::StorageInputStream::lstr
lzma_stream lstr
Definition: XMLUtils.h:176
lhef::XMLDocument::token
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token
Definition: XMLUtils.h:68
Storage::read
virtual IOSize read(void *into, IOSize n, IOOffset pos)
Definition: Storage.cc:12
lhef::XMLDocument::Handler
Definition: XMLUtils.h:37
lhef
Definition: ExhumeHadronizer.h:12
recoMuon::in
Definition: RecoMuonEnumerators.h:6
IOOffset
int64_t IOOffset
Definition: IOTypes.h:19
lhef::XMLDocument::~XMLDocument
virtual ~XMLDocument()
Definition: XMLUtils.cc:86
lhef::XMLSimpleStr
Definition: XMLUtils.h:73
createfilelist.int
int
Definition: createfilelist.py:10
Xerces.h
cms::concurrency::xercesTerminate
void xercesTerminate()
Definition: Xerces.cc:23
IOTypes.h
lhef::XMLDocument::XercesPlatform::instances
static unsigned int instances
Definition: XMLUtils.h:58
visDQMUpload.buf
buf
Definition: visDQMUpload.py:154
NULL
#define NULL
Definition: scimark2.h:8
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
lhef::StorageInputStream::in
StorageWrap & in
Definition: XMLUtils.h:174
lhef::StorageWrap::~StorageWrap
~StorageWrap()
Definition: XMLUtils.cc:27
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
mergeAndRegister.rest
rest
Definition: mergeAndRegister.py:121
lhef::STLInputStream::in
std::istream & in
Definition: XMLUtils.h:156
Storage::position
virtual IOOffset position(void) const
Definition: Storage.cc:72
Exception
Definition: hltDiff.cc:245
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
Exception.h
lhef::StorageInputStream::buffLoc_
unsigned int buffLoc_
Definition: XMLUtils.h:180
Storage::CURRENT
Definition: Storage.h:22
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:292
lhef::CBInputStream::Reader::~Reader
virtual ~Reader()
Definition: XMLUtils.cc:105
cms::Exception
Definition: Exception.h:70
lhef::CBInputStream::CBInputStream
CBInputStream(Reader &in)
Definition: XMLUtils.cc:107
edm::errors::FileReadError
Definition: EDMException.h:50
cms::concurrency::xercesInitialize
void xercesInitialize()
Definition: Xerces.cc:18
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
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37