9 #include <ext/stdio_filebuf.h>
11 #include <xercesc/util/PlatformUtils.hpp>
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/DOMWriter.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>
30 XERCES_CPP_NAMESPACE_USE
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::auto_ptr<Stream_t> &
obj) : obj(obj) {}
49 virtual ~XMLInputSourceWrapper() {}
53 {
return new T(*
obj); }
56 std::auto_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 unsigned int curPos()
const {
return pos; }
69 virtual unsigned int readBytes(XMLByte *
const buf,
70 const unsigned int size);
77 template<
int (*close)(FILE*)>
78 class stdio_istream :
public std::istream {
80 typedef __gnu_cxx::stdio_filebuf<char> __filebuf_type;
81 typedef stdio_istream<close> __istream_type;
83 stdio_istream(FILE *
file) :
84 file_(file), filebuf_(file, std::ios_base::
in)
85 { this->
init(&filebuf_); }
90 __filebuf_type *rdbuf()
const
91 {
return const_cast<__filebuf_type*
>(&filebuf_); }
95 __filebuf_type filebuf_;
101 unsigned int STLInputStream::readBytes(XMLByte*
const buf,
102 const unsigned int size)
104 char *rawBuf =
reinterpret_cast<char*
>(buf);
105 unsigned int bytes = size *
sizeof(XMLByte);
106 in.read(rawBuf, bytes);
107 unsigned int readBytes =
in.gcount();
111 <<
"I/O stream bad in STLInputStream::readBytes()"
114 unsigned int read = (
unsigned int)(readBytes /
sizeof(XMLByte));
115 unsigned int rest = (
unsigned int)(readBytes %
sizeof(XMLByte));
116 for(
unsigned int i = 1;
i <= rest;
i++)
117 in.putback(rawBuf[readBytes -
i]);
127 XMLPlatformUtils::Initialize();
128 }
catch(
const XMLException &
e) {
130 <<
"XMLPlatformUtils::Initialize failed "
140 XMLPlatformUtils::Terminate();
150 std::auto_ptr<std::istream> inputStream(
151 new std::ifstream(fileName.c_str()));
152 if (!inputStream->good())
154 <<
"XML input file \"" << fileName <<
"\" "
155 "could not be opened for reading."
164 write(
false), impl(0),
doc(0), rootNode(0)
166 FILE *
file = popen(command.c_str(),
"r");
169 <<
"Could not execute XML preprocessing "
170 " command \"" << command <<
"\"."
173 std::auto_ptr<std::istream> inputStream(
174 new stdio_istream<pclose>(file));
175 if (!inputStream->good())
177 <<
"XML preprocessing command \"" << fileName
178 <<
"\" stream could not be opened for reading."
189 std::auto_ptr<DocReleaser> docReleaser(
new DocReleaser(
doc));
191 std::auto_ptr<DOMWriter> writer(static_cast<DOMImplementationLS*>(
192 impl)->createDOMWriter());
193 assert(writer.get());
196 if (writer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent,
true))
197 writer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent,
true);
198 if (writer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true))
199 writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
202 std::auto_ptr<XMLFormatTarget>
target(
203 new LocalFileFormatTarget(
fileName.c_str()));
205 writer->writeNode(target.get(), *
doc);
214 parser.reset(
new XercesDOMParser());
215 parser->setValidationScheme(XercesDOMParser::Val_Auto);
216 parser->setDoNamespaces(
false);
217 parser->setDoSchema(
false);
218 parser->setValidationSchemaFullChecking(
false);
222 parser->setCreateEntityReferenceNodes(
false);
228 if (
parser->getErrorCount())
230 <<
"XML parser reported errors."
232 }
catch(
const XMLException &
e) {
234 <<
"XML parser reported DOM error no. "
235 << (
unsigned long)e.getCode()
238 }
catch(
const SAXException &e) {
240 <<
"XML parser reported: "
247 DOMNode *
node =
doc->getFirstChild();
248 while(node && node->getNodeType() != DOMNode::ELEMENT_NODE)
249 node = node->getNextSibling();
253 <<
"XML document didn't contain a valid "
254 <<
"root node." << std::endl;
261 impl = DOMImplementationRegistry::getDOMImplementation(
270 <<
"Document already exists in createDocument."
283 for(
unsigned int i = 0;
i < value.size();
i++)
284 if (value[
i] >=
'A' && value[
i] <=
'Z')
285 value[
i] +=
'a' -
'A';
287 if (value ==
"1" || value ==
"y" || value ==
"yes" ||
288 value ==
"true" || value ==
"ok")
291 if (value ==
"0" || value ==
"n" || value ==
"no" || value ==
"false")
295 <<
"Invalid boolean value in XML document" << std::endl;
300 return value ?
"true" :
"false";
307 return elem->hasAttribute(uniName);
316 if (!elem->hasAttribute(uniName))
318 <<
"Missing attribute " << name <<
" in tag "
321 const XMLCh *attribute = elem->getAttribute(uniName);
328 const char *
name,
const bool &defValue)
331 if (!elem->hasAttribute(uniName))
333 const XMLCh *attribute = elem->getAttribute(uniName);
349 const XMLCh *content = node->getTextContent();
static const char * makeBool(bool value)
XMLInputSourceWrapper< STLInputStream > STLInputSource
static T readContent(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node)
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER InputSource > inputSource
XERCES_CPP_NAMESPACE_QUALIFIER DOMImplementation * impl
std::auto_ptr< XercesPlatform > platform
#define XERCES_CPP_NAMESPACE_QUALIFIER
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER HandlerBase > errHandler
tuple obj
Example code starts here #.
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 openForWrite(const std::string &fileName)
static void writeAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, const char *name, const T &value)
std::auto_ptr< XERCES_CPP_NAMESPACE_QUALIFIER XercesDOMParser > parser
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * createDocument(const std::string &root)
void openForRead(std::auto_ptr< std::istream > &inputStream)
tuple size
Write out results.
string root
initialization
static bool isBool(std::string value)