From 80999d32f97398c880d01669fb2ebe21bf2cf27a Mon Sep 17 00:00:00 2001 From: "Zane U. Ji" Date: Sat, 2 Aug 2014 13:35:11 +0800 Subject: [PATCH] Improved compatibility with old libraries (Reported by Kevin Moore) --- src/wrapxerces.cpp | 116 +++++++++++++++++++++++++++++++++++++ src/wrapxerces.h | 7 +++ src/xmlcopyeditor.cpp | 2 +- src/xmlschemagenerator.cpp | 15 +++-- 4 files changed, 133 insertions(+), 7 deletions(-) diff --git a/src/wrapxerces.cpp b/src/wrapxerces.cpp index 7edd9b0..bc6aa7d 100644 --- a/src/wrapxerces.cpp +++ b/src/wrapxerces.cpp @@ -184,8 +184,12 @@ const wxMBConv &WrapXerces::getMBConv() return conv; } default: +#ifdef BOOST_STATIC_ASSERT_MSG BOOST_STATIC_ASSERT_MSG ( sizeof ( XMLCh ) == 2 , "Xerces-C doesn't use UTF-16 strings any more"); +#else + BOOST_STATIC_ASSERT ( sizeof ( XMLCh ) == 2 ); +#endif break; } return wxConvUTF8; @@ -282,3 +286,115 @@ InputSource *WrapXerces::resolveEntity ( , ( const XMLCh * ) WrapXerces::toString ( publicId ).GetData() ); } + +DOMElement *WrapXerces::getFirstElementChild ( const DOMElement &element ) +{ +#if _XERCES_VERSION >= 30100 + return element.getFirstElementChild(); +#else + // Copied from Xerces-C + DOMNode* n = element.getFirstChild(); + while ( n ) + { + switch ( n->getNodeType() ) + { + case DOMNode::ELEMENT_NODE: + return ( DOMElement * ) n; + + case DOMNode::ENTITY_REFERENCE_NODE: + { + DOMElement* e = getFirstElementChild ( n ); + if ( e ) + return e; + break; + } + + default: + break; + } + n = n->getNextSibling(); + } + return NULL; +#endif +} + +DOMElement *WrapXerces::getFirstElementChild ( const DOMNode *n ) +{ + // Copied from Xerces-C + const DOMNode *top = n; + while ( n ) + { + if ( n->getNodeType() == DOMNode::ELEMENT_NODE ) + return ( DOMElement * ) n; + + DOMNode *next = n->getFirstChild(); + while ( !next ) + { + if (top == n) + break; + + next = n->getNextSibling(); + if ( !next ) + { + n = n->getParentNode(); + if ( top == n || !n ) + return NULL; + } + } + n = next; + } + return NULL; +} + +DOMElement *WrapXerces::getNextElementSibling ( + const DOMElement &element ) +{ +#if _XERCES_VERSION >= 30100 + return element.getNextElementSibling(); +#else + // Copied from Xerces-C + DOMNode *n = getNextLogicalSibling ( &element ); + while ( n ) { + switch ( n->getNodeType() ) + { + case DOMNode::ELEMENT_NODE: + return ( DOMElement * ) n; + + case DOMNode::ENTITY_REFERENCE_NODE: + { + DOMElement* e = getFirstElementChild ( n ); + if ( e ) + return e; + break; + } + default: + break; + } + n = getNextLogicalSibling ( n ); + } + return NULL; +#endif +} + +DOMNode *WrapXerces::getNextLogicalSibling ( + const DOMNode* n ) +{ + // Copied from Xerces-C + DOMNode* next = n->getNextSibling(); + // If "n" has no following sibling and its parent is an entity reference node we + // need to continue the search through the following siblings of the entity + // reference as these are logically siblings of the given node. + if ( !next ) { + DOMNode* parent = n->getParentNode(); + while ( parent + && parent->getNodeType() == DOMNode::ENTITY_REFERENCE_NODE ) + { + next = parent->getNextSibling(); + if ( next ) + break; + + parent = parent->getParentNode(); + } + } + return next; +} diff --git a/src/wrapxerces.h b/src/wrapxerces.h index 862ff70..d809025 100644 --- a/src/wrapxerces.h +++ b/src/wrapxerces.h @@ -35,6 +35,7 @@ #include #include +#include #include "xercescatalogresolver.h" using namespace xercesc; @@ -129,6 +130,12 @@ class WrapXerces : private boost::noncopyable const wxString &publicId, const wxString &systemId, const wxString &fileName ); + + static DOMElement *getFirstElementChild ( const DOMElement &element ); + static DOMElement *getFirstElementChild ( const DOMNode *n ); + static DOMElement *getNextElementSibling ( const DOMElement &element ); + static DOMNode *getNextLogicalSibling ( const DOMNode* n ); + private: static const wxMBConv &getMBConv(); static XMLNetAccessor *mOriginalNetAccessor; diff --git a/src/xmlcopyeditor.cpp b/src/xmlcopyeditor.cpp index 1c6f8cf..ac2051c 100644 --- a/src/xmlcopyeditor.cpp +++ b/src/xmlcopyeditor.cpp @@ -878,7 +878,7 @@ MyFrame::MyFrame ( // Initialize Xerces-C++ WrapXerces::Init ( libxmlNetAccess ); -#if _XERCES_VERSION >= 30101 +#if _XERCES_VERSION >= 30100 if ( XMLPlatformUtils::fgSSE2ok && xercescSSE2Warning && wxTheApp->argc == 1 ) diff --git a/src/xmlschemagenerator.cpp b/src/xmlschemagenerator.cpp index 860ae03..73c7a2c 100644 --- a/src/xmlschemagenerator.cpp +++ b/src/xmlschemagenerator.cpp @@ -115,10 +115,11 @@ void XmlSchemaGenerator::findAllElements ( const DOMElement &element, wxString tagName = WrapXerces::toString ( element.getTagName() ); mElements[tagName].nodes.insert ( &element ); - DOMElement *child = element.getFirstElementChild(); - for ( ; child != NULL; child = child->getNextElementSibling() ) + DOMElement *child = WrapXerces::getFirstElementChild ( element ); + while ( child ) { findAllElements ( *child, nIndent ); + child = WrapXerces::getNextElementSibling ( *child ); } } @@ -131,10 +132,11 @@ void XmlSchemaGenerator::generateData ( const DOMElement &element, generateData ( name, nIndent ); } - DOMElement *child = element.getFirstElementChild(); - for ( ; child != NULL; child = child->getNextElementSibling() ) + DOMElement *child = WrapXerces::getFirstElementChild ( element ); + while ( child ) { generateData ( *child, nIndent ); + child = WrapXerces::getNextElementSibling ( *child ); } } @@ -268,10 +270,11 @@ void XmlSchemaGenerator::outputSchema ( const DOMElement &element ) mSchema << data.schema; } - DOMElement *child = element.getFirstElementChild(); - for ( ; child != NULL; child = child->getNextElementSibling() ) + DOMElement *child = WrapXerces::getFirstElementChild ( element ); + while ( child ) { outputSchema ( *child ); + child = WrapXerces::getNextElementSibling ( *child ); } }