summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/polymorphism
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cxx/tree/polymorphism')
-rw-r--r--examples/cxx/tree/polymorphism/README28
-rw-r--r--examples/cxx/tree/polymorphism/driver.cxx60
-rw-r--r--examples/cxx/tree/polymorphism/makefile72
-rw-r--r--examples/cxx/tree/polymorphism/supermen.xml26
-rw-r--r--examples/cxx/tree/polymorphism/supermen.xsd49
5 files changed, 235 insertions, 0 deletions
diff --git a/examples/cxx/tree/polymorphism/README b/examples/cxx/tree/polymorphism/README
new file mode 100644
index 0000000..05d794f
--- /dev/null
+++ b/examples/cxx/tree/polymorphism/README
@@ -0,0 +1,28 @@
+This example shows how to use XML Schema polymorphism features such as
+xsi:type attributes and substitution groups in the C++/Tree mapping.
+
+The example consists of the following files:
+
+supermen.xsd
+ XML Schema which describes the "supermen" instance documents.
+
+supermen.xml
+ Sample XML instance document.
+
+supermen.hxx
+supermen.cxx
+ C++ types that represent the given vocabulary, a set of parsing
+ functions that convert XML instance documents to a tree-like in-memory
+ object model, and a set of serialization functions that convert the
+ object model back to XML. These are generated by XSD from supermen.xsd.
+ Note the use of the --generate-polymorphic command line option.
+
+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 prints
+ the content of the object model to STDERR. Finally, the driver serializes
+ the object model back to XML.
+
+To run the example on the sample XML instance document simply execute:
+
+$ ./driver instance.xml
diff --git a/examples/cxx/tree/polymorphism/driver.cxx b/examples/cxx/tree/polymorphism/driver.cxx
new file mode 100644
index 0000000..582e2dd
--- /dev/null
+++ b/examples/cxx/tree/polymorphism/driver.cxx
@@ -0,0 +1,60 @@
+// file : examples/cxx/tree/polymorphism/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <iostream>
+
+#include "supermen.hxx"
+
+using std::cerr;
+using std::endl;
+using std::auto_ptr;
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " supermen.xml" << endl;
+ return 1;
+ }
+
+ try
+ {
+ auto_ptr<supermen> sm (supermen_ (argv[1]));
+
+ supermen copy (*sm); // Dynamic types are preserved in copies.
+
+ // Print what we've got.
+ //
+ for (supermen::person_const_iterator i (copy.person ().begin ());
+ i != copy.person ().end ();
+ ++i)
+ {
+ cerr << i->name ();
+
+ if (const superman* s = dynamic_cast<const superman*> (&*i))
+ {
+ if (s->can_fly ())
+ cerr << ", flying superman";
+ else
+ cerr << ", superman";
+ }
+
+ cerr << endl;
+ }
+
+ // Serialize back to XML.
+ //
+ xml_schema::namespace_infomap map;
+ map[""].schema = "supermen.xsd";
+
+ supermen_ (std::cout, copy, map);
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ return 1;
+ }
+}
diff --git a/examples/cxx/tree/polymorphism/makefile b/examples/cxx/tree/polymorphism/makefile
new file mode 100644
index 0000000..f4e0b16
--- /dev/null
+++ b/examples/cxx/tree/polymorphism/makefile
@@ -0,0 +1,72 @@
+# file : examples/cxx/tree/polymorphism/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC
+# license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make
+
+xsd := supermen.xsd
+cxx := driver.cxx
+
+obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=.o))
+dep := $(obj:.o=.o.d)
+
+driver := $(out_base)/driver
+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$(src_root)/libxsd
+$(obj) $(dep): $(xerces_c.l.cpp-options)
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): xsd_options := --generate-polymorphic --generate-serialization --root-element-last
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd
+
+$(call include-dep,$(dep))
+
+
+# Convenience alias for default target.
+#
+.PHONY: $(out_base)/
+$(out_base)/: $(driver)
+
+
+# Clean.
+#
+.PHONY: $(clean)
+
+$(clean): $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(obj)) \
+ $(addsuffix .cxx.clean,$(dep)) \
+ $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean))
+
+
+# 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,$(scf_root)/xsd/tree/xsd-cxx.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsd/makefile)
diff --git a/examples/cxx/tree/polymorphism/supermen.xml b/examples/cxx/tree/polymorphism/supermen.xml
new file mode 100644
index 0000000..5053142
--- /dev/null
+++ b/examples/cxx/tree/polymorphism/supermen.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/polymorphism/supermen.xml
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<supermen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="supermen.xsd">
+
+ <person>
+ <name>John Doe</name>
+ </person>
+
+ <superman can-fly="false">
+ <name>James "007" Bond</name>
+ </superman>
+
+ <superman can-fly="true" wing-span="10" xsi:type="batman">
+ <name>Bruce Wayne</name>
+ </superman>
+
+</supermen>
diff --git a/examples/cxx/tree/polymorphism/supermen.xsd b/examples/cxx/tree/polymorphism/supermen.xsd
new file mode 100644
index 0000000..ffdd21c
--- /dev/null
+++ b/examples/cxx/tree/polymorphism/supermen.xsd
@@ -0,0 +1,49 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/polymorphism/supermen.xsd
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+ <xsd:complexType name="person">
+ <xsd:sequence>
+ <xsd:element name="name" type="xsd:string"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <!-- substitution group root -->
+ <xsd:element name="person" type="person"/>
+
+
+ <xsd:complexType name="superman">
+ <xsd:complexContent>
+ <xsd:extension base="person">
+ <xsd:attribute name="can-fly" type="xsd:boolean" use="required"/>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:element name="superman" type="superman" substitutionGroup="person"/>
+
+ <xsd:complexType name="batman">
+ <xsd:complexContent>
+ <xsd:extension base="superman">
+ <xsd:attribute name="wing-span" type="xsd:unsignedInt" use="required"/>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="supermen">
+ <xsd:sequence>
+ <xsd:element ref="person" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:element name="supermen" type="supermen"/>
+
+</xsd:schema>