summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/order/mixed
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-12-18 18:48:46 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-02-25 13:45:48 +0300
commit5e527213a2430bb3018e5eebd909aef294edf9b5 (patch)
tree94de33c82080b53d9a9e300170f6d221d89078f4 /xsd-examples/cxx/tree/order/mixed
parent7420f85ea19b0562ffdd8123442f32bc8bac1267 (diff)
Switch to build2
Diffstat (limited to 'xsd-examples/cxx/tree/order/mixed')
-rw-r--r--xsd-examples/cxx/tree/order/mixed/.gitignore1
-rw-r--r--xsd-examples/cxx/tree/order/mixed/README45
-rw-r--r--xsd-examples/cxx/tree/order/mixed/buildfile25
-rw-r--r--xsd-examples/cxx/tree/order/mixed/driver.cxx89
-rw-r--r--xsd-examples/cxx/tree/order/mixed/text.xml17
-rw-r--r--xsd-examples/cxx/tree/order/mixed/text.xsd28
6 files changed, 205 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/order/mixed/.gitignore b/xsd-examples/cxx/tree/order/mixed/.gitignore
new file mode 100644
index 0000000..cbeac00
--- /dev/null
+++ b/xsd-examples/cxx/tree/order/mixed/.gitignore
@@ -0,0 +1 @@
+text.?xx
diff --git a/xsd-examples/cxx/tree/order/mixed/README b/xsd-examples/cxx/tree/order/mixed/README
new file mode 100644
index 0000000..e66c1ad
--- /dev/null
+++ b/xsd-examples/cxx/tree/order/mixed/README
@@ -0,0 +1,45 @@
+This example shows how to use ordered types to capture mixed content
+text and to maintain order information between elements and text.
+
+In this example we use mixed content model to describe text with
+embedded links in the form:
+
+ This paragraph talks about <a href="uri">time</a>.
+
+The example transforms such text into plain text with references in
+the form:
+
+ This paragraph talks about time[uri].
+
+It also saves the modified text back to XML in order to verify the
+element and text order.
+
+The example consists of the following files:
+
+text.xsd
+ XML Schema which describes "text with links" instance documents.
+
+text.xml
+ Sample XML instance document.
+
+text.hxx
+text.cxx
+ C++ types that represent the given vocabulary as well as a set of
+ parsing and serialization functions. These are generated by XSD
+ from text.xsd. Note that the --ordered-type-mixed option is used
+ to indicate to the XSD compiler that all types with mixed content
+ should be automatically treated as ordered.
+
+driver.cxx
+ Driver for the example. It first calls one of the parsing functions
+ that constructs the object model from the input XML file. It then
+ iterates over the text and elements in the content order to convert
+ the document to its plain text representation. The driver then adds
+ another paragraph of text and a link to the object model while showing
+ how to maintain the content order. Finally, it saves the modified
+ text back to XML to verify that the content order is preserved in
+ the output document.
+
+To run the example on the sample XML instance document simply execute:
+
+$ ./driver text.xml
diff --git a/xsd-examples/cxx/tree/order/mixed/buildfile b/xsd-examples/cxx/tree/order/mixed/buildfile
new file mode 100644
index 0000000..149bca0
--- /dev/null
+++ b/xsd-examples/cxx/tree/order/mixed/buildfile
@@ -0,0 +1,25 @@
+# file : cxx/tree/order/mixed/buildfile
+# license : not copyrighted - public domain
+
+import libs = libxsd%lib{xsd}
+import libs += libxerces-c%lib{xerces-c}
+
+./: exe{driver} doc{README}
+
+exe{driver}: {hxx cxx}{* -text} {hxx ixx cxx}{text} $libs
+
+exe{driver}: xml{text}: test.input = true
+
+<{hxx ixx cxx}{text}>: xsd{text} $xsd
+{{
+ diag xsd ($<[0]) # @@ TMP
+
+ $xsd cxx-tree --std c++11 \
+ --generate-inline \
+ --generate-serialization \
+ --ordered-type-mixed \
+ --output-dir $out_base \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base"
diff --git a/xsd-examples/cxx/tree/order/mixed/driver.cxx b/xsd-examples/cxx/tree/order/mixed/driver.cxx
new file mode 100644
index 0000000..a18f4ad
--- /dev/null
+++ b/xsd-examples/cxx/tree/order/mixed/driver.cxx
@@ -0,0 +1,89 @@
+// file : cxx/tree/order/mixed/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <cassert>
+#include <iostream>
+
+#include "text.hxx"
+
+using std::cerr;
+using std::endl;
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " text.xml" << endl;
+ return 1;
+ }
+
+ try
+ {
+ std::unique_ptr<text> t (text_ (argv[1]));
+
+ // Print what we've got in content order.
+ //
+ for (text::content_order_const_iterator i (t->content_order ().begin ());
+ i != t->content_order ().end ();
+ ++i)
+ {
+ switch (i->id)
+ {
+ case text::a_id:
+ {
+ const anchor& a (t->a ()[i->index]);
+ cerr << a << "[" << a.href () << "]";
+ break;
+ }
+ case text::text_content_id:
+ {
+ const xml_schema::string& s (t->text_content ()[i->index]);
+ cerr << s;
+ break;
+ }
+ default:
+ {
+ assert (false); // Unknown content id.
+ }
+ }
+ }
+
+ cerr << endl;
+
+ // Modify the document. Note that we have to update both the content
+ // itself and content order sequences.
+ //
+ typedef text::content_order_type order_type;
+
+ text::content_order_sequence& co (t->content_order ());
+ text::text_content_sequence& tc (t->text_content ());
+
+ tc.push_back ("The last paragraph doesn't talk about ");
+ co.push_back (order_type (text::text_content_id, tc.size () - 1));
+
+ t->a ().push_back (anchor ("anything", "http://en.wikipedia.org"));
+ co.push_back (order_type (text::a_id, t->a ().size () - 1));
+
+ tc.push_back (" in particular.\n\n");
+ co.push_back (order_type (text::text_content_id, tc.size () - 1));
+
+ // Serialize the modified document back to XML.
+ //
+ xml_schema::namespace_infomap map;
+
+ map[""].schema = "text.xsd";
+
+ text_ (std::cout,
+ *t,
+ map,
+ "UTF-8",
+ xml_schema::flags::dont_pretty_print);
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ return 1;
+ }
+}
diff --git a/xsd-examples/cxx/tree/order/mixed/text.xml b/xsd-examples/cxx/tree/order/mixed/text.xml
new file mode 100644
index 0000000..3d7476d
--- /dev/null
+++ b/xsd-examples/cxx/tree/order/mixed/text.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/order/mixed/text.xml
+copyright : not copyrighted - public domain
+
+-->
+
+<text xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="text.xsd">
+
+The first paragraph of this text talks about <a href="http://en.wikipedia.org/wiki/time">time</a>.
+
+And this paragraph talks about <a href="http://en.wikipedia.org/wiki/space">space</a>.
+
+</text>
diff --git a/xsd-examples/cxx/tree/order/mixed/text.xsd b/xsd-examples/cxx/tree/order/mixed/text.xsd
new file mode 100644
index 0000000..b55d214
--- /dev/null
+++ b/xsd-examples/cxx/tree/order/mixed/text.xsd
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/order/mixed/text.xsd
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+ <xsd:complexType name="anchor">
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string">
+ <xsd:attribute name="href" type="xsd:anyURI" use="required"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="text" mixed="true">
+ <xsd:sequence>
+ <xsd:element name="a" type="anchor" minOccurs="0" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:element name="text" type="text"/>
+
+</xsd:schema>