From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- examples/cxx/parser/library/README | 44 +++++ examples/cxx/parser/library/driver.cxx | 110 ++++++++++++ examples/cxx/parser/library/library-pimpl.cxx | 184 ++++++++++++++++++++ examples/cxx/parser/library/library-pimpl.hxx | 136 +++++++++++++++ examples/cxx/parser/library/library.hxx | 242 ++++++++++++++++++++++++++ examples/cxx/parser/library/library.map | 16 ++ examples/cxx/parser/library/library.xml | 53 ++++++ examples/cxx/parser/library/library.xsd | 79 +++++++++ examples/cxx/parser/library/makefile | 68 ++++++++ 9 files changed, 932 insertions(+) create mode 100644 examples/cxx/parser/library/README create mode 100644 examples/cxx/parser/library/driver.cxx create mode 100644 examples/cxx/parser/library/library-pimpl.cxx create mode 100644 examples/cxx/parser/library/library-pimpl.hxx create mode 100644 examples/cxx/parser/library/library.hxx create mode 100644 examples/cxx/parser/library/library.map create mode 100644 examples/cxx/parser/library/library.xml create mode 100644 examples/cxx/parser/library/library.xsd create mode 100644 examples/cxx/parser/library/makefile (limited to 'examples/cxx/parser/library') diff --git a/examples/cxx/parser/library/README b/examples/cxx/parser/library/README new file mode 100644 index 0000000..3f515f6 --- /dev/null +++ b/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/examples/cxx/parser/library/driver.cxx b/examples/cxx/parser/library/driver.cxx new file mode 100644 index 0000000..ea5f1d9 --- /dev/null +++ b/examples/cxx/parser/library/driver.cxx @@ -0,0 +1,110 @@ +// file : examples/cxx/parser/library/driver.cxx +// author : Boris Kolpackov +// 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/examples/cxx/parser/library/library-pimpl.cxx b/examples/cxx/parser/library/library-pimpl.cxx new file mode 100644 index 0000000..8b6c357 --- /dev/null +++ b/examples/cxx/parser/library/library-pimpl.cxx @@ -0,0 +1,184 @@ +// file : examples/cxx/parser/library/library-pimpl.cxx +// author : Boris Kolpackov +// 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/examples/cxx/parser/library/library-pimpl.hxx b/examples/cxx/parser/library/library-pimpl.hxx new file mode 100644 index 0000000..180070c --- /dev/null +++ b/examples/cxx/parser/library/library-pimpl.hxx @@ -0,0 +1,136 @@ +// file : examples/cxx/parser/library/library-pimpl.hxx +// author : Boris Kolpackov +// 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/examples/cxx/parser/library/library.hxx b/examples/cxx/parser/library/library.hxx new file mode 100644 index 0000000..fb6cc1d --- /dev/null +++ b/examples/cxx/parser/library/library.hxx @@ -0,0 +1,242 @@ +// file : examples/cxx/parser/library/library.hxx +// author : Boris Kolpackov +// 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/examples/cxx/parser/library/library.map b/examples/cxx/parser/library/library.map new file mode 100644 index 0000000..90f4938 --- /dev/null +++ b/examples/cxx/parser/library/library.map @@ -0,0 +1,16 @@ +# file : examples/cxx/parser/library/library.map +# author : Boris Kolpackov +# 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/examples/cxx/parser/library/library.xml b/examples/cxx/parser/library/library.xml new file mode 100644 index 0000000..2d9069c --- /dev/null +++ b/examples/cxx/parser/library/library.xml @@ -0,0 +1,53 @@ + + + + + + + + 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/examples/cxx/parser/library/library.xsd b/examples/cxx/parser/library/library.xsd new file mode 100644 index 0000000..57654c7 --- /dev/null +++ b/examples/cxx/parser/library/library.xsd @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/parser/library/makefile b/examples/cxx/parser/library/makefile new file mode 100644 index 0000000..c7f06c4 --- /dev/null +++ b/examples/cxx/parser/library/makefile @@ -0,0 +1,68 @@ +# file : examples/cxx/parser/library/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make + +xsd := library.xsd +cxx := driver.cxx library-pimpl.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-pskel.o)) +dep := $(obj:.o=.o.d) + +driver := $(out_base)/driver +clean := $(out_base)/.clean + + +# Import. +# +$(call import,\ + $(scf_root)/import/libxerces-c/stub.make,\ + l: xerces_c.l,cpp-options: xerces_c.l.cpp-options) + + +# Build. +# +$(driver): $(obj) $(xerces_c.l) + +$(obj) $(dep): cpp_options := -I$(src_root)/libxsd +$(obj) $(dep): $(xerces_c.l.cpp-options) + +skel := $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.ixx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) + +$(skel): xsd := $(out_root)/xsd/xsd +$(skel): xsd_options := --generate-inline --type-map $(src_base)/library.map +$(skel): $(out_root)/xsd/xsd $(src_base)/library.map + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=-pskel.cxx.xsd.clean)) + + +# How to. +# +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(scf_root)/xsd/parser/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) -- cgit v1.1