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 
6 #include <xercesc/util/PlatformUtils.hpp>
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 
21 namespace lhef {
22 
24  storage(storage)
25 {
26 }
27 
29 {
30  storage->close();
31 }
32 
34 
36 {
37  if (!instances++) {
38  try {
39  XMLPlatformUtils::Initialize();
40  } catch(const XMLException &e) {
41  throw cms::Exception("XMLDocument")
42  << "XMLPlatformUtils::Initialize failed "
43  "because of: "
44  << XMLSimpleStr(e.getMessage()) << std::endl;
45  }
46  }
47 }
48 
50 {
51  if (!--instances)
52  XMLPlatformUtils::Terminate();
53 }
54 
55 XMLDocument::XMLDocument(std::auto_ptr<std::istream> &in, Handler &handler) :
56  platform(new XercesPlatform()),
57  source(new STLInputSource(in)),
58  parser(XMLReaderFactory::createXMLReader()),
59  done(false)
60 {
61  init(handler);
62 }
63 
64 XMLDocument::XMLDocument(std::auto_ptr<StorageWrap> &in, Handler &handler) :
65  platform(new XercesPlatform()),
66  source(new StorageInputSource(in)),
67  parser(XMLReaderFactory::createXMLReader()),
68  done(false)
69 {
70  init(handler);
71 }
72 
73 void XMLDocument::init(Handler &handler)
74 {
75  try {
76  parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
77  parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
78  parser->setFeature(XMLUni::fgXercesSchema, false);
79  parser->setFeature(XMLUni::fgXercesSchemaFullChecking, false);
80 
81  parser->setContentHandler(&handler);
82  parser->setLexicalHandler(&handler);
83  parser->setErrorHandler(&handler);
84 
85  if (!parser->parseFirst(*source, token))
86  throw cms::Exception("XMLParseError")
87  << "SAXParser::parseFirst failed" << std::endl;
88  } catch(const XMLException &e) {
89  throw cms::Exception("XMLDocument")
90  << "XMLPlatformUtils::Initialize failed because of "
91  << XMLSimpleStr(e.getMessage()) << std::endl;
92  } catch(const SAXException &e) {
93  throw cms::Exception("XMLDocument")
94  << "XML parser reported: "
95  << XMLSimpleStr(e.getMessage()) << "." << std::endl;
96  }
97 }
98 
100 {
101 }
102 
104 {
105  try {
106  if (done || parser->getErrorCount())
107  return false;
108 
109  done = !parser->parseNext(token);
110  } catch(const XMLException &e) {
111  throw cms::Exception("XMLDocument")
112  << "XMLPlatformUtils::Initialize failed because of "
113  << XMLSimpleStr(e.getMessage()) << std::endl;
114  } catch(const SAXException &e) {
115  throw cms::Exception("XMLDocument")
116  << "XML parser reported: "
117  << XMLSimpleStr(e.getMessage()) << "." << std::endl;
118  }
119 
120  return !done;
121 }
122 
124 {
125 }
126 
128  reader(reader)
129 {
130 }
131 
133 {
134 }
135 
136 unsigned int CBInputStream::readBytes(XMLByte* const buf,
137  const unsigned int size)
138 {
139  char *rawBuf = reinterpret_cast<char*>(buf);
140  unsigned int bytes = size * sizeof(XMLByte);
141  unsigned int read = 0;
142 
143  while(read < bytes) {
144  if (buffer.empty()) {
145  buffer = reader.data();
146  if (buffer.empty())
147  break;
148  }
149 
150  unsigned int len = buffer.length();
151  unsigned int rem = bytes - read;
152  if (rem < len) {
153  std::memcpy(rawBuf + read, buffer.c_str(), rem);
154  buffer.erase(0, rem);
155  read += rem;
156  break;
157  }
158 
159  std::memcpy(rawBuf + read, buffer.c_str(), len);
160  buffer.clear();
161  read += len;
162  }
163 
164  read /= sizeof(XMLByte);
165  pos += read;
166 
167  return read;
168 }
169 
171  in(in)
172 {
173  if (in.bad())
174  throw cms::Exception("FileStreamError")
175  << "I/O stream bad in STLInputStream::STLInputStream()"
176  << std::endl;
177 }
178 
180 {
181 }
182 
183 unsigned int STLInputStream::readBytes(XMLByte* const buf,
184  const unsigned int size)
185 {
186  char *rawBuf = reinterpret_cast<char*>(buf);
187  unsigned int bytes = size * sizeof(XMLByte);
188  in.read(rawBuf, bytes);
189  unsigned int readBytes = in.gcount();
190 
191  if (in.bad())
192  throw cms::Exception("FileStreamError")
193  << "I/O stream bad in STLInputStream::readBytes()"
194  << std::endl;
195 
196  unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
197  unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
198  for(unsigned int i = 1; i <= rest; i++)
199  in.putback(rawBuf[readBytes - i]);
200 
201  pos += read;
202  return read;
203 }
204 
206  in(in)
207 {
208 }
209 
211 {
212 }
213 
214 unsigned int StorageInputStream::readBytes(XMLByte* const buf,
215  const unsigned int size)
216 {
217  void *rawBuf = reinterpret_cast<void*>(buf);
218  unsigned int bytes = size * sizeof(XMLByte);
219  unsigned int readBytes = in->read(rawBuf, bytes);
220 
221  unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
222  unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
223  if (rest)
225 
226  pos += read;
227  return read;
228 }
229 
230 } // namespace lhef
std::auto_ptr< Storage > storage
Definition: XMLUtils.h:30
int i
Definition: DBlmapReader.cc:9
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader > parser
Definition: XMLUtils.h:62
unsigned int pos
Definition: XMLUtils.h:163
void init(Handler &handler)
Definition: XMLUtils.cc:73
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:61
StorageInputStream(StorageWrap &in)
Definition: XMLUtils.cc:205
std::string buffer
Definition: XMLUtils.h:145
virtual ~XMLDocument()
Definition: XMLUtils.cc:99
std::auto_ptr< XercesPlatform > platform
Definition: XMLUtils.h:59
virtual const std::string & data()=0
CBInputStream(Reader &in)
Definition: XMLUtils.cc:127
virtual unsigned int readBytes(XMLByte *const buf, const unsigned int size)
Definition: XMLUtils.cc:183
Definition: Storage.h:8
virtual ~StorageInputStream()
Definition: XMLUtils.cc:210
XMLDocument(std::auto_ptr< std::istream > &in, Handler &handler)
Definition: XMLUtils.cc:55
virtual ~STLInputStream()
Definition: XMLUtils.cc:179
STLInputStream(std::istream &in)
Definition: XMLUtils.cc:170
virtual unsigned int readBytes(XMLByte *const buf, const unsigned int size)
Definition: XMLUtils.cc:214
std::istream & in
Definition: XMLUtils.h:162
XERCES_CPP_NAMESPACE_QUALIFIER XMLPScanToken token
Definition: XMLUtils.h:64
virtual IOOffset position(void) const
Definition: Storage.cc:95
unsigned int pos
Definition: XMLUtils.h:146
StorageWrap & in
Definition: XMLUtils.h:180
virtual unsigned int readBytes(XMLByte *const buf, const unsigned int size)
Definition: XMLUtils.cc:136
int64_t IOOffset
Definition: IOTypes.h:19
StorageWrap(Storage *storage)
Definition: XMLUtils.cc:23
static unsigned int instances
Definition: XMLUtils.h:54
virtual ~CBInputStream()
Definition: XMLUtils.cc:132
tuple size
Write out results.