summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/parser/multiroot
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/parser/multiroot
parent7420f85ea19b0562ffdd8123442f32bc8bac1267 (diff)
Switch to build2
Diffstat (limited to 'xsd-examples/cxx/parser/multiroot')
-rw-r--r--xsd-examples/cxx/parser/multiroot/README51
-rw-r--r--xsd-examples/cxx/parser/multiroot/balance.xml16
-rw-r--r--xsd-examples/cxx/parser/multiroot/buildfile24
-rw-r--r--xsd-examples/cxx/parser/multiroot/deposit.xml17
-rw-r--r--xsd-examples/cxx/parser/multiroot/driver.cxx161
-rw-r--r--xsd-examples/cxx/parser/multiroot/protocol-pimpl.cxx46
-rw-r--r--xsd-examples/cxx/parser/multiroot/protocol-pimpl.hxx48
-rw-r--r--xsd-examples/cxx/parser/multiroot/protocol.hxx61
-rw-r--r--xsd-examples/cxx/parser/multiroot/protocol.map11
-rw-r--r--xsd-examples/cxx/parser/multiroot/protocol.xsd50
-rw-r--r--xsd-examples/cxx/parser/multiroot/testscript6
-rw-r--r--xsd-examples/cxx/parser/multiroot/withdraw.xml17
12 files changed, 508 insertions, 0 deletions
diff --git a/xsd-examples/cxx/parser/multiroot/README b/xsd-examples/cxx/parser/multiroot/README
new file mode 100644
index 0000000..041dfec
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/README
@@ -0,0 +1,51 @@
+This example shows how to handle XML vocabularies with multiple
+root elements using the C++/Parser 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
+ C++ types that describe the protocol requests. These are
+ hand-written.
+
+protocol.map
+ Type map. It maps XML Schema types defined in protocol.xsd
+ to the C++ types defined in protocol.hxx.
+
+protocol-pskel.hxx
+protocol-pskel.cxx
+ Parser skeletons generated by XSD from protocol.xsd and
+ protocol.map.
+
+protocol-pimpl.hxx
+protocol-pimpl.cxx
+ Parser implementations that construct the custom object
+ model from an XML instance using the types from protocol.hxx.
+ These are hand-written implementations of the parser skeletons
+ defined in protocol-pskel.hxx.
+
+driver.cxx
+ Driver for the example. It implements a custom document parser
+ that determines which request is being parsed and uses the
+ corresponding parser implementation. The document parser
+ intentionally does not support the deposit request to show
+ how to handle unknown documents. The driver first constructs
+ a parser instance from all the individual parsers found in
+ protocol-pimpl.hxx. In then invokes this parser instance to
+ parse the input file and produce the in-memory object model.
+ Finally, it prints the contents of the object model to STDERR.
+
+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/parser/multiroot/balance.xml b/xsd-examples/cxx/parser/multiroot/balance.xml
new file mode 100644
index 0000000..60a6882
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/balance.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/parser/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/parser/multiroot/buildfile b/xsd-examples/cxx/parser/multiroot/buildfile
new file mode 100644
index 0000000..6b0f604
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/buildfile
@@ -0,0 +1,24 @@
+# file : cxx/parser/multiroot/buildfile
+# license : not copyrighted - public domain
+
+import libs = libxsd%lib{xsd}
+import libs += libxerces-c%lib{xerces-c}
+
+./: exe{driver} doc{README} xml{balance deposit withdraw}
+
+exe{driver}: {hxx cxx}{* -protocol-pskel} {hxx ixx cxx}{protocol-pskel} $libs \
+ testscript
+
+<{hxx ixx cxx}{protocol-pskel}>: xsd{protocol} map{protocol} $xsd
+{{
+ diag xsd ($<[0]) # @@ TMP
+
+ $xsd cxx-parser --std c++11 \
+ --generate-inline \
+ --skel-file-suffix -pskel \
+ --type-map $path($<[1]) \
+ --output-dir $out_base \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
diff --git a/xsd-examples/cxx/parser/multiroot/deposit.xml b/xsd-examples/cxx/parser/multiroot/deposit.xml
new file mode 100644
index 0000000..3d32c59
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/deposit.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/parser/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/parser/multiroot/driver.cxx b/xsd-examples/cxx/parser/multiroot/driver.cxx
new file mode 100644
index 0000000..ea854b9
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/driver.cxx
@@ -0,0 +1,161 @@
+// file : cxx/parser/multiroot/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include "protocol.hxx"
+#include "protocol-pimpl.hxx"
+
+using std::cerr;
+using std::endl;
+using xml_schema::ro_string;
+
+namespace protocol
+{
+ // Customize the xml_schema::document object to handle our protocol
+ // vocabulary with multiple root elements.
+ //
+ class document: public xml_schema::document
+ {
+ public:
+ document (balance_pskel& balance_p, withdraw_pskel& withdraw_p)
+ : balance_p_ (balance_p), withdraw_p_ (withdraw_p)
+ {
+ }
+
+ request*
+ result ()
+ {
+ return result_.release ();
+ }
+
+ protected:
+ // This function is called to obtain the root element type parser.
+ // If the returned pointed is 0 then the whole document content
+ // is ignored. The type argument is used to handle polymorphic
+ // XML documents and is not used in this example (see the polyroot
+ // example for more information on this argument).
+ //
+ virtual xml_schema::parser_base*
+ start_root_element (const ro_string& ns,
+ const ro_string& name,
+ const ro_string* /* type */)
+ {
+ if (ns == "http://www.codesynthesis.com/protocol")
+ {
+ if (name == "balance")
+ {
+ balance_p_.pre ();
+
+ return &balance_p_;
+ }
+ else if (name == "withdraw")
+ {
+ balance_p_.pre ();
+
+ return &withdraw_p_;
+ }
+ }
+
+ cerr << "ignoring unknown request: " << ns << "#" << name << endl;
+
+ return 0;
+ }
+
+ // This function is called to indicate the completion of document
+ // parsing. The parser argument contains the pointer returned by
+ // start_root_element.
+ //
+ virtual void
+ end_root_element (const ro_string& /* ns */,
+ const ro_string& /* name */,
+ xml_schema::parser_base* parser)
+ {
+ // We could have handled the result directly in this function
+ // instead of storing it in the result_ variable.
+ //
+ if (parser == &balance_p_)
+ {
+ result_.reset (balance_p_.post_balance ());
+ }
+ else if (parser == &withdraw_p_)
+ {
+ result_.reset (withdraw_p_.post_withdraw ());
+ }
+ else
+ result_.reset (0);
+ }
+
+
+ private:
+ std::unique_ptr<request> result_;
+
+ balance_pskel& balance_p_;
+ withdraw_pskel& withdraw_p_;
+ };
+}
+
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " request.xml" << endl;
+ return 1;
+ }
+
+ try
+ {
+ using namespace protocol;
+
+ // Construct the parser.
+ //
+ xml_schema::unsigned_int_pimpl unsigned_int_p;
+
+ balance_pimpl balance_p;
+ withdraw_pimpl withdraw_p;
+
+ balance_p.parsers (unsigned_int_p); // account
+
+ withdraw_p.parsers (unsigned_int_p, // account
+ unsigned_int_p); // amount
+
+ // Parse the XML instance document.
+ //
+ document doc_p (balance_p, withdraw_p);
+
+ // pre() and post() will be called as part of the start_root_element()
+ // and end_root_element() calls.
+ //
+ doc_p.parse (argv[1]);
+ std::unique_ptr<request> r (doc_p.result ());
+
+ // Let's print what we've got.
+ //
+ if (balance* b = dynamic_cast<balance*> (r.get ()))
+ {
+ cerr << "balance request for acc# " << b->account () << endl;
+ }
+ else if (withdraw* w = dynamic_cast<withdraw*> (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;
+ return 1;
+ }
+ catch (const std::ios_base::failure&)
+ {
+ cerr << argv[1] << ": unable to open or read failure" << endl;
+ return 1;
+ }
+}
diff --git a/xsd-examples/cxx/parser/multiroot/protocol-pimpl.cxx b/xsd-examples/cxx/parser/multiroot/protocol-pimpl.cxx
new file mode 100644
index 0000000..05fbd31
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/protocol-pimpl.cxx
@@ -0,0 +1,46 @@
+// file : cxx/parser/multiroot/protocol-pimpl.cxx
+// copyright : not copyrighted - public domain
+
+#include "protocol-pimpl.hxx"
+
+namespace protocol
+{
+ // request_pimpl
+ //
+ void request_pimpl::
+ account (unsigned int account)
+ {
+ account_ = account;
+ }
+
+ request* request_pimpl::
+ post_request ()
+ {
+ // This parser is never used directly.
+ //
+ return 0;
+ }
+
+ // balance_pimpl
+ //
+ balance* balance_pimpl::
+ post_balance ()
+ {
+ return new balance (account_);
+ }
+
+ // withdraw_pimpl
+ //
+ void withdraw_pimpl::
+ amount (unsigned int amount)
+ {
+ amount_ = amount;
+ }
+
+ withdraw* withdraw_pimpl::
+ post_withdraw ()
+ {
+ return new withdraw (account_, amount_);
+ }
+}
+
diff --git a/xsd-examples/cxx/parser/multiroot/protocol-pimpl.hxx b/xsd-examples/cxx/parser/multiroot/protocol-pimpl.hxx
new file mode 100644
index 0000000..1b53604
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/protocol-pimpl.hxx
@@ -0,0 +1,48 @@
+// file : cxx/parser/multiroot/protocol-pimpl.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef PROTOCOL_PIMPL_HXX
+#define PROTOCOL_PIMPL_HXX
+
+#include "protocol.hxx"
+#include "protocol-pskel.hxx"
+
+namespace protocol
+{
+ class request_pimpl: public virtual request_pskel
+ {
+ public:
+ virtual void
+ account (unsigned int);
+
+ virtual request*
+ post_request ();
+
+ protected:
+ unsigned int account_;
+ };
+
+ class balance_pimpl: public virtual balance_pskel,
+ public request_pimpl
+ {
+ public:
+ virtual balance*
+ post_balance ();
+ };
+
+ class withdraw_pimpl: public virtual withdraw_pskel,
+ public request_pimpl
+ {
+ public:
+ virtual void
+ amount (unsigned int);
+
+ virtual withdraw*
+ post_withdraw ();
+
+ private:
+ unsigned int amount_;
+ };
+}
+
+#endif // PROTOCOL_PIMPL_HXX
diff --git a/xsd-examples/cxx/parser/multiroot/protocol.hxx b/xsd-examples/cxx/parser/multiroot/protocol.hxx
new file mode 100644
index 0000000..3c1a10a
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/protocol.hxx
@@ -0,0 +1,61 @@
+// file : cxx/parser/multiroot/protocol.hxx
+// copyright : not copyrighted - public domain
+
+#ifndef PROTOCOL_HXX
+#define PROTOCOL_HXX
+
+namespace protocol
+{
+ class request
+ {
+ public:
+ virtual
+ ~request ()
+ {
+ }
+
+ unsigned int
+ account () const
+ {
+ return account_;
+ }
+
+ protected:
+ request (unsigned int account)
+ : account_ (account)
+ {
+ }
+
+ private:
+ unsigned int account_;
+ };
+
+ class balance: public request
+ {
+ public:
+ balance (unsigned int account)
+ : request (account)
+ {
+ }
+ };
+
+ class withdraw: public request
+ {
+ public:
+ withdraw (unsigned int account, unsigned int amount)
+ : request (account), amount_ (amount)
+ {
+ }
+
+ unsigned int
+ amount () const
+ {
+ return amount_;
+ }
+
+ private:
+ unsigned int amount_;
+ };
+}
+
+#endif // PROTOCOL_HXX
diff --git a/xsd-examples/cxx/parser/multiroot/protocol.map b/xsd-examples/cxx/parser/multiroot/protocol.map
new file mode 100644
index 0000000..90536ea
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/protocol.map
@@ -0,0 +1,11 @@
+# file : cxx/parser/multiroot/protocol.map
+# copyright : not copyrighted - public domain
+
+namespace http://www.codesynthesis.com/protocol ::protocol
+{
+ include "protocol.hxx";
+
+ request request*;
+ balance balance*;
+ withdraw withdraw*;
+}
diff --git a/xsd-examples/cxx/parser/multiroot/protocol.xsd b/xsd-examples/cxx/parser/multiroot/protocol.xsd
new file mode 100644
index 0000000..a33fed2
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/protocol.xsd
@@ -0,0 +1,50 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/parser/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">
+ <xsd:sequence>
+ <xsd:element name="account" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="balance">
+ <xsd:complexContent>
+ <xsd:extension base="p:request"/>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="withdraw">
+ <xsd:complexContent>
+ <xsd:extension base="p:request">
+ <xsd:sequence>
+ <xsd:element name="amount" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="deposit">
+ <xsd:complexContent>
+ <xsd:extension base="p:request">
+ <xsd:sequence>
+ <xsd:element name="amount" type="xsd:unsignedInt"/>
+ </xsd:sequence>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:element name="balance" type="p:balance"/>
+ <xsd:element name="withdraw" type="p:withdraw"/>
+ <xsd:element name="deposit" type="p:deposit"/>
+
+</xsd:schema>
diff --git a/xsd-examples/cxx/parser/multiroot/testscript b/xsd-examples/cxx/parser/multiroot/testscript
new file mode 100644
index 0000000..76329f7
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/testscript
@@ -0,0 +1,6 @@
+# file : cxx/parser/multiroot/testscript
+# license : not copyrighted - public domain
+
+$* $src_base/balance.xml 2>| : balance
+$* $src_base/deposit.xml 2>| : deposit
+$* $src_base/withdraw.xml 2>| : withdraw
diff --git a/xsd-examples/cxx/parser/multiroot/withdraw.xml b/xsd-examples/cxx/parser/multiroot/withdraw.xml
new file mode 100644
index 0000000..8b2efe0
--- /dev/null
+++ b/xsd-examples/cxx/parser/multiroot/withdraw.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/parser/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>