From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- examples/cxx/parser/multiroot/README | 51 +++++++ examples/cxx/parser/multiroot/balance.xml | 17 +++ examples/cxx/parser/multiroot/deposit.xml | 18 +++ examples/cxx/parser/multiroot/driver.cxx | 162 +++++++++++++++++++++++ examples/cxx/parser/multiroot/makefile | 68 ++++++++++ examples/cxx/parser/multiroot/protocol-pimpl.cxx | 47 +++++++ examples/cxx/parser/multiroot/protocol-pimpl.hxx | 49 +++++++ examples/cxx/parser/multiroot/protocol.hxx | 62 +++++++++ examples/cxx/parser/multiroot/protocol.map | 12 ++ examples/cxx/parser/multiroot/protocol.xsd | 51 +++++++ examples/cxx/parser/multiroot/withdraw.xml | 18 +++ 11 files changed, 555 insertions(+) create mode 100644 examples/cxx/parser/multiroot/README create mode 100644 examples/cxx/parser/multiroot/balance.xml create mode 100644 examples/cxx/parser/multiroot/deposit.xml create mode 100644 examples/cxx/parser/multiroot/driver.cxx create mode 100644 examples/cxx/parser/multiroot/makefile create mode 100644 examples/cxx/parser/multiroot/protocol-pimpl.cxx create mode 100644 examples/cxx/parser/multiroot/protocol-pimpl.hxx create mode 100644 examples/cxx/parser/multiroot/protocol.hxx create mode 100644 examples/cxx/parser/multiroot/protocol.map create mode 100644 examples/cxx/parser/multiroot/protocol.xsd create mode 100644 examples/cxx/parser/multiroot/withdraw.xml (limited to 'examples/cxx/parser/multiroot') diff --git a/examples/cxx/parser/multiroot/README b/examples/cxx/parser/multiroot/README new file mode 100644 index 0000000..041dfec --- /dev/null +++ b/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/examples/cxx/parser/multiroot/balance.xml b/examples/cxx/parser/multiroot/balance.xml new file mode 100644 index 0000000..df0a6e9 --- /dev/null +++ b/examples/cxx/parser/multiroot/balance.xml @@ -0,0 +1,17 @@ + + + + + + + 123456789 + + diff --git a/examples/cxx/parser/multiroot/deposit.xml b/examples/cxx/parser/multiroot/deposit.xml new file mode 100644 index 0000000..3043c52 --- /dev/null +++ b/examples/cxx/parser/multiroot/deposit.xml @@ -0,0 +1,18 @@ + + + + + + + 123456789 + 1000000 + + diff --git a/examples/cxx/parser/multiroot/driver.cxx b/examples/cxx/parser/multiroot/driver.cxx new file mode 100644 index 0000000..9e5d874 --- /dev/null +++ b/examples/cxx/parser/multiroot/driver.cxx @@ -0,0 +1,162 @@ +// file : examples/cxx/parser/multiroot/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#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::auto_ptr 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::auto_ptr r (doc_p.result ()); + + // Let's print what we've got. + // + if (balance* b = dynamic_cast (r.get ())) + { + cerr << "balance request for acc# " << b->account () << endl; + } + else if (withdraw* w = dynamic_cast (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/examples/cxx/parser/multiroot/makefile b/examples/cxx/parser/multiroot/makefile new file mode 100644 index 0000000..cfe4fba --- /dev/null +++ b/examples/cxx/parser/multiroot/makefile @@ -0,0 +1,68 @@ +# file : examples/cxx/parser/multiroot/makefile +# author : Boris Kolpackov +# 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 := protocol.xsd +cxx := driver.cxx protocol-pimpl.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-pskel.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) + +skel := $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.ixx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) + +$(skel): xsd := $(out_root)/xsd/xsd +$(skel): xsd_options := --type-map $(src_base)/protocol.map +$(skel): $(out_root)/xsd/xsd $(src_base)/protocol.map + +$(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=-pskel.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/parser/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) diff --git a/examples/cxx/parser/multiroot/protocol-pimpl.cxx b/examples/cxx/parser/multiroot/protocol-pimpl.cxx new file mode 100644 index 0000000..09a04b3 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol-pimpl.cxx @@ -0,0 +1,47 @@ +// file : examples/cxx/parser/multiroot/protocol-pimpl.cxx +// author : Boris Kolpackov +// 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/examples/cxx/parser/multiroot/protocol-pimpl.hxx b/examples/cxx/parser/multiroot/protocol-pimpl.hxx new file mode 100644 index 0000000..0531790 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol-pimpl.hxx @@ -0,0 +1,49 @@ +// file : examples/cxx/parser/multiroot/protocol-pimpl.hxx +// author : Boris Kolpackov +// 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/examples/cxx/parser/multiroot/protocol.hxx b/examples/cxx/parser/multiroot/protocol.hxx new file mode 100644 index 0000000..f076140 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol.hxx @@ -0,0 +1,62 @@ +// file : examples/cxx/parser/multiroot/protocol.hxx +// author : Boris Kolpackov +// 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/examples/cxx/parser/multiroot/protocol.map b/examples/cxx/parser/multiroot/protocol.map new file mode 100644 index 0000000..7389fa9 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol.map @@ -0,0 +1,12 @@ +# file : examples/cxx/parser/multiroot/protocol.map +# author : Boris Kolpackov +# copyright : not copyrighted - public domain + +namespace http://www.codesynthesis.com/protocol ::protocol +{ + include "protocol.hxx"; + + request request*; + balance balance*; + withdraw withdraw*; +} diff --git a/examples/cxx/parser/multiroot/protocol.xsd b/examples/cxx/parser/multiroot/protocol.xsd new file mode 100644 index 0000000..4b29926 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol.xsd @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/parser/multiroot/withdraw.xml b/examples/cxx/parser/multiroot/withdraw.xml new file mode 100644 index 0000000..7a80aa7 --- /dev/null +++ b/examples/cxx/parser/multiroot/withdraw.xml @@ -0,0 +1,18 @@ + + + + + + + 123456789 + 1000000 + + -- cgit v1.1