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/order/README | 11 ++ xsd-examples/cxx/tree/order/element/.gitignore | 1 + xsd-examples/cxx/tree/order/element/README | 35 +++++ xsd-examples/cxx/tree/order/element/buildfile | 30 +++++ xsd-examples/cxx/tree/order/element/driver.cxx | 147 +++++++++++++++++++++ .../cxx/tree/order/element/transactions.xml | 32 +++++ .../cxx/tree/order/element/transactions.xsd | 58 ++++++++ xsd-examples/cxx/tree/order/mixed/.gitignore | 1 + xsd-examples/cxx/tree/order/mixed/README | 45 +++++++ xsd-examples/cxx/tree/order/mixed/buildfile | 25 ++++ xsd-examples/cxx/tree/order/mixed/driver.cxx | 89 +++++++++++++ xsd-examples/cxx/tree/order/mixed/text.xml | 17 +++ xsd-examples/cxx/tree/order/mixed/text.xsd | 28 ++++ 13 files changed, 519 insertions(+) create mode 100644 xsd-examples/cxx/tree/order/README create mode 100644 xsd-examples/cxx/tree/order/element/.gitignore create mode 100644 xsd-examples/cxx/tree/order/element/README create mode 100644 xsd-examples/cxx/tree/order/element/buildfile create mode 100644 xsd-examples/cxx/tree/order/element/driver.cxx create mode 100644 xsd-examples/cxx/tree/order/element/transactions.xml create mode 100644 xsd-examples/cxx/tree/order/element/transactions.xsd create mode 100644 xsd-examples/cxx/tree/order/mixed/.gitignore create mode 100644 xsd-examples/cxx/tree/order/mixed/README create mode 100644 xsd-examples/cxx/tree/order/mixed/buildfile create mode 100644 xsd-examples/cxx/tree/order/mixed/driver.cxx create mode 100644 xsd-examples/cxx/tree/order/mixed/text.xml create mode 100644 xsd-examples/cxx/tree/order/mixed/text.xsd (limited to 'xsd-examples/cxx/tree/order') diff --git a/xsd-examples/cxx/tree/order/README b/xsd-examples/cxx/tree/order/README new file mode 100644 index 0000000..7125a2d --- /dev/null +++ b/xsd-examples/cxx/tree/order/README @@ -0,0 +1,11 @@ +This directory contains a number of examples that show how to use ordered +types to capture and maintain content order. The following list gives an +overview of each example: + +element + Shows how to use ordered types to capture and maintain element order, + including element wildcards. + +mixed + Shows how to use ordered types to capture mixed content text and to + maintain order information between elements and text. diff --git a/xsd-examples/cxx/tree/order/element/.gitignore b/xsd-examples/cxx/tree/order/element/.gitignore new file mode 100644 index 0000000..2fff792 --- /dev/null +++ b/xsd-examples/cxx/tree/order/element/.gitignore @@ -0,0 +1 @@ +transactions.?xx diff --git a/xsd-examples/cxx/tree/order/element/README b/xsd-examples/cxx/tree/order/element/README new file mode 100644 index 0000000..19f2381 --- /dev/null +++ b/xsd-examples/cxx/tree/order/element/README @@ -0,0 +1,35 @@ +This example shows how to use ordered types to capture and maintain +element order, including element wildcards. + +The example consists of the following files: + +transactions.xsd + XML Schema which describes various bank transactions. A batch of + transactions can contain any number of different transactions in + any order but the order of transaction in the batch is significant. + +library.xml + Sample XML instance document. + +transactions.hxx +transactions.cxx + C++ types that represent the given vocabulary as well as a set of + parsing and serialization functions. These are generated by XSD + from transactions.xsd. Note that the --ordered-type option is + used to indicate to the XSD compiler that the batch type is + ordered. We also use the --generate-wildcard option to enable + wildcard support. An element wildcard is used in the batch to + allow transaction extensions. + +driver.cxx + Driver for the example. It first calls one of the parsing functions + that constructs the object model from the input XML file. It then + iterates over transactions in the batch using the content order + sequence. The driver then performs various modifications of the + object model while showing how to maintain the content order. + Finally, it saves the modified transaction batch back to XML to + verify that the content order is preserved in the output document. + +To run the example on the sample XML instance document simply execute: + +$ ./driver transactions.xml diff --git a/xsd-examples/cxx/tree/order/element/buildfile b/xsd-examples/cxx/tree/order/element/buildfile new file mode 100644 index 0000000..eb3834e --- /dev/null +++ b/xsd-examples/cxx/tree/order/element/buildfile @@ -0,0 +1,30 @@ +# file : cxx/tree/order/element/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}{* -transactions} {hxx ixx cxx}{transactions} $libs + +exe{driver}: xml{transactions}: test.input = true + +<{hxx ixx cxx}{transactions}>: xsd{transactions} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-tree --std c++11 \ + --generate-inline \ + --generate-serialization \ + --generate-wildcard \ + --ordered-type batch \ + --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/order/element/driver.cxx b/xsd-examples/cxx/tree/order/element/driver.cxx new file mode 100644 index 0000000..2f62700 --- /dev/null +++ b/xsd-examples/cxx/tree/order/element/driver.cxx @@ -0,0 +1,147 @@ +// file : cxx/tree/order/element/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include +#include + +#include +#include + +#include "transactions.hxx" + +// The following string class keeps us sane 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::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " transactions.xml" << endl; + return 1; + } + + using namespace xercesc; + + int r (0); + + // The Xerces-C++ DOM objects that will be used to store the + // content matched by the wildcard "out-lives" the call to the + // parsing function. Therefore we need to initialize the + // Xerces-C++ runtime ourselves. + // + XMLPlatformUtils::Initialize (); + + try + { + using namespace transactions; + + // Parse the batch. + // + std::unique_ptr b ( + batch_ (argv[1], xml_schema::flags::dont_initialize)); + + // Print what we've got in content order. + // + for (batch::content_order_const_iterator i (b->content_order ().begin ()); + i != b->content_order ().end (); + ++i) + { + switch (i->id) + { + case batch::balance_id: + { + const balance& t (b->balance ()[i->index]); + cerr << t.account () << " balance" << endl; + break; + } + case batch::withdraw_id: + { + const withdraw& t (b->withdraw ()[i->index]); + cerr << t.account () << " withdraw " << t.amount () << endl; + break; + } + case batch::deposit_id: + { + const deposit& t (b->deposit ()[i->index]); + cerr << t.account () << " deposit " << t.amount () << endl; + break; + } + case batch::any_id: + { + namespace xml = xsd::cxx::xml; + + const DOMElement& e (b->any ()[i->index]); + cerr << xml::transcode (e.getLocalName ()) << endl; + break; + } + default: + { + assert (false); // Unknown content id. + } + } + } + + cerr << endl; + + // Modify the transaction batch. First remove the last transaction. + // Note that we have to update both the content itself and content + // order sequences. + // + batch::content_order_sequence& co (b->content_order ()); + + co.pop_back (); + b->withdraw ().pop_back (); + + // Now add a few more transactions. Again we have to add both the + // content and its ordering. The order information consists of the + // content id and, in case of a sequence, the index. + // + b->deposit ().push_back (deposit (123456789, 100000)); + co.push_back ( + batch::content_order_type ( + batch::deposit_id, b->deposit ().size () - 1)); + + // The next transaction we add at the beginning of the batch. + // + b->balance ().push_back (balance (123456789)); + co.insert (co.begin (), + batch::content_order_type ( + batch::balance_id, b->balance ().size () - 1)); + + // Note also that when we merely modify the content of one + // of the elements in place, we don't need to update its + // order. For example: + // + b->deposit ()[0].amount (2000000); + + // Serialize the modified transaction batch back to XML. + // + xml_schema::namespace_infomap map; + + map[""].name = "http://www.codesynthesis.com/transactions"; + map[""].schema = "transactions.xsd"; + map["te"].name = "http://www.codesynthesis.com/transactions-extras"; + + batch_ (std::cout, + *b, + map, + "UTF-8", + xml_schema::flags::dont_initialize); + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + r = 1; + } + + XMLPlatformUtils::Terminate (); + return r; +} diff --git a/xsd-examples/cxx/tree/order/element/transactions.xml b/xsd-examples/cxx/tree/order/element/transactions.xml new file mode 100644 index 0000000..7af9ab1 --- /dev/null +++ b/xsd-examples/cxx/tree/order/element/transactions.xml @@ -0,0 +1,32 @@ + + + + + + + 123456789 + 1000000 + + + + 123456789 + + + + 123456789 + 500000 + + + + 123456789 + 500000 + + diff --git a/xsd-examples/cxx/tree/order/element/transactions.xsd b/xsd-examples/cxx/tree/order/element/transactions.xsd new file mode 100644 index 0000000..7a63c9f --- /dev/null +++ b/xsd-examples/cxx/tree/order/element/transactions.xsd @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xsd-examples/cxx/tree/order/mixed/.gitignore b/xsd-examples/cxx/tree/order/mixed/.gitignore new file mode 100644 index 0000000..cbeac00 --- /dev/null +++ b/xsd-examples/cxx/tree/order/mixed/.gitignore @@ -0,0 +1 @@ +text.?xx diff --git a/xsd-examples/cxx/tree/order/mixed/README b/xsd-examples/cxx/tree/order/mixed/README new file mode 100644 index 0000000..e66c1ad --- /dev/null +++ b/xsd-examples/cxx/tree/order/mixed/README @@ -0,0 +1,45 @@ +This example shows how to use ordered types to capture mixed content +text and to maintain order information between elements and text. + +In this example we use mixed content model to describe text with +embedded links in the form: + + This paragraph talks about time. + +The example transforms such text into plain text with references in +the form: + + This paragraph talks about time[uri]. + +It also saves the modified text back to XML in order to verify the +element and text order. + +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 as well as a set of + parsing and serialization functions. These are generated by XSD + from text.xsd. Note that the --ordered-type-mixed option is used + to indicate to the XSD compiler that all types with mixed content + should be automatically treated as ordered. + +driver.cxx + Driver for the example. It first calls one of the parsing functions + that constructs the object model from the input XML file. It then + iterates over the text and elements in the content order to convert + the document to its plain text representation. The driver then adds + another paragraph of text and a link to the object model while showing + how to maintain the content order. Finally, it saves the modified + text back to XML to verify that the content order is preserved in + the output document. + +To run the example on the sample XML instance document simply execute: + +$ ./driver text.xml diff --git a/xsd-examples/cxx/tree/order/mixed/buildfile b/xsd-examples/cxx/tree/order/mixed/buildfile new file mode 100644 index 0000000..149bca0 --- /dev/null +++ b/xsd-examples/cxx/tree/order/mixed/buildfile @@ -0,0 +1,25 @@ +# file : cxx/tree/order/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 \ + --generate-serialization \ + --ordered-type-mixed \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" diff --git a/xsd-examples/cxx/tree/order/mixed/driver.cxx b/xsd-examples/cxx/tree/order/mixed/driver.cxx new file mode 100644 index 0000000..a18f4ad --- /dev/null +++ b/xsd-examples/cxx/tree/order/mixed/driver.cxx @@ -0,0 +1,89 @@ +// file : cxx/tree/order/mixed/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include +#include + +#include "text.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " text.xml" << endl; + return 1; + } + + try + { + std::unique_ptr t (text_ (argv[1])); + + // Print what we've got in content order. + // + for (text::content_order_const_iterator i (t->content_order ().begin ()); + i != t->content_order ().end (); + ++i) + { + switch (i->id) + { + case text::a_id: + { + const anchor& a (t->a ()[i->index]); + cerr << a << "[" << a.href () << "]"; + break; + } + case text::text_content_id: + { + const xml_schema::string& s (t->text_content ()[i->index]); + cerr << s; + break; + } + default: + { + assert (false); // Unknown content id. + } + } + } + + cerr << endl; + + // Modify the document. Note that we have to update both the content + // itself and content order sequences. + // + typedef text::content_order_type order_type; + + text::content_order_sequence& co (t->content_order ()); + text::text_content_sequence& tc (t->text_content ()); + + tc.push_back ("The last paragraph doesn't talk about "); + co.push_back (order_type (text::text_content_id, tc.size () - 1)); + + t->a ().push_back (anchor ("anything", "http://en.wikipedia.org")); + co.push_back (order_type (text::a_id, t->a ().size () - 1)); + + tc.push_back (" in particular.\n\n"); + co.push_back (order_type (text::text_content_id, tc.size () - 1)); + + // Serialize the modified document back to XML. + // + xml_schema::namespace_infomap map; + + map[""].schema = "text.xsd"; + + text_ (std::cout, + *t, + map, + "UTF-8", + xml_schema::flags::dont_pretty_print); + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/xsd-examples/cxx/tree/order/mixed/text.xml b/xsd-examples/cxx/tree/order/mixed/text.xml new file mode 100644 index 0000000..3d7476d --- /dev/null +++ b/xsd-examples/cxx/tree/order/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/order/mixed/text.xsd b/xsd-examples/cxx/tree/order/mixed/text.xsd new file mode 100644 index 0000000..b55d214 --- /dev/null +++ b/xsd-examples/cxx/tree/order/mixed/text.xsd @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1