From 2615896faa646e5830abf2c269150e1165c66515 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 18 Dec 2020 18:48:46 +0300 Subject: Switch to build2 --- xsd-examples/cxx/tree/multiroot/README | 45 ++++++++++ xsd-examples/cxx/tree/multiroot/balance.xml | 16 ++++ xsd-examples/cxx/tree/multiroot/deposit.xml | 17 ++++ xsd-examples/cxx/tree/multiroot/dom-parse.cxx | 93 +++++++++++++++++++ xsd-examples/cxx/tree/multiroot/dom-parse.hxx | 22 +++++ xsd-examples/cxx/tree/multiroot/driver.cxx | 124 ++++++++++++++++++++++++++ xsd-examples/cxx/tree/multiroot/makefile | 108 ++++++++++++++++++++++ xsd-examples/cxx/tree/multiroot/protocol.xsd | 50 +++++++++++ xsd-examples/cxx/tree/multiroot/withdraw.xml | 17 ++++ 9 files changed, 492 insertions(+) create mode 100644 xsd-examples/cxx/tree/multiroot/README create mode 100644 xsd-examples/cxx/tree/multiroot/balance.xml create mode 100644 xsd-examples/cxx/tree/multiroot/deposit.xml create mode 100644 xsd-examples/cxx/tree/multiroot/dom-parse.cxx create mode 100644 xsd-examples/cxx/tree/multiroot/dom-parse.hxx create mode 100644 xsd-examples/cxx/tree/multiroot/driver.cxx create mode 100644 xsd-examples/cxx/tree/multiroot/makefile create mode 100644 xsd-examples/cxx/tree/multiroot/protocol.xsd create mode 100644 xsd-examples/cxx/tree/multiroot/withdraw.xml (limited to 'xsd-examples/cxx/tree/multiroot') diff --git a/xsd-examples/cxx/tree/multiroot/README b/xsd-examples/cxx/tree/multiroot/README new file mode 100644 index 0000000..b742422 --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/README @@ -0,0 +1,45 @@ +This example shows how to handle XML vocabularies with multiple +root elements using the C++/Tree mapping. + +See also the messaging example for an alternative approach that +uses the element type and element map features of the C++/Tree +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 + C++ types that represent the given vocabulary and a set of + parsing functions that convert XML documents to a tree-like + in-memory object model. These are generated by XSD from + protocol.xsd. + +dom-parse.hxx +dom-parse.cxx + Definition and implementation of the parse() function that + parses an XML document to a DOM document. + +driver.cxx + Driver for the example. It first calls the above parse() function + to parse the input file to a DOM document. It then determines the + type of request being handled and calls the corresponding parsing + function that constructs the object model from this DOM document. + Finally, it prints the content of this object model to STDERR. + This example intentionally does not support the deposit request + to show how to handle unknown documents. + +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/tree/multiroot/balance.xml b/xsd-examples/cxx/tree/multiroot/balance.xml new file mode 100644 index 0000000..5f4a441 --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/balance.xml @@ -0,0 +1,16 @@ + + + + + + + 123456789 + + diff --git a/xsd-examples/cxx/tree/multiroot/deposit.xml b/xsd-examples/cxx/tree/multiroot/deposit.xml new file mode 100644 index 0000000..9db2a2a --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/deposit.xml @@ -0,0 +1,17 @@ + + + + + + + 123456789 + 1000000 + + diff --git a/xsd-examples/cxx/tree/multiroot/dom-parse.cxx b/xsd-examples/cxx/tree/multiroot/dom-parse.cxx new file mode 100644 index 0000000..3d04922 --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/dom-parse.cxx @@ -0,0 +1,93 @@ +// file : cxx/tree/multiroot/dom-parse.cxx +// copyright : not copyrighted - public domain + +#include "dom-parse.hxx" + +#include + +#include +#include // chLatin_* +#include + +#include +#include + +#include +#include + +using namespace xercesc; +namespace xml = xsd::cxx::xml; +namespace tree = xsd::cxx::tree; + +xml::dom::auto_ptr +parse (std::istream& is, const std::string& id, bool validate) +{ + const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull}; + + // Get an implementation of the Load-Store (LS) interface. + // + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls_id)); + + xml::dom::auto_ptr parser ( + impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0)); + + DOMConfiguration* conf (parser->getDomConfig ()); + + // Discard comment nodes in the document. + // + conf->setParameter (XMLUni::fgDOMComments, false); + + // Enable datatype normalization. + // + conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true); + + // Do not create EntityReference nodes in the DOM tree. No + // EntityReference nodes will be created, only the nodes + // corresponding to their fully expanded substitution text + // will be created. + // + conf->setParameter (XMLUni::fgDOMEntities, false); + + // Perform namespace processing. + // + conf->setParameter (XMLUni::fgDOMNamespaces, true); + + // Do not include ignorable whitespace in the DOM tree. + // + conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false); + + // Enable/Disable validation. + // + conf->setParameter (XMLUni::fgDOMValidate, validate); + conf->setParameter (XMLUni::fgXercesSchema, validate); + conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); + + // Xerces-C++ 3.1.0 is the first version with working multi import + // support. + // +#if _XERCES_VERSION >= 30100 + conf->setParameter (XMLUni::fgXercesHandleMultipleImports, true); +#endif + + // We will release the DOM document ourselves. + // + conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + // Set error handler. + // + tree::error_handler eh; + xml::dom::bits::error_handler_proxy ehp (eh); + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + + // Prepare input stream. + // + xml::sax::std_input_source isrc (is, id); + Wrapper4InputSource wrap (&isrc, false); + + xml::dom::auto_ptr doc (parser->parse (&wrap)); + + eh.throw_if_failed > (); + + return doc; +} diff --git a/xsd-examples/cxx/tree/multiroot/dom-parse.hxx b/xsd-examples/cxx/tree/multiroot/dom-parse.hxx new file mode 100644 index 0000000..b6b9b7b --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/dom-parse.hxx @@ -0,0 +1,22 @@ +// file : cxx/tree/multiroot/dom-parse.hxx +// copyright : not copyrighted - public domain + +#ifndef DOM_PARSE +#define DOM_PARSE + +#include +#include + +#include +#include + +// Parse an XML document from the standard input stream with an +// optional resource id. Resource id is used in diagnostics as +// well as to locate schemas referenced from inside the document. +// +xsd::cxx::xml::dom::auto_ptr +parse (std::istream& is, + const std::string& id, + bool validate); + +#endif // DOM_PARSE diff --git a/xsd-examples/cxx/tree/multiroot/driver.cxx b/xsd-examples/cxx/tree/multiroot/driver.cxx new file mode 100644 index 0000000..fb219bd --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/driver.cxx @@ -0,0 +1,124 @@ +// file : cxx/tree/multiroot/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include +#include +#include + +#include +#include + +#include // xml::transcode + +#include "dom-parse.hxx" + +#include "protocol.hxx" + +using namespace std; +using namespace protocol; + +// Parse an XML document and return a pointer to request_t which can +// then be tested with dynamic_cast. If your vocabulary does not have +// a common base type for all root element types then you can use +// xml_schema::type which is a base for all generated types. +// +auto_ptr +parse (istream& is, const string& id) +{ + using namespace xercesc; + namespace xml = xsd::cxx::xml; + + // Parse an XML instance to a DOM document using the parse() + // function from dom-parse.hxx. + // + xml_schema::dom::auto_ptr doc (parse (is, id, true)); + + DOMElement* root (doc->getDocumentElement ()); + + string ns (xml::transcode (root->getNamespaceURI ())); + string name (xml::transcode (root->getLocalName ())); + + auto_ptr r; + + // We could have handled the result directly in this function + // instead of returning it as an opaque pointer and using + // dynamic_cast later to figure out which request we are dealing + // with. + // + if (ns == "http://www.codesynthesis.com/protocol") + { + if (name == "balance") + { + // Use the balance parsing function. + // + r.reset (balance (*doc).release ()); + } + else if (name == "withdraw") + { + // Use the withdraw parsing function. + // + r.reset (withdraw (*doc).release ()); + } + } + + if (r.get () == 0) + cerr << "ignoring unknown request: " << ns << "#" << name << endl; + + return r; +} + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " request.xml" << endl; + return 1; + } + + int r (0); + + // We need to initialize the Xerces-C++ runtime because we + // are doing the XML-to-DOM parsing ourselves. + // + xercesc::XMLPlatformUtils::Initialize (); + + try + { + ifstream ifs; + ifs.exceptions (ifstream::badbit | ifstream::failbit); + ifs.open (argv[1]); + + auto_ptr r (parse (ifs, argv[1])); + + // Let's print what we've got. + // + if (balance_t* b = dynamic_cast (r.get ())) + { + cerr << "balance request for acc# " << b->account () << endl; + } + else if (withdraw_t* 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; + r = 1; + } + catch (const std::ios_base::failure&) + { + cerr << argv[1] << ": unable to open or read failure" << endl; + r = 1; + } + + xercesc::XMLPlatformUtils::Terminate (); + return r; +} diff --git a/xsd-examples/cxx/tree/multiroot/makefile b/xsd-examples/cxx/tree/multiroot/makefile new file mode 100644 index 0000000..ccc6f6c --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/makefile @@ -0,0 +1,108 @@ +# file : cxx/tree/multiroot/makefile +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make + +xsd := protocol.xsd +cxx := driver.cxx dom-parse.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=.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$(out_base) -I$(src_base) -I$(src_root)/libxsd +$(obj) $(dep): $(xerces_c.l.cpp-options) + +genf := $(xsd:.xsd=.hxx) $(xsd:.xsd=.ixx) $(xsd:.xsd=.cxx) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): xsd := $(out_root)/xsd/xsd +$(gen): xsd_options += --root-element-all +$(gen): $(out_root)/xsd/xsd + +$(call include-dep,$(dep),$(obj),$(gen)) + +# 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)/dom-parse.hxx,$(install_doc_dir)/xsd/$(path)/dom-parse.hxx) + $(call install-data,$(src_base)/dom-parse.cxx,$(install_doc_dir)/xsd/$(path)/dom-parse.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)/dom-parse.hxx,$(dist_prefix)/$(path)/dom-parse.hxx) + $(call install-data,$(src_base)/dom-parse.cxx,$(dist_prefix)/$(path)/dom-parse.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,,todos $(dist_prefix)/$(path)/README.txt) + +# Clean. +# +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=.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/tree/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) diff --git a/xsd-examples/cxx/tree/multiroot/protocol.xsd b/xsd-examples/cxx/tree/multiroot/protocol.xsd new file mode 100644 index 0000000..2fe9456 --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/protocol.xsd @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xsd-examples/cxx/tree/multiroot/withdraw.xml b/xsd-examples/cxx/tree/multiroot/withdraw.xml new file mode 100644 index 0000000..f98ad16 --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/withdraw.xml @@ -0,0 +1,17 @@ + + + + + + + 123456789 + 1000000 + + -- cgit v1.1