diff options
Diffstat (limited to 'xsd/libxsd/xsd/cxx/xml/dom')
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/auto-ptr.hxx | 158 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.hxx | 61 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.txx | 80 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/elements.hxx | 36 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/elements.txx | 57 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/parsing-header.hxx | 24 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/parsing-source.hxx | 138 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/parsing-source.txx | 510 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/serialization-header.hxx | 81 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/serialization-header.txx | 192 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/serialization-source.hxx | 181 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/serialization-source.txx | 394 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.hxx | 31 | ||||
-rw-r--r-- | xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.txx | 38 |
14 files changed, 1981 insertions, 0 deletions
diff --git a/xsd/libxsd/xsd/cxx/xml/dom/auto-ptr.hxx b/xsd/libxsd/xsd/cxx/xml/dom/auto-ptr.hxx new file mode 100644 index 0000000..f22e652 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/auto-ptr.hxx @@ -0,0 +1,158 @@ +// file : xsd/cxx/xml/dom/auto-ptr.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_AUTO_PTR_HXX +#define XSD_CXX_XML_DOM_AUTO_PTR_HXX + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // Simple auto_ptr version that calls release() instead of delete. + // + + template <typename T> + struct remove_c + { + typedef T r; + }; + + template <typename T> + struct remove_c<const T> + { + typedef T r; + }; + + template <typename T> + struct auto_ptr_ref + { + T* x_; + + explicit + auto_ptr_ref (T* x) + : x_ (x) + { + } + }; + + template <typename T> + struct auto_ptr + { + ~auto_ptr () + { + reset (); + } + + explicit + auto_ptr (T* x = 0) + : x_ (x) + { + } + + auto_ptr (auto_ptr& y) + : x_ (y.release ()) + { + } + + template <typename T2> + auto_ptr (auto_ptr<T2>& y) + : x_ (y.release ()) + { + } + + auto_ptr (auto_ptr_ref<T> r) + : x_ (r.x_) + { + } + + auto_ptr& + operator= (auto_ptr& y) + { + if (x_ != y.x_) + reset (y.release ()); + + return *this; + } + + template <typename T2> + auto_ptr& + operator= (auto_ptr<T2>& y) + { + if (x_ != y.x_) + reset (y.release ()); + + return *this; + } + + auto_ptr& + operator= (auto_ptr_ref<T> r) + { + if (r.x_ != x_) + reset (r.x_); + + return *this; + } + + template <typename T2> + operator auto_ptr_ref<T2> () + { + return auto_ptr_ref<T2> (release ()); + } + + template <typename T2> + operator auto_ptr<T2> () + { + return auto_ptr<T2> (release ()); + } + + public: + T& + operator* () const + { + return *x_; + } + + T* + operator-> () const + { + return x_; + } + + T* + get () const + { + return x_; + } + + T* + release () + { + T* x (x_); + x_ = 0; + return x; + } + + void + reset (T* x = 0) + { + if (x_) + const_cast<typename remove_c<T>::r*> (x_)->release (); + + x_ = x; + } + + private: + T* x_; + }; + } + } + } +} + +#endif // XSD_CXX_XML_DOM_AUTO_PTR_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.hxx b/xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.hxx new file mode 100644 index 0000000..83e9a7e --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.hxx @@ -0,0 +1,61 @@ +// file : xsd/cxx/xml/dom/bits/error-handler-proxy.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_BITS_ERROR_HANDLER_PROXY_HXX +#define XSD_CXX_XML_DOM_BITS_ERROR_HANDLER_PROXY_HXX + +#include <xercesc/dom/DOMError.hpp> +#include <xercesc/dom/DOMLocator.hpp> +#include <xercesc/dom/DOMErrorHandler.hpp> + +#include <xsd/cxx/xml/error-handler.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + namespace bits + { + template <typename C> + class error_handler_proxy: public xercesc::DOMErrorHandler + { + public: + error_handler_proxy (error_handler<C>& eh) + : failed_ (false), eh_ (&eh), native_eh_ (0) + { + } + + error_handler_proxy (xercesc::DOMErrorHandler& eh) + : failed_ (false), eh_ (0), native_eh_ (&eh) + { + } + + virtual bool + handleError (const xercesc::DOMError& e); + + bool + failed () const + { + return failed_; + } + + private: + bool failed_; + error_handler<C>* eh_; + xercesc::DOMErrorHandler* native_eh_; + }; + } + } + } + } +} + +#include <xsd/cxx/xml/dom/bits/error-handler-proxy.txx> + +#endif // XSD_CXX_XML_DOM_BITS_ERROR_HANDLER_PROXY_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.txx b/xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.txx new file mode 100644 index 0000000..23e71cd --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/bits/error-handler-proxy.txx @@ -0,0 +1,80 @@ +// file : xsd/cxx/xml/dom/bits/error-handler-proxy.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <xsd/cxx/xml/string.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + namespace bits + { + template <typename C> + bool error_handler_proxy<C>:: + handleError (const xercesc::DOMError& e) + { + using xercesc::DOMError; + + if (e.getSeverity() != DOMError::DOM_SEVERITY_WARNING) + failed_ = true; + + if (native_eh_) + return native_eh_->handleError (e); + else + { + typedef typename error_handler<C>::severity severity; + + severity s (severity::error); + + switch (e.getSeverity()) + { + case DOMError::DOM_SEVERITY_WARNING: + { + s = severity::warning; + break; + } + case DOMError::DOM_SEVERITY_ERROR: + { + s = severity::error; + break; + } + case DOMError::DOM_SEVERITY_FATAL_ERROR: + { + s = severity::fatal; + break; + } + } + + xercesc::DOMLocator* loc (e.getLocation ()); + +#if _XERCES_VERSION >= 30000 + return eh_->handle ( + transcode<C> (loc->getURI ()), + static_cast<unsigned long> (loc->getLineNumber ()), + static_cast<unsigned long> (loc->getColumnNumber ()), + s, + transcode<C> (e.getMessage ())); +#else + XMLSSize_t l (loc->getLineNumber ()); + XMLSSize_t c (loc->getColumnNumber ()); + + return eh_->handle ( + transcode<C> (loc->getURI ()), + (l == -1 ? 0 : static_cast<unsigned long> (l)), + (c == -1 ? 0 : static_cast<unsigned long> (c)), + s, + transcode<C> (e.getMessage ())); +#endif + } + } + } + } + } + } +} diff --git a/xsd/libxsd/xsd/cxx/xml/dom/elements.hxx b/xsd/libxsd/xsd/cxx/xml/dom/elements.hxx new file mode 100644 index 0000000..e8ce8d2 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/elements.hxx @@ -0,0 +1,36 @@ +// file : xsd/cxx/xml/dom/elements.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_ELEMENTS_HXX +#define XSD_CXX_XML_DOM_ELEMENTS_HXX + +#include <xercesc/dom/DOMAttr.hpp> +#include <xercesc/dom/DOMElement.hpp> + +#include <xsd/cxx/xml/qualified-name.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + template <typename C> + qualified_name<C> + name (const xercesc::DOMAttr&); + + template <typename C> + qualified_name<C> + name (const xercesc::DOMElement&); + } + } + } +} + +#include <xsd/cxx/xml/dom/elements.txx> + +#endif // XSD_CXX_XML_DOM_ELEMENTS_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/elements.txx b/xsd/libxsd/xsd/cxx/xml/dom/elements.txx new file mode 100644 index 0000000..540d0fb --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/elements.txx @@ -0,0 +1,57 @@ +// file : xsd/cxx/xml/dom/elements.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <xsd/cxx/xml/string.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + template <typename C> + qualified_name<C> + name (const xercesc::DOMAttr& a) + { + const XMLCh* n (a.getLocalName ()); + + // If this DOM doesn't support namespaces then use getName. + // + if (n != 0) + { + if (const XMLCh* ns = a.getNamespaceURI ()) + return qualified_name<C> (transcode<C> (n), transcode<C> (ns)); + else + return qualified_name<C> (transcode<C> (n)); + } + else + return qualified_name<C> (transcode<C> (a.getName ())); + } + + + template <typename C> + qualified_name<C> + name (const xercesc::DOMElement& e) + { + const XMLCh* n (e.getLocalName ()); + + // If this DOM doesn't support namespaces then use getTagName. + // + if (n != 0) + { + if (const XMLCh* ns = e.getNamespaceURI ()) + return qualified_name<C> (transcode<C> (n), transcode<C> (ns)); + else + return qualified_name<C> (transcode<C> (n)); + } + else + return qualified_name<C> (transcode<C> (e.getTagName ())); + } + } + } + } +} diff --git a/xsd/libxsd/xsd/cxx/xml/dom/parsing-header.hxx b/xsd/libxsd/xsd/cxx/xml/dom/parsing-header.hxx new file mode 100644 index 0000000..d9a3695 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/parsing-header.hxx @@ -0,0 +1,24 @@ +// file : xsd/cxx/xml/dom/parsing-header.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_PARSING_HEADER_HXX +#define XSD_CXX_XML_DOM_PARSING_HEADER_HXX + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + template <typename C> + class parser; + } + } + } +} + +#endif // XSD_CXX_XML_DOM_PARSING_HEADER_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/parsing-source.hxx b/xsd/libxsd/xsd/cxx/xml/dom/parsing-source.hxx new file mode 100644 index 0000000..2c90cf3 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/parsing-source.hxx @@ -0,0 +1,138 @@ +// file : xsd/cxx/xml/dom/parsing-source.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_PARSING_SOURCE_HXX +#define XSD_CXX_XML_DOM_PARSING_SOURCE_HXX + +#include <string> + +#include <xercesc/dom/DOMNode.hpp> +#include <xercesc/dom/DOMAttr.hpp> +#include <xercesc/dom/DOMElement.hpp> +#include <xercesc/dom/DOMDocument.hpp> +#include <xercesc/dom/DOMNamedNodeMap.hpp> +#include <xercesc/dom/DOMErrorHandler.hpp> + +#include <xercesc/sax/InputSource.hpp> + +#include <xsd/cxx/xml/elements.hxx> // properies +#include <xsd/cxx/xml/error-handler.hxx> + +#include <xsd/cxx/xml/dom/auto-ptr.hxx> +#include <xsd/cxx/xml/dom/elements.hxx> // name +#include <xsd/cxx/xml/dom/parsing-header.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // Parser state object. Can be used for parsing element, attributes, + // or both. + // + template <typename C> + class parser + { + public: + parser (const xercesc::DOMElement& e, bool ep, bool ap); + + bool + more_elements () + { + return next_element_ != 0; + } + + const xercesc::DOMElement& + cur_element () + { + return *static_cast<const xercesc::DOMElement*> (next_element_); + } + + void + next_element (); + + bool + more_attributes () + { + return as_ > ai_; + } + + const xercesc::DOMAttr& + next_attribute () + { + return *static_cast<const xercesc::DOMAttr*> (a_->item (ai_++)); + } + + void + reset_attributes () + { + ai_ = 0; + } + + const xercesc::DOMElement& + element () const + { + return element_; + } + + private: + parser (const parser&); + + parser& + operator= (const parser&); + + private: + const xercesc::DOMElement& element_; + const xercesc::DOMNode* next_element_; + + const xercesc::DOMNamedNodeMap* a_; + XMLSize_t ai_; // Index of the next DOMAttr. + XMLSize_t as_; // Cached size of a_. + }; + + + // Parsing flags. + // + const unsigned long dont_validate = 0x00000400UL; + const unsigned long no_muliple_imports = 0x00000800UL; + + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + parse (xercesc::InputSource&, + error_handler<C>&, + const properties<C>&, + unsigned long flags); + + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + parse (xercesc::InputSource&, + xercesc::DOMErrorHandler&, + const properties<C>&, + unsigned long flags); + + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + parse (const std::basic_string<C>& uri, + error_handler<C>&, + const properties<C>&, + unsigned long flags); + + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + parse (const std::basic_string<C>& uri, + xercesc::DOMErrorHandler&, + const properties<C>&, + unsigned long flags); + } + } + } +} + +#include <xsd/cxx/xml/dom/parsing-source.txx> + +#endif // XSD_CXX_XML_DOM_PARSING_SOURCE_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/parsing-source.txx b/xsd/libxsd/xsd/cxx/xml/dom/parsing-source.txx new file mode 100644 index 0000000..50fa285 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/parsing-source.txx @@ -0,0 +1,510 @@ +// file : xsd/cxx/xml/dom/parsing-source.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#if _XERCES_VERSION >= 30000 +# include <xercesc/dom/DOMLSParser.hpp> +# include <xercesc/dom/DOMLSException.hpp> +#else +# include <xercesc/dom/DOMBuilder.hpp> +#endif +#include <xercesc/dom/DOMNamedNodeMap.hpp> +#include <xercesc/dom/DOMImplementation.hpp> +#include <xercesc/dom/DOMImplementationRegistry.hpp> + +#include <xercesc/util/XMLUni.hpp> // xercesc::fg* +#include <xercesc/util/XMLUniDefs.hpp> // chLatin_L, etc + +#include <xercesc/framework/Wrapper4InputSource.hpp> + +#include <xsd/cxx/xml/string.hxx> +#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // parser + // + template <typename C> + parser<C>:: + 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 <typename C> + void parser<C>:: + 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 <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + parse (xercesc::InputSource& is, + error_handler<C>& eh, + const properties<C>& prop, + unsigned long flags) + { + bits::error_handler_proxy<C> ehp (eh); + return xml::dom::parse (is, ehp, prop, flags); + } + + template <typename C> + auto_ptr<xercesc::DOMDocument> + parse (xercesc::InputSource& is, + xercesc::DOMErrorHandler& eh, + const properties<C>& 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<DOMLSParser> 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<void*> (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<void*> (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<C> ehp (eh); + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + +#else // _XERCES_VERSION >= 30000 + + // Same as above but for Xerces-C++ 2 series. + // + auto_ptr<DOMBuilder> 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<void*> (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<void*> (v)); + } + + bits::error_handler_proxy<C> ehp (eh); + parser->setErrorHandler (&ehp); + +#endif // _XERCES_VERSION >= 30000 + + xercesc::Wrapper4InputSource wrap (&is, false); + +#if _XERCES_VERSION >= 30000 + auto_ptr<DOMDocument> doc; + + try + { + doc.reset (parser->parse (&wrap)); + } + catch (const xercesc::DOMLSException&) + { + } +#else + auto_ptr<DOMDocument> doc (parser->parse (wrap)); +#endif + if (ehp.failed ()) + doc.reset (); + + return doc; + } + + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + parse (const std::basic_string<C>& uri, + error_handler<C>& eh, + const properties<C>& prop, + unsigned long flags) + { + bits::error_handler_proxy<C> ehp (eh); + return xml::dom::parse (uri, ehp, prop, flags); + } + + template <typename C> + auto_ptr<xercesc::DOMDocument> + parse (const std::basic_string<C>& uri, + xercesc::DOMErrorHandler& eh, + const properties<C>& 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<DOMLSParser> 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<void*> (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<void*> (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<C> ehp (eh); + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + +#else // _XERCES_VERSION >= 30000 + + // Same as above but for Xerces-C++ 2 series. + // + auto_ptr<DOMBuilder> 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<void*> (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<void*> (v)); + } + + bits::error_handler_proxy<C> ehp (eh); + parser->setErrorHandler (&ehp); + +#endif // _XERCES_VERSION >= 30000 + + +#if _XERCES_VERSION >= 30000 + auto_ptr<DOMDocument> doc; + + try + { + doc.reset (parser->parseURI (string (uri).c_str ())); + } + catch (const xercesc::DOMLSException&) + { + } +#else + auto_ptr<DOMDocument> doc ( + parser->parseURI (string (uri).c_str ())); +#endif + + if (ehp.failed ()) + doc.reset (); + + return doc; + } + } + } + } +} diff --git a/xsd/libxsd/xsd/cxx/xml/dom/serialization-header.hxx b/xsd/libxsd/xsd/cxx/xml/dom/serialization-header.hxx new file mode 100644 index 0000000..3b879b1 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/serialization-header.hxx @@ -0,0 +1,81 @@ +// file : xsd/cxx/xml/dom/serialization-header.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_SERIALIZATION_HEADER_HXX +#define XSD_CXX_XML_DOM_SERIALIZATION_HEADER_HXX + +#include <map> +#include <string> + +#include <xercesc/dom/DOMElement.hpp> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // Find an existing prefix or establish a new one. Try to use + // hint if provided and available. + // + template <typename C> + std::basic_string<C> + prefix (const C* ns, xercesc::DOMElement&, const C* hint = 0); + + template <typename C> + inline std::basic_string<C> + prefix (const std::basic_string<C>& ns, + xercesc::DOMElement& e, + const C* hint = 0) + { + return prefix (ns.c_str (), e, hint); + } + + // + // + template <typename C> + void + clear (xercesc::DOMElement&); + + // + // + template <typename C> + class namespace_info + { + public: + typedef std::basic_string<C> string; + + namespace_info () + { + } + + namespace_info (const string& name_, const string& schema_) + : name (name_), + schema (schema_) + { + } + + std::basic_string<C> name; + std::basic_string<C> schema; + }; + + + // Map of namespace prefix to namespace_info. + // + template <typename C> + class namespace_infomap: + public std::map<std::basic_string<C>, namespace_info<C> > + { + }; + } + } + } +} + +#include <xsd/cxx/xml/dom/serialization-header.txx> + +#endif // XSD_CXX_XML_DOM_SERIALIZATION_HEADER_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/serialization-header.txx b/xsd/libxsd/xsd/cxx/xml/dom/serialization-header.txx new file mode 100644 index 0000000..3052b7e --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/serialization-header.txx @@ -0,0 +1,192 @@ +// file : xsd/cxx/xml/dom/serialization-header.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <vector> +#include <sstream> +#include <cstddef> // std::size_t + +#include <xercesc/dom/DOMNode.hpp> +#include <xercesc/dom/DOMAttr.hpp> +#include <xercesc/dom/DOMNamedNodeMap.hpp> + +#include <xercesc/util/XMLUni.hpp> // xercesc::fg* +#include <xercesc/util/XMLString.hpp> +#include <xercesc/validators/schema/SchemaSymbols.hpp> + +#include <xsd/cxx/xml/string.hxx> +#include <xsd/cxx/xml/bits/literals.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // + // + template <typename C> + std::basic_string<C> + prefix (const C* ns, xercesc::DOMElement& e, const C* hint) + { + string xns (ns); + +#if _XERCES_VERSION >= 30000 + const XMLCh* p (e.lookupPrefix (xns.c_str ())); +#else + const XMLCh* p (e.lookupNamespacePrefix (xns.c_str (), false)); +#endif + if (p != 0) + return transcode<C> (p); + + if (e.isDefaultNamespace (xns.c_str ())) + return std::basic_string<C> (); + + // 'xml' prefix requires special handling and Xerces folks + // refuse to handle this in DOM so I have to do it myself. + // + if (std::basic_string<C> (ns) == xml::bits::xml_namespace<C> ()) + return xml::bits::xml_prefix<C> (); + + // No prefix for this namespace. Will need to establish one. + // + std::basic_string<C> prefix; + + if (hint != 0 && + e.lookupNamespaceURI (xml::string (hint).c_str ()) == 0) + { + prefix = hint; + } + else + { + for (unsigned long n (1);; ++n) + { + // Make finding the first few prefixes fast. + // + switch (n) + { + case 1: + { + prefix = xml::bits::first_prefix<C> (); + break; + } + case 2: + { + prefix = xml::bits::second_prefix<C> (); + break; + } + case 3: + { + prefix = xml::bits::third_prefix<C> (); + break; + } + case 4: + { + prefix = xml::bits::fourth_prefix<C> (); + break; + } + case 5: + { + prefix = xml::bits::fifth_prefix<C> (); + break; + } + default: + { + std::basic_ostringstream<C> ostr; + ostr << C ('p') << n; + prefix = ostr.str (); + break; + } + } + + if (e.lookupNamespaceURI (xml::string (prefix).c_str ()) == 0) + break; + } + } + + std::basic_string<C> name (xml::bits::xmlns_prefix<C> ()); + name += C(':'); + name += prefix; + + e.setAttributeNS ( + xercesc::XMLUni::fgXMLNSURIName, + xml::string (name).c_str (), + xns.c_str ()); + + return prefix; + } + + // + // + template <typename C> + void + clear (xercesc::DOMElement& e) + { + // HP aCC cannot handle using namespace xercesc; + // + using xercesc::DOMNode; + using xercesc::DOMAttr; + using xercesc::DOMNamedNodeMap; + using xercesc::XMLString; + using xercesc::SchemaSymbols; + + // Remove child nodes. + // + while (xercesc::DOMNode* n = e.getFirstChild ()) + { + e.removeChild (n); + n->release (); + } + + // Remove attributes. + // + DOMNamedNodeMap* att_map (e.getAttributes ()); + XMLSize_t n (att_map->getLength ()); + + if (n != 0) + { + std::vector<DOMAttr*> atts; + + // Collect all attributes to be removed while filtering + // out special cases (xmlns & xsi). + // + for (XMLSize_t i (0); i != n; ++i) + { + DOMAttr* a (static_cast<DOMAttr*> (att_map->item (i))); + const XMLCh* ns (a->getNamespaceURI ()); + + if (ns != 0) + { + if (XMLString::equals (ns, xercesc::XMLUni::fgXMLNSURIName)) + continue; + + if (XMLString::equals (ns, SchemaSymbols::fgURI_XSI)) + { + const XMLCh* name (a->getLocalName ()); + + if (XMLString::equals ( + name, SchemaSymbols::fgXSI_SCHEMALOCACTION) || + XMLString::equals ( + name, SchemaSymbols::fgXSI_NONAMESPACESCHEMALOCACTION)) + continue; + } + } + + atts.push_back (a); + } + + for (std::vector<DOMAttr*>::iterator i (atts.begin ()), + end (atts.end ()); i != end; ++i) + { + e.removeAttributeNode (*i); + (*i)->release (); + } + } + } + } + } + } +} diff --git a/xsd/libxsd/xsd/cxx/xml/dom/serialization-source.hxx b/xsd/libxsd/xsd/cxx/xml/dom/serialization-source.hxx new file mode 100644 index 0000000..325c1ec --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/serialization-source.hxx @@ -0,0 +1,181 @@ +// file : xsd/cxx/xml/dom/serialization-source.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX +#define XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX + +#include <string> +#include <cstring> // std::memcpy +#include <ostream> + +#include <xercesc/dom/DOMAttr.hpp> +#include <xercesc/dom/DOMElement.hpp> +#include <xercesc/dom/DOMDocument.hpp> +#include <xercesc/dom/DOMErrorHandler.hpp> +#include <xercesc/framework/XMLFormatter.hpp> // XMLFormatTarget, XMLFormatter + +#include <xsd/cxx/xml/error-handler.hxx> +#include <xsd/cxx/xml/dom/auto-ptr.hxx> +#include <xsd/cxx/xml/dom/elements.hxx> // name +#include <xsd/cxx/xml/dom/serialization-header.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // + // + template <typename C> + xercesc::DOMAttr& + create_attribute (const C* name, xercesc::DOMElement&); + + template <typename C> + xercesc::DOMAttr& + create_attribute (const C* name, const C* ns, xercesc::DOMElement&); + + template <typename C> + xercesc::DOMElement& + create_element (const C* name, xercesc::DOMElement&); + + template <typename C> + xercesc::DOMElement& + create_element (const C* name, const C* ns, xercesc::DOMElement&); + + // Serialization flags. + // + const unsigned long no_xml_declaration = 0x00010000UL; + const unsigned long dont_pretty_print = 0x00020000UL; + + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + serialize (const std::basic_string<C>& root_element, + const std::basic_string<C>& root_element_namespace, + const namespace_infomap<C>& map, + unsigned long flags); + + // This one helps Sun C++ to overcome its fears. + // + template <typename C> + inline xml::dom::auto_ptr<xercesc::DOMDocument> + serialize (const C* root_element, + const C* root_element_namespace, + const namespace_infomap<C>& map, + unsigned long flags) + { + return serialize (std::basic_string<C> (root_element), + std::basic_string<C> (root_element_namespace), + map, + flags); + } + + // + // + template <typename C> + bool + serialize (xercesc::XMLFormatTarget& target, + const xercesc::DOMDocument& doc, + const std::basic_string<C>& enconding, + error_handler<C>& eh, + unsigned long flags); + + template <typename C> + bool + serialize (xercesc::XMLFormatTarget& target, + const xercesc::DOMDocument& doc, + const std::basic_string<C>& enconding, + xercesc::DOMErrorHandler& eh, + unsigned long flags); + + + class ostream_format_target: public xercesc::XMLFormatTarget + { + public: + ostream_format_target (std::ostream& os) + : n_ (0), os_ (os) + { + } + + public: + // I know, some of those consts are stupid. But that's what + // Xerces folks put into their interfaces and VC-7.1 thinks + // there are different signatures if one strips this fluff off. + // + virtual void + writeChars (const XMLByte* const buf, +#if _XERCES_VERSION >= 30000 + const XMLSize_t size, +#else + const unsigned int size, +#endif + xercesc::XMLFormatter* const) + { + // Ignore the write request if there was a stream failure and the + // stream is not using exceptions. + // + if (os_.fail ()) + return; + + // Flush the buffer if the block is too large or if we don't have + // any space left. + // + if ((size >= buf_size_ / 8 || n_ + size > buf_size_) && n_ != 0) + { + os_.write (buf_, static_cast<std::streamsize> (n_)); + n_ = 0; + + if (os_.fail ()) + return; + } + + if (size < buf_size_ / 8) + { + std::memcpy (buf_ + n_, reinterpret_cast<const char*> (buf), size); + n_ += size; + } + else + os_.write (reinterpret_cast<const char*> (buf), + static_cast<std::streamsize> (size)); + } + + + virtual void + flush () + { + // Ignore the flush request if there was a stream failure + // and the stream is not using exceptions. + // + if (!os_.fail ()) + { + if (n_ != 0) + { + os_.write (buf_, static_cast<std::streamsize> (n_)); + n_ = 0; + + if (os_.fail ()) + return; + } + + os_.flush (); + } + } + + private: + static const std::size_t buf_size_ = 1024; + char buf_[buf_size_]; + std::size_t n_; + std::ostream& os_; + }; + } + } + } +} + +#include <xsd/cxx/xml/dom/serialization-source.txx> + +#endif // XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/serialization-source.txx b/xsd/libxsd/xsd/cxx/xml/dom/serialization-source.txx new file mode 100644 index 0000000..2b27dd5 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/serialization-source.txx @@ -0,0 +1,394 @@ +// file : xsd/cxx/xml/dom/serialization-source.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <xercesc/util/XMLUni.hpp> // xercesc::fg* +#include <xercesc/util/XMLUniDefs.hpp> // chLatin_L, etc +#include <xercesc/validators/schema/SchemaSymbols.hpp> + +#if _XERCES_VERSION >= 30000 +# include <xercesc/dom/DOMLSOutput.hpp> +# include <xercesc/dom/DOMLSSerializer.hpp> +#else +# include <xercesc/dom/DOMWriter.hpp> +#endif +#include <xercesc/dom/DOMElement.hpp> +#include <xercesc/dom/DOMImplementation.hpp> +#include <xercesc/dom/DOMImplementationRegistry.hpp> + +#include <xsd/cxx/xml/string.hxx> +#include <xsd/cxx/xml/bits/literals.hxx> +#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + // + // + template <typename C> + xercesc::DOMAttr& + create_attribute (const C* name, xercesc::DOMElement& parent) + { + xercesc::DOMDocument* doc (parent.getOwnerDocument ()); + xercesc::DOMAttr* a (doc->createAttribute (string (name).c_str ())); + parent.setAttributeNode (a); + return *a; + } + + template <typename C> + xercesc::DOMAttr& + create_attribute (const C* name, + const C* ns, + xercesc::DOMElement& parent) + { + if (ns[0] == C ('\0')) + return create_attribute (name, parent); + + xercesc::DOMDocument* doc (parent.getOwnerDocument ()); + + xercesc::DOMAttr* a; + std::basic_string<C> p (prefix<C> (ns, parent)); + + if (!p.empty ()) + { + p += ':'; + p += name; + a = doc->createAttributeNS (string (ns).c_str (), + string (p).c_str ()); + } + else + a = doc->createAttributeNS (string (ns).c_str (), + string (name).c_str ()); + + parent.setAttributeNodeNS (a); + return *a; + } + + template <typename C> + xercesc::DOMElement& + create_element (const C* name, xercesc::DOMElement& parent) + { + xercesc::DOMDocument* doc (parent.getOwnerDocument ()); + xercesc::DOMElement* e (doc->createElement (string (name).c_str ())); + parent.appendChild (e); + return *e; + } + + template <typename C> + xercesc::DOMElement& + create_element (const C* name, + const C* ns, + xercesc::DOMElement& parent) + { + if (ns[0] == C ('\0')) + return create_element (name, parent); + + xercesc::DOMDocument* doc (parent.getOwnerDocument ()); + + xercesc::DOMElement* e; + std::basic_string<C> p (prefix<C> (ns, parent)); + + if (!p.empty ()) + { + p += ':'; + p += name; + e = doc->createElementNS (string (ns).c_str (), + string (p).c_str ()); + } + else + e = doc->createElementNS (string (ns).c_str (), + string (name).c_str ()); + + parent.appendChild (e); + return *e; + } + + + // + // + template <typename C> + auto_ptr<xercesc::DOMDocument> + serialize (const std::basic_string<C>& el, + const std::basic_string<C>& ns, + const namespace_infomap<C>& map, + unsigned long) + { + // HP aCC cannot handle using namespace xercesc; + // + using xercesc::DOMImplementationRegistry; + using xercesc::DOMImplementation; + using xercesc::DOMDocument; + using xercesc::DOMElement; + + // + // + typedef std::basic_string<C> string; + typedef namespace_infomap<C> infomap; + typedef typename infomap::const_iterator infomap_iterator; + + C colon (':'), space (' '); + + string prefix; + + if (!ns.empty ()) + { + infomap_iterator i (map.begin ()), e (map.end ()); + + for ( ;i != e; ++i) + { + if (i->second.name == ns) + { + prefix = i->first; + break; + } + } + + // Since this is the first namespace in document we don't + // need to worry about conflicts. + // + if (i == e) + prefix = xml::bits::first_prefix<C> (); + } + + const XMLCh ls[] = {xercesc::chLatin_L, + xercesc::chLatin_S, + xercesc::chNull}; + + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls)); + + auto_ptr<DOMDocument> doc ( + impl->createDocument ( + (ns.empty () ? 0 : xml::string (ns).c_str ()), + xml::string ((prefix.empty () + ? el + : prefix + colon + el)).c_str (), + 0)); + + DOMElement* root (doc->getDocumentElement ()); + + // Check if we need to provide xsi mapping. + // + bool xsi (false); + string xsi_prefix; + string xmlns_prefix (xml::bits::xmlns_prefix<C> ()); + + for (infomap_iterator i (map.begin ()), e (map.end ()); i != e; ++i) + { + if (!i->second.schema.empty ()) + { + xsi = true; + break; + } + } + + // Check if we were told to provide xsi mapping. + // + if (xsi) + { + for (infomap_iterator i (map.begin ()), e (map.end ()); + i != e; + ++i) + { + if (i->second.name == xml::bits::xsi_namespace<C> ()) + { + xsi_prefix = i->first; + xsi = false; + break; + } + } + } + + // Create user-defined mappings. + // + for (infomap_iterator i (map.begin ()), e (map.end ()); i != e; ++i) + { + if (i->first.empty ()) + { + // Empty prefix. + // + if (!i->second.name.empty ()) + root->setAttributeNS ( + xercesc::XMLUni::fgXMLNSURIName, + xml::string (xmlns_prefix).c_str (), + xml::string (i->second.name).c_str ()); + } + else + { + root->setAttributeNS ( + xercesc::XMLUni::fgXMLNSURIName, + xml::string (xmlns_prefix + colon + i->first).c_str (), + xml::string (i->second.name).c_str ()); + } + } + + // If we were not told to provide xsi mapping but we need it + // then we will have to add it ourselves. + // + if (xsi) + xsi_prefix = dom::prefix (xml::bits::xsi_namespace<C> (), + *root, + xml::bits::xsi_prefix<C> ()); + + // Create xsi:schemaLocation and xsi:noNamespaceSchemaLocation + // attributes. + // + string schema_location; + string no_namespace_schema_location; + + for (infomap_iterator i (map.begin ()), e (map.end ()); i != e; ++i) + { + if (!i->second.schema.empty ()) + { + if (i->second.name.empty ()) + { + if (!no_namespace_schema_location.empty ()) + no_namespace_schema_location += space; + + no_namespace_schema_location += i->second.schema; + } + else + { + if (!schema_location.empty ()) + schema_location += space; + + schema_location += i->second.name + space + i->second.schema; + } + } + } + + if (!schema_location.empty ()) + { + root->setAttributeNS ( + xercesc::SchemaSymbols::fgURI_XSI, + xml::string (xsi_prefix + colon + + xml::bits::schema_location<C> ()).c_str (), + xml::string (schema_location).c_str ()); + } + + if (!no_namespace_schema_location.empty ()) + { + root->setAttributeNS ( + xercesc::SchemaSymbols::fgURI_XSI, + xml::string ( + xsi_prefix + colon + + xml::bits::no_namespace_schema_location<C> ()).c_str (), + xml::string (no_namespace_schema_location).c_str ()); + } + + return doc; + } + + + template <typename C> + bool + serialize (xercesc::XMLFormatTarget& target, + const xercesc::DOMDocument& doc, + const std::basic_string<C>& encoding, + xercesc::DOMErrorHandler& eh, + unsigned long flags) + { + // HP aCC cannot handle using namespace xercesc; + // + using xercesc::DOMImplementationRegistry; + using xercesc::DOMImplementation; +#if _XERCES_VERSION >= 30000 + using xercesc::DOMLSSerializer; + using xercesc::DOMConfiguration; + using xercesc::DOMLSOutput; +#else + using xercesc::DOMWriter; +#endif + using xercesc::XMLUni; + + const XMLCh ls[] = {xercesc::chLatin_L, + xercesc::chLatin_S, + xercesc::chNull}; + + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls)); + + bits::error_handler_proxy<C> ehp (eh); + +#if _XERCES_VERSION >= 30000 + xml::dom::auto_ptr<DOMLSSerializer> writer ( + impl->createLSSerializer ()); + + DOMConfiguration* conf (writer->getDomConfig ()); + + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + + // Set some nice features if the serializer supports them. + // + if (conf->canSetParameter ( + XMLUni::fgDOMWRTDiscardDefaultContent, true)) + conf->setParameter (XMLUni::fgDOMWRTDiscardDefaultContent, true); + + if (!(flags & dont_pretty_print) && + conf->canSetParameter (XMLUni::fgDOMWRTFormatPrettyPrint, true)) + conf->setParameter (XMLUni::fgDOMWRTFormatPrettyPrint, true); + + // See if we need to write XML declaration. + // + if ((flags & no_xml_declaration) && + conf->canSetParameter (XMLUni::fgDOMXMLDeclaration, false)) + conf->setParameter (XMLUni::fgDOMXMLDeclaration, false); + + xml::dom::auto_ptr<DOMLSOutput> out (impl->createLSOutput ()); + + out->setEncoding (xml::string (encoding).c_str ()); + out->setByteStream (&target); + + bool r (writer->write (&doc, out.get ())); +#else + xml::dom::auto_ptr<DOMWriter> writer (impl->createDOMWriter ()); + + writer->setErrorHandler (&ehp); + writer->setEncoding (xml::string (encoding).c_str ()); + + // Set some nice features if the serializer supports them. + // + if (writer->canSetFeature ( + XMLUni::fgDOMWRTDiscardDefaultContent, true)) + writer->setFeature (XMLUni::fgDOMWRTDiscardDefaultContent, true); + + if (!(flags & dont_pretty_print) && + writer->canSetFeature (XMLUni::fgDOMWRTFormatPrettyPrint, true)) + writer->setFeature (XMLUni::fgDOMWRTFormatPrettyPrint, true); + + // See if we need to write XML declaration. + // + if ((flags & no_xml_declaration) && + writer->canSetFeature (XMLUni::fgDOMXMLDeclaration, false)) + writer->setFeature (XMLUni::fgDOMXMLDeclaration, false); + + bool r (writer->writeNode (&target, doc)); +#endif + + if (!r || ehp.failed ()) + return false; + + return true; + } + + template <typename C> + bool + serialize (xercesc::XMLFormatTarget& target, + const xercesc::DOMDocument& doc, + const std::basic_string<C>& enconding, + error_handler<C>& eh, + unsigned long flags) + { + bits::error_handler_proxy<C> ehp (eh); + return serialize (target, doc, enconding, ehp, flags); + } + } + } + } +} diff --git a/xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.hxx b/xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.hxx new file mode 100644 index 0000000..11d7114 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.hxx @@ -0,0 +1,31 @@ +// file : xsd/cxx/xml/dom/wildcard-source.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_XML_DOM_WILDCARD_SOURCE_HXX +#define XSD_CXX_XML_DOM_WILDCARD_SOURCE_HXX + +#include <xercesc/dom/DOMDocument.hpp> + +#include <xsd/cxx/xml/dom/auto-ptr.hxx> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + create_document (); + } + } + } +} + +#include <xsd/cxx/xml/dom/wildcard-source.txx> + +#endif // XSD_CXX_XML_DOM_WILDCARD_SOURCE_HXX diff --git a/xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.txx b/xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.txx new file mode 100644 index 0000000..32043c0 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/xml/dom/wildcard-source.txx @@ -0,0 +1,38 @@ +// file : xsd/cxx/xml/dom/wildcard-source.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <xercesc/util/XMLUniDefs.hpp> // chLatin_L, etc + +#include <xercesc/dom/DOMImplementation.hpp> +#include <xercesc/dom/DOMImplementationRegistry.hpp> + +namespace xsd +{ + namespace cxx + { + namespace xml + { + namespace dom + { + template <typename C> + xml::dom::auto_ptr<xercesc::DOMDocument> + create_document () + { + const XMLCh ls[] = {xercesc::chLatin_L, + xercesc::chLatin_S, + xercesc::chNull}; + + // Get an implementation of the Load-Store (LS) interface. + // + xercesc::DOMImplementation* impl ( + xercesc::DOMImplementationRegistry::getDOMImplementation (ls)); + + return xml::dom::auto_ptr<xercesc::DOMDocument> ( + impl->createDocument ()); + } + } + } + } +} |