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/library/.gitignore | 1 + xsd-examples/cxx/tree/library/README | 32 ++++++++ xsd-examples/cxx/tree/library/buildfile | 25 ++++++ xsd-examples/cxx/tree/library/driver.cxx | 130 ++++++++++++++++++++++++++++++ xsd-examples/cxx/tree/library/library.xml | 52 ++++++++++++ xsd-examples/cxx/tree/library/library.xsd | 72 +++++++++++++++++ 6 files changed, 312 insertions(+) create mode 100644 xsd-examples/cxx/tree/library/.gitignore create mode 100644 xsd-examples/cxx/tree/library/README create mode 100644 xsd-examples/cxx/tree/library/buildfile create mode 100644 xsd-examples/cxx/tree/library/driver.cxx create mode 100644 xsd-examples/cxx/tree/library/library.xml create mode 100644 xsd-examples/cxx/tree/library/library.xsd (limited to 'xsd-examples/cxx/tree/library') diff --git a/xsd-examples/cxx/tree/library/.gitignore b/xsd-examples/cxx/tree/library/.gitignore new file mode 100644 index 0000000..c116ec1 --- /dev/null +++ b/xsd-examples/cxx/tree/library/.gitignore @@ -0,0 +1 @@ +library.?xx diff --git a/xsd-examples/cxx/tree/library/README b/xsd-examples/cxx/tree/library/README new file mode 100644 index 0000000..0b8638c --- /dev/null +++ b/xsd-examples/cxx/tree/library/README @@ -0,0 +1,32 @@ +This example shows how to use the C++/Tree mapping to parse XML documents +into a tree-like in-memory object model, modify this object model, and +finally serialize it back to XML. + +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.ixx +library.cxx + C++ types that represent the given vocabulary, a set of parsing + functions that convert XML documents to a tree-like in-memory object + model, and a set of serialization functions that convert the object + model back to XML. These are generated by XSD from library.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 prints + the content of the object model to STDERR. Finally, the driver modifies + the object model and serializes it back to XML. + +To run the example on the sample XML instance document simply execute: + +$ ./driver library.xml + +This example also shows how to use the ID/IDREF cross-referencing +mechanism and the xsd:enumeration to C++ enum mapping. diff --git a/xsd-examples/cxx/tree/library/buildfile b/xsd-examples/cxx/tree/library/buildfile new file mode 100644 index 0000000..4ab023b --- /dev/null +++ b/xsd-examples/cxx/tree/library/buildfile @@ -0,0 +1,25 @@ +# file : cxx/tree/library/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{library}: test.input = true + +<{hxx ixx cxx}{library}>: xsd{library} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-tree --std c++11 \ + --generate-inline \ + --generate-ostream \ + --generate-serialization \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" diff --git a/xsd-examples/cxx/tree/library/driver.cxx b/xsd-examples/cxx/tree/library/driver.cxx new file mode 100644 index 0000000..9086891 --- /dev/null +++ b/xsd-examples/cxx/tree/library/driver.cxx @@ -0,0 +1,130 @@ +// file : cxx/tree/library/driver.cxx +// copyright : not copyrighted - public domain + +#include // std::unique_ptr +#include + +#include "library.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " library.xml" << endl; + return 1; + } + + try + { + using namespace library; + + // Read in the XML file and obtain its object model. + // + std::unique_ptr c (catalog_ (argv[1])); + + + // Let's print what we've got. + // + for (catalog::book_const_iterator bi (c->book ().begin ()); + bi != c->book ().end (); + ++bi) + { + cerr << endl + << "ID : " << bi->id () << endl + << "ISBN : " << bi->isbn () << endl + << "Title : " << bi->title () << endl + << "Genre : " << bi->genre () << endl; + + for (book::author_const_iterator ai (bi->author ().begin ()); + ai != bi->author ().end (); + ++ai) + { + cerr << "Author : " << ai->name () << endl; + cerr << " Born : " << ai->born () << endl; + + if (ai->died ()) + cerr << " Died : " << *ai->died () << endl; + + if (ai->recommends ()) + cerr << " Recommends : " << (*ai->recommends ())->title () << endl; + } + + cerr << "Available : " << std::boolalpha << bi->available () << endl; + } + + + // Now we are going to modify the object model and serialize it + // back to XML. + // + + catalog::book_sequence& books (c->book ()); + + + // Get rid of all unavailable books. + // + for (catalog::book_iterator bi (books.begin ()); bi != books.end ();) + { + if (!bi->available ()) + bi = books.erase (bi); + else + ++bi; + } + + + // Insert a new book. + // + book b (679776443, // ISBN + "Dead Souls", // Title + genre::philosophy, // Genre + "DS"); // ID + + b.author ().push_back (author ("Nikolai Gogol", + xml_schema::date (1809, 3, 31))); + + books.insert (books.begin (), b); + + + // Because we removed all unavailable books, some IDREFs might be + // broken. Let's fix this. + // + for (catalog::book_iterator bi (books.begin ()); bi != books.end (); ++bi) + { + for (book::author_iterator ai (bi->author ().begin ()); + ai != bi->author ().end (); + ++ai) + { + author::recommends_optional& c (ai->recommends ()); + + if (c.present ()) + { + author::recommends_type& ref (c.get ()); + + if (!ref) + c.reset (); + } + } + } + + + // Prepare namespace mapping and schema location information. + // + xml_schema::namespace_infomap map; + + map["lib"].name = "http://www.codesynthesis.com/library"; + map["lib"].schema = "library.xsd"; + + + // Write it out. + // + catalog_ (std::cout, *c, map); + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/xsd-examples/cxx/tree/library/library.xml b/xsd-examples/cxx/tree/library/library.xml new file mode 100644 index 0000000..d048aa4 --- /dev/null +++ b/xsd-examples/cxx/tree/library/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/library/library.xsd b/xsd-examples/cxx/tree/library/library.xsd new file mode 100644 index 0000000..4db07ef --- /dev/null +++ b/xsd-examples/cxx/tree/library/library.xsd @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1