// file : xsd/cxx/xml/dom/parsing-source.txx // author : Boris Kolpackov // copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC // license : GNU GPL v2 + exceptions; see accompanying LICENSE file #include #include #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) { using namespace xercesc; // 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)); 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); // Xerces-C++ 3.1.0 is the first version with working multi import // support. // #if _XERCES_VERSION >= 30100 if (!(flags & no_muliple_imports)) conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true); #endif // 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)); } // If external schema location was specified, disable loading // schemas via the schema location attributes in the document. // #if _XERCES_VERSION >= 30100 if (!prop.schema_location ().empty () || !prop.no_namespace_schema_location ().empty ()) { conf->setParameter (XMLUni::fgXercesLoadSchema, false); } #endif // Set error handler. // bits::error_handler_proxy ehp (eh); conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); xercesc::Wrapper4InputSource wrap (&is, false); auto_ptr doc; try { doc.reset (parser->parse (&wrap)); } catch (const xercesc::DOMLSException&) { } 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) { using namespace xercesc; // 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)); 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); // Xerces-C++ 3.1.0 is the first version with working multi import // support. // #if _XERCES_VERSION >= 30100 if (!(flags & no_muliple_imports)) conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true); #endif // 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)); } // If external schema location was specified, disable loading // schemas via the schema location attributes in the document. // #if _XERCES_VERSION >= 30100 if (!prop.schema_location ().empty () || !prop.no_namespace_schema_location ().empty ()) { conf->setParameter (XMLUni::fgXercesLoadSchema, false); } #endif // Set error handler. // bits::error_handler_proxy ehp (eh); conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); auto_ptr doc; try { doc.reset (parser->parseURI (string (uri).c_str ())); } catch (const xercesc::DOMLSException&) { } if (ehp.failed ()) doc.reset (); return doc; } } } } }