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/polymorphism/README | 30 ++++++++ examples/cxx/parser/polymorphism/driver.cxx | 71 ++++++++++++++++++ examples/cxx/parser/polymorphism/makefile | 68 +++++++++++++++++ .../cxx/parser/polymorphism/supermen-pimpl.cxx | 86 ++++++++++++++++++++++ .../cxx/parser/polymorphism/supermen-pimpl.hxx | 69 +++++++++++++++++ examples/cxx/parser/polymorphism/supermen.xml | 26 +++++++ examples/cxx/parser/polymorphism/supermen.xsd | 49 ++++++++++++ 7 files changed, 399 insertions(+) create mode 100644 examples/cxx/parser/polymorphism/README create mode 100644 examples/cxx/parser/polymorphism/driver.cxx create mode 100644 examples/cxx/parser/polymorphism/makefile create mode 100644 examples/cxx/parser/polymorphism/supermen-pimpl.cxx create mode 100644 examples/cxx/parser/polymorphism/supermen-pimpl.hxx create mode 100644 examples/cxx/parser/polymorphism/supermen.xml create mode 100644 examples/cxx/parser/polymorphism/supermen.xsd (limited to 'examples/cxx/parser/polymorphism') diff --git a/examples/cxx/parser/polymorphism/README b/examples/cxx/parser/polymorphism/README new file mode 100644 index 0000000..60a97e9 --- /dev/null +++ b/examples/cxx/parser/polymorphism/README @@ -0,0 +1,30 @@ +This example shows how to handle XML Schema polymorphism features such +as xsi:type attributes and substitution groups in the C++/Parser mapping. +The case when xsi:type is used on root elements is covered in the +polyroot examples. + +The example consists of the following files: + +supermen.xsd + XML Schema which describes the "supermen" instance documents. + +supermen.xml + Sample XML instance document. + +supermen-pskel.hxx +supermen-pskel.cxx + Parser skeletons generated by the XSD compiler from supermen.xsd. + Note the use of the --generate-polymorphic command line option. + +supermen-pimpl.hxx +supermen-pimpl.cxx + Parser implementations that print the XML data to STDOUT. + +driver.cxx + Driver for the example. It first constructs a parser instance from + all the individual parsers found in supermen-pimpl.hxx. It then invokes + this parser instance to parse the input file. + +To run the example on the sample XML instance document simply execute: + +$ ./driver supermen.xml diff --git a/examples/cxx/parser/polymorphism/driver.cxx b/examples/cxx/parser/polymorphism/driver.cxx new file mode 100644 index 0000000..842a88b --- /dev/null +++ b/examples/cxx/parser/polymorphism/driver.cxx @@ -0,0 +1,71 @@ +// file : examples/cxx/parser/polymorphism/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include + +#include "supermen-pimpl.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " supermen.xml" << endl; + return 1; + } + + try + { + // Construct the parser. + // + xml_schema::string_pimpl string_p; + xml_schema::boolean_pimpl boolean_p; + xml_schema::unsigned_int_pimpl unsigned_int_p; + + person_pimpl person_p; + superman_pimpl superman_p; + batman_pimpl batman_p; + + xml_schema::parser_map_impl person_map; + + supermen_pimpl supermen_p; + + + person_p.parsers (string_p); + superman_p.parsers (string_p, boolean_p); + batman_p.parsers (string_p, boolean_p, unsigned_int_p); + + // Here we are specifying a parser map which containes several parsers + // that can be used to parse the person element. + // + person_map.insert (person_p); + person_map.insert (superman_p); + person_map.insert (batman_p); + + supermen_p.person_parser (person_map); + + // Parse the XML document. The last argument to the document's + // constructor indicates that we are parsing polymorphic XML + // documents. + // + xml_schema::document doc_p (supermen_p, "supermen", true); + + supermen_p.pre (); + doc_p.parse (argv[1]); + supermen_p.post_supermen (); + } + 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/polymorphism/makefile b/examples/cxx/parser/polymorphism/makefile new file mode 100644 index 0000000..2c4054d --- /dev/null +++ b/examples/cxx/parser/polymorphism/makefile @@ -0,0 +1,68 @@ +# file : examples/cxx/parser/polymorphism/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 := supermen.xsd +cxx := driver.cxx supermen-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-polymorphic +$(skel): $(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=-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) diff --git a/examples/cxx/parser/polymorphism/supermen-pimpl.cxx b/examples/cxx/parser/polymorphism/supermen-pimpl.cxx new file mode 100644 index 0000000..8a30cbd --- /dev/null +++ b/examples/cxx/parser/polymorphism/supermen-pimpl.cxx @@ -0,0 +1,86 @@ +// file : examples/cxx/parser/polymorphism/supermen-pimpl.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain +// + +#include + +#include "supermen-pimpl.hxx" + +using std::cout; +using std::endl; + +// person_pimpl +// +void person_pimpl:: +pre () +{ + cout << "starting to parse person" << endl; +} + +void person_pimpl:: +name (const std::string& v) +{ + cout << "name: " << v << endl; +} + +void person_pimpl:: +post_person () +{ + cout << "finished parsing person" << endl + << endl; +} + +// superman_pimpl +// +void superman_pimpl:: +pre () +{ + cout << "starting to parse superman" << endl; +} + +void superman_pimpl:: +can_fly (bool v) +{ + cout << "can-fly: " << v << endl; +} + +void superman_pimpl:: +post_person () +{ + post_superman (); +} + +void superman_pimpl:: +post_superman () +{ + cout << "finished parsing superman" << endl + << endl; +} + +// batman_pimpl +// +void batman_pimpl:: +pre () +{ + cout << "starting to parse batman" << endl; +} + +void batman_pimpl:: +wing_span (unsigned int v) +{ + cout << "wing-span: " << v << endl; +} + +void batman_pimpl:: +post_superman () +{ + post_batman (); +} + +void batman_pimpl:: +post_batman () +{ + cout << "finished parsing batman" << endl + << endl; +} diff --git a/examples/cxx/parser/polymorphism/supermen-pimpl.hxx b/examples/cxx/parser/polymorphism/supermen-pimpl.hxx new file mode 100644 index 0000000..3c0f549 --- /dev/null +++ b/examples/cxx/parser/polymorphism/supermen-pimpl.hxx @@ -0,0 +1,69 @@ +// file : examples/cxx/parser/polymorphism/supermen-pimpl.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef SUPERMEN_PIMPL_HXX +#define SUPERMEN_PIMPL_HXX + +#include "supermen-pskel.hxx" + +class person_pimpl: public virtual person_pskel +{ +public: + virtual void + pre (); + + virtual void + name (const std::string&); + + virtual void + post_person (); +}; + +class superman_pimpl: public virtual superman_pskel, + public person_pimpl +{ +public: + virtual void + pre (); + + virtual void + can_fly (bool); + + // By default, post_superman() calls post_person(). In case of + // polymorphic parsing we want the opposite: post_person() calls + // post_superman(). + // + virtual void + post_person (); + + virtual void + post_superman (); +}; + +class batman_pimpl: public virtual batman_pskel, + public superman_pimpl +{ +public: + virtual void + pre (); + + virtual void + wing_span (unsigned int); + + // By default, post_batman() calls post_superman(). In case of + // polymorphic parsing we want the opposite: post_superman() + // calls post_batman(). + // + virtual void + post_superman (); + + virtual void + post_batman (); +}; + +class supermen_pimpl: public supermen_pskel +{ +}; + +#endif // SUPERMEN_PIMPL_HXX diff --git a/examples/cxx/parser/polymorphism/supermen.xml b/examples/cxx/parser/polymorphism/supermen.xml new file mode 100644 index 0000000..c2bc7b4 --- /dev/null +++ b/examples/cxx/parser/polymorphism/supermen.xml @@ -0,0 +1,26 @@ + + + + + + + + John Doe + + + + James "007" Bond + + + + Bruce Wayne + + + diff --git a/examples/cxx/parser/polymorphism/supermen.xsd b/examples/cxx/parser/polymorphism/supermen.xsd new file mode 100644 index 0000000..83a7aac --- /dev/null +++ b/examples/cxx/parser/polymorphism/supermen.xsd @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1