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());
218 parser.reset(
new XercesDOMParser());
219 parser->setValidationScheme(XercesDOMParser::Val_Auto);
220 parser->setDoNamespaces(
false);
221 parser->setDoSchema(
false);
222 parser->setValidationSchemaFullChecking(
false);
225 parser->setCreateEntityReferenceNodes(
false);
231 if (
parser->getErrorCount())
233 <<
"XML parser reported errors." 235 }
catch(
const XMLException &
e) {
237 <<
"XML parser reported DOM error no. " 238 << (
unsigned long)e.getCode()
241 }
catch(
const SAXException &e) {
243 <<
"XML parser reported: " 250 DOMNode *node = doc->getFirstChild();
251 while(node && node->getNodeType() != DOMNode::ELEMENT_NODE)
252 node = node->getNextSibling();
256 <<
"XML document didn't contain a valid " 257 <<
"root node." << std::endl;
259 rootNode =
static_cast<DOMElement*
>(node);
264 impl = DOMImplementationRegistry::getDOMImplementation(
273 <<
"Document already exists in createDocument." 286 for(
unsigned int i = 0;
i < value.size();
i++)
287 if (value[
i] >=
'A' && value[
i] <=
'Z')
288 value[
i] +=
'a' -
'A';
290 if (value ==
"1" || value ==
"y" || value ==
"yes" ||
291 value ==
"true" || value ==
"ok")
294 if (value ==
"0" || value ==
"n" || value ==
"no" || value ==
"false")
298 <<
"Invalid boolean value in XML document" << std::endl;
303 return value ?
"true" :
"false";
310 return elem->hasAttribute(uniName);
319 if (!elem->hasAttribute(uniName))
321 <<
"Missing attribute " << name <<
" in tag " 324 const XMLCh *attribute = elem->getAttribute(uniName);
331 const char *
name,
const bool &defValue)
334 if (!elem->hasAttribute(uniName))
336 const XMLCh *attribute = elem->getAttribute(uniName);
352 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)