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/caching/.gitignore | 1 + xsd-examples/cxx/tree/caching/README | 29 ++++++ xsd-examples/cxx/tree/caching/buildfile | 27 +++++ xsd-examples/cxx/tree/caching/driver.cxx | 165 ++++++++++++++++++++++++++++++ xsd-examples/cxx/tree/caching/library.xml | 52 ++++++++++ xsd-examples/cxx/tree/caching/library.xsd | 72 +++++++++++++ 6 files changed, 346 insertions(+) create mode 100644 xsd-examples/cxx/tree/caching/.gitignore create mode 100644 xsd-examples/cxx/tree/caching/README create mode 100644 xsd-examples/cxx/tree/caching/buildfile create mode 100644 xsd-examples/cxx/tree/caching/driver.cxx create mode 100644 xsd-examples/cxx/tree/caching/library.xml create mode 100644 xsd-examples/cxx/tree/caching/library.xsd (limited to 'xsd-examples/cxx/tree/caching') diff --git a/xsd-examples/cxx/tree/caching/.gitignore b/xsd-examples/cxx/tree/caching/.gitignore new file mode 100644 index 0000000..c116ec1 --- /dev/null +++ b/xsd-examples/cxx/tree/caching/.gitignore @@ -0,0 +1 @@ +library.?xx diff --git a/xsd-examples/cxx/tree/caching/README b/xsd-examples/cxx/tree/caching/README new file mode 100644 index 0000000..64dffb3 --- /dev/null +++ b/xsd-examples/cxx/tree/caching/README @@ -0,0 +1,29 @@ +This example shows how to use the C++/Tree mapping to parse several +XML documents while reusing the underlying XML parser and caching the +schemas used for validation. + +The example consists of the following files: + +library.xsd + XML Schema which describes a library of books. + +library.xml + Sample XML instance document. + +library.hxx +library.cxx + C++ types that represent the given vocabulary and a set of parsing + functions that convert XML instance documents to a tree-like in-memory + object model. These are generated by XSD from library.xsd. + +driver.cxx + Driver for the example. It first sets up the Xerces-C++ DOM parser + and caches the library.xsd schema for validation. It then performs + ten iterations that parse the input file to a DOM document using + the DOM parser and call one of the parsing functions that constructs + the object model from this DOM document. On each iteration the driver + prints a number of books in the object model to STDERR. + +To run the example on the sample XML instance document simply execute: + +$ ./driver library.xml library.xsd diff --git a/xsd-examples/cxx/tree/caching/buildfile b/xsd-examples/cxx/tree/caching/buildfile new file mode 100644 index 0000000..cb369b7 --- /dev/null +++ b/xsd-examples/cxx/tree/caching/buildfile @@ -0,0 +1,27 @@ +# file : cxx/tree/caching/buildfile +# license : not copyrighted - public domain + +import libs = libxsd%lib{xsd} +import libs += libxerces-c%lib{xerces-c} + +./: exe{driver} doc{README} + +exe{driver}: {hxx cxx}{* -library} {hxx ixx cxx}{library} $libs + +exe{driver}: {xml xsd}{library}: test.input = true + +<{hxx ixx cxx}{library}>: xsd{library} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-tree --std c++11 \ + --generate-inline \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" + +# Define XSD_CXX11 since we include libxsd headers directly. +# +cxx.poptions += -DXSD_CXX11 diff --git a/xsd-examples/cxx/tree/caching/driver.cxx b/xsd-examples/cxx/tree/caching/driver.cxx new file mode 100644 index 0000000..9cabbd4 --- /dev/null +++ b/xsd-examples/cxx/tree/caching/driver.cxx @@ -0,0 +1,165 @@ +// file : cxx/tree/caching/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include +#include + +#include +#include // chLatin_* +#include +#include // xercesc::Grammar +#include + +#include +#include +#include + +#include + +#include "library.hxx" + +using namespace std; + +int +main (int argc, char* argv[]) +{ + if (argc != 3) + { + cerr << "usage: " << argv[0] << " library.xml library.xsd" << 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 + { + using namespace xercesc; + namespace xml = xsd::cxx::xml; + namespace tree = xsd::cxx::tree; + + 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 validation. + // + conf->setParameter (XMLUni::fgDOMValidate, true); + conf->setParameter (XMLUni::fgXercesSchema, true); + 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 + + // Set error handler. + // + tree::error_handler eh; + xml::dom::bits::error_handler_proxy ehp (eh); + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + + // Initialize the schema cache. + // + if (!parser->loadGrammar (argv[2], Grammar::SchemaGrammarType, true)) + { + // In Xerces-C++ grammar loading failure results in just a warning. + // Make it a fatal error. + // + eh.handle (argv[2], 0, 0, + tree::error_handler::severity::fatal, + "unable to load schema"); + } + + eh.throw_if_failed (); + + // Use the loaded grammar during parsing. + // + conf->setParameter (XMLUni::fgXercesUseCachedGrammarInParse, true); + + // Disable loading schemas via other means (e.g., schemaLocation). + // + conf->setParameter (XMLUni::fgXercesLoadSchema, false); + + // We will release the DOM document ourselves. + // + conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + // Parse XML documents. + // + for (unsigned long i (0); i < 10; ++i) + { + ifstream ifs; + ifs.exceptions (ifstream::badbit | ifstream::failbit); + ifs.open (argv[1]); + + // Wrap the standard input stream. + // + xml::sax::std_input_source isrc (ifs, argv[1]); + Wrapper4InputSource wrap (&isrc, false); + + // Parse XML to DOM. + // + xml_schema::dom::unique_ptr doc (parser->parse (&wrap)); + + eh.throw_if_failed (); + + // Parse DOM to the object model. + // + unique_ptr c (library::catalog_ (*doc)); + + cerr << "catalog with " << c->book ().size () << " books" << 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/caching/library.xml b/xsd-examples/cxx/tree/caching/library.xml new file mode 100644 index 0000000..b36786f --- /dev/null +++ b/xsd-examples/cxx/tree/caching/library.xml @@ -0,0 +1,52 @@ + + + + + + + + 0679760806 + The Master and Margarita + fiction + + + Mikhail Bulgakov + 1891-05-15 + 1940-03-10 + + + + + + 0679600841 + War and Peace + history + + + Leo Tolstoy + 1828-09-09 + 1910-11-20 + + + + + + 0679420290 + Crime and Punishment + philosophy + + + Fyodor Dostoevsky + 1821-11-11 + 1881-02-09 + + + + diff --git a/xsd-examples/cxx/tree/caching/library.xsd b/xsd-examples/cxx/tree/caching/library.xsd new file mode 100644 index 0000000..2dd5037 --- /dev/null +++ b/xsd-examples/cxx/tree/caching/library.xsd @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1