diff options
| author | Jörg Frings-Fürst <jff@merkur> | 2014-05-18 16:08:14 +0200 | 
|---|---|---|
| committer | Jörg Frings-Fürst <jff@merkur> | 2014-05-18 16:08:14 +0200 | 
| commit | a15cf65c44d5c224169c32ef5495b68c758134b7 (patch) | |
| tree | 3419f58fc8e1b315ba8171910ee044c5d467c162 /xsd/examples/cxx/parser/multiroot | |
Imported Upstream version 3.3.0.2upstream/3.3.0.2
Diffstat (limited to 'xsd/examples/cxx/parser/multiroot')
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/README | 51 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/balance.xml | 17 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/deposit.xml | 18 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/driver.cxx | 162 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/makefile | 114 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/protocol-pimpl.cxx | 47 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/protocol-pimpl.hxx | 49 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/protocol.hxx | 62 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/protocol.map | 12 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/protocol.xsd | 51 | ||||
| -rw-r--r-- | xsd/examples/cxx/parser/multiroot/withdraw.xml | 18 | 
11 files changed, 601 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..df0a6e9 --- /dev/null +++ b/xsd/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/xsd/examples/cxx/parser/multiroot/deposit.xml b/xsd/examples/cxx/parser/multiroot/deposit.xml new file mode 100644 index 0000000..3043c52 --- /dev/null +++ b/xsd/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/xsd/examples/cxx/parser/multiroot/driver.cxx b/xsd/examples/cxx/parser/multiroot/driver.cxx new file mode 100644 index 0000000..9e5d874 --- /dev/null +++ b/xsd/examples/cxx/parser/multiroot/driver.cxx @@ -0,0 +1,162 @@ +// 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-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<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::auto_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/makefile b/xsd/examples/cxx/parser/multiroot/makefile new file mode 100644 index 0000000..57fa732 --- /dev/null +++ b/xsd/examples/cxx/parser/multiroot/makefile @@ -0,0 +1,114 @@ +# file      : examples/cxx/parser/multiroot/makefile +# author    : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2005-2010 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 +install  := $(out_base)/.install +dist     := $(out_base)/.dist +dist-win := $(out_base)/.dist-win +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) + +genf := $(xsd:.xsd=-pskel.hxx) $(xsd:.xsd=-pskel.ixx) $(xsd:.xsd=-pskel.cxx) +gen  := $(addprefix $(out_base)/,$(genf)) + +$(gen): xsd := $(out_root)/xsd/xsd +$(gen): xsd_options := --type-map $(src_base)/protocol.map +$(gen): $(out_root)/xsd/xsd $(src_base)/protocol.map + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +$(out_base)/: $(driver) + + +# Install & Dist. +# +dist-common := $(out_base)/.dist-common + +$(install) $(dist) $(dist-win) $(dist-common): path := $(subst $(src_root)/,,$(src_base)) + +$(install): +	$(call install-data,$(src_base)/README,$(install_doc_dir)/xsd/$(path)/README) +	$(call install-data,$(src_base)/driver.cxx,$(install_doc_dir)/xsd/$(path)/driver.cxx) +	$(call install-data,$(src_base)/protocol.xsd,$(install_doc_dir)/xsd/$(path)/protocol.xsd) +	$(call install-data,$(src_base)/balance.xml,$(install_doc_dir)/xsd/$(path)/balance.xml) +	$(call install-data,$(src_base)/deposit.xml,$(install_doc_dir)/xsd/$(path)/deposit.xml) +	$(call install-data,$(src_base)/withdraw.xml,$(install_doc_dir)/xsd/$(path)/withdraw.xml) +	$(call install-data,$(src_base)/protocol.map,$(install_doc_dir)/xsd/$(path)/protocol.map) +	$(call install-data,$(src_base)/protocol.hxx,$(install_doc_dir)/xsd/$(path)/protocol.hxx) +	$(call install-data,$(src_base)/protocol-pimpl.hxx,$(install_doc_dir)/xsd/$(path)/protocol-pimpl.hxx) +	$(call install-data,$(src_base)/protocol-pimpl.cxx,$(install_doc_dir)/xsd/$(path)/protocol-pimpl.cxx) + +$(dist-common): +	$(call install-data,$(src_base)/driver.cxx,$(dist_prefix)/$(path)/driver.cxx) +	$(call install-data,$(src_base)/protocol.xsd,$(dist_prefix)/$(path)/protocol.xsd) +	$(call install-data,$(src_base)/balance.xml,$(dist_prefix)/$(path)/balance.xml) +	$(call install-data,$(src_base)/deposit.xml,$(dist_prefix)/$(path)/deposit.xml) +	$(call install-data,$(src_base)/withdraw.xml,$(dist_prefix)/$(path)/withdraw.xml) +	$(call install-data,$(src_base)/protocol.map,$(dist_prefix)/$(path)/protocol.map) +	$(call install-data,$(src_base)/protocol.hxx,$(dist_prefix)/$(path)/protocol.hxx) +	$(call install-data,$(src_base)/protocol-pimpl.hxx,$(dist_prefix)/$(path)/protocol-pimpl.hxx) +	$(call install-data,$(src_base)/protocol-pimpl.cxx,$(dist_prefix)/$(path)/protocol-pimpl.cxx) + +$(dist): $(dist-common) +	$(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README) + +$(dist-win): $(dist-common) +	$(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README.txt) +	$(call message,,unix2dos $(dist_prefix)/$(path)/README.txt) + +# Clean. +# +$(clean): $(driver).o.clean                                 \ +  $(addsuffix .cxx.clean,$(obj))                            \ +  $(addsuffix .cxx.clean,$(dep))                            \ +  $(addprefix $(out_base)/,$(xsd:.xsd=-pskel.cxx.xsd.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(gen): | $(out_base)/.gitignore +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# 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,$(bld_root)/install.make) +$(call include,$(scf_root)/xsd/parser/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) 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..09a04b3 --- /dev/null +++ b/xsd/examples/cxx/parser/multiroot/protocol-pimpl.cxx @@ -0,0 +1,47 @@ +// file      : examples/cxx/parser/multiroot/protocol-pimpl.cxx +// author    : Boris Kolpackov <boris@codesynthesis.com> +// 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..0531790 --- /dev/null +++ b/xsd/examples/cxx/parser/multiroot/protocol-pimpl.hxx @@ -0,0 +1,49 @@ +// file      : examples/cxx/parser/multiroot/protocol-pimpl.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/xsd/examples/cxx/parser/multiroot/protocol.hxx b/xsd/examples/cxx/parser/multiroot/protocol.hxx new file mode 100644 index 0000000..f076140 --- /dev/null +++ b/xsd/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/xsd/examples/cxx/parser/multiroot/protocol.map b/xsd/examples/cxx/parser/multiroot/protocol.map new file mode 100644 index 0000000..7389fa9 --- /dev/null +++ b/xsd/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/xsd/examples/cxx/parser/multiroot/protocol.xsd b/xsd/examples/cxx/parser/multiroot/protocol.xsd new file mode 100644 index 0000000..4b29926 --- /dev/null +++ b/xsd/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/xsd/examples/cxx/parser/multiroot/withdraw.xml b/xsd/examples/cxx/parser/multiroot/withdraw.xml new file mode 100644 index 0000000..7a80aa7 --- /dev/null +++ b/xsd/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> | 
