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/messaging/.gitignore | 1 + xsd-examples/cxx/tree/messaging/README | 58 +++++++++ xsd-examples/cxx/tree/messaging/balance.xml | 16 +++ xsd-examples/cxx/tree/messaging/buildfile | 29 +++++ xsd-examples/cxx/tree/messaging/deposit.xml | 17 +++ xsd-examples/cxx/tree/messaging/dom-parse.cxx | 93 ++++++++++++++ xsd-examples/cxx/tree/messaging/dom-parse.hxx | 22 ++++ xsd-examples/cxx/tree/messaging/dom-serialize.cxx | 64 ++++++++++ xsd-examples/cxx/tree/messaging/dom-serialize.hxx | 20 +++ xsd-examples/cxx/tree/messaging/driver.cxx | 146 ++++++++++++++++++++++ xsd-examples/cxx/tree/messaging/protocol.xsd | 53 ++++++++ xsd-examples/cxx/tree/messaging/testscript | 6 + xsd-examples/cxx/tree/messaging/withdraw.xml | 17 +++ 13 files changed, 542 insertions(+) create mode 100644 xsd-examples/cxx/tree/messaging/.gitignore create mode 100644 xsd-examples/cxx/tree/messaging/README create mode 100644 xsd-examples/cxx/tree/messaging/balance.xml create mode 100644 xsd-examples/cxx/tree/messaging/buildfile create mode 100644 xsd-examples/cxx/tree/messaging/deposit.xml create mode 100644 xsd-examples/cxx/tree/messaging/dom-parse.cxx create mode 100644 xsd-examples/cxx/tree/messaging/dom-parse.hxx create mode 100644 xsd-examples/cxx/tree/messaging/dom-serialize.cxx create mode 100644 xsd-examples/cxx/tree/messaging/dom-serialize.hxx create mode 100644 xsd-examples/cxx/tree/messaging/driver.cxx create mode 100644 xsd-examples/cxx/tree/messaging/protocol.xsd create mode 100644 xsd-examples/cxx/tree/messaging/testscript create mode 100644 xsd-examples/cxx/tree/messaging/withdraw.xml (limited to 'xsd-examples/cxx/tree/messaging') diff --git a/xsd-examples/cxx/tree/messaging/.gitignore b/xsd-examples/cxx/tree/messaging/.gitignore new file mode 100644 index 0000000..f493b6c --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/.gitignore @@ -0,0 +1 @@ +protocol.?xx diff --git a/xsd-examples/cxx/tree/messaging/README b/xsd-examples/cxx/tree/messaging/README new file mode 100644 index 0000000..435a4cf --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/README @@ -0,0 +1,58 @@ +This example shows how to handle XML vocabularies with multiple +root elements using the element type and element map features +of the C++/Tree mapping. The main purpose of element types is +to distinguish object models with the same root type but with +different root elements. The element map allows uniform parsing +and serialization of multiple root elements. + +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. Note that some request + and response elements are of the same type. + +balance.xml +withdraw.xml +deposit.xml + Sample XML instances for the protocol requests. + +protocol.hxx +protocol.cxx + C++ types that represent the given vocabulary. These are + generated by the XSD compiler from protocol.xsd. Generation of + element types instead of parsing and serialization functions is + requested with the --generate-element-type option. Generation of + the element map is requested with the --generate-element-map + option. + +dom-parse.hxx +dom-parse.cxx + Definition and implementation of the parse() function that + parses an XML document to a DOM document. + +dom-serialize.hxx +dom-serialize.cxx + Definition and implementation of the serialize() function that + serializes a DOM document to XML. + +driver.cxx + Driver for the example. It first calls the above-mentioned parse() + function to parse the input file to a DOM document. It then calls + the parse() function on the element map to parse the root document + element to the object model. The object model is returned as a + pointer to xml_schema::element_type which is a common base type for + all element types. The driver then determines which request it has + received either using RTTI or by comparing the root element names. + Once the request type is determined, information about it is printed + to STDERR and the corresponding response is created. Finally, the + driver serializes the opaque response object to a DOM document + using the element map and then serializes this DOM document to + STDOUT using the above-mentioned serialize() function. + +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/messaging/balance.xml b/xsd-examples/cxx/tree/messaging/balance.xml new file mode 100644 index 0000000..27c1509 --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/balance.xml @@ -0,0 +1,16 @@ + + + + + + + 123456789 + + diff --git a/xsd-examples/cxx/tree/messaging/buildfile b/xsd-examples/cxx/tree/messaging/buildfile new file mode 100644 index 0000000..62fadda --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/buildfile @@ -0,0 +1,29 @@ +# file : cxx/tree/messaging/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 \ + --generate-element-type \ + --generate-element-map \ + --generate-serialization \ + --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/messaging/deposit.xml b/xsd-examples/cxx/tree/messaging/deposit.xml new file mode 100644 index 0000000..5be2a0a --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/deposit.xml @@ -0,0 +1,17 @@ + + + + + + + 123456789 + 1000000 + + diff --git a/xsd-examples/cxx/tree/messaging/dom-parse.cxx b/xsd-examples/cxx/tree/messaging/dom-parse.cxx new file mode 100644 index 0000000..3ae5787 --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/dom-parse.cxx @@ -0,0 +1,93 @@ +// file : cxx/tree/messaging/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/messaging/dom-parse.hxx b/xsd-examples/cxx/tree/messaging/dom-parse.hxx new file mode 100644 index 0000000..b73c52d --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/dom-parse.hxx @@ -0,0 +1,22 @@ +// file : cxx/tree/messaging/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/messaging/dom-serialize.cxx b/xsd-examples/cxx/tree/messaging/dom-serialize.cxx new file mode 100644 index 0000000..b32c5a2 --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/dom-serialize.cxx @@ -0,0 +1,64 @@ +// file : cxx/tree/messaging/dom-serialize.cxx +// copyright : not copyrighted - public domain + +#include "dom-serialize.hxx" + +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include + +using namespace xercesc; +namespace xml = xsd::cxx::xml; +namespace tree = xsd::cxx::tree; + +void +serialize (std::ostream& os, + const xercesc::DOMDocument& doc, + const std::string& encoding) +{ + const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull}; + + // Get an implementation of the Load-Store (LS) interface. + // + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls_id)); + + tree::error_handler eh; + xml::dom::bits::error_handler_proxy ehp (eh); + + xml::dom::ostream_format_target oft (os); + + // Create a DOMSerializer. + // + xml::dom::unique_ptr writer ( + impl->createLSSerializer ()); + + DOMConfiguration* conf (writer->getDomConfig ()); + + // Set error handler. + // + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + + // Set some generally nice features. + // + conf->setParameter (XMLUni::fgDOMWRTDiscardDefaultContent, true); + conf->setParameter (XMLUni::fgDOMWRTFormatPrettyPrint, true); + conf->setParameter (XMLUni::fgDOMWRTXercesPrettyPrint, false); + + xml::dom::unique_ptr out (impl->createLSOutput ()); + out->setEncoding (xml::string (encoding).c_str ()); + out->setByteStream (&oft); + + writer->write (&doc, out.get ()); + + eh.throw_if_failed > (); +} diff --git a/xsd-examples/cxx/tree/messaging/dom-serialize.hxx b/xsd-examples/cxx/tree/messaging/dom-serialize.hxx new file mode 100644 index 0000000..bf1e290 --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/dom-serialize.hxx @@ -0,0 +1,20 @@ +// file : cxx/tree/messaging/dom-serialize.hxx +// copyright : not copyrighted - public domain + +#ifndef DOM_SERIALIZE +#define DOM_SERIALIZE + +#include +#include + +#include + +// Serialize a DOM document to XML which is written to the standard +// output stream. +// +void +serialize (std::ostream& os, + const xercesc::DOMDocument& doc, + const std::string& encoding = "UTF-8"); + +#endif // DOM_SERIALIZE diff --git a/xsd-examples/cxx/tree/messaging/driver.cxx b/xsd-examples/cxx/tree/messaging/driver.cxx new file mode 100644 index 0000000..a358014 --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/driver.cxx @@ -0,0 +1,146 @@ +// file : cxx/tree/messaging/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include +#include +#include +#include + +#include +#include + +#include + +#include "dom-parse.hxx" +#include "dom-serialize.hxx" + +#include "protocol.hxx" + +using namespace std; +using namespace protocol; +using namespace xercesc; + +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. + // + XMLPlatformUtils::Initialize (); + + try + { + ifstream ifs; + ifs.exceptions (ifstream::badbit | ifstream::failbit); + ifs.open (argv[1]); + + unique_ptr req, res; + + // Parse the XML request to a DOM document using the parse() + // function from dom-parse.hxx. + // + { + xml_schema::dom::unique_ptr doc ( + parse (ifs, argv[1], true)); + + DOMElement& root (*doc->getDocumentElement ()); + + req = xml_schema::element_map::parse (root); + } + + // We can test which request we've got either using RTTI or by + // comparing the element names, as shown below. + // + if (balance* b = dynamic_cast (req.get ())) + { + account_t& a (b->value ()); + + cerr << "balance request for acc# " << a.account () << endl; + + res.reset (new success (balance_t (a.account (), 1000))); + } + else if (req->_name () == withdraw::name ()) + { + withdraw& w (static_cast (*req)); + change_t& c (w.value ()); + + wcerr << "withdrawal request for acc# " << c.account () << ", " + << "amount: " << c.amount () << endl; + + if (c.amount () > 1000) + res.reset (new insufficient_funds (balance_t (c.account (), 1000))); + else + res.reset (new success (balance_t (c.account (), 1000 - c.amount ()))); + + } + else if (req->_name () == deposit::name ()) + { + deposit& d (static_cast (*req)); + change_t& c (d.value ()); + + wcerr << "deposit request for acc# " << c.account () << ", " + << "amount: " << c.amount () << endl; + + res.reset (new success (balance_t (c.account (), 1000 + c.amount ()))); + } + + // Serialize the response to a DOM document. + // + namespace xml = xsd::cxx::xml; + + const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull}; + + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls_id)); + + const string& name (res->_name ()); + const string& ns (res->_namespace ()); + + xml_schema::dom::unique_ptr doc ( + impl->createDocument ( + xml::string (ns).c_str (), + xml::string ("p:" + name).c_str (), + 0)); + + xml_schema::element_map::serialize (*doc->getDocumentElement (), *res); + + // Serialize the DOM document to XML using the serialize() function + // from dom-serialize.hxx. + // + cout << "response:" << endl + << endl; + + serialize (cout, *doc); + } + catch (const xml_schema::no_element_info& e) + { + // This exception indicates that we tried to parse or serialize + // an unknown element. + // + cerr << "unknown request: " << e.element_namespace () << "#" << + e.element_name () << endl; + r = 1; + } + 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; + } + + XMLPlatformUtils::Terminate (); + return r; +} diff --git a/xsd-examples/cxx/tree/messaging/protocol.xsd b/xsd-examples/cxx/tree/messaging/protocol.xsd new file mode 100644 index 0000000..4a28ac3 --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/protocol.xsd @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xsd-examples/cxx/tree/messaging/testscript b/xsd-examples/cxx/tree/messaging/testscript new file mode 100644 index 0000000..c9df49f --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/testscript @@ -0,0 +1,6 @@ +# file : cxx/tree/messaging/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/messaging/withdraw.xml b/xsd-examples/cxx/tree/messaging/withdraw.xml new file mode 100644 index 0000000..e9ef526 --- /dev/null +++ b/xsd-examples/cxx/tree/messaging/withdraw.xml @@ -0,0 +1,17 @@ + + + + + + + 123456789 + 1000000 + + -- cgit v1.1