CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/GeneratorInterface/LHEInterface/src/XMLUtils.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <memory>
00003 #include <string>
00004 #include <cstring>
00005 
00006 #include <xercesc/util/PlatformUtils.hpp>
00007 #include <xercesc/util/XMLString.hpp>
00008 #include <xercesc/util/XMLUni.hpp>
00009 #include <xercesc/sax2/SAX2XMLReader.hpp>
00010 #include <xercesc/sax2/XMLReaderFactory.hpp>
00011 
00012 #include "FWCore/Utilities/interface/Exception.h"
00013 
00014 #include "Utilities/StorageFactory/interface/IOTypes.h"
00015 #include "Utilities/StorageFactory/interface/Storage.h"
00016 
00017 #include "XMLUtils.h"
00018 
00019 XERCES_CPP_NAMESPACE_USE
00020 
00021 namespace lhef {
00022 
00023 StorageWrap::StorageWrap(Storage *storage) :
00024         storage(storage)
00025 {
00026 }
00027 
00028 StorageWrap::~StorageWrap()
00029 {
00030         storage->close();
00031 }
00032 
00033 unsigned int XMLDocument::XercesPlatform::instances = 0;
00034 
00035 XMLDocument::XercesPlatform::XercesPlatform()
00036 {
00037         if (!instances++) {
00038                 try {
00039                         XMLPlatformUtils::Initialize();
00040                 } catch(const XMLException &e) {
00041                         throw cms::Exception("XMLDocument")
00042                                 << "XMLPlatformUtils::Initialize failed "
00043                                    "because of: "
00044                                 << XMLSimpleStr(e.getMessage()) << std::endl;
00045                 }
00046         }
00047 }
00048 
00049 XMLDocument::XercesPlatform::~XercesPlatform()
00050 {
00051         if (!--instances)
00052                 XMLPlatformUtils::Terminate();
00053 }
00054 
00055 XMLDocument::XMLDocument(std::auto_ptr<std::istream> &in, Handler &handler) :
00056         platform(new XercesPlatform()),
00057         source(new STLInputSource(in)),
00058         parser(XMLReaderFactory::createXMLReader()),
00059         done(false)
00060 {
00061         init(handler);
00062 }
00063 
00064 XMLDocument::XMLDocument(std::auto_ptr<StorageWrap> &in, Handler &handler) :
00065         platform(new XercesPlatform()),
00066         source(new StorageInputSource(in)),
00067         parser(XMLReaderFactory::createXMLReader()),
00068         done(false)
00069 {
00070         init(handler);
00071 }
00072 
00073 void XMLDocument::init(Handler &handler)
00074 {
00075         try {
00076                 parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
00077                 parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, false);
00078                 parser->setFeature(XMLUni::fgXercesSchema, false);
00079                 parser->setFeature(XMLUni::fgXercesSchemaFullChecking, false);
00080 
00081                 parser->setContentHandler(&handler);
00082                 parser->setLexicalHandler(&handler);
00083                 parser->setErrorHandler(&handler);
00084 
00085                 if (!parser->parseFirst(*source, token))
00086                         throw cms::Exception("XMLParseError")
00087                                 << "SAXParser::parseFirst failed" << std::endl;
00088         } catch(const XMLException &e) {
00089                 throw cms::Exception("XMLDocument")
00090                         << "XMLPlatformUtils::Initialize failed because of "
00091                         << XMLSimpleStr(e.getMessage()) << std::endl;
00092         } catch(const SAXException &e) {
00093                 throw cms::Exception("XMLDocument")
00094                         << "XML parser reported: "
00095                         << XMLSimpleStr(e.getMessage()) << "." << std::endl;  
00096         }
00097 }
00098 
00099 XMLDocument::~XMLDocument()
00100 {
00101 }
00102 
00103 bool XMLDocument::parse()
00104 {
00105         try {
00106                 if (done || parser->getErrorCount())
00107                         return false;
00108 
00109                 done = !parser->parseNext(token);
00110         } catch(const XMLException &e) {
00111                 throw cms::Exception("XMLDocument")
00112                         << "XMLPlatformUtils::Initialize failed because of "
00113                         << XMLSimpleStr(e.getMessage()) << std::endl;
00114         } catch(const SAXException &e) {
00115                 throw cms::Exception("XMLDocument")
00116                         << "XML parser reported: "
00117                         << XMLSimpleStr(e.getMessage()) << "." << std::endl;  
00118         }
00119 
00120         return !done;
00121 }
00122 
00123 CBInputStream::Reader::~Reader()
00124 {
00125 }
00126 
00127 CBInputStream::CBInputStream(Reader &reader) :
00128         reader(reader)
00129 {
00130 }
00131 
00132 CBInputStream::~CBInputStream()
00133 {
00134 }
00135 
00136 unsigned int CBInputStream::readBytes(XMLByte* const buf,
00137                                       const unsigned int size)
00138 {
00139         char *rawBuf = reinterpret_cast<char*>(buf);
00140         unsigned int bytes = size * sizeof(XMLByte);
00141         unsigned int read = 0;
00142 
00143         while(read < bytes) {
00144                 if (buffer.empty()) {
00145                         buffer = reader.data();
00146                         if (buffer.empty())
00147                                 break;
00148                 }
00149 
00150                 unsigned int len = buffer.length();
00151                 unsigned int rem = bytes - read;
00152                 if (rem < len) {
00153                         std::memcpy(rawBuf + read, buffer.c_str(), rem);
00154                         buffer.erase(0, rem);
00155                         read += rem;
00156                         break;
00157                 }
00158 
00159                 std::memcpy(rawBuf + read, buffer.c_str(), len);
00160                 buffer.clear();
00161                 read += len;
00162         }
00163 
00164         read /= sizeof(XMLByte);
00165         pos += read;
00166 
00167         return read;
00168 }
00169 
00170 STLInputStream::STLInputStream(std::istream &in) :
00171         in(in)
00172 {
00173         if (in.bad())
00174                 throw cms::Exception("FileStreamError")
00175                         << "I/O stream bad in STLInputStream::STLInputStream()"
00176                         << std::endl;
00177 }
00178 
00179 STLInputStream::~STLInputStream()
00180 {
00181 }
00182 
00183 unsigned int STLInputStream::readBytes(XMLByte* const buf,
00184                                        const unsigned int size)
00185 {
00186         char *rawBuf = reinterpret_cast<char*>(buf);
00187         unsigned int bytes = size * sizeof(XMLByte);
00188         in.read(rawBuf, bytes);
00189         unsigned int readBytes = in.gcount();
00190 
00191         if (in.bad())
00192                 throw cms::Exception("FileStreamError")
00193                         << "I/O stream bad in STLInputStream::readBytes()"
00194                         << std::endl;
00195 
00196         unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
00197         unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
00198         for(unsigned int i = 1; i <= rest; i++)
00199                 in.putback(rawBuf[readBytes - i]);
00200 
00201         pos += read;
00202         return read;
00203 }
00204 
00205 StorageInputStream::StorageInputStream(StorageWrap &in) :
00206         in(in)
00207 {
00208 }
00209 
00210 StorageInputStream::~StorageInputStream()
00211 {
00212 }
00213 
00214 unsigned int StorageInputStream::readBytes(XMLByte* const buf,
00215                                            const unsigned int size)
00216 {
00217         void *rawBuf = reinterpret_cast<void*>(buf);
00218         unsigned int bytes = size * sizeof(XMLByte);
00219         unsigned int readBytes = in->read(rawBuf, bytes);
00220 
00221         unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
00222         unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
00223         if (rest)
00224                 in->position(-(IOOffset)rest, Storage::CURRENT);
00225 
00226         pos += read;
00227         return read;
00228 }
00229 
00230 } // namespace lhef