diff options
Diffstat (limited to 'examples/cxx/hybrid/multiroot')
-rw-r--r-- | examples/cxx/hybrid/multiroot/README | 48 | ||||
-rw-r--r-- | examples/cxx/hybrid/multiroot/balance.xml | 17 | ||||
-rw-r--r-- | examples/cxx/hybrid/multiroot/deposit.xml | 18 | ||||
-rw-r--r-- | examples/cxx/hybrid/multiroot/driver.cxx | 224 | ||||
-rw-r--r-- | examples/cxx/hybrid/multiroot/makefile | 72 | ||||
-rw-r--r-- | examples/cxx/hybrid/multiroot/protocol.xsd | 51 | ||||
-rw-r--r-- | examples/cxx/hybrid/multiroot/withdraw.xml | 18 |
7 files changed, 448 insertions, 0 deletions
diff --git a/examples/cxx/hybrid/multiroot/README b/examples/cxx/hybrid/multiroot/README new file mode 100644 index 0000000..3a7cfae --- /dev/null +++ b/examples/cxx/hybrid/multiroot/README @@ -0,0 +1,48 @@ +This example shows how to parse XML vocabularies with multiple root +elements using the Embedded C++/Hybrid 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 + +protocol-pskel.hxx +protocol-pskel.cxx +protocol-pimpl.hxx +protocol-pimpl.cxx + Object model (the first pair of files), parser skeletons (the + second pair) and parser implementations (the third pair). These + files are generated by the XSD/e compiler from protocol.xsd. The + --generate-parser and --generate-aggregate options were used to + request the generation of the parsing code. + +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 calls the parser + that constructs the object model from the input XML file. It + then prints the content 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/hybrid/multiroot/balance.xml b/examples/cxx/hybrid/multiroot/balance.xml new file mode 100644 index 0000000..9cffa83 --- /dev/null +++ b/examples/cxx/hybrid/multiroot/balance.xml @@ -0,0 +1,17 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/hybrid/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/hybrid/multiroot/deposit.xml b/examples/cxx/hybrid/multiroot/deposit.xml new file mode 100644 index 0000000..6b5ed30 --- /dev/null +++ b/examples/cxx/hybrid/multiroot/deposit.xml @@ -0,0 +1,18 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/hybrid/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/hybrid/multiroot/driver.cxx b/examples/cxx/hybrid/multiroot/driver.cxx new file mode 100644 index 0000000..5611222 --- /dev/null +++ b/examples/cxx/hybrid/multiroot/driver.cxx @@ -0,0 +1,224 @@ +// file : examples/cxx/hybrid/multiroot/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <iostream> + +#include "protocol.hxx" +#include "protocol-pimpl.hxx" + +using namespace std; +using xml_schema::ro_string; + +namespace protocol +{ + // We are going to use our own "type ids" for the request types. + // + 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 () + : result_type_ (unknown_type) + { + } + + request_type + result_type () const + { + return result_type_; + } + + protocol::balance + balance () const + { + return balance_; + } + + protocol::withdraw + withdraw () const + { + return withdraw_; + } + + 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_p_.root_name ()) + { + balance_p_.pre (); + return &balance_p_.root_parser (); + } + else if (name == "withdraw") + { + balance_p_.pre (); + return &withdraw_p_.root_parser (); + } + + // Ignore unknown request. + // + 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. Otherwise, we ignore it. + // +#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 variables. + // + if (parser == &balance_p_.root_parser ()) + { + result_type_ = balance_type; + balance_ = balance_p_.post (); + } + else if (parser == &withdraw_p_.root_parser ()) + { + result_type_ = withdraw_type; + withdraw_ = withdraw_p_.post (); + } + else + result_type_ = unknown_type; + } + + + 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: + // Result. + // + request_type result_type_; + protocol::balance balance_; + protocol::withdraw withdraw_; + + // Parsers for various root elements. + // + balance_paggr balance_p_; + withdraw_paggr 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; + + // Parse. + // + document_pimpl doc_p; + + // pre() and post() are 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]); + + // Print what we've got. + // + switch (doc_p.result_type ()) + { + case balance_type: + { + balance b (doc_p.balance ()); + cerr << "balance request for acc# " << b.account () << endl; + break; + } + case withdraw_type: + { + withdraw w (doc_p.withdraw ()); + 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/hybrid/multiroot/makefile b/examples/cxx/hybrid/multiroot/makefile new file mode 100644 index 0000000..1426d38 --- /dev/null +++ b/examples/cxx/hybrid/multiroot/makefile @@ -0,0 +1,72 @@ +# file : examples/cxx/hybrid/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 + +obj := $(addprefix $(out_base)/,\ +$(cxx:.cxx=.o) \ +$(xsd:.xsd=.o) \ +$(xsd:.xsd=-pskel.o) \ +$(xsd:.xsd=-pimpl.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): $(xsde.l.cpp-options) + +gen := $(out_base)/$(xsd:.xsd=.hxx) \ + $(out_base)/$(xsd:.xsd=.cxx) \ + $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) \ + $(out_base)/$(xsd:.xsd=-pimpl.hxx) \ + $(out_base)/$(xsd:.xsd=-pimpl.cxx) + +$(gen): $(out_root)/xsde/xsde +$(gen): xsde := $(out_root)/xsde/xsde +$(gen): xsde_options += --generate-parser --generate-aggregate \ +--root-element-all + +$(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=.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/hybrid/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsde/makefile) +$(call import,$(src_root)/libxsde/xsde/makefile) diff --git a/examples/cxx/hybrid/multiroot/protocol.xsd b/examples/cxx/hybrid/multiroot/protocol.xsd new file mode 100644 index 0000000..78a275d --- /dev/null +++ b/examples/cxx/hybrid/multiroot/protocol.xsd @@ -0,0 +1,51 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/hybrid/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/hybrid/multiroot/withdraw.xml b/examples/cxx/hybrid/multiroot/withdraw.xml new file mode 100644 index 0000000..36c7d6c --- /dev/null +++ b/examples/cxx/hybrid/multiroot/withdraw.xml @@ -0,0 +1,18 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/hybrid/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> |