From a1bc52f9ae499a672b05b3264f82a24637a16a02 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 4 Jul 2014 11:12:32 +0200 Subject: Factor out namespace declaration and schema location code --- libxsd/xsd/cxx/xml/dom/serialization-source.hxx | 6 ++ libxsd/xsd/cxx/xml/dom/serialization-source.txx | 115 +++++++++++++----------- 2 files changed, 69 insertions(+), 52 deletions(-) diff --git a/libxsd/xsd/cxx/xml/dom/serialization-source.hxx b/libxsd/xsd/cxx/xml/dom/serialization-source.hxx index a1c9c3a..9056917 100644 --- a/libxsd/xsd/cxx/xml/dom/serialization-source.hxx +++ b/libxsd/xsd/cxx/xml/dom/serialization-source.hxx @@ -46,6 +46,12 @@ namespace xsd xercesc::DOMElement& create_element (const C* name, const C* ns, xercesc::DOMElement&); + // Add namespace declarations and schema locations. + // + template + void + add_namespaces (xercesc::DOMElement&, const namespace_infomap&); + // Serialization flags. // const unsigned long no_xml_declaration = 0x00010000UL; diff --git a/libxsd/xsd/cxx/xml/dom/serialization-source.txx b/libxsd/xsd/cxx/xml/dom/serialization-source.txx index f394b60..eed1196 100644 --- a/libxsd/xsd/cxx/xml/dom/serialization-source.txx +++ b/libxsd/xsd/cxx/xml/dom/serialization-source.txx @@ -105,15 +105,10 @@ namespace xsd return *e; } - - // - // template - XSD_DOM_AUTO_PTR - serialize (const std::basic_string& el, - const std::basic_string& ns, - const namespace_infomap& map, - unsigned long) + void + add_namespaces (xercesc::DOMElement& el, + const namespace_infomap& map) { using namespace xercesc; @@ -123,45 +118,6 @@ namespace xsd 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 (); - } - - const XMLCh ls[] = {xercesc::chLatin_L, - xercesc::chLatin_S, - xercesc::chNull}; - - DOMImplementation* impl ( - DOMImplementationRegistry::getDOMImplementation (ls)); - - XSD_DOM_AUTO_PTR 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); @@ -203,14 +159,14 @@ namespace xsd // Empty prefix. // if (!i->second.name.empty ()) - root->setAttributeNS ( + el.setAttributeNS ( xercesc::XMLUni::fgXMLNSURIName, xml::string (xmlns_prefix).c_str (), xml::string (i->second.name).c_str ()); } else { - root->setAttributeNS ( + el.setAttributeNS ( xercesc::XMLUni::fgXMLNSURIName, xml::string (xmlns_prefix + colon + i->first).c_str (), xml::string (i->second.name).c_str ()); @@ -222,7 +178,7 @@ namespace xsd // if (xsi) xsi_prefix = dom::prefix (xml::bits::xsi_namespace (), - *root, + el, xml::bits::xsi_prefix ()); // Create xsi:schemaLocation and xsi:noNamespaceSchemaLocation @@ -254,7 +210,7 @@ namespace xsd if (!schema_location.empty ()) { - root->setAttributeNS ( + el.setAttributeNS ( xercesc::SchemaSymbols::fgURI_XSI, xml::string (xsi_prefix + colon + xml::bits::schema_location ()).c_str (), @@ -263,13 +219,68 @@ namespace xsd if (!no_namespace_schema_location.empty ()) { - root->setAttributeNS ( + el.setAttributeNS ( xercesc::SchemaSymbols::fgURI_XSI, xml::string ( xsi_prefix + colon + xml::bits::no_namespace_schema_location ()).c_str (), xml::string (no_namespace_schema_location).c_str ()); } + } + + // + // + template + XSD_DOM_AUTO_PTR + serialize (const std::basic_string& el, + const std::basic_string& ns, + const namespace_infomap& map, + unsigned long) + { + using namespace xercesc; + + typedef std::basic_string string; + typedef namespace_infomap infomap; + typedef typename infomap::const_iterator infomap_iterator; + + 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 (); + } + + const XMLCh ls[] = {xercesc::chLatin_L, + xercesc::chLatin_S, + xercesc::chNull}; + + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls)); + + XSD_DOM_AUTO_PTR doc ( + impl->createDocument ( + (ns.empty () ? 0 : xml::string (ns).c_str ()), + xml::string ((prefix.empty () + ? el + : prefix + C (':') + el)).c_str (), + 0)); + + add_namespaces (*doc->getDocumentElement (), map); return doc; } -- cgit v1.1