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/tree/library/README | 32 +++++++++ examples/cxx/tree/library/driver.cxx | 131 ++++++++++++++++++++++++++++++++++ examples/cxx/tree/library/library.xml | 53 ++++++++++++++ examples/cxx/tree/library/library.xsd | 73 +++++++++++++++++++ examples/cxx/tree/library/makefile | 73 +++++++++++++++++++ 5 files changed, 362 insertions(+) create mode 100644 examples/cxx/tree/library/README create mode 100644 examples/cxx/tree/library/driver.cxx create mode 100644 examples/cxx/tree/library/library.xml create mode 100644 examples/cxx/tree/library/library.xsd create mode 100644 examples/cxx/tree/library/makefile (limited to 'examples/cxx/tree/library') diff --git a/examples/cxx/tree/library/README b/examples/cxx/tree/library/README new file mode 100644 index 0000000..960d1d4 --- /dev/null +++ b/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 to use the ID/IDREF cross-referencing +mechanism and the xsd:enumeration to C++ enum mapping. diff --git a/examples/cxx/tree/library/driver.cxx b/examples/cxx/tree/library/driver.cxx new file mode 100644 index 0000000..ae68b87 --- /dev/null +++ b/examples/cxx/tree/library/driver.cxx @@ -0,0 +1,131 @@ +// file : examples/cxx/tree/library/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_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::auto_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/examples/cxx/tree/library/library.xml b/examples/cxx/tree/library/library.xml new file mode 100644 index 0000000..e9acb75 --- /dev/null +++ b/examples/cxx/tree/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/tree/library/library.xsd b/examples/cxx/tree/library/library.xsd new file mode 100644 index 0000000..baf2221 --- /dev/null +++ b/examples/cxx/tree/library/library.xsd @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/library/makefile b/examples/cxx/tree/library/makefile new file mode 100644 index 0000000..ceecbb2 --- /dev/null +++ b/examples/cxx/tree/library/makefile @@ -0,0 +1,73 @@ +# file : examples/cxx/tree/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 + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=.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) + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := --generate-inline \ + --generate-ostream \ + --generate-serialization + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd + +$(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=.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/tree/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) -- cgit v1.1