9 #include <ext/stdio_filebuf.h> 12 #include <xercesc/util/XMLString.hpp> 13 #include <xercesc/util/XMLUni.hpp> 14 #include <xercesc/util/BinInputStream.hpp> 15 #include <xercesc/dom/DOM.hpp> 16 #include <xercesc/dom/DOMImplementationLS.hpp> 17 #include <xercesc/dom/DOMLSSerializer.hpp> 18 #include <xercesc/framework/LocalFileFormatTarget.hpp> 19 #include <xercesc/parsers/XercesDOMParser.hpp> 20 #include <xercesc/sax/InputSource.hpp> 21 #include <xercesc/sax/HandlerBase.hpp> 36 inline DocReleaser(DOMDocument *
doc) : doc(doc) {}
37 inline ~DocReleaser() {
doc->release(); }
43 class XMLInputSourceWrapper :
46 typedef typename T::Stream_t Stream_t;
48 XMLInputSourceWrapper(std::unique_ptr<Stream_t>&
obj) : obj(obj) {}
49 virtual ~XMLInputSourceWrapper() {}
52 makeStream()
const override 53 {
return new T(*
obj); }
56 std::unique_ptr<Stream_t>&
obj;
59 class STLInputStream :
62 typedef std::istream Stream_t;
64 STLInputStream(std::istream &
in) : in(in) {}
65 virtual ~STLInputStream() {}
67 virtual XMLFilePos curPos()
const override {
return pos; }
69 virtual XMLSize_t readBytes(XMLByte *
const buf,
70 const XMLSize_t
size)
override;
72 virtual const XMLCh* getContentType()
const override {
return 0; }
79 template<
int (*close)(FILE*)>
80 class stdio_istream :
public std::istream {
82 typedef __gnu_cxx::stdio_filebuf<char> __filebuf_type;
83 typedef stdio_istream<close> __istream_type;
85 stdio_istream(FILE *
file) :
86 file_(file), filebuf_(file,
std::ios_base::
in)
87 { this->
init(&filebuf_); }
92 __filebuf_type *rdbuf()
const 93 {
return const_cast<__filebuf_type*
>(&filebuf_); }
97 __filebuf_type filebuf_;
103 XMLSize_t STLInputStream::readBytes(XMLByte*
const buf,
104 const XMLSize_t
size)
106 char *rawBuf =
reinterpret_cast<char*
>(buf);
107 unsigned int bytes = size *
sizeof(XMLByte);
108 in.read(rawBuf, bytes);
109 unsigned int readBytes =
in.gcount();
113 <<
"I/O stream bad in STLInputStream::readBytes()" 116 unsigned int read = (
unsigned int)(readBytes /
sizeof(XMLByte));
117 unsigned int rest = (
unsigned int)(readBytes %
sizeof(XMLByte));
118 for(
unsigned int i = 1;
i <= rest;
i++)
119 in.putback(rawBuf[readBytes -
i]);
130 }
catch(
const XMLException &
e) {
132 <<
"cms::concurrency::xercesInitialize failed " 147 write(write),
impl(0),
doc(0), rootNode(0)
152 std::unique_ptr<std::istream> inputStream(
153 new std::ifstream(fileName.c_str()));
154 if (!inputStream->good())
156 <<
"XML input file \"" << fileName <<
"\" " 157 "could not be opened for reading." 168 FILE *
file = popen(command.c_str(),
"r");
171 <<
"Could not execute XML preprocessing " 172 " command \"" << command <<
"\"." 175 std::unique_ptr<std::istream> inputStream(
176 new stdio_istream<pclose>(file));
177 if (!inputStream->good())
179 <<
"XML preprocessing command \"" << fileName
180 <<
"\" stream could not be opened for reading." 191 std::unique_ptr<DocReleaser> docReleaser(
new DocReleaser(
doc));
193 std::unique_ptr<DOMLSSerializer>
writer(((DOMImplementationLS*)
impl)->createLSSerializer());
194 assert(writer.get());
196 if( writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent,
true))
197 writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent,
true);
198 if( writer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint,
true))
199 writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
201 std::unique_ptr<DOMLSOutput> outputDesc(((DOMImplementationLS*)
impl)->createLSOutput());
202 assert(outputDesc.get());
203 outputDesc->setEncoding(
XMLUniStr(
"UTF-8"));
206 std::unique_ptr<XMLFormatTarget>
target(
207 new LocalFileFormatTarget(
fileName.c_str()));
208 outputDesc->setByteStream(target.get());
209 writer->write(
doc, outputDesc.get());
217 parser.reset(
new XercesDOMParser());
218 parser->setValidationScheme(XercesDOMParser::Val_Auto);
219 parser->setDoNamespaces(
false);
220 parser->setDoSchema(
false);
221 parser->setValidationSchemaFullChecking(
false);
224 parser->setCreateEntityReferenceNodes(
false);
230 if (
parser->getErrorCount())
232 <<
"XML parser reported errors." 234 }
catch(
const XMLException &
e) {
236 <<
"XML parser reported DOM error no. " 237 << (
unsigned long)e.getCode()
240 }
catch(
const SAXException &e) {
242 <<
"XML parser reported: " 249 DOMNode *node = doc->getFirstChild();
250 while(node && node->getNodeType() != DOMNode::ELEMENT_NODE)
251 node = node->getNextSibling();
255 <<
"XML document didn't contain a valid " 256 <<
"root node." << std::endl;
258 rootNode =
static_cast<DOMElement*
>(node);
263 impl = DOMImplementationRegistry::getDOMImplementation(
272 <<
"Document already exists in createDocument." 285 for(
unsigned int i = 0;
i < value.size();
i++)
286 if (value[
i] >=
'A' && value[
i] <=
'Z')
287 value[
i] +=
'a' -
'A';
289 if (value ==
"1" || value ==
"y" || value ==
"yes" ||
290 value ==
"true" || value ==
"ok")
293 if (value ==
"0" || value ==
"n" || value ==
"no" || value ==
"false")
297 <<
"Invalid boolean value in XML document" << std::endl;
302 return value ?
"true" :
"false";
309 return elem->hasAttribute(uniName);
318 if (!elem->hasAttribute(uniName))
320 <<
"Missing attribute " << name <<
" in tag " 323 const XMLCh *attribute = elem->getAttribute(uniName);
330 const char *
name,
const bool &defValue)
333 if (!elem->hasAttribute(uniName))
335 const XMLCh *attribute = elem->getAttribute(uniName);
351 const XMLCh *
content = node->getTextContent();
static const char * makeBool(bool value)
XMLInputSourceWrapper< STLInputStream > STLInputSource
static T readContent(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node)
std::unique_ptr< XercesPlatform > platform
#define XERCES_CPP_NAMESPACE_QUALIFIER
std::unique_ptr< XERCES_CPP_NAMESPACE_QUALIFIER InputSource > inputSource
static bool hasAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, const char *name)
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement * rootNode
static T readAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, const char *name)
static void writeContent(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc, const T &value)
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * doc
void openForRead(std::unique_ptr< std::istream > &inputStream)
void openForWrite(const std::string &fileName)
static void writeAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, const char *name, const T &value)
def elem(elemtype, innerHTML='', html_class='', kwargs)
std::unique_ptr< XERCES_CPP_NAMESPACE_QUALIFIER HandlerBase > errHandler
std::unique_ptr< XERCES_CPP_NAMESPACE_QUALIFIER XercesDOMParser > parser
def remove(d, key, TELL=False)
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * createDocument(const std::string &root)
static bool isBool(std::string value)