From 5e527213a2430bb3018e5eebd909aef294edf9b5 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/.gitignore | 1 + xsd-examples/cxx/tree/multiroot/README | 45 ++++++++++ xsd-examples/cxx/tree/multiroot/balance.xml | 16 ++++ xsd-examples/cxx/tree/multiroot/buildfile | 26 ++++++ 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/protocol.xsd | 50 +++++++++++ xsd-examples/cxx/tree/multiroot/testscript | 6 ++ xsd-examples/cxx/tree/multiroot/withdraw.xml | 17 ++++ 11 files changed, 417 insertions(+) create mode 100644 xsd-examples/cxx/tree/multiroot/.gitignore 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/buildfile 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/protocol.xsd create mode 100644 xsd-examples/cxx/tree/multiroot/testscript 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/.gitignore b/xsd-examples/cxx/tree/multiroot/.gitignore new file mode 100644 index 0000000..f493b6c --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/.gitignore @@ -0,0 +1 @@ +protocol.?xx 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/buildfile b/xsd-examples/cxx/tree/multiroot/buildfile new file mode 100644 index 0000000..dc10588 --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/buildfile @@ -0,0 +1,26 @@ +# file : cxx/tree/multiroot/buildfile +# license : not copyrighted - public domain + +import libs = libxsd%lib{xsd} +import libs += libxerces-c%lib{xerces-c} + +./: exe{driver} xml{balance deposit withdraw} doc{README} + +exe{driver}: {hxx cxx}{* -protocol} {hxx ixx cxx}{protocol} $libs testscript + +<{hxx ixx cxx}{protocol}>: xsd{protocol} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-tree --std c++11 \ + --generate-inline \ + --root-element-all \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" "-I$src_base" + +# Define XSD_CXX11 since we include libxsd headers directly. +# +cxx.poptions += -DXSD_CXX11 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..e67c187 --- /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::unique_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::unique_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::unique_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..3699a77 --- /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::unique_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..d22c25e --- /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::unique_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. +// +unique_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::unique_ptr doc (parse (is, id, true)); + + DOMElement* root (doc->getDocumentElement ()); + + string ns (xml::transcode (root->getNamespaceURI ())); + string name (xml::transcode (root->getLocalName ())); + + unique_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]); + + unique_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/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/testscript b/xsd-examples/cxx/tree/multiroot/testscript new file mode 100644 index 0000000..383bed4 --- /dev/null +++ b/xsd-examples/cxx/tree/multiroot/testscript @@ -0,0 +1,6 @@ +# file : cxx/tree/multiroot/testscript +# license : not copyrighted - public domain + +$* $src_base/balance.xml 2>| : balance +$* $src_base/withdraw.xml 2>| : withdraw +$* $src_base/deposit.xml 2>| : deposit 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