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/mixed/.gitignore | 1 + xsd-examples/cxx/tree/mixed/README | 45 ++++++++++++ xsd-examples/cxx/tree/mixed/buildfile | 27 ++++++++ xsd-examples/cxx/tree/mixed/driver.cxx | 122 +++++++++++++++++++++++++++++++++ xsd-examples/cxx/tree/mixed/text.xml | 17 +++++ xsd-examples/cxx/tree/mixed/text.xsd | 28 ++++++++ 6 files changed, 240 insertions(+) create mode 100644 xsd-examples/cxx/tree/mixed/.gitignore create mode 100644 xsd-examples/cxx/tree/mixed/README create mode 100644 xsd-examples/cxx/tree/mixed/buildfile create mode 100644 xsd-examples/cxx/tree/mixed/driver.cxx create mode 100644 xsd-examples/cxx/tree/mixed/text.xml create mode 100644 xsd-examples/cxx/tree/mixed/text.xsd (limited to 'xsd-examples/cxx/tree/mixed') diff --git a/xsd-examples/cxx/tree/mixed/.gitignore b/xsd-examples/cxx/tree/mixed/.gitignore new file mode 100644 index 0000000..cbeac00 --- /dev/null +++ b/xsd-examples/cxx/tree/mixed/.gitignore @@ -0,0 +1 @@ +text.?xx diff --git a/xsd-examples/cxx/tree/mixed/README b/xsd-examples/cxx/tree/mixed/README new file mode 100644 index 0000000..fc23faa --- /dev/null +++ b/xsd-examples/cxx/tree/mixed/README @@ -0,0 +1,45 @@ +This example shows how to access the underlying DOM nodes in the +C++/Tree mapping in order to handle raw, "type-less content" such +as mixed content models, anyType/anySimpleType, and any/anyAttribute. + +For an alternative (and recommended) approach that employs ordered +types see the order/mixed example. + +For an alternative approach that employes type customization see +examples in the custom/ directory, in particular, custom/mixed and +custom/wildcard. + +In this example we use mixed content model to describe text with +embedded links, e.g., + + This paragraph talks about time. + +The example transforms such text into plain text with references, e.g., + + This paragraph talks about time[0]. + + [0] uri + +The example consists of the following files: + +text.xsd + XML Schema which describes "text with links" instance documents. + +text.xml + Sample XML instance document. + +text.hxx +text.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 text.xsd. + +driver.cxx + Driver for the example. It first calls one of the parsing functions + that constructs the object model from the input file. It then uses + both the underlying DOM and statically-typed mapping to perform the + transformation. + +To run the example on the sample XML instance document simply execute: + +$ ./driver text.xml diff --git a/xsd-examples/cxx/tree/mixed/buildfile b/xsd-examples/cxx/tree/mixed/buildfile new file mode 100644 index 0000000..4c1be8e --- /dev/null +++ b/xsd-examples/cxx/tree/mixed/buildfile @@ -0,0 +1,27 @@ +# file : cxx/tree/mixed/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}{* -text} {hxx ixx cxx}{text} $libs + +exe{driver}: xml{text}: test.input = true + +<{hxx ixx cxx}{text}>: xsd{text} $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/mixed/driver.cxx b/xsd-examples/cxx/tree/mixed/driver.cxx new file mode 100644 index 0000000..cf64e7f --- /dev/null +++ b/xsd-examples/cxx/tree/mixed/driver.cxx @@ -0,0 +1,122 @@ +// file : cxx/tree/mixed/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include + +#include + +#include "text.hxx" + +// The following transcode() utility function is handy when working with +// Xerces. Include it after the generated header in order to get only char +// or wchar_t version depending on how you compiled your schemas. +// +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::unique_ptr; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " text.xml" << endl; + return 1; + } + + using namespace xercesc; + + int r (0); + + // The Xerces-C++ DOM objects that will be associated with the + // document tree "out-live" the call to the parsing function. + // Therefore we need to initialize the Xerces-C++ runtime + // ourselves. + // + XMLPlatformUtils::Initialize (); + + try + { + unique_ptr t ( + text_ (argv[1], + xml_schema::flags::keep_dom | + xml_schema::flags::dont_initialize)); + + // The DOM association can be recreated in a copy (the underlying + // DOM document is cloned) if explicitly requested with the keep_dom + // flag and only if this copy is "complete", i.e., made from the root + // of the tree. + // + text copy (*t, xml_schema::flags::keep_dom); + + // Print text. + // + { + namespace xml = xsd::cxx::xml; + + unsigned long ref (0); + DOMNode* root (copy._node ()); + + for (DOMNode* n (root->getFirstChild ()); + n != 0; + n = n->getNextSibling ()) + { + switch (n->getNodeType ()) + { + case DOMNode::TEXT_NODE: + { + cout << xml::transcode (n->getTextContent ()); + break; + } + case DOMNode::ELEMENT_NODE: + { + // Let's get back to a tree node from this DOM node. + // + xml_schema::type& t ( + *reinterpret_cast ( + n->getUserData (xml_schema::dom::tree_node_key))); + + anchor& a (dynamic_cast (t)); + + cout << a << "[" << ref << "]"; + + // Or we could continue using DOM interface: + // + //cout << xml::transcode (n->getTextContent ()) + // << "[" << ref << "]"; + + ++ref; + break; + } + default: + break; // Ignore all other nodes (e.g., comments, etc). + } + } + } + + // Print references. + // + { + unsigned long r (0); + + for (text::a_const_iterator i (copy.a ().begin ()); + i != copy.a ().end (); + ++i, ++r) + { + cout << "[" << r << "] " << i->href () << endl; + } + } + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + r = 1; + } + + XMLPlatformUtils::Terminate (); + return r; +} diff --git a/xsd-examples/cxx/tree/mixed/text.xml b/xsd-examples/cxx/tree/mixed/text.xml new file mode 100644 index 0000000..dfe730e --- /dev/null +++ b/xsd-examples/cxx/tree/mixed/text.xml @@ -0,0 +1,17 @@ + + + + + + +The first paragraph of this text talks about time. + +And this paragraph talks about space. + + diff --git a/xsd-examples/cxx/tree/mixed/text.xsd b/xsd-examples/cxx/tree/mixed/text.xsd new file mode 100644 index 0000000..f3c023d --- /dev/null +++ b/xsd-examples/cxx/tree/mixed/text.xsd @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1