36 #include <xercesc/util/XercesVersion.hpp>
38 #if XERCES_VERSION_MAJOR >= 3
39 #include <xercesc/dom/DOMLSParser.hpp>
40 #include <xercesc/dom/DOMLSSerializer.hpp>
41 #include <xercesc/dom/DOMDocument.hpp>
42 #include <xercesc/dom/DOMLSOutput.hpp>
44 #include <xercesc/dom/DOMWriter.hpp>
45 #include <xercesc/dom/DOMBuilder.hpp>
48 #include <xercesc/dom/DOMImplementation.hpp>
49 #include <xercesc/dom/DOMElement.hpp>
50 #include <xercesc/dom/DOMText.hpp>
51 #include <xercesc/dom/DOMNamedNodeMap.hpp>
52 #include <xercesc/dom/DOMException.hpp>
53 #include <xercesc/framework/LocalFileFormatTarget.hpp>
54 #include <xercesc/util/PlatformUtils.hpp>
55 #include <xercesc/validators/common/Grammar.hpp>
56 #include <xercesc/sax/SAXException.hpp>
57 #include <xercesc/dom/DOMException.hpp>
58 #include <xercesc/util/XMLException.hpp>
59 #include <xercesc/util/PlatformUtils.hpp>
60 #include <xercesc/dom/DOMImplementationRegistry.hpp>
61 #include <xercesc/framework/MemBufInputSource.hpp>
62 #include <xercesc/framework/Wrapper4InputSource.hpp>
63 #include <xercesc/framework/MemBufFormatTarget.hpp>
79 Serializer(), sourceFile_(
""), destinationFile_(
""), schemaFile_(
""),
80 useSchema_(
false), parser_(NULL), domImplementation_(NULL),
81 sourceString_(NULL), destinationString_(NULL), nsUri_(
"") {
83 XMLPlatformUtils::Initialize();
85 const XMLCh gLS[] = {chLatin_L, chLatin_S, chNull};
87 DOMImplementationRegistry::getDOMImplementation(gLS);
89 #if XERCES_VERSION_MAJOR >= 3
91 DOMImplementationLS::MODE_SYNCHRONOUS, 0);
94 DOMImplementationLS::MODE_SYNCHRONOUS, 0);
103 XMLPlatformUtils::Terminate();
206 string errorMsg =
"No Source file or string set.";
225 string errorMsg =
"No destination file or string set.";
237 string errorMsg =
"No schema file set.";
262 #if XERCES_VERSION_MAJOR >= 3
263 parser_->getDomConfig()->setParameter(XMLUni::fgXercesSchema,
true);
264 parser_->getDomConfig()->setParameter(XMLUni::fgDOMValidate,
true);
265 parser_->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces,
true);
266 parser_->getDomConfig()->setParameter(
267 XMLUni::fgXercesSchemaFullChecking,
true);
268 parser_->getDomConfig()->setParameter(
269 XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
272 parser_->setFeature(XMLUni::fgXercesSchema,
true);
273 parser_->setFeature(XMLUni::fgDOMValidation,
true);
274 parser_->setFeature(XMLUni::fgDOMNamespaces,
true);
275 parser_->setFeature(XMLUni::fgXercesSchemaFullChecking,
true);
277 XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
281 XMLString::release(&filePath);
283 #if XERCES_VERSION_MAJOR >= 3
284 parser_->getDomConfig()->setParameter(
285 XMLUni::fgDOMElementContentWhitespace,
false);
286 parser_->getDomConfig()->setParameter(XMLUni::fgDOMComments,
false);
288 parser_->setFeature(XMLUni::fgDOMWhitespaceInElementContent,
false);
289 parser_->setFeature(XMLUni::fgDOMComments,
false);
303 DOMDocument* dom = NULL;
305 #if XERCES_VERSION_MAJOR >= 3
306 parser_->getDomConfig()->setParameter(
307 XMLUni::fgDOMErrorHandler, errHandler);
309 parser_->setErrorHandler(errHandler);
316 MemBufInputSource* buf =
new MemBufInputSource(
317 (
const XMLByte*)source.c_str(),
322 Wrapper4InputSource* domBuf =
new Wrapper4InputSource(buf);
323 #if XERCES_VERSION_MAJOR >= 3
330 string errorLog = errHandler->
errorLog();
340 string errorLog = errHandler->
errorLog();
345 if (dom == NULL || dom->getDocumentElement() == NULL) {
348 __FILE__, __LINE__,
__func__,
"Illegal file: " + source);
370 DOMDocument* dom = NULL;
373 string errorMsg =
"No source file set.";
386 #if XERCES_VERSION_MAJOR >= 3
387 parser_->getDomConfig()->setParameter(
388 XMLUni::fgDOMErrorHandler, errHandler);
390 parser_->setErrorHandler(errHandler);
398 string errorLog = errHandler->
errorLog();
404 if (dom == NULL || dom->getDocumentElement() == NULL) {
417 string errorLog = errHandler->
errorLog();
436 const std::string& destinationFile,
const ObjectState* rootState) {
437 #if XERCES_VERSION_MAJOR >= 3
439 domWriter->getDomConfig()->setParameter(
440 XMLUni::fgDOMWRTFormatPrettyPrint,
true);
444 domWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
453 #if XERCES_VERSION_MAJOR >= 3
454 LocalFileFormatTarget targetFile(destinationFile.c_str());
455 lsOutput->setByteStream(&targetFile);
456 domWriter->write(document, lsOutput);
457 domWriter->release();
460 LocalFileFormatTarget targetFile(destinationFile.c_str());
461 domWriter->writeNode(&targetFile, *document);
462 domWriter->release();
466 #if XERCES_VERSION_MAJOR >= 3
467 domWriter->release();
472 string errorMessage =
"Cannot write to " + destinationFile;
486 #if XERCES_VERSION_MAJOR >= 3
488 domWriter->getDomConfig()->setParameter(
489 XMLUni::fgDOMWRTFormatPrettyPrint,
true);
493 domWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
499 #if XERCES_VERSION_MAJOR >= 3
500 MemBufFormatTarget* buf =
new MemBufFormatTarget();
501 lsOutput->setByteStream(buf);
502 domWriter->write(document, lsOutput);
504 MemBufFormatTarget* buf =
new MemBufFormatTarget();
505 domWriter->writeNode(buf, *document);
507 target.append((
char*)(buf->getRawBuffer()));
508 domWriter->release();
512 domWriter->release();
514 string errorMessage =
"Error writing xml to a string.";
545 XMLString::release(&docName);
547 XMLString::release(&nsUri);
550 DOMElement* rootElem = doc->getDocumentElement();
556 rootElem->setAttribute(attribName, attribValue);
557 XMLString::release(&attribName);
558 XMLString::release(&attribValue);
564 DOMText* valueNode = doc->createTextNode(nodeValue);
565 rootElem->appendChild(valueNode);
566 XMLString::release(&nodeValue);
569 for (
int i = 0; i < state->
childCount(); i++) {
571 DOMElement* childElement =
createDOM(childState, doc);
572 rootElem->appendChild(childElement);
589 filestream.open(fileName.c_str());
590 if(!filestream.is_open() || !filestream.good()) {
592 string procName =
"XMLSerializer::ensureValidStream";
593 string errorMessage =
"Cannot open file \'";
594 errorMessage += fileName;
595 errorMessage +=
"\'.";
611 DOMDocument* doc)
const {
614 DOMElement* rootElem =
615 doc->createElement(elemName);
616 XMLString::release(&elemName);
622 rootElem->setAttribute(attribName, attribValue);
623 XMLString::release(&attribName);
624 XMLString::release(&attribValue);
630 DOMText* value = doc->createTextNode(text);
631 rootElem->appendChild(value);
632 XMLString::release(&text);
636 for (
int i = 0; i < state->
childCount(); i++) {
638 rootElem->appendChild(
createDOM(childState, doc));
656 DOMNamedNodeMap* attributes = node->getAttributes();
659 if (attributes != NULL) {
660 for (
unsigned int i = 0; i < attributes->getLength(); i++) {
661 DOMNode* attribute = attributes->item(i);
668 DOMNode* child = node->getFirstChild();
669 while (child != NULL) {
670 if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
673 child = child->getNextSibling();
678 if (node->getFirstChild() != NULL) {
696 DOMNode* child = node->getFirstChild();
697 while (child != NULL) {
698 if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
701 child = child->getNextSibling();