diff options
Diffstat (limited to 'examples/cxx/parser/multiroot')
-rw-r--r-- | examples/cxx/parser/multiroot/README | 61 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/balance.xml | 17 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/deposit.xml | 18 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/driver.cxx | 245 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/makefile | 71 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/protocol-pimpl-mixin.cxx | 47 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/protocol-pimpl-mixin.hxx | 49 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/protocol-pimpl-tiein.cxx | 59 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/protocol-pimpl-tiein.hxx | 55 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/protocol.hxx | 62 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/protocol.map | 12 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/protocol.xsd | 51 | ||||
-rw-r--r-- | examples/cxx/parser/multiroot/withdraw.xml | 18 |
13 files changed, 765 insertions, 0 deletions
diff --git a/examples/cxx/parser/multiroot/README b/examples/cxx/parser/multiroot/README new file mode 100644 index 0000000..817e522 --- /dev/null +++ b/examples/cxx/parser/multiroot/README @@ -0,0 +1,61 @@ +This example shows how to handle XML vocabularies with multiple +root elements using the Embedded 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/e from protocol.xsd and + protocol.map. + +protocol-pimpl-mixin.hxx +protocol-pimpl-mixin.cxx + +protocol-pimpl-tiein.hxx +protocol-pimpl-tiein.cxx + Parser implementations (using either mixin or tiein parser + reuse style) 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 one + of 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 + +The example reads from STDIN if input file is not specified: + +$ ./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 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/parser/multiroot/balance.xml +author : Boris Kolpackov <boris@codesynthesis.com> +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/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 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/parser/multiroot/deposit.xml +author : Boris Kolpackov <boris@codesynthesis.com> +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/examples/cxx/parser/multiroot/driver.cxx b/examples/cxx/parser/multiroot/driver.cxx new file mode 100644 index 0000000..32aced2 --- /dev/null +++ b/examples/cxx/parser/multiroot/driver.cxx @@ -0,0 +1,245 @@ +// file : examples/cxx/parser/multiroot/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <iostream> + +#include "protocol.hxx" +#include "protocol-pskel.hxx" // Get the configuration macros (XSDE_*). + +#if defined(XSDE_REUSE_STYLE_MIXIN) +# include "protocol-pimpl-mixin.hxx" +#elif defined(XSDE_REUSE_STYLE_TIEIN) +# include "protocol-pimpl-tiein.hxx" +#else +# error this example requires mixin or tiein parser reuse support +#endif + +using std::cerr; +using std::endl; +using xml_schema::ro_string; + +namespace protocol +{ + // We are going to use our own "type ids" for the request + // types. You could instead use dynamic_cast if your system + // provides RTTI. + // + enum request_type + { + unknown_type, + balance_type, + withdraw_type + }; + + // Customize the xml_schema::document_pimpl object to handle our protocol + // vocabulary with multiple root elements. + // + class document_pimpl: public xml_schema::document_pimpl + { + public: + document_pimpl (balance_pskel& balance_p, withdraw_pskel& withdraw_p) + : result_type_ (unknown_type), + balance_p_ (balance_p), + withdraw_p_ (withdraw_p) + { + } + + request_type + result_type () const + { + return result_type_; + } + + 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. Note that the signature of this function varies + // depending on whether the runtime was built with polymorphism + // support. + // + virtual xml_schema::parser_base* +#ifndef XSDE_POLYMORPHIC + start_root_element (const ro_string& ns, + const ro_string& name) +#else + start_root_element (const ro_string& ns, + const ro_string& name, + const char* /* xsi:type */) +#endif + { + 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: " << name << endl; + return 0; + } + else + { + // This document is from the wrong namespace. If the runtime and + // the generated code are built with validation enabled then we + // can set an XML Schema error. + // +#ifdef XSDE_PARSER_VALIDATION + context_.schema_error ( + xml_schema::parser_schema_error::unexpected_element); +#endif + 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_type_ = balance_type; + result_.reset (balance_p_.post_balance ()); + } + else if (parser == &withdraw_p_) + { + result_type_ = withdraw_type; + result_.reset (withdraw_p_.post_withdraw ()); + } + else + { + result_type_ = unknown_type; + result_.reset (0); + } + } + + + public: + // If we need to be able to reset and reuse the parser after + // an error then we also need to override reset() and reset + // the root parsers. We can also get smarter here by caching + // the parser that was used last and only reset that. Note + // that you always need to call _reset() from the base. + // + virtual void + reset () + { + xml_schema::document_pimpl::reset (); + + balance_p_._reset (); + withdraw_p_._reset (); + } + + private: + request_type result_type_; + std::auto_ptr<request> result_; + + balance_pskel& balance_p_; + withdraw_pskel& withdraw_p_; + }; +} + + +int +main (int argc, char* argv[]) +{ + const char* input; + + if (argc < 2) + { + input = "STDIN"; + cerr << "XML file not specified, reading from STDIN" << endl; + } + else + input = argv[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_pimpl doc_p (balance_p, withdraw_p); + + // pre() and post() will be called as part of the start_root_element() + // and end_root_element() calls. + // + if (argc < 2) + doc_p.parse (std::cin); + else + doc_p.parse (argv[1]); + + std::auto_ptr<request> r (doc_p.result ()); + request_type t = doc_p.result_type (); + + // Let's print what we've got. + // + switch (t) + { + case balance_type: + { + balance* b = static_cast<balance*> (r.get ()); + cerr << "balance request for acc# " << b->account () << endl; + break; + } + case withdraw_type: + { + withdraw* w = static_cast<withdraw*> (r.get ()); + cerr << "withdrawal request for acc# " << w->account () << ", " + << "amount: " << w->amount () << endl; + break; + } + case unknown_type: + { + cerr << "unknown request" << endl; + break; + } + } + } + catch (const xml_schema::parser_exception& e) + { + cerr << input << ":" << e.line () << ":" << e.column () << ": " + << e.text () << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << input << ": unable to open or read failure" << endl; + return 1; + } + + return 0; +} diff --git a/examples/cxx/parser/multiroot/makefile b/examples/cxx/parser/multiroot/makefile new file mode 100644 index 0000000..44e8980 --- /dev/null +++ b/examples/cxx/parser/multiroot/makefile @@ -0,0 +1,71 @@ +# file : examples/cxx/parser/multiroot/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# 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 + +ifeq ($(xsde_reuse_style),mixin) +cxx += protocol-pimpl-mixin.cxx +else +cxx += protocol-pimpl-tiein.cxx +endif + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-pskel.o)) +dep := $(obj:.o=.o.d) + +xsde.l := $(out_root)/libxsde/xsde/xsde.l +xsde.l.cpp-options := $(out_root)/libxsde/xsde/xsde.l.cpp-options + +driver := $(out_base)/driver +clean := $(out_base)/.clean + + +# Build. +# +$(driver): $(obj) $(xsde.l) + +$(obj) $(dep): cpp_options := -I$(out_base) -I$(src_base) +$(obj) $(dep): $(xsde.l.cpp-options) + +skel := $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.ixx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) + +$(skel): xsde := $(out_root)/xsde/xsde +$(skel): xsde_options += --type-map $(src_base)/protocol.map +$(skel): $(out_root)/xsde/xsde $(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)/xsde/parser/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsde/makefile) +$(call import,$(src_root)/libxsde/xsde/makefile) diff --git a/examples/cxx/parser/multiroot/protocol-pimpl-mixin.cxx b/examples/cxx/parser/multiroot/protocol-pimpl-mixin.cxx new file mode 100644 index 0000000..3cfd0b0 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol-pimpl-mixin.cxx @@ -0,0 +1,47 @@ +// file : examples/cxx/parser/multiroot/protocol-pimpl-mixin.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include "protocol-pimpl-mixin.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-mixin.hxx b/examples/cxx/parser/multiroot/protocol-pimpl-mixin.hxx new file mode 100644 index 0000000..dc26e9a --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol-pimpl-mixin.hxx @@ -0,0 +1,49 @@ +// file : examples/cxx/parser/multiroot/protocol-pimpl-mixin.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// 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-pimpl-tiein.cxx b/examples/cxx/parser/multiroot/protocol-pimpl-tiein.cxx new file mode 100644 index 0000000..def6f98 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol-pimpl-tiein.cxx @@ -0,0 +1,59 @@ +// file : examples/cxx/parser/multiroot/protocol-pimpl-tiein.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include "protocol-pimpl-tiein.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_pimpl:: + balance_pimpl () + : balance_pskel (&base_impl_) + { + } + + balance* balance_pimpl:: + post_balance () + { + return new balance (base_impl_.account_); + } + + // withdraw_pimpl + // + withdraw_pimpl:: + withdraw_pimpl () + : withdraw_pskel (&base_impl_) + { + } + + void withdraw_pimpl:: + amount (unsigned int amount) + { + amount_ = amount; + } + + withdraw* withdraw_pimpl:: + post_withdraw () + { + return new withdraw (base_impl_.account_, amount_); + } +} + diff --git a/examples/cxx/parser/multiroot/protocol-pimpl-tiein.hxx b/examples/cxx/parser/multiroot/protocol-pimpl-tiein.hxx new file mode 100644 index 0000000..3a18da0 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol-pimpl-tiein.hxx @@ -0,0 +1,55 @@ +// file : examples/cxx/parser/multiroot/protocol-pimpl-tiein.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// 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 request_pskel + { + public: + virtual void + account (unsigned int); + + virtual request* + post_request (); + + public: + unsigned int account_; + }; + + class balance_pimpl: public balance_pskel + { + public: + balance_pimpl (); + + virtual balance* + post_balance (); + + private: + request_pimpl base_impl_; + }; + + class withdraw_pimpl: public withdraw_pskel + { + public: + withdraw_pimpl (); + + virtual void + amount (unsigned int); + + virtual withdraw* + post_withdraw (); + + private: + request_pimpl base_impl_; + 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 <boris@codesynthesis.com> +// 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..43c0f21 --- /dev/null +++ b/examples/cxx/parser/multiroot/protocol.map @@ -0,0 +1,12 @@ +# file : examples/cxx/parser/multiroot/protocol.map +# author : Boris Kolpackov <boris@codesynthesis.com> +# 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 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/parser/multiroot/protocol.xsd +author : Boris Kolpackov <boris@codesynthesis.com> +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/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 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/parser/multiroot/withdraw.xml +author : Boris Kolpackov <boris@codesynthesis.com> +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> |