From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- libxsd/xsd/cxx/xml/dom/parsing-source.txx | 458 ++++++++++++++++++++++++++++++ 1 file changed, 458 insertions(+) create mode 100644 libxsd/xsd/cxx/xml/dom/parsing-source.txx (limited to 'libxsd/xsd/cxx/xml/dom/parsing-source.txx') diff --git a/libxsd/xsd/cxx/xml/dom/parsing-source.txx b/libxsd/xsd/cxx/xml/dom/parsing-source.txx new file mode 100644 index 0000000..ae7ceea --- /dev/null +++ b/libxsd/xsd/cxx/xml/dom/parsing-source.txx @@ -0,0 +1,458 @@ +// file : xsd/cxx/xml/dom/parsing-source.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#if _XERCES_VERSION >= 30000 +# include +#else +# include +#endif +#include +#include +#include + +#include // xercesc::fg* +#include // chLatin_L, etc + +#include + +#include +#include + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // parser + // + template + parser:: + parser (const xercesc::DOMElement& e, bool ep, bool ap) + : element_ (e), + next_element_ (0), + a_ (0), + ai_ (0) + { + using xercesc::DOMNode; + + if (ep) + { + for (next_element_ = e.getFirstChild (); + next_element_ != 0 && + next_element_->getNodeType () != DOMNode::ELEMENT_NODE; + next_element_ = next_element_->getNextSibling ()) /*noop*/; + } + + if (ap) + { + a_ = e.getAttributes (); + as_ = a_->getLength (); + } + } + + template + void parser:: + next_element () + { + using xercesc::DOMNode; + + for (next_element_ = next_element_->getNextSibling (); + next_element_ != 0 && + next_element_->getNodeType () != DOMNode::ELEMENT_NODE; + next_element_ = next_element_->getNextSibling ())/*noop*/; + } + + // parse() + // + template + xml::dom::auto_ptr + parse (xercesc::InputSource& is, + error_handler& eh, + const properties& prop, + unsigned long flags) + { + bits::error_handler_proxy ehp (eh); + return xml::dom::parse (is, ehp, prop, flags); + } + + template + auto_ptr + parse (xercesc::InputSource& is, + xercesc::DOMErrorHandler& eh, + const properties& prop, + unsigned long flags) + { + // HP aCC cannot handle using namespace xercesc; + // + using xercesc::DOMImplementationRegistry; + using xercesc::DOMImplementationLS; + using xercesc::DOMImplementation; + using xercesc::DOMDocument; +#if _XERCES_VERSION >= 30000 + using xercesc::DOMLSParser; + using xercesc::DOMConfiguration; +#else + using xercesc::DOMBuilder; +#endif + + using xercesc::Wrapper4InputSource; + using xercesc::XMLUni; + + + // Instantiate the DOM parser. + // + const XMLCh ls_id[] = {xercesc::chLatin_L, + xercesc::chLatin_S, + xercesc::chNull}; + + // Get an implementation of the Load-Store (LS) interface. + // + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls_id)); + +#if _XERCES_VERSION >= 30000 + auto_ptr parser ( + impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0)); + + DOMConfiguration* conf (parser->getDomConfig ()); + + // Discard comment nodes in the document. + // + conf->setParameter (XMLUni::fgDOMComments, false); + + // Enable datatype normalization. + // + conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true); + + // Do not create EntityReference nodes in the DOM tree. No + // EntityReference nodes will be created, only the nodes + // corresponding to their fully expanded substitution text + // will be created. + // + conf->setParameter (XMLUni::fgDOMEntities, false); + + // Perform namespace processing. + // + conf->setParameter (XMLUni::fgDOMNamespaces, true); + + // Do not include ignorable whitespace in the DOM tree. + // + conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false); + + if (flags & dont_validate) + { + conf->setParameter (XMLUni::fgDOMValidate, false); + conf->setParameter (XMLUni::fgXercesSchema, false); + conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); + } + else + { + conf->setParameter (XMLUni::fgDOMValidate, true); + conf->setParameter (XMLUni::fgXercesSchema, true); + + if (!(flags & no_muliple_imports)) + conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true); + + // This feature checks the schema grammar for additional + // errors. We most likely do not need it when validating + // instances (assuming the schema is valid). + // + conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); + } + + // We will release DOM ourselves. + // + conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + + // Transfer properies if any. + // + + if (!prop.schema_location ().empty ()) + { + xml::string sl (prop.schema_location ()); + const void* v (sl.c_str ()); + + conf->setParameter ( + XMLUni::fgXercesSchemaExternalSchemaLocation, + const_cast (v)); + } + + if (!prop.no_namespace_schema_location ().empty ()) + { + xml::string sl (prop.no_namespace_schema_location ()); + const void* v (sl.c_str ()); + + conf->setParameter ( + XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, + const_cast (v)); + } + + // Set error handler. + // + bits::error_handler_proxy ehp (eh); + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + +#else // _XERCES_VERSION >= 30000 + + // Same as above but for Xerces-C++ 2 series. + // + auto_ptr parser ( + impl->createDOMBuilder (DOMImplementationLS::MODE_SYNCHRONOUS, 0)); + + parser->setFeature (XMLUni::fgDOMComments, false); + parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true); + parser->setFeature (XMLUni::fgDOMEntities, false); + parser->setFeature (XMLUni::fgDOMNamespaces, true); + parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false); + + if (flags & dont_validate) + { + parser->setFeature (XMLUni::fgDOMValidation, false); + parser->setFeature (XMLUni::fgXercesSchema, false); + parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); + } + else + { + parser->setFeature (XMLUni::fgDOMValidation, true); + parser->setFeature (XMLUni::fgXercesSchema, true); + parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); + } + + parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + if (!prop.schema_location ().empty ()) + { + xml::string sl (prop.schema_location ()); + const void* v (sl.c_str ()); + + parser->setProperty ( + XMLUni::fgXercesSchemaExternalSchemaLocation, + const_cast (v)); + } + + if (!prop.no_namespace_schema_location ().empty ()) + { + xml::string sl (prop.no_namespace_schema_location ()); + const void* v (sl.c_str ()); + + parser->setProperty ( + XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, + const_cast (v)); + } + + bits::error_handler_proxy ehp (eh); + parser->setErrorHandler (&ehp); + +#endif // _XERCES_VERSION >= 30000 + + xercesc::Wrapper4InputSource wrap (&is, false); + +#if _XERCES_VERSION >= 30000 + auto_ptr doc (parser->parse (&wrap)); +#else + auto_ptr doc (parser->parse (wrap)); +#endif + if (ehp.failed ()) + doc.reset (); + + return doc; + } + + template + xml::dom::auto_ptr + parse (const std::basic_string& uri, + error_handler& eh, + const properties& prop, + unsigned long flags) + { + bits::error_handler_proxy ehp (eh); + return xml::dom::parse (uri, ehp, prop, flags); + } + + template + auto_ptr + parse (const std::basic_string& uri, + xercesc::DOMErrorHandler& eh, + const properties& prop, + unsigned long flags) + { + // HP aCC cannot handle using namespace xercesc; + // + using xercesc::DOMImplementationRegistry; + using xercesc::DOMImplementationLS; + using xercesc::DOMImplementation; + using xercesc::DOMDocument; +#if _XERCES_VERSION >= 30000 + using xercesc::DOMLSParser; + using xercesc::DOMConfiguration; +#else + using xercesc::DOMBuilder; +#endif + using xercesc::XMLUni; + + + // Instantiate the DOM parser. + // + const XMLCh ls_id[] = {xercesc::chLatin_L, + xercesc::chLatin_S, + xercesc::chNull}; + + // Get an implementation of the Load-Store (LS) interface. + // + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls_id)); + +#if _XERCES_VERSION >= 30000 + auto_ptr parser ( + impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0)); + + DOMConfiguration* conf (parser->getDomConfig ()); + + // Discard comment nodes in the document. + // + conf->setParameter (XMLUni::fgDOMComments, false); + + // Enable datatype normalization. + // + conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true); + + // Do not create EntityReference nodes in the DOM tree. No + // EntityReference nodes will be created, only the nodes + // corresponding to their fully expanded substitution text + // will be created. + // + conf->setParameter (XMLUni::fgDOMEntities, false); + + // Perform namespace processing. + // + conf->setParameter (XMLUni::fgDOMNamespaces, true); + + // Do not include ignorable whitespace in the DOM tree. + // + conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false); + + if (flags & dont_validate) + { + conf->setParameter (XMLUni::fgDOMValidate, false); + conf->setParameter (XMLUni::fgXercesSchema, false); + conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); + } + else + { + conf->setParameter (XMLUni::fgDOMValidate, true); + conf->setParameter (XMLUni::fgXercesSchema, true); + + if (!(flags & no_muliple_imports)) + conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true); + + // This feature checks the schema grammar for additional + // errors. We most likely do not need it when validating + // instances (assuming the schema is valid). + // + conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); + } + + // We will release DOM ourselves. + // + conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + + // Transfer properies if any. + // + + if (!prop.schema_location ().empty ()) + { + xml::string sl (prop.schema_location ()); + const void* v (sl.c_str ()); + + conf->setParameter ( + XMLUni::fgXercesSchemaExternalSchemaLocation, + const_cast (v)); + } + + if (!prop.no_namespace_schema_location ().empty ()) + { + xml::string sl (prop.no_namespace_schema_location ()); + const void* v (sl.c_str ()); + + conf->setParameter ( + XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, + const_cast (v)); + } + + // Set error handler. + // + bits::error_handler_proxy ehp (eh); + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + +#else // _XERCES_VERSION >= 30000 + + // Same as above but for Xerces-C++ 2 series. + // + auto_ptr parser ( + impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0)); + + parser->setFeature (XMLUni::fgDOMComments, false); + parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true); + parser->setFeature (XMLUni::fgDOMEntities, false); + parser->setFeature (XMLUni::fgDOMNamespaces, true); + parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false); + + if (flags & dont_validate) + { + parser->setFeature (XMLUni::fgDOMValidation, false); + parser->setFeature (XMLUni::fgXercesSchema, false); + parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); + } + else + { + parser->setFeature (XMLUni::fgDOMValidation, true); + parser->setFeature (XMLUni::fgXercesSchema, true); + parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); + } + + parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + if (!prop.schema_location ().empty ()) + { + xml::string sl (prop.schema_location ()); + const void* v (sl.c_str ()); + + parser->setProperty ( + XMLUni::fgXercesSchemaExternalSchemaLocation, + const_cast (v)); + } + + if (!prop.no_namespace_schema_location ().empty ()) + { + xml::string sl (prop.no_namespace_schema_location ()); + const void* v (sl.c_str ()); + + parser->setProperty ( + XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, + const_cast (v)); + } + + bits::error_handler_proxy ehp (eh); + parser->setErrorHandler (&ehp); + +#endif // _XERCES_VERSION >= 30000 + + auto_ptr doc ( + parser->parseURI (string (uri).c_str ())); + + if (ehp.failed ()) + doc.reset (); + + return doc; + } + } + } + } +} -- cgit v1.1