aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2014-05-08 17:50:12 -0700
committerBoris Kolpackov <boris@codesynthesis.com>2014-05-08 17:50:12 -0700
commitfec681be83c91268ee4db97f34ce4e47179316dd (patch)
treeb59a2bc6a916225a989015a2f33168b8d35b0946
parenta6e24513d89067c16a3df214a7e2679e1f1675f1 (diff)
Add helpers for serializing elements with simple content
-rw-r--r--tests/serializer/driver.cxx36
-rw-r--r--xml/serializer.cxx7
-rw-r--r--xml/serializer.hxx36
-rw-r--r--xml/serializer.ixx49
4 files changed, 128 insertions, 0 deletions
diff --git a/tests/serializer/driver.cxx b/tests/serializer/driver.cxx
index 7e56efc..0f22107 100644
--- a/tests/serializer/driver.cxx
+++ b/tests/serializer/driver.cxx
@@ -63,4 +63,40 @@ main ()
assert (os.str () == "<root version=\"123\">true</root>\n");
}
+
+ // Test helpers for serializing elements with simple content.
+ //
+ {
+ ostringstream os;
+ serializer s (os, "element", 0);
+
+ s.start_element ("root");
+
+ s.start_element ("nested");
+ s.element ("X");
+
+ s.start_element ("nested");
+ s.element (123);
+
+ s.element ("nested", "X");
+ s.element ("nested", 123);
+ s.element ("test", "nested", "X");
+ s.element ("test", "nested", 123);
+ s.element (qname ("test", "nested"), "X");
+ s.element (qname ("test", "nested"), 123);
+
+ s.end_element (); // root
+
+ assert (os.str () ==
+ "<root>"
+ "<nested>X</nested>"
+ "<nested>123</nested>"
+ "<nested>X</nested>"
+ "<nested>123</nested>"
+ "<g1:nested xmlns:g1=\"test\">X</g1:nested>"
+ "<g1:nested xmlns:g1=\"test\">123</g1:nested>"
+ "<g1:nested xmlns:g1=\"test\">X</g1:nested>"
+ "<g1:nested xmlns:g1=\"test\">123</g1:nested>"
+ "</root>\n");
+ }
}
diff --git a/xml/serializer.cxx b/xml/serializer.cxx
index 8147f91..a6afcca 100644
--- a/xml/serializer.cxx
+++ b/xml/serializer.cxx
@@ -173,6 +173,13 @@ namespace xml
}
void serializer::
+ element (const string& ns, const string& n, const string& v)
+ {
+ start_element (ns, n);
+ element (v);
+ }
+
+ void serializer::
start_attribute (const string& ns, const string& name)
{
if (genxStatus e = genxStartAttributeLiteral (
diff --git a/xml/serializer.hxx b/xml/serializer.hxx
index e0041f9..d07221d 100644
--- a/xml/serializer.hxx
+++ b/xml/serializer.hxx
@@ -94,6 +94,42 @@ namespace xml
void
end_element ();
+ // Helpers for serializing elements with simple content. The first two
+ // functions assume that start_element() has already been called. The
+ // other two serialize the complete element, from start to end.
+ //
+ void
+ element (const std::string& value);
+
+ template <typename T>
+ void
+ element (const T& value);
+
+ void
+ element (const std::string& name, const std::string& value);
+
+ template <typename T>
+ void
+ element (const std::string& name, const T& value);
+
+ void
+ element (const qname_type& qname, const std::string& value);
+
+ template <typename T>
+ void
+ element (const qname_type& qname, const T& value);
+
+ void
+ element (const std::string& namespace_,
+ const std::string& name,
+ const std::string& value);
+
+ template <typename T>
+ void
+ element (const std::string& namespace_,
+ const std::string& name,
+ const T& value);
+
// Attributes.
//
void
diff --git a/xml/serializer.ixx b/xml/serializer.ixx
index d329bbd..81631f2 100644
--- a/xml/serializer.ixx
+++ b/xml/serializer.ixx
@@ -19,6 +19,55 @@ namespace xml
}
inline void serializer::
+ element (const std::string& v)
+ {
+ if (!v.empty ())
+ characters (v);
+
+ end_element ();
+ }
+
+ template <typename T>
+ inline void serializer::
+ element (const T& v)
+ {
+ element (value_traits<T>::serialize (v, *this));
+ }
+
+ inline void serializer::
+ element (const std::string& n, const std::string& v)
+ {
+ element (std::string (), n, v);
+ }
+
+ template <typename T>
+ inline void serializer::
+ element (const std::string& n, const T& v)
+ {
+ element (n, value_traits<T>::serialize (v, *this));
+ }
+
+ inline void serializer::
+ element (const qname_type& qn, const std::string& v)
+ {
+ element (qn.namespace_ (), qn.name (), v);
+ }
+
+ template <typename T>
+ inline void serializer::
+ element (const qname_type& qn, const T& v)
+ {
+ element (qn, value_traits<T>::serialize (v, *this));
+ }
+
+ template <typename T>
+ inline void serializer::
+ element (const std::string& ns, const std::string& n, const T& v)
+ {
+ element (ns, n, value_traits<T>::serialize (v, *this));
+ }
+
+ inline void serializer::
start_attribute (const qname_type& qname)
{
start_attribute (qname.namespace_ (), qname.name ());