summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/mixed
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-12-18 18:48:46 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-01-13 22:32:43 +0300
commit2615896faa646e5830abf2c269150e1165c66515 (patch)
tree7d95978ec0a83094c9462ed4e1f59d42f4ff8ddb /xsd-examples/cxx/tree/mixed
parent7420f85ea19b0562ffdd8123442f32bc8bac1267 (diff)
Switch to build2
Diffstat (limited to 'xsd-examples/cxx/tree/mixed')
-rw-r--r--xsd-examples/cxx/tree/mixed/README45
-rw-r--r--xsd-examples/cxx/tree/mixed/driver.cxx122
-rw-r--r--xsd-examples/cxx/tree/mixed/makefile101
-rw-r--r--xsd-examples/cxx/tree/mixed/text.xml17
-rw-r--r--xsd-examples/cxx/tree/mixed/text.xsd28
5 files changed, 313 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/mixed/README b/xsd-examples/cxx/tree/mixed/README
new file mode 100644
index 0000000..fc23faa
--- /dev/null
+++ b/xsd-examples/cxx/tree/mixed/README
@@ -0,0 +1,45 @@
+This example shows how to access the underlying DOM nodes in the
+C++/Tree mapping in order to handle raw, "type-less content" such
+as mixed content models, anyType/anySimpleType, and any/anyAttribute.
+
+For an alternative (and recommended) approach that employs ordered
+types see the order/mixed example.
+
+For an alternative approach that employes type customization see
+examples in the custom/ directory, in particular, custom/mixed and
+custom/wildcard.
+
+In this example we use mixed content model to describe text with
+embedded links, e.g.,
+
+ This paragraph talks about <a href="uri">time</a>.
+
+The example transforms such text into plain text with references, e.g.,
+
+ This paragraph talks about time[0].
+
+ [0] uri
+
+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 and a set of parsing
+ functions that convert XML instance documents to a tree-like in-memory
+ object model. These are generated by XSD from text.xsd.
+
+driver.cxx
+ Driver for the example. It first calls one of the parsing functions
+ that constructs the object model from the input file. It then uses
+ both the underlying DOM and statically-typed mapping to perform the
+ transformation.
+
+To run the example on the sample XML instance document simply execute:
+
+$ ./driver text.xml
diff --git a/xsd-examples/cxx/tree/mixed/driver.cxx b/xsd-examples/cxx/tree/mixed/driver.cxx
new file mode 100644
index 0000000..501a886
--- /dev/null
+++ b/xsd-examples/cxx/tree/mixed/driver.cxx
@@ -0,0 +1,122 @@
+// file : cxx/tree/mixed/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <iostream>
+
+#include <xercesc/dom/DOM.hpp>
+
+#include "text.hxx"
+
+// The following transcode() utility function is handy when working with
+// Xerces. Include it after the generated header in order to get only char
+// or wchar_t version depending on how you compiled your schemas.
+//
+#include <xsd/cxx/xml/string.hxx>
+
+using std::cout;
+using std::cerr;
+using std::endl;
+using std::auto_ptr;
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " text.xml" << endl;
+ return 1;
+ }
+
+ using namespace xercesc;
+
+ int r (0);
+
+ // The Xerces-C++ DOM objects that will be associated with the
+ // document tree "out-live" the call to the parsing function.
+ // Therefore we need to initialize the Xerces-C++ runtime
+ // ourselves.
+ //
+ XMLPlatformUtils::Initialize ();
+
+ try
+ {
+ auto_ptr<text> t (
+ text_ (argv[1],
+ xml_schema::flags::keep_dom |
+ xml_schema::flags::dont_initialize));
+
+ // The DOM association can be recreated in a copy (the underlying
+ // DOM document is cloned) if explicitly requested with the keep_dom
+ // flag and only if this copy is "complete", i.e., made from the root
+ // of the tree.
+ //
+ text copy (*t, xml_schema::flags::keep_dom);
+
+ // Print text.
+ //
+ {
+ namespace xml = xsd::cxx::xml;
+
+ unsigned long ref (0);
+ DOMNode* root (copy._node ());
+
+ for (DOMNode* n (root->getFirstChild ());
+ n != 0;
+ n = n->getNextSibling ())
+ {
+ switch (n->getNodeType ())
+ {
+ case DOMNode::TEXT_NODE:
+ {
+ cout << xml::transcode<char> (n->getTextContent ());
+ break;
+ }
+ case DOMNode::ELEMENT_NODE:
+ {
+ // Let's get back to a tree node from this DOM node.
+ //
+ xml_schema::type& t (
+ *reinterpret_cast<xml_schema::type*> (
+ n->getUserData (xml_schema::dom::tree_node_key)));
+
+ anchor& a (dynamic_cast<anchor&> (t));
+
+ cout << a << "[" << ref << "]";
+
+ // Or we could continue using DOM interface:
+ //
+ //cout << xml::transcode<char> (n->getTextContent ())
+ // << "[" << ref << "]";
+
+ ++ref;
+ break;
+ }
+ default:
+ break; // Ignore all other nodes (e.g., comments, etc).
+ }
+ }
+ }
+
+ // Print references.
+ //
+ {
+ unsigned long r (0);
+
+ for (text::a_const_iterator i (copy.a ().begin ());
+ i != copy.a ().end ();
+ ++i, ++r)
+ {
+ cout << "[" << r << "] " << i->href () << endl;
+ }
+ }
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ r = 1;
+ }
+
+ XMLPlatformUtils::Terminate ();
+ return r;
+}
diff --git a/xsd-examples/cxx/tree/mixed/makefile b/xsd-examples/cxx/tree/mixed/makefile
new file mode 100644
index 0000000..91521cd
--- /dev/null
+++ b/xsd-examples/cxx/tree/mixed/makefile
@@ -0,0 +1,101 @@
+# file : cxx/tree/mixed/makefile
+# license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make
+
+xsd := text.xsd
+cxx := driver.cxx
+
+obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=.o))
+dep := $(obj:.o=.o.d)
+
+driver := $(out_base)/driver
+install := $(out_base)/.install
+dist := $(out_base)/.dist
+dist-win := $(out_base)/.dist-win
+clean := $(out_base)/.clean
+
+
+# Import.
+#
+$(call import,\
+ $(scf_root)/import/libxerces-c/stub.make,\
+ l: xerces_c.l,cpp-options: xerces_c.l.cpp-options)
+
+
+# Build.
+#
+$(driver): $(obj) $(xerces_c.l)
+
+$(obj) $(dep): cpp_options := -I$(out_base) -I$(src_base) -I$(src_root)/libxsd
+$(obj) $(dep): $(xerces_c.l.cpp-options)
+
+genf := $(xsd:.xsd=.hxx) $(xsd:.xsd=.ixx) $(xsd:.xsd=.cxx)
+gen := $(addprefix $(out_base)/,$(genf))
+
+$(gen): xsd := $(out_root)/xsd/xsd
+$(gen): $(out_root)/xsd/xsd
+
+$(call include-dep,$(dep),$(obj),$(gen))
+
+
+# Convenience alias for default target.
+#
+$(out_base)/: $(driver)
+
+
+# Install & Dist.
+#
+dist-common := $(out_base)/.dist-common
+
+$(install) $(dist) $(dist-win) $(dist-common): path := $(subst $(src_root)/,,$(src_base))
+
+$(install):
+ $(call install-data,$(src_base)/README,$(install_doc_dir)/xsd/$(path)/README)
+ $(call install-data,$(src_base)/driver.cxx,$(install_doc_dir)/xsd/$(path)/driver.cxx)
+ $(call install-data,$(src_base)/text.xsd,$(install_doc_dir)/xsd/$(path)/text.xsd)
+ $(call install-data,$(src_base)/text.xml,$(install_doc_dir)/xsd/$(path)/text.xml)
+
+$(dist-common):
+ $(call install-data,$(src_base)/driver.cxx,$(dist_prefix)/$(path)/driver.cxx)
+ $(call install-data,$(src_base)/text.xsd,$(dist_prefix)/$(path)/text.xsd)
+ $(call install-data,$(src_base)/text.xml,$(dist_prefix)/$(path)/text.xml)
+
+$(dist): $(dist-common)
+ $(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README)
+
+$(dist-win): $(dist-common)
+ $(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README.txt)
+ $(call message,,todos $(dist_prefix)/$(path)/README.txt)
+
+# Clean.
+#
+$(clean): $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(obj)) \
+ $(addsuffix .cxx.clean,$(dep)) \
+ $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean))
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(gen): | $(out_base)/.gitignore
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver $(genf)
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+# How to.
+#
+$(call include,$(bld_root)/cxx/o-e.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(bld_root)/install.make)
+$(call include,$(scf_root)/xsd/tree/xsd-cxx.make)
+
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsd/makefile)
diff --git a/xsd-examples/cxx/tree/mixed/text.xml b/xsd-examples/cxx/tree/mixed/text.xml
new file mode 100644
index 0000000..dfe730e
--- /dev/null
+++ b/xsd-examples/cxx/tree/mixed/text.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/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/mixed/text.xsd b/xsd-examples/cxx/tree/mixed/text.xsd
new file mode 100644
index 0000000..f3c023d
--- /dev/null
+++ b/xsd-examples/cxx/tree/mixed/text.xsd
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/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>