CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
XMLUtils.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 #include <string>
4 #include <cstring>
5 
7 #include <xercesc/util/XMLString.hpp>
8 #include <xercesc/util/XMLUni.hpp>
9 #include <xercesc/sax2/SAX2XMLReader.hpp>
10 #include <xercesc/sax2/XMLReaderFactory.hpp>
11 
13 
16 
17 #include "XMLUtils.h"
18 
19 XERCES_CPP_NAMESPACE_USE
20 #define BUF_SIZE 8192
21 
22 namespace lhef {
23 
25  storage(storage)
26 {
27 }
28 
30 {
31  storage->close();
32 }
33 
35 
37 {
38  if (!instances++) {
39  try {
41  } catch(const XMLException &e) {
42  throw cms::Exception("XMLDocument")
43  << "cms::concurrency::xercesInitialize failed "
44  "because of: "
45  << XMLSimpleStr(e.getMessage()) << std::endl;
46  }
47  }
48 }
49 
51 {
52  if (!--instances)
54 }
55 
56 XMLDocument::XMLDocument(std::auto_ptr<std::istream> &in, Handler &handler) :
57  platform(new XercesPlatform()),
58  source(new STLInputSource(in)),
59  parser(XMLReaderFactory::createXMLReader()),
60  done(false)
61 {
62  init(handler);
63 }
64 
65 XMLDocument::XMLDocument(std::auto_ptr<StorageWrap> &in, Handler &handler) :
66  platform(new XercesPlatform()),
67  source(new StorageInputSource(in)),
68  parser(XMLReaderFactory::createXMLReader()),
69  done(false)
70 {
71  init(handler);
72 }
73 
74 void XMLDocument::init(Handler &handler)
75 {
76  try {
77  parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
78  parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
79  parser->setFeature(XMLUni::fgXercesSchema, false);
80  parser->setFeature(XMLUni::fgXercesSchemaFullChecking, false);
81 
82  parser->setContentHandler(&handler);
83  parser->setLexicalHandler(&handler);
84  parser->setErrorHandler(&handler);
85 
86  if (!parser->parseFirst(*source, token))
87  throw cms::Exception("XMLParseError")
88  << "SAXParser::parseFirst failed" << std::endl;
89  } catch(const XMLException &e) {
90  throw cms::Exception("XMLDocument")
91  << "cms::concurrency::xercesInitialize failed because of "
92  << XMLSimpleStr(e.getMessage()) << std::endl;
93  } catch(const SAXException &e) {
94  throw cms::Exception("XMLDocument")
95  << "XML parser reported: "
96  << XMLSimpleStr(e.getMessage()) << "." << std::endl;
97  }
98 }
99 
101 {
102 }
103 
105 {
106  try {
107  if (done || parser->getErrorCount())
108  return false;
109 
110  done = !parser->parseNext(token);
111  } catch(const XMLException &e) {
112  throw cms::Exception("XMLDocument")
113  << "cms::concurrency::xercesInitialize failed because of "
114  << XMLSimpleStr(e.getMessage()) << std::endl;
115  } catch(const SAXException &e) {
116  throw cms::Exception("XMLDocument")
117  << "XML parser reported: "
118  << XMLSimpleStr(e.getMessage()) << "." << std::endl;
119  }
120 
121  return !done;
122 }
123 
125 {
126 }
127 
129  reader(reader)
130 {
131 }
132 
134 {
135 }
136 
137 unsigned int CBInputStream::readBytes(XMLByte* const buf,
138  const unsigned int size)
139 {
140  char *rawBuf = reinterpret_cast<char*>(buf);
141  unsigned int bytes = size * sizeof(XMLByte);
142  unsigned int read = 0;
143 
144  while(read < bytes) {
145  if (buffer.empty()) {
146  buffer = reader.data();
147  if (buffer.empty())
148  break;
149  }
150 
151  unsigned int len = buffer.length();
152  unsigned int rem = bytes - read;
153  if (rem < len) {
154  std::memcpy(rawBuf + read, buffer.c_str(), rem);
155  buffer.erase(0, rem);
156  read += rem;
157  break;
158  }
159 
160  std::memcpy(rawBuf + read, buffer.c_str(), len);
161  buffer.clear();
162  read += len;
163  }
164 
165  read /= sizeof(XMLByte);
166  pos += read;
167 
168  return read;
169 }
170 
172  in(in)
173 {
174  if (in.bad())
175  throw cms::Exception("FileStreamError")
176  << "I/O stream bad in STLInputStream::STLInputStream()"
177  << std::endl;
178 }
179 
181 {
182 }
183 
184 unsigned int STLInputStream::readBytes(XMLByte* const buf,
185  const unsigned int size)
186 {
187  char *rawBuf = reinterpret_cast<char*>(buf);
188  unsigned int bytes = size * sizeof(XMLByte);
189  in.read(rawBuf, bytes);
190  unsigned int readBytes = in.gcount();
191 
192  if (in.bad())
193  throw cms::Exception("FileStreamError")
194  << "I/O stream bad in STLInputStream::readBytes()"
195  << std::endl;
196 
197  unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
198  unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
199  for(unsigned int i = 1; i <= rest; i++)
200  in.putback(rawBuf[readBytes - i]);
201 
202  pos += read;
203  return read;
204 }
205 
207  in(in),
208  lstr(LZMA_STREAM_INIT),
209  compression_(false),
210  lasttotal_(0)
211 {
212  // Check the kind of file.
213  char header[6];
214  /*unsigned int s = */ in->read(header, 6);
215  in->position(0, Storage::SET);
216  // Let's use lzma to start with.
217  if (header[1] == '7'
218  && header[2] == 'z'
219  && header[3] == 'X'
220  && header[4] == 'Z')
221  {
222  compression_ = true;
223  lstr = LZMA_STREAM_INIT;
224  // We store the beginning of the outBuffer to make sure
225  // we can always update previous results.
226 
227 #if LZMA_VERSION <= UINT32_C(49990030)
228  int ret = lzma_auto_decoder(&lstr, NULL, NULL);
229 #else
230  int ret = lzma_auto_decoder(&lstr, -1, 0);
231 #endif
232 
233  if (ret != LZMA_OK)
234  {
235  lzma_end(&lstr);
236  throw cms::Exception("IO") << "Error while reading compressed LHE file";
237  }
238  }
239 }
240 
242 {
243  lzma_end(&(lstr));
244 }
245 
246 unsigned int StorageInputStream::readBytes(XMLByte* const buf,
247  const unsigned int size)
248 {
249  // Simple read-in write-out in case
250  if (!compression_)
251  {
252  void *rawBuf = reinterpret_cast<void*>(buf);
253  unsigned int bytes = size * sizeof(XMLByte);
254  unsigned int readBytes = in->read(rawBuf, bytes);
255 
256  unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
257  unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
258  if (rest)
260 
261  /*for (unsigned int i = 0; i < read; ++i){
262  std::cout << buf[i] ;
263  }*/
264 
265  pos += read;
266  return read;
267  }
268  // Compressed case.
269  // We simply read as many bytes as we can and we
270  // uncompress them in the output buffer.
271  // We never decompress more bytes we were asked by
272  // xerces.
273  // In case we read from file more bytes than needed
274  // we simply rollback by the (hopefully) correct amount.
275  // If we don't read enough bytes, we simply return
276  // the amount of bytes read and we wait for being called
277  // again by xerces.
278  unsigned int bytes = size * sizeof(XMLByte);
279 // std::cout << "bites " << bytes << std::endl;
280  uint8_t inBuf[BUF_SIZE];
281  unsigned int rd = in->read((void*)inBuf, BUF_SIZE);
282  lstr.next_in = inBuf;
283  lstr.avail_in = rd;
284  lstr.next_out = buf;
285  lstr.avail_out = bytes;
286 /* for (unsigned int i = 0; i < size; ++i){
287  std::cout << buf[i] ;
288  }
289  std::cout << std::endl;*/
290 
291  int ret = lzma_code(&lstr, LZMA_RUN);
292  if(ret != LZMA_OK && ret != LZMA_STREAM_END) { /* decompression error */
293  lzma_end(&lstr);
294  throw cms::Exception("IO") << "Error while reading compressed LHE file";
295  }
296  // If we did not consume everything we put it back.
297 // std::cout << "lstr.avail_in " << lstr.avail_in << " lstr.total_in " << lstr.total_in << std::endl;
298 // std::cout << "lstr.avail_out " << lstr.avail_out << " lstr.total_out " << lstr.total_out << std::endl;
299  if (lstr.avail_in){
300 // std::cout << "rolling back" << std::endl;
301  in->position(-(IOOffset)(lstr.avail_in), Storage::CURRENT);
302  }
303  pos = lstr.total_out;
304  unsigned int read = lstr.total_out - lasttotal_;
305  lasttotal_ = lstr.total_out;
306  return read;
307 }
308 
309 } // namespace lhef
std::auto_ptr< Storage > storage
Definition: XMLUtils.h:31
int i
Definition: DBlmapReader.cc:9
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader > parser
Definition: XMLUtils.h:63
unsigned int pos
Definition: XMLUtils.h:164
void init(Handler &handler)
Definition: XMLUtils.cc:74
virtual IOSize read(void *into, IOSize n, IOOffset pos)
Definition: Storage.cc:17
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER InputSource > source
Definition: XMLUtils.h:62
StorageInputStream(StorageWrap &in)
Definition: XMLUtils.cc:206
std::string buffer
Definition: XMLUtils.h:146
void xercesTerminate()
Definition: Xerces.cc:22
virtual ~XMLDocument()
Definition: XMLUtils.cc:100
std::auto_ptr< XercesPlatform > platform
Definition: XMLUtils.h:60
virtual const std::string & data()=0
CBInputStream(Reader &in)
Definition: XMLUtils.cc:128
virtual unsigned int readBytes(XMLByte *const buf, const unsigned int size)
Definition: XMLUtils.cc:184
#define NULL
Definition: scimark2.h:8
virtual ~StorageInputStream()
Definition: XMLUtils.cc:241
XMLDocument(std::auto_ptr< std::istream > &in, Handler &handler)
Definition: XMLUtils.cc:56
virtual ~STLInputStream()
Definition: XMLUtils.cc:180
void xercesInitialize()
Definition: Xerces.cc:17
STLInputStream(std::istream &in)
Definition: XMLUtils.cc:171
unsigned int lasttotal_
Definition: XMLUtils.h:185
virtual unsigned int readBytes(XMLByte *const buf, const unsigned int size)
Definition: XMLUtils.cc:246
std::istream & in
Definition: XMLUtils.h:163
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token
Definition: XMLUtils.h:65
virtual IOOffset position(void) const
Definition: Storage.cc:95
unsigned int pos
Definition: XMLUtils.h:147
StorageWrap & in
Definition: XMLUtils.h:181
virtual unsigned int readBytes(XMLByte *const buf, const unsigned int size)
Definition: XMLUtils.cc:137
int64_t IOOffset
Definition: IOTypes.h:19
StorageWrap(Storage *storage)
Definition: XMLUtils.cc:24
#define BUF_SIZE
Definition: XMLUtils.cc:20
static unsigned int instances
Definition: XMLUtils.h:55
volatile std::atomic< bool > shutdown_flag false
virtual ~CBInputStream()
Definition: XMLUtils.cc:133
static std::string const source
Definition: EdmProvDump.cc:43
tuple size
Write out results.