summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/messaging
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/messaging
parent7420f85ea19b0562ffdd8123442f32bc8bac1267 (diff)
Switch to build2
Diffstat (limited to 'xsd-examples/cxx/tree/messaging')
-rw-r--r--xsd-examples/cxx/tree/messaging/.gitignore1
-rw-r--r--xsd-examples/cxx/tree/messaging/README58
-rw-r--r--xsd-examples/cxx/tree/messaging/balance.xml16
-rw-r--r--xsd-examples/cxx/tree/messaging/buildfile29
-rw-r--r--xsd-examples/cxx/tree/messaging/deposit.xml17
-rw-r--r--xsd-examples/cxx/tree/messaging/dom-parse.cxx93
-rw-r--r--xsd-examples/cxx/tree/messaging/dom-parse.hxx22
-rw-r--r--xsd-examples/cxx/tree/messaging/dom-serialize.cxx64
-rw-r--r--xsd-examples/cxx/tree/messaging/dom-serialize.hxx20
-rw-r--r--xsd-examples/cxx/tree/messaging/driver.cxx146
-rw-r--r--xsd-examples/cxx/tree/messaging/protocol.xsd53
-rw-r--r--xsd-examples/cxx/tree/messaging/testscript6
-rw-r--r--xsd-examples/cxx/tree/messaging/withdraw.xml17
13 files changed, 542 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/messaging/.gitignore b/xsd-examples/cxx/tree/messaging/.gitignore
new file mode 100644
index 0000000..f493b6c
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/.gitignore
@@ -0,0 +1 @@
+protocol.?xx
diff --git a/xsd-examples/cxx/tree/messaging/README b/xsd-examples/cxx/tree/messaging/README
new file mode 100644
index 0000000..435a4cf
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/README
@@ -0,0 +1,58 @@
+This example shows how to handle XML vocabularies with multiple
+root elements using the element type and element map features
+of the C++/Tree mapping. The main purpose of element types is
+to distinguish object models with the same root type but with
+different root elements. The element map allows uniform parsing
+and serialization of multiple root elements.
+
+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. Note that some request
+ and response elements are of the same type.
+
+balance.xml
+withdraw.xml
+deposit.xml
+ Sample XML instances for the protocol requests.
+
+protocol.hxx
+protocol.cxx
+ C++ types that represent the given vocabulary. These are
+ generated by the XSD compiler from protocol.xsd. Generation of
+ element types instead of parsing and serialization functions is
+ requested with the --generate-element-type option. Generation of
+ the element map is requested with the --generate-element-map
+ option.
+
+dom-parse.hxx
+dom-parse.cxx
+ Definition and implementation of the parse() function that
+ parses an XML document to a DOM document.
+
+dom-serialize.hxx
+dom-serialize.cxx
+ Definition and implementation of the serialize() function that
+ serializes a DOM document to XML.
+
+driver.cxx
+ Driver for the example. It first calls the above-mentioned parse()
+ function to parse the input file to a DOM document. It then calls
+ the parse() function on the element map to parse the root document
+ element to the object model. The object model is returned as a
+ pointer to xml_schema::element_type which is a common base type for
+ all element types. The driver then determines which request it has
+ received either using RTTI or by comparing the root element names.
+ Once the request type is determined, information about it is printed
+ to STDERR and the corresponding response is created. Finally, the
+ driver serializes the opaque response object to a DOM document
+ using the element map and then serializes this DOM document to
+ STDOUT using the above-mentioned serialize() function.
+
+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/messaging/balance.xml b/xsd-examples/cxx/tree/messaging/balance.xml
new file mode 100644
index 0000000..27c1509
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/balance.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/messaging/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/messaging/buildfile b/xsd-examples/cxx/tree/messaging/buildfile
new file mode 100644
index 0000000..62fadda
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/buildfile
@@ -0,0 +1,29 @@
+# file : cxx/tree/messaging/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 \
+ --generate-element-type \
+ --generate-element-map \
+ --generate-serialization \
+ --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/messaging/deposit.xml b/xsd-examples/cxx/tree/messaging/deposit.xml
new file mode 100644
index 0000000..5be2a0a
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/deposit.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/messaging/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/messaging/dom-parse.cxx b/xsd-examples/cxx/tree/messaging/dom-parse.cxx
new file mode 100644
index 0000000..3ae5787
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/dom-parse.cxx
@@ -0,0 +1,93 @@
+// file : cxx/tree/messaging/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/messaging/dom-parse.hxx b/xsd-examples/cxx/tree/messaging/dom-parse.hxx
new file mode 100644
index 0000000..b73c52d
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/dom-parse.hxx
@@ -0,0 +1,22 @@
+// file : cxx/tree/messaging/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/messaging/dom-serialize.cxx b/xsd-examples/cxx/tree/messaging/dom-serialize.cxx
new file mode 100644
index 0000000..b32c5a2
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/dom-serialize.cxx
@@ -0,0 +1,64 @@
+// file : cxx/tree/messaging/dom-serialize.cxx
+// copyright : not copyrighted - public domain
+
+#include "dom-serialize.hxx"
+
+#include <ostream>
+
+#include <xercesc/dom/DOM.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
+
+#include <xsd/cxx/xml/string.hxx>
+#include <xsd/cxx/xml/dom/auto-ptr.hxx>
+#include <xsd/cxx/xml/dom/serialization-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;
+
+void
+serialize (std::ostream& os,
+ const xercesc::DOMDocument& doc,
+ const std::string& encoding)
+{
+ const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};
+
+ // Get an implementation of the Load-Store (LS) interface.
+ //
+ DOMImplementation* impl (
+ DOMImplementationRegistry::getDOMImplementation (ls_id));
+
+ tree::error_handler<char> eh;
+ xml::dom::bits::error_handler_proxy<char> ehp (eh);
+
+ xml::dom::ostream_format_target oft (os);
+
+ // Create a DOMSerializer.
+ //
+ xml::dom::unique_ptr<DOMLSSerializer> writer (
+ impl->createLSSerializer ());
+
+ DOMConfiguration* conf (writer->getDomConfig ());
+
+ // Set error handler.
+ //
+ conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp);
+
+ // Set some generally nice features.
+ //
+ conf->setParameter (XMLUni::fgDOMWRTDiscardDefaultContent, true);
+ conf->setParameter (XMLUni::fgDOMWRTFormatPrettyPrint, true);
+ conf->setParameter (XMLUni::fgDOMWRTXercesPrettyPrint, false);
+
+ xml::dom::unique_ptr<DOMLSOutput> out (impl->createLSOutput ());
+ out->setEncoding (xml::string (encoding).c_str ());
+ out->setByteStream (&oft);
+
+ writer->write (&doc, out.get ());
+
+ eh.throw_if_failed<tree::serialization<char> > ();
+}
diff --git a/xsd-examples/cxx/tree/messaging/dom-serialize.hxx b/xsd-examples/cxx/tree/messaging/dom-serialize.hxx
new file mode 100644
index 0000000..bf1e290
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/dom-serialize.hxx
@@ -0,0 +1,20 @@
+// file : cxx/tree/messaging/dom-serialize.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef DOM_SERIALIZE
+#define DOM_SERIALIZE
+
+#include <string>
+#include <iosfwd>
+
+#include <xercesc/dom/DOMDocument.hpp>
+
+// Serialize a DOM document to XML which is written to the standard
+// output stream.
+//
+void
+serialize (std::ostream& os,
+ const xercesc::DOMDocument& doc,
+ const std::string& encoding = "UTF-8");
+
+#endif // DOM_SERIALIZE
diff --git a/xsd-examples/cxx/tree/messaging/driver.cxx b/xsd-examples/cxx/tree/messaging/driver.cxx
new file mode 100644
index 0000000..a358014
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/driver.cxx
@@ -0,0 +1,146 @@
+// file : cxx/tree/messaging/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <string>
+#include <fstream>
+#include <typeinfo>
+#include <iostream>
+
+#include <xercesc/dom/DOM.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+
+#include <xsd/cxx/xml/string.hxx>
+
+#include "dom-parse.hxx"
+#include "dom-serialize.hxx"
+
+#include "protocol.hxx"
+
+using namespace std;
+using namespace protocol;
+using namespace xercesc;
+
+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.
+ //
+ XMLPlatformUtils::Initialize ();
+
+ try
+ {
+ ifstream ifs;
+ ifs.exceptions (ifstream::badbit | ifstream::failbit);
+ ifs.open (argv[1]);
+
+ unique_ptr<xml_schema::element_type> req, res;
+
+ // Parse the XML request to a DOM document using the parse()
+ // function from dom-parse.hxx.
+ //
+ {
+ xml_schema::dom::unique_ptr<DOMDocument> doc (
+ parse (ifs, argv[1], true));
+
+ DOMElement& root (*doc->getDocumentElement ());
+
+ req = xml_schema::element_map::parse (root);
+ }
+
+ // We can test which request we've got either using RTTI or by
+ // comparing the element names, as shown below.
+ //
+ if (balance* b = dynamic_cast<balance*> (req.get ()))
+ {
+ account_t& a (b->value ());
+
+ cerr << "balance request for acc# " << a.account () << endl;
+
+ res.reset (new success (balance_t (a.account (), 1000)));
+ }
+ else if (req->_name () == withdraw::name ())
+ {
+ withdraw& w (static_cast<withdraw&> (*req));
+ change_t& c (w.value ());
+
+ wcerr << "withdrawal request for acc# " << c.account () << ", "
+ << "amount: " << c.amount () << endl;
+
+ if (c.amount () > 1000)
+ res.reset (new insufficient_funds (balance_t (c.account (), 1000)));
+ else
+ res.reset (new success (balance_t (c.account (), 1000 - c.amount ())));
+
+ }
+ else if (req->_name () == deposit::name ())
+ {
+ deposit& d (static_cast<deposit&> (*req));
+ change_t& c (d.value ());
+
+ wcerr << "deposit request for acc# " << c.account () << ", "
+ << "amount: " << c.amount () << endl;
+
+ res.reset (new success (balance_t (c.account (), 1000 + c.amount ())));
+ }
+
+ // Serialize the response to a DOM document.
+ //
+ namespace xml = xsd::cxx::xml;
+
+ const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};
+
+ DOMImplementation* impl (
+ DOMImplementationRegistry::getDOMImplementation (ls_id));
+
+ const string& name (res->_name ());
+ const string& ns (res->_namespace ());
+
+ xml_schema::dom::unique_ptr<DOMDocument> doc (
+ impl->createDocument (
+ xml::string (ns).c_str (),
+ xml::string ("p:" + name).c_str (),
+ 0));
+
+ xml_schema::element_map::serialize (*doc->getDocumentElement (), *res);
+
+ // Serialize the DOM document to XML using the serialize() function
+ // from dom-serialize.hxx.
+ //
+ cout << "response:" << endl
+ << endl;
+
+ serialize (cout, *doc);
+ }
+ catch (const xml_schema::no_element_info& e)
+ {
+ // This exception indicates that we tried to parse or serialize
+ // an unknown element.
+ //
+ cerr << "unknown request: " << e.element_namespace () << "#" <<
+ e.element_name () << endl;
+ r = 1;
+ }
+ 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;
+ }
+
+ XMLPlatformUtils::Terminate ();
+ return r;
+}
diff --git a/xsd-examples/cxx/tree/messaging/protocol.xsd b/xsd-examples/cxx/tree/messaging/protocol.xsd
new file mode 100644
index 0000000..4a28ac3
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/protocol.xsd
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/messaging/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">
+
+ <!-- types -->
+
+ <xsd:complexType name="account_t">
+ <xsd:sequence>
+ <xsd:element name="account" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="change_t">
+ <xsd:complexContent>
+ <xsd:extension base="p:account_t">
+ <xsd:sequence>
+ <xsd:element name="amount" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="balance_t">
+ <xsd:complexContent>
+ <xsd:extension base="p:account_t">
+ <xsd:sequence>
+ <xsd:element name="balance" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <!-- request elements -->
+
+ <xsd:element name="balance" type="p:account_t"/>
+ <xsd:element name="withdraw" type="p:change_t"/>
+ <xsd:element name="deposit" type="p:change_t"/>
+
+ <!-- response elements -->
+
+ <xsd:element name="success" type="p:balance_t"/>
+ <xsd:element name="insufficient-funds" type="p:balance_t"/>
+
+</xsd:schema>
diff --git a/xsd-examples/cxx/tree/messaging/testscript b/xsd-examples/cxx/tree/messaging/testscript
new file mode 100644
index 0000000..c9df49f
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/testscript
@@ -0,0 +1,6 @@
+# file : cxx/tree/messaging/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/messaging/withdraw.xml b/xsd-examples/cxx/tree/messaging/withdraw.xml
new file mode 100644
index 0000000..e9ef526
--- /dev/null
+++ b/xsd-examples/cxx/tree/messaging/withdraw.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/messaging/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>