aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2014-07-04 11:12:32 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2014-07-04 11:12:32 +0200
commita1bc52f9ae499a672b05b3264f82a24637a16a02 (patch)
treee6efa435f63bafa8e7f51e4028b05531c3efde2b
parent1a4099b78717b16f632b0e7e0980a27811221e52 (diff)
Factor out namespace declaration and schema location code
-rw-r--r--libxsd/xsd/cxx/xml/dom/serialization-source.hxx6
-rw-r--r--libxsd/xsd/cxx/xml/dom/serialization-source.txx115
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 <typename C>
+ void
+ add_namespaces (xercesc::DOMElement&, const namespace_infomap<C>&);
+
// 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 <typename C>
- XSD_DOM_AUTO_PTR<xercesc::DOMDocument>
- serialize (const std::basic_string<C>& el,
- const std::basic_string<C>& ns,
- const namespace_infomap<C>& map,
- unsigned long)
+ void
+ add_namespaces (xercesc::DOMElement& el,
+ const namespace_infomap<C>& 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<C> ();
- }
-
- const XMLCh ls[] = {xercesc::chLatin_L,
- xercesc::chLatin_S,
- xercesc::chNull};
-
- DOMImplementation* impl (
- DOMImplementationRegistry::getDOMImplementation (ls));
-
- XSD_DOM_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);
@@ -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<C> (),
- *root,
+ el,
xml::bits::xsi_prefix<C> ());
// 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> ()).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> ()).c_str (),
xml::string (no_namespace_schema_location).c_str ());
}
+ }
+
+ //
+ //
+ template <typename C>
+ XSD_DOM_AUTO_PTR<xercesc::DOMDocument>
+ serialize (const std::basic_string<C>& el,
+ const std::basic_string<C>& ns,
+ const namespace_infomap<C>& map,
+ unsigned long)
+ {
+ using namespace xercesc;
+
+ typedef std::basic_string<C> string;
+ typedef namespace_infomap<C> 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<C> ();
+ }
+
+ const XMLCh ls[] = {xercesc::chLatin_L,
+ xercesc::chLatin_S,
+ xercesc::chNull};
+
+ DOMImplementation* impl (
+ DOMImplementationRegistry::getDOMImplementation (ls));
+
+ XSD_DOM_AUTO_PTR<DOMDocument> 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;
}