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/.gitignore1
-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/buildfile26
-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/protocol.xsd50
-rw-r--r--xsd-examples/cxx/tree/multiroot/testscript6
-rw-r--r--xsd-examples/cxx/tree/multiroot/withdraw.xml17
11 files changed, 417 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/multiroot/.gitignore b/xsd-examples/cxx/tree/multiroot/.gitignore
new file mode 100644
index 0000000..f493b6c
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/.gitignore
@@ -0,0 +1 @@
+protocol.?xx
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/buildfile b/xsd-examples/cxx/tree/multiroot/buildfile
new file mode 100644
index 0000000..dc10588
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/buildfile
@@ -0,0 +1,26 @@
+# file : cxx/tree/multiroot/buildfile
+# license : not copyrighted - public domain
+
+import libs = libxsd%lib{xsd}
+import libs += libxerces-c%lib{xerces-c}
+
+./: exe{driver} xml{balance deposit withdraw} doc{README}
+
+exe{driver}: {hxx cxx}{* -protocol} {hxx ixx cxx}{protocol} $libs testscript
+
+<{hxx ixx cxx}{protocol}>: xsd{protocol} $xsd
+{{
+ diag xsd ($<[0]) # @@ TMP
+
+ $xsd cxx-tree --std c++11 \
+ --generate-inline \
+ --root-element-all \
+ --output-dir $out_base \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Define XSD_CXX11 since we include libxsd headers directly.
+#
+cxx.poptions += -DXSD_CXX11
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..e67c187
--- /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::unique_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::unique_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::unique_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..3699a77
--- /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::unique_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..d22c25e
--- /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::unique_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.
+//
+unique_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::unique_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 ()));
+
+ unique_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]);
+
+ unique_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/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/testscript b/xsd-examples/cxx/tree/multiroot/testscript
new file mode 100644
index 0000000..383bed4
--- /dev/null
+++ b/xsd-examples/cxx/tree/multiroot/testscript
@@ -0,0 +1,6 @@
+# file : cxx/tree/multiroot/testscript
+# license : not copyrighted - public domain
+
+$* $src_base/balance.xml 2>| : balance
+$* $src_base/withdraw.xml 2>| : withdraw
+$* $src_base/deposit.xml 2>| : deposit
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>