aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-08-12 19:48:42 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-08-12 19:48:42 +0200
commit54333e04060f50cb9d8babb40d3469b7a2a7c38e (patch)
tree06542e3db289c222708e2d81773979cd36f63bf2 /examples
parentb25ac8848dea4df378861f518b562df38a77ed91 (diff)
Add support for setting namespace declarations
Diffstat (limited to 'examples')
-rw-r--r--examples/cxx/tree/streaming/serializer.cxx65
-rw-r--r--examples/cxx/tree/streaming/serializer.hxx54
2 files changed, 104 insertions, 15 deletions
diff --git a/examples/cxx/tree/streaming/serializer.cxx b/examples/cxx/tree/streaming/serializer.cxx
index 58bc5cb..f6516f6 100644
--- a/examples/cxx/tree/streaming/serializer.cxx
+++ b/examples/cxx/tree/streaming/serializer.cxx
@@ -24,21 +24,27 @@ namespace tree = xsd::cxx::tree;
class serializer_impl
{
public:
+ typedef serializer::namespace_infomap namespace_infomap;
+
serializer_impl ();
void
start (ostream& os, const string& encoding);
DOMElement*
- create (const string& name);
+ create (const string& name, const namespace_infomap&);
DOMElement*
- create (const string& ns, const string& qname);
+ create (const string& ns, const string& qname, const namespace_infomap&);
void
serialize (DOMElement& e);
private:
+ void
+ add_namespaces (xercesc::DOMElement*, const namespace_infomap&);
+
+private:
// Serializer.
//
#if _XERCES_VERSION >= 30000
@@ -101,16 +107,53 @@ start (ostream& os, const string& encoding)
}
DOMElement* serializer_impl::
-create (const string& name)
+create (const string& name, const namespace_infomap& map)
{
- return doc_->createElement (xml::string (name).c_str ());
+ DOMElement* r (doc_->createElement (xml::string (name).c_str ()));
+
+ if (!map.empty ())
+ add_namespaces (r, map);
+
+ return r;
}
DOMElement* serializer_impl::
-create (const string& ns, const string& qname)
+create (const string& ns, const string& qname, const namespace_infomap& map)
+{
+ DOMElement* r (
+ doc_->createElementNS (
+ xml::string (ns).c_str (), xml::string (qname).c_str ()));
+
+ if (!map.empty ())
+ add_namespaces (r, map);
+
+ return r;
+}
+
+void serializer_impl::
+add_namespaces (DOMElement* e, const namespace_infomap& map)
{
- return doc_->createElementNS (
- xml::string (ns).c_str (), xml::string (qname).c_str ());
+ for (namespace_infomap::const_iterator i (map.begin ()), end (map.end ());
+ i != end; ++i)
+ {
+ if (i->first.empty ())
+ {
+ // Empty prefix.
+ //
+ if (!i->second.name.empty ())
+ e->setAttributeNS (
+ XMLUni::fgXMLNSURIName,
+ xml::string ("xmlns").c_str (),
+ xml::string (i->second.name).c_str ());
+ }
+ else
+ {
+ e->setAttributeNS (
+ XMLUni::fgXMLNSURIName,
+ xml::string ("xmlns:" + i->first).c_str (),
+ xml::string (i->second.name).c_str ());
+ }
+ }
}
void serializer_impl::
@@ -147,15 +190,15 @@ start (ostream& os, const string& encoding)
}
DOMElement* serializer::
-create (const string& name)
+create (const string& name, const namespace_infomap& map)
{
- return impl_->create (name);
+ return impl_->create (name, map);
}
DOMElement* serializer::
-create (const string& ns, const string& qname)
+create (const string& ns, const string& qname, const namespace_infomap& map)
{
- return impl_->create (ns, qname);
+ return impl_->create (ns, qname, map);
}
void serializer::
diff --git a/examples/cxx/tree/streaming/serializer.hxx b/examples/cxx/tree/streaming/serializer.hxx
index 6ff0114..48a06bf 100644
--- a/examples/cxx/tree/streaming/serializer.hxx
+++ b/examples/cxx/tree/streaming/serializer.hxx
@@ -12,12 +12,15 @@
#include <xercesc/dom/DOMElement.hpp>
#include <xsd/cxx/xml/dom/auto-ptr.hxx>
+#include <xsd/cxx/xml/dom/serialization-header.hxx> // namespace_infomap
class serializer_impl;
class serializer
{
public:
+ typedef xsd::cxx::xml::dom::namespace_infomap<char> namespace_infomap;
+
~serializer ();
serializer ();
@@ -34,12 +37,29 @@ public:
next (const std::string& name, const T& x);
// Serialize next object model fragment into an element with the specified
+ // name and namespace declarations.
+ //
+ template <typename T>
+ void
+ next (const std::string& name, const namespace_infomap&, const T& x);
+
+ // Serialize next object model fragment into an element with the specified
// namespace and qualified name.
//
template <typename T>
void
next (const std::string& ns, const std::string& name, const T& x);
+ // Serialize next object model fragment into an element with the specified
+ // namespace and qualified name as well as namespace declarations.
+ //
+ template <typename T>
+ void
+ next (const std::string& ns,
+ const std::string& name,
+ const namespace_infomap&,
+ const T& x);
+
private:
serializer (const serializer&);
@@ -48,10 +68,12 @@ private:
private:
xercesc::DOMElement*
- create (const std::string& name);
+ create (const std::string& name, const namespace_infomap&);
xercesc::DOMElement*
- create (const std::string& ns, const std::string& name);
+ create (const std::string& ns,
+ const std::string& name,
+ const namespace_infomap&);
void
serialize (xercesc::DOMElement&);
@@ -64,7 +86,17 @@ template <typename T>
inline void serializer::
next (const std::string& name, const T& x)
{
- xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (name));
+ xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (
+ create (name, namespace_infomap ()));
+ *e << x;
+ serialize (*e);
+}
+
+template <typename T>
+inline void serializer::
+next (const std::string& name, const namespace_infomap& map, const T& x)
+{
+ xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (name, map));
*e << x;
serialize (*e);
}
@@ -73,7 +105,21 @@ template <typename T>
inline void serializer::
next (const std::string& ns, const std::string& name, const T& x)
{
- xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (ns, name));
+ xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (
+ create (ns, name, namespace_infomap ()));
+
+ *e << x;
+ serialize (*e);
+}
+
+template <typename T>
+inline void serializer::
+next (const std::string& ns,
+ const std::string& name,
+ const namespace_infomap& map,
+ const T& x)
+{
+ xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (ns, name, map));
*e << x;
serialize (*e);
}