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/parser/library/README | 44 ++++ xsd-examples/cxx/parser/library/buildfile | 25 +++ xsd-examples/cxx/parser/library/driver.cxx | 109 ++++++++++ xsd-examples/cxx/parser/library/library-pimpl.cxx | 183 ++++++++++++++++ xsd-examples/cxx/parser/library/library-pimpl.hxx | 135 ++++++++++++ xsd-examples/cxx/parser/library/library.hxx | 241 ++++++++++++++++++++++ xsd-examples/cxx/parser/library/library.map | 15 ++ xsd-examples/cxx/parser/library/library.xml | 52 +++++ xsd-examples/cxx/parser/library/library.xsd | 78 +++++++ 9 files changed, 882 insertions(+) create mode 100644 xsd-examples/cxx/parser/library/README create mode 100644 xsd-examples/cxx/parser/library/buildfile create mode 100644 xsd-examples/cxx/parser/library/driver.cxx create mode 100644 xsd-examples/cxx/parser/library/library-pimpl.cxx create mode 100644 xsd-examples/cxx/parser/library/library-pimpl.hxx create mode 100644 xsd-examples/cxx/parser/library/library.hxx create mode 100644 xsd-examples/cxx/parser/library/library.map create mode 100644 xsd-examples/cxx/parser/library/library.xml create mode 100644 xsd-examples/cxx/parser/library/library.xsd (limited to 'xsd-examples/cxx/parser/library') diff --git a/xsd-examples/cxx/parser/library/README b/xsd-examples/cxx/parser/library/README new file mode 100644 index 0000000..3f515f6 --- /dev/null +++ b/xsd-examples/cxx/parser/library/README @@ -0,0 +1,44 @@ +This example shows how to use the C++/Parser mapping to construct +a custom in-memory object model from XML instance documents. + +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 + Types that describe a library of books in C++. These are + hand-written. + +library.map + Type map. It maps XML Schema types defined in library.xsd + to the C++ types defined in library.hxx. + +library-pskel.hxx +library-pskel.ixx +library-pskel.cxx + Parser skeletons generated by XSD from library.xsd and + library.map. + +library-pimpl.hxx +library-pimpl.cxx + Parser implementations that construct the custom in-memory + object model from an XML instance using the types from + library.hxx. These are hand-written implementations of + the parser skeletons defined in library-pskel.hxx. + +driver.cxx + Driver for the example. It first constructs a parser + instance from all the individual parsers found in + library-pimpl.hxx. In then invokes this parser instance + to parse the input file and produce the in-memory + object model. Finally, it prints the contents of the + in-memory object model to STDERR. + +To run the example on the sample XML instance document simply +execute: + +$ ./driver library.xml diff --git a/xsd-examples/cxx/parser/library/buildfile b/xsd-examples/cxx/parser/library/buildfile new file mode 100644 index 0000000..40810b3 --- /dev/null +++ b/xsd-examples/cxx/parser/library/buildfile @@ -0,0 +1,25 @@ +# file : cxx/parser/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-pskel} {hxx ixx cxx}{library-pskel} $libs + +exe{driver}: xml{library}: test.input = true + +<{hxx ixx cxx}{library-pskel}>: xsd{library} map{library} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-parser --std c++11 \ + --generate-inline \ + --skel-file-suffix -pskel \ + --type-map $path($<[1]) \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" "-I$src_base" diff --git a/xsd-examples/cxx/parser/library/driver.cxx b/xsd-examples/cxx/parser/library/driver.cxx new file mode 100644 index 0000000..d718e53 --- /dev/null +++ b/xsd-examples/cxx/parser/library/driver.cxx @@ -0,0 +1,109 @@ +// file : cxx/parser/library/driver.cxx +// copyright : not copyrighted - public domain + +#include + +#include "library.hxx" +#include "library-pimpl.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; + + + // Construct the parser. + // + xml_schema::id_pimpl id_p; + xml_schema::idref_pimpl idref_p; + xml_schema::string_pimpl string_p; + xml_schema::boolean_pimpl boolean_p; + + isbn_pimpl isbn_p; + + title_pimpl title_p; + title_p.lang_parser (string_p); + + genre_pimpl genre_p; + + author_pimpl author_p; + author_p.parsers (string_p, // name + string_p, // born + string_p, // died + idref_p); // recommends + + book_pimpl book_p; + book_p.parsers (isbn_p, // isbn + title_p, // title + genre_p, // genre + author_p, // author + boolean_p, // available + id_p); // id + + catalog_pimpl catalog_p; + catalog_p.book_parser (book_p); + + + // Parse the XML instance document. + // + xml_schema::document doc_p ( + catalog_p, + "http://www.codesynthesis.com/library", // root element namespace + "catalog"); // root element name + + catalog_p.pre (); + doc_p.parse (argv[1]); + catalog c (catalog_p.post_catalog ()); + + + // Let's print what we've got. + // + for (catalog::const_iterator bi (c.begin ()); bi != c.end (); ++bi) + { + cerr << endl + << "ID : " << bi->id () << endl + << "ISBN : " << bi->isbn () << endl + << "Title : " << bi->title () << endl + << "Genre : " << bi->genre () << endl; + + for (book::authors::const_iterator ai (bi->author ().begin ()); + ai != bi->author ().end (); + ++ai) + { + cerr << "Author : " << ai->name () << endl; + cerr << " Born : " << ai->born () << endl; + + if (!ai->died ().empty ()) + cerr << " Died : " << ai->died () << endl; + + if (!ai->recommends ().empty ()) + { + cerr << " Recommends : " << ai->recommends () << endl; + } + } + + cerr << "Available : " << std::boolalpha << bi->available () << endl; + } + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << argv[1] << ": unable to open or read failure" << endl; + return 1; + } +} diff --git a/xsd-examples/cxx/parser/library/library-pimpl.cxx b/xsd-examples/cxx/parser/library/library-pimpl.cxx new file mode 100644 index 0000000..852b90d --- /dev/null +++ b/xsd-examples/cxx/parser/library/library-pimpl.cxx @@ -0,0 +1,183 @@ +// file : cxx/parser/library/library-pimpl.cxx +// copyright : not copyrighted - public domain + +#include "library-pimpl.hxx" + +namespace library +{ + // isbn_impl + // + isbn isbn_pimpl:: + post_isbn () + { + return post_unsigned_int (); + } + + // title_pimpl + // + void title_pimpl:: + _pre () + { + title_.lang (""); + } + + void title_pimpl:: + lang (const std::string& lang) + { + title_.lang (lang); + } + + title title_pimpl:: + post_title () + { + title_.assign (post_string ()); + return title_; + } + + // genre_pimpl + // + genre genre_pimpl:: + post_genre () + { + genre r (romance); + std::string v (post_string ()); + + if (v == "romance") r = romance; else + if (v == "fiction") r = fiction; else + if (v == "horror") r = horror; else + if (v == "history") r = history; else + if (v == "philosophy") r = philosophy; + + return r; + } + + // person_pimpl + // + void person_pimpl:: + _pre () + { + person_.died (""); + } + + void person_pimpl:: + name (const std::string& name) + { + person_.name (name); + } + + void person_pimpl:: + born (const std::string& born) + { + person_.born (born); + } + + void person_pimpl:: + died (const std::string& died) + { + person_.died (died); + } + + person person_pimpl:: + post_person () + { + return person_; + } + + // author_pimpl + // + void author_pimpl:: + _pre () + { + person_pimpl::_pre (); + author_.recommends (""); + } + + void author_pimpl:: + recommends (const std::string& recommends) + { + author_.recommends (recommends); + } + + author author_pimpl:: + post_author () + { + person p (post_person ()); + + author_.name (p.name ()); + author_.born (p.born ()); + author_.died (p.died ()); + + return author_; + } + + // book_pimpl + // + void book_pimpl:: + _pre () + { + book_.author ().clear (); + } + + void book_pimpl:: + isbn (library::isbn isbn) + { + book_.isbn (isbn); + } + + void book_pimpl:: + title (const library::title& title) + { + book_.title (title); + } + + void book_pimpl:: + genre (library::genre genre) + { + book_.genre (genre); + } + + void book_pimpl:: + author (const library::author& author) + { + book_.author ().push_back (author); + } + + void book_pimpl:: + available (bool available) + { + book_.available (available); + } + + void book_pimpl:: + id (const std::string& id) + { + book_.id (id); + } + + book book_pimpl:: + post_book () + { + return book_; + } + + // catalog_pimpl + // + void catalog_pimpl:: + _pre () + { + catalog_.clear (); + } + + void catalog_pimpl:: + book (const library::book& book) + { + catalog_.push_back (book); + } + + catalog catalog_pimpl:: + post_catalog () + { + return catalog_; + } +} + diff --git a/xsd-examples/cxx/parser/library/library-pimpl.hxx b/xsd-examples/cxx/parser/library/library-pimpl.hxx new file mode 100644 index 0000000..fb8ba24 --- /dev/null +++ b/xsd-examples/cxx/parser/library/library-pimpl.hxx @@ -0,0 +1,135 @@ +// file : cxx/parser/library/library-pimpl.hxx +// copyright : not copyrighted - public domain + +#ifndef LIBRARY_PIMPL_HXX +#define LIBRARY_PIMPL_HXX + +#include "library.hxx" +#include "library-pskel.hxx" + +namespace library +{ + // + // + struct isbn_pimpl: isbn_pskel, xml_schema::unsigned_int_pimpl + { + virtual isbn + post_isbn (); + }; + + // + // + struct title_pimpl: title_pskel, xml_schema::string_pimpl + { + virtual void + _pre (); + + virtual void + lang (const std::string&); + + virtual title + post_title (); + + private: + title title_; + }; + + // + // + struct genre_pimpl: genre_pskel, xml_schema::string_pimpl + { + virtual genre + post_genre (); + }; + + // + // + struct person_pimpl: virtual person_pskel + { + virtual void + _pre (); + + virtual void + name (const std::string&); + + virtual void + born (const std::string&); + + virtual void + died (const std::string&); + + virtual person + post_person (); + + private: + person person_; + }; + + // + // + struct author_pimpl: author_pskel, person_pimpl + { + virtual void + _pre (); + + virtual void + recommends (const std::string&); + + virtual author + post_author (); + + private: + author author_; + }; + + // + // + struct book_pimpl: book_pskel + { + virtual void + _pre (); + + virtual void + isbn (library::isbn); + + virtual void + title (const library::title&); + + virtual void + genre (library::genre); + + virtual void + author (const library::author&); + + virtual void + available (bool); + + virtual void + id (const std::string&); + + virtual book + post_book (); + + private: + book book_; + }; + + // + // + struct catalog_pimpl: catalog_pskel + { + virtual void + _pre (); + + virtual void + book (const library::book&); + + virtual catalog + post_catalog (); + + private: + catalog catalog_; + }; +} + +#endif // LIBRARY_PIMPL_HXX diff --git a/xsd-examples/cxx/parser/library/library.hxx b/xsd-examples/cxx/parser/library/library.hxx new file mode 100644 index 0000000..844e474 --- /dev/null +++ b/xsd-examples/cxx/parser/library/library.hxx @@ -0,0 +1,241 @@ +// file : cxx/parser/library/library.hxx +// copyright : not copyrighted - public domain + +#ifndef LIBRARY_HXX +#define LIBRARY_HXX + +#include +#include + +namespace library +{ + // + // + typedef unsigned int isbn; + + + // + // + struct title: std::string + { + // lang + // + const std::string& + lang () const + { + return lang_; + } + + void + lang (const std::string& lang) + { + lang_ = lang; + } + + private: + std::string lang_; + }; + + + // + // + enum genre + { + romance, + fiction, + horror, + history, + philosophy + }; + + + // + // + struct person + { + // name + // + const std::string& + name () const + { + return name_; + } + + void + name (const std::string& name) + { + name_ = name; + } + + // born + // + const std::string& + born () const + { + return born_; + } + + void + born (const std::string& born) + { + born_ = born; + } + + + // died + // + const std::string& + died () const + { + return died_; + } + + void + died (const std::string& died) + { + died_ = died; + } + + private: + std::string name_; + std::string born_; + std::string died_; + }; + + + // + // + struct author: person + { + // recommends + // + const std::string& + recommends () const + { + return recommends_; + } + + void + recommends (const std::string& recommends) + { + recommends_ = recommends; + } + + private: + std::string recommends_; + }; + + + // + // + struct book + { + // isbn + // + library::isbn + isbn () const + { + return isbn_; + } + + void + isbn (const library::isbn& isbn) + { + isbn_ = isbn; + } + + + // title + // + library::title + title () const + { + return title_; + } + + void + title (const library::title& title) + { + title_ = title; + } + + + // genre + // + library::genre + genre () const + { + return genre_; + } + + void + genre (const library::genre& genre) + { + genre_ = genre; + } + + + // author + // + typedef std::vector authors; + + const authors& + author () const + { + return author_; + } + + authors& + author () + { + return author_; + } + + + // available + // + bool + available () const + { + return available_; + } + + void + available (bool available) + { + available_ = available; + } + + + // id + // + const std::string& + id () const + { + return id_; + } + + void + id (const std::string& id) + { + id_ = id; + } + + private: + library::isbn isbn_; + library::title title_; + library::genre genre_; + + authors author_; + + bool available_; + std::string id_; + }; + + + // + // + typedef std::vector catalog; +} + +#endif // LIBRARY_HXX diff --git a/xsd-examples/cxx/parser/library/library.map b/xsd-examples/cxx/parser/library/library.map new file mode 100644 index 0000000..4bca410 --- /dev/null +++ b/xsd-examples/cxx/parser/library/library.map @@ -0,0 +1,15 @@ +# file : cxx/parser/library/library.map +# copyright : not copyrighted - public domain + +namespace http://www.codesynthesis.com/library ::library +{ + include "library.hxx"; + + isbn isbn isbn; + title title; + genre genre genre; + person person; + author author; + book book; + catalog catalog; +} diff --git a/xsd-examples/cxx/parser/library/library.xml b/xsd-examples/cxx/parser/library/library.xml new file mode 100644 index 0000000..1c84b9b --- /dev/null +++ b/xsd-examples/cxx/parser/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/parser/library/library.xsd b/xsd-examples/cxx/parser/library/library.xsd new file mode 100644 index 0000000..4d64a48 --- /dev/null +++ b/xsd-examples/cxx/parser/library/library.xsd @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1