summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/multiroot
diff options
context:
space:
mode:
Diffstat (limited to 'xsd-examples/cxx/tree/multiroot')
-rw-r--r--xsd-examples/cxx/tree/multiroot/README45
-rw-r--r--xsd-examples/cxx/tree/multiroot/balance.xml16
-rw-r--r--xsd-examples/cxx/tree/multiroot/deposit.xml17
-rw-r--r--xsd-examples/cxx/tree/multiroot/dom-parse.cxx93
-rw-r--r--xsd-examples/cxx/tree/multiroot/dom-parse.hxx22
-rw-r--r--xsd-examples/cxx/tree/multiroot/driver.cxx124
-rw-r--r--xsd-examples/cxx/tree/multiroot/makefile108
-rw-r--r--xsd-examples/cxx/tree/multiroot/protocol.xsd50
-rw-r--r--xsd-examples/cxx/tree/multiroot/withdraw.xml17
9 files changed, 492 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/multiroot/README b/xsd-examples/cxx/tree/multiroot/README
new file mode 100644
index 0000000..b742422
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/README
@@ -0,0 +1,45 @@
+This example shows how to handle XML vocabularies with multiple
+root elements using the C++/Tree mapping.
+
+See also the messaging example for an alternative approach that
+uses the element type and element map features of the C++/Tree
+mapping.
+
+The example consists of the following files:
+
+protocol.xsd
+ XML Schema which defines a simple bank account protocol with
+ requests such as withdraw and deposit.
+
+balance.xml
+withdraw.xml
+deposit.xml
+ Sample XML instances for the protocol requests.
+
+protocol.hxx
+protocol.cxx
+ C++ types that represent the given vocabulary and a set of
+ parsing functions that convert XML documents to a tree-like
+ in-memory object model. These are generated by XSD from
+ protocol.xsd.
+
+dom-parse.hxx
+dom-parse.cxx
+ Definition and implementation of the parse() function that
+ parses an XML document to a DOM document.
+
+driver.cxx
+ Driver for the example. It first calls the above parse() function
+ to parse the input file to a DOM document. It then determines the
+ type of request being handled and calls the corresponding parsing
+ function that constructs the object model from this DOM document.
+ Finally, it prints the content of this object model to STDERR.
+ This example intentionally does not support the deposit request
+ to show how to handle unknown documents.
+
+To run the example on the sample XML request documents simply
+execute:
+
+$ ./driver balance.xml
+$ ./driver withdraw.xml
+$ ./driver deposit.xml
diff --git a/xsd-examples/cxx/tree/multiroot/balance.xml b/xsd-examples/cxx/tree/multiroot/balance.xml
new file mode 100644
index 0000000..5f4a441
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/balance.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/multiroot/balance.xml
+copyright : not copyrighted - public domain
+
+-->
+
+<p:balance xmlns:p="http://www.codesynthesis.com/protocol"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.codesynthesis.com/protocol protocol.xsd">
+
+ <account>123456789</account>
+
+</p:balance>
diff --git a/xsd-examples/cxx/tree/multiroot/deposit.xml b/xsd-examples/cxx/tree/multiroot/deposit.xml
new file mode 100644
index 0000000..9db2a2a
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/deposit.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/multiroot/deposit.xml
+copyright : not copyrighted - public domain
+
+-->
+
+<p:deposit xmlns:p="http://www.codesynthesis.com/protocol"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.codesynthesis.com/protocol protocol.xsd">
+
+ <account>123456789</account>
+ <amount>1000000</amount>
+
+</p:deposit>
diff --git a/xsd-examples/cxx/tree/multiroot/dom-parse.cxx b/xsd-examples/cxx/tree/multiroot/dom-parse.cxx
new file mode 100644
index 0000000..3d04922
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/dom-parse.cxx
@@ -0,0 +1,93 @@
+// file : cxx/tree/multiroot/dom-parse.cxx
+// copyright : not copyrighted - public domain
+
+#include "dom-parse.hxx"
+
+#include <istream>
+
+#include <xercesc/dom/DOM.hpp>
+#include <xercesc/util/XMLUniDefs.hpp> // chLatin_*
+#include <xercesc/framework/Wrapper4InputSource.hpp>
+
+#include <xsd/cxx/xml/sax/std-input-source.hxx>
+#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx>
+
+#include <xsd/cxx/tree/exceptions.hxx>
+#include <xsd/cxx/tree/error-handler.hxx>
+
+using namespace xercesc;
+namespace xml = xsd::cxx::xml;
+namespace tree = xsd::cxx::tree;
+
+xml::dom::auto_ptr<DOMDocument>
+parse (std::istream& is, const std::string& id, bool validate)
+{
+ const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};
+
+ // Get an implementation of the Load-Store (LS) interface.
+ //
+ DOMImplementation* impl (
+ DOMImplementationRegistry::getDOMImplementation (ls_id));
+
+ xml::dom::auto_ptr<DOMLSParser> parser (
+ impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0));
+
+ DOMConfiguration* conf (parser->getDomConfig ());
+
+ // Discard comment nodes in the document.
+ //
+ conf->setParameter (XMLUni::fgDOMComments, false);
+
+ // Enable datatype normalization.
+ //
+ conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true);
+
+ // Do not create EntityReference nodes in the DOM tree. No
+ // EntityReference nodes will be created, only the nodes
+ // corresponding to their fully expanded substitution text
+ // will be created.
+ //
+ conf->setParameter (XMLUni::fgDOMEntities, false);
+
+ // Perform namespace processing.
+ //
+ conf->setParameter (XMLUni::fgDOMNamespaces, true);
+
+ // Do not include ignorable whitespace in the DOM tree.
+ //
+ conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false);
+
+ // Enable/Disable validation.
+ //
+ conf->setParameter (XMLUni::fgDOMValidate, validate);
+ conf->setParameter (XMLUni::fgXercesSchema, validate);
+ conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);
+
+ // Xerces-C++ 3.1.0 is the first version with working multi import
+ // support.
+ //
+#if _XERCES_VERSION >= 30100
+ conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true);
+#endif
+
+ // We will release the DOM document ourselves.
+ //
+ conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true);
+
+ // Set error handler.
+ //
+ tree::error_handler<char> eh;
+ xml::dom::bits::error_handler_proxy<char> ehp (eh);
+ conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp);
+
+ // Prepare input stream.
+ //
+ xml::sax::std_input_source isrc (is, id);
+ Wrapper4InputSource wrap (&isrc, false);
+
+ xml::dom::auto_ptr<DOMDocument> doc (parser->parse (&wrap));
+
+ eh.throw_if_failed<tree::parsing<char> > ();
+
+ return doc;
+}
diff --git a/xsd-examples/cxx/tree/multiroot/dom-parse.hxx b/xsd-examples/cxx/tree/multiroot/dom-parse.hxx
new file mode 100644
index 0000000..b6b9b7b
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/dom-parse.hxx
@@ -0,0 +1,22 @@
+// file : cxx/tree/multiroot/dom-parse.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef DOM_PARSE
+#define DOM_PARSE
+
+#include <string>
+#include <iosfwd>
+
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xsd/cxx/xml/dom/auto-ptr.hxx>
+
+// Parse an XML document from the standard input stream with an
+// optional resource id. Resource id is used in diagnostics as
+// well as to locate schemas referenced from inside the document.
+//
+xsd::cxx::xml::dom::auto_ptr<xercesc::DOMDocument>
+parse (std::istream& is,
+ const std::string& id,
+ bool validate);
+
+#endif // DOM_PARSE
diff --git a/xsd-examples/cxx/tree/multiroot/driver.cxx b/xsd-examples/cxx/tree/multiroot/driver.cxx
new file mode 100644
index 0000000..fb219bd
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/driver.cxx
@@ -0,0 +1,124 @@
+// file : cxx/tree/multiroot/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <string>
+#include <fstream>
+#include <iostream>
+
+#include <xercesc/dom/DOM.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+
+#include <xsd/cxx/xml/string.hxx> // xml::transcode
+
+#include "dom-parse.hxx"
+
+#include "protocol.hxx"
+
+using namespace std;
+using namespace protocol;
+
+// Parse an XML document and return a pointer to request_t which can
+// then be tested with dynamic_cast. If your vocabulary does not have
+// a common base type for all root element types then you can use
+// xml_schema::type which is a base for all generated types.
+//
+auto_ptr<request_t>
+parse (istream& is, const string& id)
+{
+ using namespace xercesc;
+ namespace xml = xsd::cxx::xml;
+
+ // Parse an XML instance to a DOM document using the parse()
+ // function from dom-parse.hxx.
+ //
+ xml_schema::dom::auto_ptr<DOMDocument> doc (parse (is, id, true));
+
+ DOMElement* root (doc->getDocumentElement ());
+
+ string ns (xml::transcode<char> (root->getNamespaceURI ()));
+ string name (xml::transcode<char> (root->getLocalName ()));
+
+ auto_ptr<request_t> r;
+
+ // We could have handled the result directly in this function
+ // instead of returning it as an opaque pointer and using
+ // dynamic_cast later to figure out which request we are dealing
+ // with.
+ //
+ if (ns == "http://www.codesynthesis.com/protocol")
+ {
+ if (name == "balance")
+ {
+ // Use the balance parsing function.
+ //
+ r.reset (balance (*doc).release ());
+ }
+ else if (name == "withdraw")
+ {
+ // Use the withdraw parsing function.
+ //
+ r.reset (withdraw (*doc).release ());
+ }
+ }
+
+ if (r.get () == 0)
+ cerr << "ignoring unknown request: " << ns << "#" << name << endl;
+
+ return r;
+}
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " request.xml" << endl;
+ return 1;
+ }
+
+ int r (0);
+
+ // We need to initialize the Xerces-C++ runtime because we
+ // are doing the XML-to-DOM parsing ourselves.
+ //
+ xercesc::XMLPlatformUtils::Initialize ();
+
+ try
+ {
+ ifstream ifs;
+ ifs.exceptions (ifstream::badbit | ifstream::failbit);
+ ifs.open (argv[1]);
+
+ auto_ptr<request_t> r (parse (ifs, argv[1]));
+
+ // Let's print what we've got.
+ //
+ if (balance_t* b = dynamic_cast<balance_t*> (r.get ()))
+ {
+ cerr << "balance request for acc# " << b->account () << endl;
+ }
+ else if (withdraw_t* w = dynamic_cast<withdraw_t*> (r.get ()))
+ {
+ cerr << "withdrawal request for acc# " << w->account () << ", "
+ << "amount: " << w->amount () << endl;
+ }
+ else
+ {
+ cerr << "unknown request" << endl;
+ }
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ r = 1;
+ }
+ catch (const std::ios_base::failure&)
+ {
+ cerr << argv[1] << ": unable to open or read failure" << endl;
+ r = 1;
+ }
+
+ xercesc::XMLPlatformUtils::Terminate ();
+ return r;
+}
diff --git a/xsd-examples/cxx/tree/multiroot/makefile b/xsd-examples/cxx/tree/multiroot/makefile
new file mode 100644
index 0000000..ccc6f6c
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/makefile
@@ -0,0 +1,108 @@
+# file : cxx/tree/multiroot/makefile
+# license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make
+
+xsd := protocol.xsd
+cxx := driver.cxx dom-parse.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): xsd_options += --root-element-all
+$(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)/protocol.xsd,$(install_doc_dir)/xsd/$(path)/protocol.xsd)
+ $(call install-data,$(src_base)/balance.xml,$(install_doc_dir)/xsd/$(path)/balance.xml)
+ $(call install-data,$(src_base)/deposit.xml,$(install_doc_dir)/xsd/$(path)/deposit.xml)
+ $(call install-data,$(src_base)/withdraw.xml,$(install_doc_dir)/xsd/$(path)/withdraw.xml)
+ $(call install-data,$(src_base)/dom-parse.hxx,$(install_doc_dir)/xsd/$(path)/dom-parse.hxx)
+ $(call install-data,$(src_base)/dom-parse.cxx,$(install_doc_dir)/xsd/$(path)/dom-parse.cxx)
+
+$(dist-common):
+ $(call install-data,$(src_base)/driver.cxx,$(dist_prefix)/$(path)/driver.cxx)
+ $(call install-data,$(src_base)/protocol.xsd,$(dist_prefix)/$(path)/protocol.xsd)
+ $(call install-data,$(src_base)/balance.xml,$(dist_prefix)/$(path)/balance.xml)
+ $(call install-data,$(src_base)/deposit.xml,$(dist_prefix)/$(path)/deposit.xml)
+ $(call install-data,$(src_base)/withdraw.xml,$(dist_prefix)/$(path)/withdraw.xml)
+ $(call install-data,$(src_base)/dom-parse.hxx,$(dist_prefix)/$(path)/dom-parse.hxx)
+ $(call install-data,$(src_base)/dom-parse.cxx,$(dist_prefix)/$(path)/dom-parse.cxx)
+
+$(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/multiroot/protocol.xsd b/xsd-examples/cxx/tree/multiroot/protocol.xsd
new file mode 100644
index 0000000..2fe9456
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/protocol.xsd
@@ -0,0 +1,50 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/multiroot/protocol.xsd
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:p="http://www.codesynthesis.com/protocol"
+ targetNamespace="http://www.codesynthesis.com/protocol">
+
+ <xsd:complexType name="request_t">
+ <xsd:sequence>
+ <xsd:element name="account" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="balance_t">
+ <xsd:complexContent>
+ <xsd:extension base="p:request_t"/>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="withdraw_t">
+ <xsd:complexContent>
+ <xsd:extension base="p:request_t">
+ <xsd:sequence>
+ <xsd:element name="amount" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="deposit_t">
+ <xsd:complexContent>
+ <xsd:extension base="p:request_t">
+ <xsd:sequence>
+ <xsd:element name="amount" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:element name="balance" type="p:balance_t"/>
+ <xsd:element name="withdraw" type="p:withdraw_t"/>
+ <xsd:element name="deposit" type="p:deposit_t"/>
+
+</xsd:schema>
diff --git a/xsd-examples/cxx/tree/multiroot/withdraw.xml b/xsd-examples/cxx/tree/multiroot/withdraw.xml
new file mode 100644
index 0000000..f98ad16
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/withdraw.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/multiroot/withdraw.xml
+copyright : not copyrighted - public domain
+
+-->
+
+<p:withdraw xmlns:p="http://www.codesynthesis.com/protocol"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.codesynthesis.com/protocol protocol.xsd">
+
+ <account>123456789</account>
+ <amount>1000000</amount>
+
+</p:withdraw>