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/custom/README | 37 +++++ examples/cxx/tree/custom/calendar/README | 47 ++++++ examples/cxx/tree/custom/calendar/calendar.xml | 23 +++ examples/cxx/tree/custom/calendar/calendar.xsd | 32 ++++ examples/cxx/tree/custom/calendar/driver.cxx | 40 +++++ examples/cxx/tree/custom/calendar/makefile | 90 +++++++++++ .../cxx/tree/custom/calendar/xml-schema-custom.cxx | 57 +++++++ .../cxx/tree/custom/calendar/xml-schema-custom.hxx | 34 ++++ examples/cxx/tree/custom/comments/README | 57 +++++++ examples/cxx/tree/custom/comments/dom-parse.cxx | 118 ++++++++++++++ examples/cxx/tree/custom/comments/dom-parse.hxx | 23 +++ examples/cxx/tree/custom/comments/driver.cxx | 91 +++++++++++ examples/cxx/tree/custom/comments/makefile | 84 ++++++++++ examples/cxx/tree/custom/comments/people.xml | 21 +++ examples/cxx/tree/custom/comments/people.xsd | 30 ++++ .../cxx/tree/custom/comments/xml-schema-custom.cxx | 118 ++++++++++++++ .../cxx/tree/custom/comments/xml-schema-custom.hxx | 58 +++++++ examples/cxx/tree/custom/contacts/README | 40 +++++ .../cxx/tree/custom/contacts/contacts-custom.cxx | 51 ++++++ .../cxx/tree/custom/contacts/contacts-custom.hxx | 44 +++++ examples/cxx/tree/custom/contacts/contacts.xml | 21 +++ examples/cxx/tree/custom/contacts/contacts.xsd | 32 ++++ examples/cxx/tree/custom/contacts/driver.cxx | 39 +++++ examples/cxx/tree/custom/contacts/makefile | 77 +++++++++ examples/cxx/tree/custom/double/README | 62 +++++++ examples/cxx/tree/custom/double/double-custom.cxx | 97 +++++++++++ examples/cxx/tree/custom/double/double-custom.hxx | 68 ++++++++ examples/cxx/tree/custom/double/driver.cxx | 32 ++++ examples/cxx/tree/custom/double/makefile | 79 +++++++++ examples/cxx/tree/custom/double/order.xsd | 26 +++ examples/cxx/tree/custom/makefile | 22 +++ examples/cxx/tree/custom/taxonomy/README | 50 ++++++ examples/cxx/tree/custom/taxonomy/driver.cxx | 39 +++++ examples/cxx/tree/custom/taxonomy/makefile | 82 ++++++++++ .../cxx/tree/custom/taxonomy/people-custom-fwd.hxx | 20 +++ .../cxx/tree/custom/taxonomy/people-custom.cxx | 180 +++++++++++++++++++++ .../cxx/tree/custom/taxonomy/people-custom.hxx | 103 ++++++++++++ examples/cxx/tree/custom/taxonomy/people.xml | 27 ++++ examples/cxx/tree/custom/taxonomy/people.xsd | 45 ++++++ examples/cxx/tree/custom/wildcard/README | 45 ++++++ examples/cxx/tree/custom/wildcard/driver.cxx | 48 ++++++ examples/cxx/tree/custom/wildcard/makefile | 79 +++++++++ .../cxx/tree/custom/wildcard/wildcard-custom.cxx | 85 ++++++++++ .../cxx/tree/custom/wildcard/wildcard-custom.hxx | 67 ++++++++ examples/cxx/tree/custom/wildcard/wildcard.xml | 15 ++ examples/cxx/tree/custom/wildcard/wildcard.xsd | 26 +++ 46 files changed, 2561 insertions(+) create mode 100644 examples/cxx/tree/custom/README create mode 100644 examples/cxx/tree/custom/calendar/README create mode 100644 examples/cxx/tree/custom/calendar/calendar.xml create mode 100644 examples/cxx/tree/custom/calendar/calendar.xsd create mode 100644 examples/cxx/tree/custom/calendar/driver.cxx create mode 100644 examples/cxx/tree/custom/calendar/makefile create mode 100644 examples/cxx/tree/custom/calendar/xml-schema-custom.cxx create mode 100644 examples/cxx/tree/custom/calendar/xml-schema-custom.hxx create mode 100644 examples/cxx/tree/custom/comments/README create mode 100644 examples/cxx/tree/custom/comments/dom-parse.cxx create mode 100644 examples/cxx/tree/custom/comments/dom-parse.hxx create mode 100644 examples/cxx/tree/custom/comments/driver.cxx create mode 100644 examples/cxx/tree/custom/comments/makefile create mode 100644 examples/cxx/tree/custom/comments/people.xml create mode 100644 examples/cxx/tree/custom/comments/people.xsd create mode 100644 examples/cxx/tree/custom/comments/xml-schema-custom.cxx create mode 100644 examples/cxx/tree/custom/comments/xml-schema-custom.hxx create mode 100644 examples/cxx/tree/custom/contacts/README create mode 100644 examples/cxx/tree/custom/contacts/contacts-custom.cxx create mode 100644 examples/cxx/tree/custom/contacts/contacts-custom.hxx create mode 100644 examples/cxx/tree/custom/contacts/contacts.xml create mode 100644 examples/cxx/tree/custom/contacts/contacts.xsd create mode 100644 examples/cxx/tree/custom/contacts/driver.cxx create mode 100644 examples/cxx/tree/custom/contacts/makefile create mode 100644 examples/cxx/tree/custom/double/README create mode 100644 examples/cxx/tree/custom/double/double-custom.cxx create mode 100644 examples/cxx/tree/custom/double/double-custom.hxx create mode 100644 examples/cxx/tree/custom/double/driver.cxx create mode 100644 examples/cxx/tree/custom/double/makefile create mode 100644 examples/cxx/tree/custom/double/order.xsd create mode 100644 examples/cxx/tree/custom/makefile create mode 100644 examples/cxx/tree/custom/taxonomy/README create mode 100644 examples/cxx/tree/custom/taxonomy/driver.cxx create mode 100644 examples/cxx/tree/custom/taxonomy/makefile create mode 100644 examples/cxx/tree/custom/taxonomy/people-custom-fwd.hxx create mode 100644 examples/cxx/tree/custom/taxonomy/people-custom.cxx create mode 100644 examples/cxx/tree/custom/taxonomy/people-custom.hxx create mode 100644 examples/cxx/tree/custom/taxonomy/people.xml create mode 100644 examples/cxx/tree/custom/taxonomy/people.xsd create mode 100644 examples/cxx/tree/custom/wildcard/README create mode 100644 examples/cxx/tree/custom/wildcard/driver.cxx create mode 100644 examples/cxx/tree/custom/wildcard/makefile create mode 100644 examples/cxx/tree/custom/wildcard/wildcard-custom.cxx create mode 100644 examples/cxx/tree/custom/wildcard/wildcard-custom.hxx create mode 100644 examples/cxx/tree/custom/wildcard/wildcard.xml create mode 100644 examples/cxx/tree/custom/wildcard/wildcard.xsd (limited to 'examples/cxx/tree/custom') diff --git a/examples/cxx/tree/custom/README b/examples/cxx/tree/custom/README new file mode 100644 index 0000000..482f5a9 --- /dev/null +++ b/examples/cxx/tree/custom/README @@ -0,0 +1,37 @@ +This directory contains a number of examples that show how to customize the +C++/Tree mapping. The following list gives an overview of each example: + +calendar + Shows how to customize XML Schema built-in types by mapping xsd:date + built-in type to the date class from the Boost date_time library. + +comments + This example shows how to customize the anyType XML Schema built-in + type to implement preservation of comments stored in XML documents. + Because anyType is a base type for every generated type, you can use + this technique to implement custom functionality that spans the + entire type system. + +contacts + Shows how to map a user-defined XML Schema type to a custom C++ class. + This example presents the simple case where the customized type is not + used as a base in the same schema. + +double + Shows how to customize parsing and serialization code for the xsd:double + XML Schema built-in type. It can be used as a guide on how to customize + built-in XML Schema types that are mapped to fundamental C++ types. + +taxonomy + Shows how to map user-defined XML Schema types to custom C++ classes. + This example presents the complex case where the customized types are + inherited from in the same schema. + +wildcard + Shows how to use type customization to parse and serialize a specific + attribute that is matched by a wildcard (anyAttribute). + +For more information on the C++/Tree mapping customization see the +C++/Tree Mapping Customization Guide[1]. + +[1] http://wiki.codesynthesis.com/Tree/Customization_guide diff --git a/examples/cxx/tree/custom/calendar/README b/examples/cxx/tree/custom/calendar/README new file mode 100644 index 0000000..f7f6989 --- /dev/null +++ b/examples/cxx/tree/custom/calendar/README @@ -0,0 +1,47 @@ +This example shows how to customize the XML Schema built-in types by mapping +xsd:date built-in type to the date class from the Boost date_time library. +You will need the Boost date_time library[1] installed in order to build +and run this example. For more information on the C++/Tree mapping +customization see the C++/Tree Mapping Customization Guide[2]. + +[1] http://www.boost.org +[2] http://wiki.codesynthesis.com/Tree/Customization_guide + +The example consists of the following files: + +calendar.xsd + XML Schema definition for a simple calendar format. + +calendar.xml + Sample XML instance document. + +xml-schema.hxx + C++ types for XML Schema built-in types. This header file is generated + by XSD using the --generate-xml-schema option. The --custom-type option + is also used to customize the xsd:date type. + +calendar.hxx +calendar.ixx +calendar.cxx + C++ types that represent the given vocabulary and a set of parsing + functions that convert XML instance documents to a tree-like in-memory + object model. These are generated by XSD from calendar.xsd with the + --extern-xml-schema option in order to include xml-schema.hxx. + +xml-schema-custom.hxx + Header file which defines our own xml_schema::date class. It is + included at the end of xml-schema.hxx using the --hxx-epilogue + option. + +xml-schema-custom.cxx + Source file which contains the implementation of our xml_schema:date + class. + +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 calendar events to STDERR. + +To run the example on the sample XML instance document simply execute: + +$ ./driver calendar.xml diff --git a/examples/cxx/tree/custom/calendar/calendar.xml b/examples/cxx/tree/custom/calendar/calendar.xml new file mode 100644 index 0000000..ae9e570 --- /dev/null +++ b/examples/cxx/tree/custom/calendar/calendar.xml @@ -0,0 +1,23 @@ + + + + + + + + Don't forget to change the tire. + + + + Can be cancelled if it is too cold. + + + diff --git a/examples/cxx/tree/custom/calendar/calendar.xsd b/examples/cxx/tree/custom/calendar/calendar.xsd new file mode 100644 index 0000000..fa612a7 --- /dev/null +++ b/examples/cxx/tree/custom/calendar/calendar.xsd @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/custom/calendar/driver.cxx b/examples/cxx/tree/custom/calendar/driver.cxx new file mode 100644 index 0000000..02f1c27 --- /dev/null +++ b/examples/cxx/tree/custom/calendar/driver.cxx @@ -0,0 +1,40 @@ +// file : examples/cxx/tree/custom/calendar/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include "calendar.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " calendar.xml" << endl; + return 1; + } + + try + { + using namespace calendar; + + std::auto_ptr e (events_ (argv[1])); + + for (events::event_const_iterator i (e->event ().begin ()); + i != e->event ().end (); ++i) + { + cerr << i->date () << " " << i->title () << endl + << *i << endl; + } + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/examples/cxx/tree/custom/calendar/makefile b/examples/cxx/tree/custom/calendar/makefile new file mode 100644 index 0000000..36525aa --- /dev/null +++ b/examples/cxx/tree/custom/calendar/makefile @@ -0,0 +1,90 @@ +# file : examples/cxx/tree/custom/calendar/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 := calendar.xsd +cxx := driver.cxx xml-schema-custom.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) + +$(call import,\ + $(scf_root)/import/libboost/date-time/stub.make,\ + l: boost_date_time.l,cpp-options: boost_date_time.l.cpp-options) + +# Build. +# +$(driver): $(obj) $(xerces_c.l) $(boost_date_time.l) + +$(obj) $(dep): cpp_options := -I$(src_root)/libxsd +$(obj) $(dep): $(xerces_c.l.cpp-options) $(boost_date_time.l.cpp-options) + +# Header file for XML Schema namespace. +# +$(out_base)/xml-schema.hxx: $(out_root)/xsd/xsd + $(call message,xsd $(src_base)/xml-schema.xsd,\ +$(out_root)/xsd/xsd cxx-tree --output-dir $(out_base) --generate-xml-schema \ +--custom-type date \ +--hxx-epilogue '\#include "xml-schema-custom.hxx"' xml-schema.xsd) + +# +# +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd + +# We have to double-escape '#' because the message function +# (which is used in command scripts) expands things twice. +# +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \ +--generate-inline \ +--extern-xml-schema xml-schema.xsd + +$(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)) + $(call message,rm $$1,rm -f $$1,$(out_base)/xml-schema.hxx) + + +# 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) diff --git a/examples/cxx/tree/custom/calendar/xml-schema-custom.cxx b/examples/cxx/tree/custom/calendar/xml-schema-custom.cxx new file mode 100644 index 0000000..440a407 --- /dev/null +++ b/examples/cxx/tree/custom/calendar/xml-schema-custom.cxx @@ -0,0 +1,57 @@ +// file : examples/cxx/tree/custom/calendar/xml-schema-custom.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Include xml-schema.hxx instead of xml-schema-custom.hxx here. +// +#include "xml-schema.hxx" + +#include // xsd::cxx::xml::transcode +#include // xsd::cxx::tree::text_content + +using namespace boost; +using namespace boost::gregorian; + +namespace xml_schema +{ + date:: + date (const xercesc::DOMElement& e, flags f, container* c) + : simple_type (e, f, c), + gregorian::date ( + from_simple_string ( + xsd::cxx::tree::text_content (e))) + { + } + + date:: + date (const xercesc::DOMAttr& a, flags f, container* c) + : simple_type (a, f, c), + gregorian::date ( + from_simple_string ( + xsd::cxx::xml::transcode (a.getValue ()))) + { + } + + date:: + date (const std::string& s, + const xercesc::DOMElement* e, + flags f, + container* c) + : simple_type (s, e, f, c), + gregorian::date (from_simple_string (s)) + { + } + + date:: + date (const date& d, flags f, container* c) + : simple_type (d, f, c), + gregorian::date (d) + { + } + + date* date:: + _clone (flags f, container* c) const + { + return new date (*this, f, c); + } +} diff --git a/examples/cxx/tree/custom/calendar/xml-schema-custom.hxx b/examples/cxx/tree/custom/calendar/xml-schema-custom.hxx new file mode 100644 index 0000000..2bab0b5 --- /dev/null +++ b/examples/cxx/tree/custom/calendar/xml-schema-custom.hxx @@ -0,0 +1,34 @@ +// file : examples/cxx/tree/custom/calendar/xml-schema-custom.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Do not include this file directly, use xml-schema.hxx instead. This +// file is included into generated xml-schema.hxx so we do not need to +// guard against multiple inclusions. +// + +#include // boost::gregorian::date + +namespace xml_schema +{ + class date: public simple_type, + public boost::gregorian::date + { + public: + // Parsing c-tors: element, attribute, and list item. + // + date (const xercesc::DOMElement&, flags = 0, container* = 0); + date (const xercesc::DOMAttr&, flags = 0, container* = 0); + date (const std::string&, + const xercesc::DOMElement*, + flags = 0, + container* = 0); + + // Copy c-tor and _clone. + // + date (const date&, flags = 0, container* = 0); + + virtual date* + _clone (flags = 0, container* = 0) const; + }; +} diff --git a/examples/cxx/tree/custom/comments/README b/examples/cxx/tree/custom/comments/README new file mode 100644 index 0000000..8fd69d0 --- /dev/null +++ b/examples/cxx/tree/custom/comments/README @@ -0,0 +1,57 @@ +This example shows how to customize the anyType XML Schema built-in +type to implement preservation of comments stored in XML documents. +Because anyType is a base type for every generated type, you can use +this technique to implement custom functionality that spans the +entire type system. For more information on the C++/Tree mapping +customization see the C++/Tree Mapping Customization Guide[2]. + +[2] http://wiki.codesynthesis.com/Tree/Customization_guide + +The example consists of the following files: + +people.xsd + XML Schema definition for a simple person record vocabulary. + +people.xml + Sample XML instance document. + +xml-schema.hxx + C++ types for XML Schema built-in types. This header file is generated + by XSD using the --generate-xml-schema option. The --custom-type option + is also used to customize the xsd:anyType type. + +people.hxx +people.ixx +people.cxx + C++ types that represent the person record vocabulary, a set of + parsing functions that convert XML instance 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 people.xsd with the --extern-xml-schema option in order to + include xml-schema.hxx. + +xml-schema-custom.hxx + Header file which defines our own xml_schema::type class. It is + included at the end of xml-schema.hxx using the --hxx-epilogue + option. + +xml-schema-custom.cxx + Source file which contains the implementation of our xml_schema:type + class. + +dom-parse.hxx +dom-parse.cxx + Definition and implementation of the parse() function that + parses an XML document to a DOM document while preserving + XML comments. + +driver.cxx + Driver for the example. It first calls the above parse() function + to parse the input file to a DOM document. It then parses the DOM + document to the object model and performs a number of modifications + on this object model. Finally, it serializes the modified object + model back to XML, including XML comments. + +To run the example on the sample XML instance document simply execute: + +$ ./driver people.xml diff --git a/examples/cxx/tree/custom/comments/dom-parse.cxx b/examples/cxx/tree/custom/comments/dom-parse.cxx new file mode 100644 index 0000000..7ad6aa5 --- /dev/null +++ b/examples/cxx/tree/custom/comments/dom-parse.cxx @@ -0,0 +1,118 @@ +// file : examples/cxx/tree/custom/comments/dom-parse.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include "dom-parse.hxx" + +#include + +#include +#include // chLatin_* +#include + +#include +#include + +#include +#include + +using namespace xercesc; +namespace xml = xsd::cxx::xml; +namespace tree = xsd::cxx::tree; + +xml::dom::auto_ptr +parse (std::istream& is, const std::string& id, bool validate) +{ + const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull}; + + // Get an implementation of the Load-Store (LS) interface. + // + DOMImplementation* impl ( + DOMImplementationRegistry::getDOMImplementation (ls_id)); + +#if _XERCES_VERSION >= 30000 + + // Xerces-C++ 3.0.0 and later. + // + xml::dom::auto_ptr parser ( + impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0)); + + DOMConfiguration* conf (parser->getDomConfig ()); + + // Discard comment nodes in the document. + // + conf->setParameter (XMLUni::fgDOMComments, false); + + // Enable datatype normalization. + // + conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true); + + // Do not create EntityReference nodes in the DOM tree. No + // EntityReference nodes will be created, only the nodes + // corresponding to their fully expanded substitution text + // will be created. + // + conf->setParameter (XMLUni::fgDOMEntities, false); + + // Perform namespace processing. + // + conf->setParameter (XMLUni::fgDOMNamespaces, true); + + // Do not include ignorable whitespace in the DOM tree. + // + conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false); + + // Enable/Disable validation. + // + conf->setParameter (XMLUni::fgDOMValidate, validate); + conf->setParameter (XMLUni::fgXercesSchema, validate); + conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false); + + // We will release the DOM document ourselves. + // + conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + // Set error handler. + // + tree::error_handler eh; + xml::dom::bits::error_handler_proxy ehp (eh); + conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp); + +#else // _XERCES_VERSION >= 30000 + + // Same as above but for Xerces-C++ 2 series. + // + xml::dom::auto_ptr parser ( + impl->createDOMBuilder (DOMImplementationLS::MODE_SYNCHRONOUS, 0)); + + parser->setFeature (XMLUni::fgDOMComments, false); + parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true); + parser->setFeature (XMLUni::fgDOMEntities, false); + parser->setFeature (XMLUni::fgDOMNamespaces, true); + parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false); + parser->setFeature (XMLUni::fgDOMValidation, validate); + parser->setFeature (XMLUni::fgXercesSchema, validate); + parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false); + parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true); + + tree::error_handler eh; + xml::dom::bits::error_handler_proxy ehp (eh); + parser->setErrorHandler (&ehp); + +#endif // _XERCES_VERSION >= 30000 + + // Prepare input stream. + // + xml::sax::std_input_source isrc (is, id); + Wrapper4InputSource wrap (&isrc, false); + +#if _XERCES_VERSION >= 30000 + xml::dom::auto_ptr doc (parser->parse (&wrap)); +#else + xml::dom::auto_ptr doc (parser->parse (wrap)); +#endif + + eh.throw_if_failed > (); + + return doc; +} diff --git a/examples/cxx/tree/custom/comments/dom-parse.hxx b/examples/cxx/tree/custom/comments/dom-parse.hxx new file mode 100644 index 0000000..05bfa2e --- /dev/null +++ b/examples/cxx/tree/custom/comments/dom-parse.hxx @@ -0,0 +1,23 @@ +// file : examples/cxx/tree/custom/comments/dom-parse.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#ifndef DOM_PARSE +#define DOM_PARSE + +#include +#include + +#include +#include + +// Parse an XML document from the standard input stream with an +// optional resource id. Resource id is used in diagnostics as +// well as to locate schemas referenced from inside the document. +// +xsd::cxx::xml::dom::auto_ptr +parse (std::istream& is, + const std::string& id, + bool validate); + +#endif // DOM_PARSE diff --git a/examples/cxx/tree/custom/comments/driver.cxx b/examples/cxx/tree/custom/comments/driver.cxx new file mode 100644 index 0000000..4ce2573 --- /dev/null +++ b/examples/cxx/tree/custom/comments/driver.cxx @@ -0,0 +1,91 @@ +// file : examples/cxx/tree/custom/commens/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include +#include + +#include +#include + +#include "people.hxx" +#include "dom-parse.hxx" + +using namespace std; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " people.xml" << endl; + return 1; + } + + int r (0); + + // We need to initialize the Xerces-C++ runtime because we + // are doing the XML-to-DOM parsing ourselves (see below). + // + xercesc::XMLPlatformUtils::Initialize (); + + try + { + using namespace people; + namespace xml = xsd::cxx::xml; + + ifstream ifs; + ifs.exceptions (ifstream::badbit | ifstream::failbit); + ifs.open (argv[1]); + + // For performance reasons the internal XML to DOM parsing code + // discards comments in the resulting DOM document. To overcome + // this we are going to use our own parse() function from + // dom-parse.hxx that preserves comments in the resulting DOM + // documents. + // + xml_schema::dom::auto_ptr doc ( + parse (ifs, argv[1], true)); + + // Parse the DOM document to the object model. + // + std::auto_ptr c (catalog_ (*doc)); + + // Change the object model. + // + catalog::person_sequence& ps (c->person ()); + + for (catalog::person_iterator i (ps.begin ()); i != ps.end (); ++i) + { + i->age (i->age () + 1); + } + + person john ("John Doe", 30); + john.comment ("Record for John Doe"); + + ps.push_back (john); + + // Serialize. + // + xml_schema::namespace_infomap map; + + map["ppl"].name = "http://www.codesynthesis.com/people"; + map["ppl"].schema = "people.xsd"; + + catalog_ (std::cout, *c, map); + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + r = 1; + } + catch (const std::ios_base::failure&) + { + cerr << argv[1] << ": unable to open or read failure" << endl; + r = 1; + } + + xercesc::XMLPlatformUtils::Terminate (); + return r; +} diff --git a/examples/cxx/tree/custom/comments/makefile b/examples/cxx/tree/custom/comments/makefile new file mode 100644 index 0000000..a12b3fe --- /dev/null +++ b/examples/cxx/tree/custom/comments/makefile @@ -0,0 +1,84 @@ +# file : examples/cxx/tree/custom/comments/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 := people.xsd +cxx := driver.cxx xml-schema-custom.cxx dom-parse.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) + +# Header file for XML Schema namespace. +# +$(out_base)/xml-schema.hxx: $(out_root)/xsd/xsd + $(call message,xsd $(src_base)/xml-schema.xsd,\ +$(out_root)/xsd/xsd cxx-tree --output-dir $(out_base) --generate-xml-schema \ +--generate-serialization --custom-type anyType=/type_base \ +--hxx-epilogue '\#include "xml-schema-custom.hxx"' xml-schema.xsd) + +# +# +$(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-serialization \ +--extern-xml-schema xml-schema.xsd + +$(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)) + $(call message,rm $$1,rm -f $$1,$(out_base)/xml-schema.hxx) + + +# 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) diff --git a/examples/cxx/tree/custom/comments/people.xml b/examples/cxx/tree/custom/comments/people.xml new file mode 100644 index 0000000..b6a44e5 --- /dev/null +++ b/examples/cxx/tree/custom/comments/people.xml @@ -0,0 +1,21 @@ + + + + + + + + + Joe Dirt + 28 + + + diff --git a/examples/cxx/tree/custom/comments/people.xsd b/examples/cxx/tree/custom/comments/people.xsd new file mode 100644 index 0000000..2f40754 --- /dev/null +++ b/examples/cxx/tree/custom/comments/people.xsd @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/custom/comments/xml-schema-custom.cxx b/examples/cxx/tree/custom/comments/xml-schema-custom.cxx new file mode 100644 index 0000000..d86d6af --- /dev/null +++ b/examples/cxx/tree/custom/comments/xml-schema-custom.cxx @@ -0,0 +1,118 @@ +// file : examples/cxx/tree/custom/comments/xml-schema-custom.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Include xml-schema.hxx instead of xml-schema-custom.hxx here. +// +#include "xml-schema.hxx" + +#include +#include + +#include // xml::transcode, xml::string + +namespace xml = xsd::cxx::xml; + +namespace xml_schema +{ + type:: + type () + : type_base () + { + } + + type:: + type (const xercesc::DOMElement& e, flags f, container* c) + : type_base (e, f, c) + { + using namespace xercesc; + + // Here we are only handling a comment that is the first + // node in the element's content. + // + const DOMNode* n (e.getFirstChild ()); + + if (n != 0 && n->getNodeType () == DOMNode::COMMENT_NODE) + { + const DOMComment* c (static_cast (n)); + comment_ = xml::transcode (c->getData ()); + } + } + + type:: + type (const xercesc::DOMAttr& a, flags f, container* c) + : type_base (a, f, c) + { + // No comments for attributes. + // + } + + type:: + type (const std::string& s, const xercesc::DOMElement* e, + flags f, container* c) + : type_base (s, e, f, c) + { + // No comments for list items. + // + } + + type:: + type (const type& x, flags f, container* c) + : type_base (x, f, c), comment_ (x.comment_) + { + } + + type* type:: + _clone (flags f, container* c) const + { + return new type (*this, f, c); + } + + // Serialization operators. + // + void + operator<< (xercesc::DOMElement& e, const type& x) + { + // Call our base first. + // + const type_base& b (x); + e << b; + + // Add the comment if any. + // + const std::string s (x.comment ()); + + if (!s.empty ()) + { + using namespace xercesc; + + DOMDocument* doc (e.getOwnerDocument ()); + DOMComment* c (doc->createComment (xml::string (s).c_str ())); + e.appendChild (c); + } + } + + void + operator<< (xercesc::DOMAttr& a, const type& x) + { + // Call our base first. + // + const type_base& b (x); + a << b; + + // No comments for attributes. + // + } + + void + operator<< (xml_schema::list_stream& ls, const type& x) + { + // Call our base first. + // + const type_base& b (x); + ls << b; + + // No comments for list items. + // + } +} diff --git a/examples/cxx/tree/custom/comments/xml-schema-custom.hxx b/examples/cxx/tree/custom/comments/xml-schema-custom.hxx new file mode 100644 index 0000000..821d0dd --- /dev/null +++ b/examples/cxx/tree/custom/comments/xml-schema-custom.hxx @@ -0,0 +1,58 @@ +// file : examples/cxx/tree/custom/comments/xml-schema-custom.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Do not include this file directly, use xml-schema.hxx instead. This +// file is included into generated xml-schema.hxx so we do not need to +// guard against multiple inclusions. +// + +#include + +namespace xml_schema +{ + // When customizing anyType always inherit from the original type. + // + class type: public type_base + { + public: + type (); + type (const xercesc::DOMElement&, flags = 0, container* = 0); + type (const xercesc::DOMAttr&, flags = 0, container* = 0); + type (const std::string&, const xercesc::DOMElement*, + flags = 0, container* = 0); + type (const type&, flags = 0, container* = 0); + + virtual type* + _clone (flags = 0, container* = 0) const; + + public: + // Comment manipulation API. + // + const std::string& + comment () const + { + return comment_; + } + + void + comment (const std::string& c) + { + comment_ = c; + } + + private: + std::string comment_; + }; + + // New serialization operators. + // + void + operator<< (xercesc::DOMElement&, const type&); + + void + operator<< (xercesc::DOMAttr&, const type&); + + void + operator<< (xml_schema::list_stream&, const type&); +} diff --git a/examples/cxx/tree/custom/contacts/README b/examples/cxx/tree/custom/contacts/README new file mode 100644 index 0000000..a3b237c --- /dev/null +++ b/examples/cxx/tree/custom/contacts/README @@ -0,0 +1,40 @@ +This example shows how to map a user-defined XML Schema type to a custom +C++ class. It presents the simple case where the customized type is not +used as a base in the same schema. For the complex case see the taxonomy +example. For more information on the C++/Tree mapping customization see +the C++/Tree Mapping Customization Guide[1]. + +[1] http://wiki.codesynthesis.com/Tree/Customization_guide + +The example consists of the following files: + +contacts.xsd + XML Schema definition for a simple contacts database. + +contacts.xml + Sample XML instance document. + +contacts.hxx +contacts.ixx +contacts.cxx + C++ types that represent the given vocabulary and a set of parsing + functions that convert XML instance documents to a tree-like in-memory + object model. These are generated by XSD from contacts.xsd with the + --custom-type option in order to to customize the contact type. + +contacts-custom.hxx + Header file which defines our own contact class by inheriting from the + generated contact_base. It is included at the end of contacts.hxx using + the --hxx-epilogue option. + +contacts-custom.cxx + Source file which contains the implementation of our contact class. + +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 contacts to STDERR. + +To run the example on the sample XML instance document simply execute: + +$ ./driver contacts.xml diff --git a/examples/cxx/tree/custom/contacts/contacts-custom.cxx b/examples/cxx/tree/custom/contacts/contacts-custom.cxx new file mode 100644 index 0000000..dd7c453 --- /dev/null +++ b/examples/cxx/tree/custom/contacts/contacts-custom.cxx @@ -0,0 +1,51 @@ +// file : examples/cxx/tree/custom/contacts/contacts-custom.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include + +// Include contacts.hxx instead of contacts-custom.hxx here. +// +#include "contacts.hxx" + +namespace contacts +{ + // We implement the following constructs by simply forwarding + // to our base. + // + contact:: + contact (const name_type& n, + const email_type& e, + const phone_type& p) + : contact_base (n, e, p) + { + } + + contact:: + contact (const xercesc::DOMElement& e, + xml_schema::flags f, + xml_schema::container* c) + : contact_base (e, f, c) + { + } + + contact:: + contact (const contact& x, + xml_schema::flags f, + xml_schema::container* c) + : contact_base (x, f, c) + { + } + + contact* contact:: + _clone (xml_schema::flags f, xml_schema::container* c) const + { + return new contact (*this, f, c); + } + + void contact:: + print (std::ostream& os) const + { + os << name () << " e| " << email () << " t| " << phone () << std::endl; + } +} diff --git a/examples/cxx/tree/custom/contacts/contacts-custom.hxx b/examples/cxx/tree/custom/contacts/contacts-custom.hxx new file mode 100644 index 0000000..fdb6810 --- /dev/null +++ b/examples/cxx/tree/custom/contacts/contacts-custom.hxx @@ -0,0 +1,44 @@ +// file : examples/cxx/tree/custom/contacts/contacts-custom.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Do not include this file directly, use contacts.hxx instead. This +// file is included into generated contacts.hxx so we do not need to +// guard against multiple inclusions. +// + +#include // std::ostream + +namespace contacts +{ + class contact: public contact_base + { + // The following constructor signatures are copied from + // contact_base except for the copy constructor and the + // _clone function where we had to change the type from + // contact_base to contact. + // + public: + contact (const name_type&, + const email_type&, + const phone_type&); + + contact (const xercesc::DOMElement&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + contact (const contact&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + virtual contact* + _clone (xml_schema::flags = 0, + xml_schema::container* = 0) const; + + // Our customizations. + // + public: + void + print (std::ostream&) const; + }; +} diff --git a/examples/cxx/tree/custom/contacts/contacts.xml b/examples/cxx/tree/custom/contacts/contacts.xml new file mode 100644 index 0000000..c6dc38f --- /dev/null +++ b/examples/cxx/tree/custom/contacts/contacts.xml @@ -0,0 +1,21 @@ + + + + + + + + Joe Dirt + joe@dirt.com + 555 DIRT + + + diff --git a/examples/cxx/tree/custom/contacts/contacts.xsd b/examples/cxx/tree/custom/contacts/contacts.xsd new file mode 100644 index 0000000..da73c78 --- /dev/null +++ b/examples/cxx/tree/custom/contacts/contacts.xsd @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/custom/contacts/driver.cxx b/examples/cxx/tree/custom/contacts/driver.cxx new file mode 100644 index 0000000..9015e87 --- /dev/null +++ b/examples/cxx/tree/custom/contacts/driver.cxx @@ -0,0 +1,39 @@ +// file : examples/cxx/tree/custom/contacts/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include "contacts.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " contacts.xml" << endl; + return 1; + } + + try + { + using namespace contacts; + + std::auto_ptr c (catalog_ (argv[1])); + + for (catalog::contact_const_iterator i (c->contact ().begin ()); + i != c->contact ().end (); ++i) + { + i->print (cerr); + } + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/examples/cxx/tree/custom/contacts/makefile b/examples/cxx/tree/custom/contacts/makefile new file mode 100644 index 0000000..9a8dc89 --- /dev/null +++ b/examples/cxx/tree/custom/contacts/makefile @@ -0,0 +1,77 @@ +# file : examples/cxx/tree/custom/contacts/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 := contacts.xsd +cxx := driver.cxx contacts-custom.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 + +# We have to double-escape '#' because the message function +# (which is used in command scripts) expands things twice. +# +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \ +--generate-inline \ +--custom-type contact=/contact_base \ +--hxx-epilogue '\\\#include "contacts-custom.hxx"' + +$(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) diff --git a/examples/cxx/tree/custom/double/README b/examples/cxx/tree/custom/double/README new file mode 100644 index 0000000..15348d2 --- /dev/null +++ b/examples/cxx/tree/custom/double/README @@ -0,0 +1,62 @@ +This example shows how to customize parsing and serialization code for the +xsd:double XML Schema built-in type using the type customization mechanism +provided by the C++/Tree Mapping. For more information on type customization +see the C++/Tree Mapping Customization Guide, particularly sections 1 and 4: + +http://wiki.codesynthesis.com/Tree/Customization_guide + +In this example our schema uses xsd:double to represent a price. There are +two potential problems with this choice of a price type. First, xsd:double +can be serialized in the scientific notation which would be an unusual way +of representing a price. Second, we would like to limit the number of +fraction digits in our prices to 2. Furthermore, we would like to always +have two fraction digits, even if one or both of them are zeros, for +example: 12.99, 12.90, 12.00. + +In case we can modify the schema, a better approach would be to define the +price type as a restriction of the xsd:decimal type (always fixed notation) +and specify the fractionDigits facet to limit the number of fraction digits +to 2. However, there is no way in XML Schema to specify that there should +always be exactly 2 fraction digits. Therefore, it may still be desirable +to customize this price type to get the required serialization behavior. + +Finally, it is worth noting that the behavior achieved in this example via +type customization can also be achieved by compiling your code with the +following macros defined: + +XSD_TREE_DOUBLE_FIXED +XSD_TREE_DOUBLE_PRECISION 2 + +However, the type customization approach while requiring more work is +cleaner since it does not rely on global macro definitions. + +This example consists of the following files: + +order.xsd + XML Schema definition for a simple order vocabulary. + +double-custom.hxx +double-custom.cxx + Custom parsing and serialization code for the xsd:double types. The + double-custom.hxx file is included at the end of the xml-schema.hxx + file described below. + +xml-schema.hxx + C++ types for XML Schema built-in types. This header file is generated + by the XSD compiler using the --generate-xml-schema option. The + --custom-type option is used to customize the xsd:double type. The + --hxx-epilogue option is used to include the double-custom.hxx file + at the end of this file. + +order.hxx +order.cxx + C++ types generated from order.xsd. The --extern-xml-schema option + is used to include xml-schema.hxx into order.hxx. + +driver.cxx + Test driver for the example. It creates a sample order and then + writes it to XML to test the custom xsd:double serialization code. + +To run the example simply execute: + +$ ./driver diff --git a/examples/cxx/tree/custom/double/double-custom.cxx b/examples/cxx/tree/custom/double/double-custom.cxx new file mode 100644 index 0000000..78904a0 --- /dev/null +++ b/examples/cxx/tree/custom/double/double-custom.cxx @@ -0,0 +1,97 @@ +// file : examples/cxx/tree/custom/double/double-custom.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Include xml-schema.hxx instead of double-custom.hxx here. +// +#include "xml-schema.hxx" + +#include +#include +#include + +#include +#include + +using namespace std; + +// Parsing. +// +namespace xsd +{ + namespace cxx + { + namespace tree + { + double traits:: + create (const std::string& s, + const xercesc::DOMElement*, + flags, + type*) + { + // This type cannot have whitespaces in its values. As result we + // don't need to waste time collapsing whitespaces. All we need to + // do is trim the string representation which can be done without + // copying. + // + ro_string tmp (s); + trim (tmp); + + zc_istream is (tmp); + is.imbue (locale::classic ()); + + double t; + is >> t; + + return t; + } + } + } +} + +// Serialization. +// +namespace XERCES_CPP_NAMESPACE +{ + void + operator<< (xercesc::DOMElement& e, const xml_schema::as_double& d) + { + ostringstream os; + os.imbue (locale::classic ()); + + os.precision (2); + os << fixed << d.x; + + e << os.str (); + } + + void + operator<< (xercesc::DOMAttr& a, const xml_schema::as_double& d) + { + ostringstream os; + os.imbue (locale::classic ()); + + os.precision (2); + os << fixed << d.x; + + a << os.str (); + } +} + +namespace xsd +{ + namespace cxx + { + namespace tree + { + void + operator<< (xml_schema::list_stream& ls, + const xml_schema::as_double& d) + { + ls.os_.imbue (locale::classic ()); + ls.os_.precision (2); + ls.os_ << fixed << d.x; + } + } + } +} diff --git a/examples/cxx/tree/custom/double/double-custom.hxx b/examples/cxx/tree/custom/double/double-custom.hxx new file mode 100644 index 0000000..17d9a44 --- /dev/null +++ b/examples/cxx/tree/custom/double/double-custom.hxx @@ -0,0 +1,68 @@ +// file : examples/cxx/tree/custom/double/double-custom.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Do not include this file directly, use xml-schema.hxx instead. This +// file is included into generated xml-schema.hxx so we do not need to +// guard against multiple inclusions. +// + +#include // xml::transcode +#include // text_content + +// Parsing. +// +namespace xsd +{ + namespace cxx + { + namespace tree + { + template<> + struct traits + { + static double + create (const xercesc::DOMElement& e, flags f, type* c) + { + return create (text_content (e), 0, f, c); + } + + static double + create (const xercesc::DOMAttr& a, flags f, type* c) + { + return create (xml::transcode (a.getValue ()), 0, f, c); + } + + static double + create (const std::string& s, + const xercesc::DOMElement*, + flags, + type*); + }; + } + } +} + +// Serialization. +// +namespace XERCES_CPP_NAMESPACE +{ + void + operator<< (xercesc::DOMElement& e, const xml_schema::as_double& d); + + void + operator<< (xercesc::DOMAttr& a, const xml_schema::as_double& d); +} + +namespace xsd +{ + namespace cxx + { + namespace tree + { + void + operator<< (xml_schema::list_stream& ls, + const xml_schema::as_double& d); + } + } +} diff --git a/examples/cxx/tree/custom/double/driver.cxx b/examples/cxx/tree/custom/double/driver.cxx new file mode 100644 index 0000000..2e5b44d --- /dev/null +++ b/examples/cxx/tree/custom/double/driver.cxx @@ -0,0 +1,32 @@ +// file : examples/cxx/tree/custom/double/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include + +#include "order.hxx" + +using std::cerr; +using std::endl; + +int +main () +{ + try + { + // Order one Airbus A380. + // + order o; + o.item ().push_back (item ("Airbus A380", 317000000.90)); + + + // Serialize. + // + order_ (std::cout, o); + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/examples/cxx/tree/custom/double/makefile b/examples/cxx/tree/custom/double/makefile new file mode 100644 index 0000000..249ed01 --- /dev/null +++ b/examples/cxx/tree/custom/double/makefile @@ -0,0 +1,79 @@ +# file : examples/cxx/tree/custom/double/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 := order.xsd +cxx := driver.cxx double-custom.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)/xml-schema.hxx: $(out_root)/xsd/xsd + $(call message,xsd $(src_base)/xml-schema.xsd,\ +$(out_root)/xsd/xsd cxx-tree --output-dir $(out_base) --generate-xml-schema \ +--generate-serialization --custom-type double=double \ +--hxx-epilogue '\#include "double-custom.hxx"' xml-schema.xsd) + +$(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-serialization --extern-xml-schema xml-schema.xsd + +$(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)) + $(call message,rm $$1,rm -f $$1,$(out_base)/xml-schema.hxx) + + +# 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) diff --git a/examples/cxx/tree/custom/double/order.xsd b/examples/cxx/tree/custom/double/order.xsd new file mode 100644 index 0000000..0d7fe05 --- /dev/null +++ b/examples/cxx/tree/custom/double/order.xsd @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/custom/makefile b/examples/cxx/tree/custom/makefile new file mode 100644 index 0000000..c29fd03 --- /dev/null +++ b/examples/cxx/tree/custom/makefile @@ -0,0 +1,22 @@ +# file : examples/cxx/tree/custom/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make + +examples := comments contacts double taxonomy wildcard + +ifeq ($(xsd_with_boost_date_time),y) +examples += calendar +endif + +default := $(out_base)/ +clean := $(out_base)/.clean + +.PHONY: $(default) $(clean) + +$(default): $(addprefix $(out_base)/,$(addsuffix /,$(examples))) +$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(examples))) + +$(foreach e,$(examples),$(call import,$(src_base)/$e/makefile)) diff --git a/examples/cxx/tree/custom/taxonomy/README b/examples/cxx/tree/custom/taxonomy/README new file mode 100644 index 0000000..addce83 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/README @@ -0,0 +1,50 @@ +This example shows how to map user-defined XML Schema types to custom C++ +classes. It presents the complex case where the customized types are +inherited from in the same schema. For the simple case see the contacts +example. For more information on the C++/Tree mapping customization see +the C++/Tree Mapping Customization Guide[1]. + +[1] http://wiki.codesynthesis.com/Tree/Customization_guide + +The example consists of the following files: + +people.xsd + XML Schema definition for a simple people database. + +people.xml + Sample XML instance document. + +people-fwd.hxx +people.hxx +people.ixx +people.cxx + C++ types that represent the given vocabulary and a set of parsing + functions that convert XML instance documents to a tree-like in-memory + object model. These are generated by XSD from people.xsd with the + --custom-type option in order to to customize the person, superman, + and batman types. Generation of the people-fwd.hxx forward declaration + file is requested with the --generate-forward option. + +people-custom-fwd.hxx + Header file which forward-declares our own person, superman, and batman + as class templates. It is included at the beginning of people-fwd.hxx + using the --fwd-prologue option. + +people-custom.hxx + Header file which defines our own person, superman, and batman class + templates by inheriting from the generated person_base, superman_base, + and batman_base. It is included at the beginning of people.hxx using + the --hxx-prologue option. + +people-custom.cxx + Source file which contains the implementations and instantiations of + our person, superman, and batman class templates. + +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 database to STDERR. + +To run the example on the sample XML instance document simply execute: + +$ ./driver people.xml diff --git a/examples/cxx/tree/custom/taxonomy/driver.cxx b/examples/cxx/tree/custom/taxonomy/driver.cxx new file mode 100644 index 0000000..f102fc3 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/driver.cxx @@ -0,0 +1,39 @@ +// file : examples/cxx/tree/custom/taxonomy/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include "people.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " people.xml" << endl; + return 1; + } + + try + { + using namespace people; + + std::auto_ptr c (catalog_ (argv[1])); + + for (catalog::person_const_iterator i (c->person ().begin ()); + i != c->person ().end (); ++i) + { + i->print (cerr); + } + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/examples/cxx/tree/custom/taxonomy/makefile b/examples/cxx/tree/custom/taxonomy/makefile new file mode 100644 index 0000000..a5fa4a3 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/makefile @@ -0,0 +1,82 @@ +# file : examples/cxx/tree/custom/taxonomy/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 := people.xsd +cxx := driver.cxx people-custom.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 + +# We have to double-escape '#' because the message function +# (which is used in command scripts) expands things twice. +# +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \ +--generate-inline \ +--generate-forward \ +--generate-polymorphic \ +--custom-type "person=person_impl/person_base" \ +--custom-type "superman=superman_impl/superman_base" \ +--custom-type "batman=batman_impl/batman_base" \ +--fwd-prologue '\\\#include "people-custom-fwd.hxx"' \ +--hxx-prologue '\\\#include "people-custom.hxx"' + +$(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) diff --git a/examples/cxx/tree/custom/taxonomy/people-custom-fwd.hxx b/examples/cxx/tree/custom/taxonomy/people-custom-fwd.hxx new file mode 100644 index 0000000..973b6e5 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/people-custom-fwd.hxx @@ -0,0 +1,20 @@ +// file : examples/cxx/tree/custom/taxonomy/people-custom-fwd.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Do not include this file directly, use people-fwd.hxx instead. This +// file is included into generated people-fwd.hxx so we do not need to +// guard against multiple inclusions. +// + +namespace people +{ + template + class person_impl; + + template + class superman_impl; + + template + class batman_impl; +} diff --git a/examples/cxx/tree/custom/taxonomy/people-custom.cxx b/examples/cxx/tree/custom/taxonomy/people-custom.cxx new file mode 100644 index 0000000..14c7087 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/people-custom.cxx @@ -0,0 +1,180 @@ +// file : examples/cxx/tree/custom/taxonomy/people-custom.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include + +// Include people.hxx instead of people-custom.hxx here. +// +#include "people.hxx" + +namespace people +{ + // person_impl + // + template + person_impl:: + person_impl (const xml_schema::string& name) + : base (name) + { + } + + template + person_impl:: + person_impl (std::auto_ptr& name) + : base (name) + { + } + + template + person_impl:: + person_impl (const xercesc::DOMElement& e, + xml_schema::flags f, + xml_schema::container* c) + : base (e, f, c) + { + } + + template + person_impl:: + person_impl (const person_impl& p, + xml_schema::flags f, + xml_schema::container* c) + : base (p, f, c) + { + } + + template + person_impl* person_impl:: + _clone (xml_schema::flags f, xml_schema::container* c) const + { + return new person_impl (*this, f, c); + } + + template + void person_impl:: + print (std::ostream& os) const + { + os << this->name () << std::endl; + } + + // Explicitly instantiate person_impl class template for person_base. + // + template class person_impl; + + + // superman_impl + // + template + superman_impl:: + superman_impl (const xml_schema::string& name, bool can_fly) + : base (name, can_fly) + { + } + + template + superman_impl:: + superman_impl (std::auto_ptr& name, bool can_fly) + : base (name, can_fly) + { + } + + template + superman_impl:: + superman_impl (const xercesc::DOMElement& e, + xml_schema::flags f, + xml_schema::container* c) + : base (e, f, c) + { + } + + template + superman_impl:: + superman_impl (const superman_impl& s, + xml_schema::flags f, + xml_schema::container* c) + : base (s, f, c) + { + } + + template + superman_impl* superman_impl:: + _clone (xml_schema::flags f, xml_schema::container* c) const + { + return new superman_impl (*this, f, c); + } + + template + void superman_impl:: + print (std::ostream& os) const + { + if (this->can_fly ()) + os << "Flying superman "; + else + os << "Superman "; + + os << this->name () << std::endl; + } + + // Explicitly instantiate superman_impl class template for superman_base. + // + template class superman_impl; + + + // batman_impl + // + template + batman_impl:: + batman_impl (const xml_schema::string& name, + bool can_fly, + unsigned int wing_span) + : base (name, can_fly, wing_span) + { + } + + template + batman_impl:: + batman_impl (std::auto_ptr& name, + bool can_fly, + unsigned int wing_span) + : base (name, can_fly, wing_span) + { + } + + template + batman_impl:: + batman_impl (const xercesc::DOMElement& e, + xml_schema::flags f, + xml_schema::container* c) + : base (e, f, c) + { + } + + template + batman_impl:: + batman_impl (const batman_impl& s, + xml_schema::flags f, + xml_schema::container* c) + : base (s, f, c) + { + } + + template + batman_impl* batman_impl:: + _clone (xml_schema::flags f, xml_schema::container* c) const + { + return new batman_impl (*this, f, c); + } + + template + void batman_impl:: + print (std::ostream& os) const + { + os << "Batman " << this->name () << " with " << + this->wing_span () << "m wing span" << std::endl; + } + + // Explicitly instantiate batman_impl class template for batman_base. + // + template class batman_impl; +} diff --git a/examples/cxx/tree/custom/taxonomy/people-custom.hxx b/examples/cxx/tree/custom/taxonomy/people-custom.hxx new file mode 100644 index 0000000..23e7731 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/people-custom.hxx @@ -0,0 +1,103 @@ +// file : examples/cxx/tree/custom/taxonomy/people-custom.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Do not include this file directly, use people.hxx instead. This +// file is included into generated people.hxx so we do not need to +// guard against multiple inclusions. +// + +#include // std::ostream + +// Include people-fwd.hxx here so that we can refer to the generated +// types. +// +#include "people-fwd.hxx" + +namespace people +{ + // + // + template + class person_impl: public base + { + public: + person_impl (const xml_schema::string& name); + person_impl (std::auto_ptr& name); + + person_impl (const xercesc::DOMElement&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + person_impl (const person_impl&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + virtual person_impl* + _clone (xml_schema::flags = 0, + xml_schema::container* = 0) const; + + public: + virtual void + print (std::ostream&) const; + }; + + + // + // + template + class superman_impl: public base + { + public: + superman_impl (const xml_schema::string& name, bool can_fly); + superman_impl (std::auto_ptr& name, bool can_fly); + + superman_impl (const xercesc::DOMElement&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + superman_impl (const superman_impl&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + virtual superman_impl* + _clone (xml_schema::flags = 0, + xml_schema::container* = 0) const; + + public: + virtual void + print (std::ostream&) const; + }; + + + // + // + template + class batman_impl: public base + { + public: + batman_impl (const xml_schema::string& name, + bool can_fly, + unsigned int wing_span); + + batman_impl (std::auto_ptr& name, + bool can_fly, + unsigned int wing_span); + + batman_impl (const xercesc::DOMElement&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + batman_impl (const batman_impl&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + virtual batman_impl* + _clone (xml_schema::flags = 0, + xml_schema::container* = 0) const; + + public: + virtual void + print (std::ostream&) const; + }; +} diff --git a/examples/cxx/tree/custom/taxonomy/people.xml b/examples/cxx/tree/custom/taxonomy/people.xml new file mode 100644 index 0000000..5f84855 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/people.xml @@ -0,0 +1,27 @@ + + + + + + + + Joe Dirt + + + + James "007" Bond + + + + Bruce Wayne + + + diff --git a/examples/cxx/tree/custom/taxonomy/people.xsd b/examples/cxx/tree/custom/taxonomy/people.xsd new file mode 100644 index 0000000..c77a866 --- /dev/null +++ b/examples/cxx/tree/custom/taxonomy/people.xsd @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/custom/wildcard/README b/examples/cxx/tree/custom/wildcard/README new file mode 100644 index 0000000..5ca25c0 --- /dev/null +++ b/examples/cxx/tree/custom/wildcard/README @@ -0,0 +1,45 @@ +This example shows how to use type customization to parse and serialize +a specific attribute that is matched by a wildcard (anyAttribute). The +example achieves this by customizing the type to include the data +members and accessors/modifiers that represent the attribute as well as +the parsing constructor and serialization operator where the attribute +value is extracted from and inserted back to DOM, respectively. For +more information on the C++/Tree mapping customization see the C++/Tree +Mapping Customization Guide[1]. + +[1] http://wiki.codesynthesis.com/Tree/Customization_guide + +The example consists of the following files: + +wildcard.xsd + XML Schema definition for simple data type and element. + +contacts.xml + Sample XML instance document. + +wildcard.hxx +wildcard.ixx +wildcard.cxx + C++ types that represent the given vocabulary, a set of parsing + functions that convert XML instance 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 wildcard.xsd + with the --custom-type option in order to to customize the data type. + +wildcard-custom.hxx + Header file which defines our own data class by inheriting from the + generated data_base. It is included at the end of wildcard.hxx using + the --hxx-epilogue option. + +wildcard-custom.cxx + Source file which contains the implementation of our data class. + +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 data to STDERR, including the extra attribute. Finally, the driver + serializes the object model back to XML. + +To run the example on the sample XML instance document simply execute: + +$ ./driver wildcard.xml diff --git a/examples/cxx/tree/custom/wildcard/driver.cxx b/examples/cxx/tree/custom/wildcard/driver.cxx new file mode 100644 index 0000000..d63fb11 --- /dev/null +++ b/examples/cxx/tree/custom/wildcard/driver.cxx @@ -0,0 +1,48 @@ +// file : examples/cxx/tree/custom/wildcard/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include "wildcard.hxx" + +using std::cerr; +using std::endl; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " wildcard.xml" << endl; + return 1; + } + + try + { + using namespace wildcard; + + // Parse. + // + std::auto_ptr d (data_ (argv[1])); + + // Print. + // + cerr << *d << endl; + + // Serialize. + // + xml_schema::namespace_infomap map; + + map["wc"].name = "http://www.codesynthesis.com/wildcard"; + map["wc"].schema = "wildcard.xsd"; + + data_ (std::cout, *d, map); + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/examples/cxx/tree/custom/wildcard/makefile b/examples/cxx/tree/custom/wildcard/makefile new file mode 100644 index 0000000..87f95ea --- /dev/null +++ b/examples/cxx/tree/custom/wildcard/makefile @@ -0,0 +1,79 @@ +# file : examples/cxx/tree/custom/wildcard/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 := wildcard.xsd +cxx := driver.cxx wildcard-custom.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 + +# We have to double-escape '#' because the message function +# (which is used in command scripts) expands things twice. +# +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := \ +--generate-inline \ +--generate-ostream \ +--generate-serialization \ +--custom-type data=/data_base \ +--hxx-epilogue '\\\#include "wildcard-custom.hxx"' + +$(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) diff --git a/examples/cxx/tree/custom/wildcard/wildcard-custom.cxx b/examples/cxx/tree/custom/wildcard/wildcard-custom.cxx new file mode 100644 index 0000000..422a4b3 --- /dev/null +++ b/examples/cxx/tree/custom/wildcard/wildcard-custom.cxx @@ -0,0 +1,85 @@ +// file : examples/cxx/tree/custom/wildcard/wildcard-custom.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include + +// Include wildcard.hxx instead of wildcard-custom.hxx here. +// +#include "wildcard.hxx" + +namespace wildcard +{ + data:: + data (const xml_schema::string& d) + : data_base (d), scope_present_ (false) + { + } + + data:: + data (const xercesc::DOMElement& e, + xml_schema::flags f, + xml_schema::container* c) + : data_base (e, f, c), scope_present_ (false) + { + // Check if we've got the scope attribute. + // + namespace xml = xsd::cxx::xml; + xml::string name ("scope"); + + if (e.hasAttribute (name.c_str ())) + { + scope (xml::transcode (e.getAttribute (name.c_str ()))); + } + } + + data:: + data (const data& d, + xml_schema::flags f, + xml_schema::container* c) + : data_base (d, f, c), + scope_present_ (d.scope_present_), + scope_ (d.scope_) + { + } + + data* data:: + _clone (xml_schema::flags f, xml_schema::container* c) const + { + return new data (*this, f, c); + } + + void + operator<< (xercesc::DOMElement& e, const data& x) + { + // Use our base to serialize data and id. + // + const data_base& b (x); + e << b; + + // Add the scope attribute if present. + // + if (x.scope_present ()) + { + namespace xml = xsd::cxx::xml; + xml::string name ("scope"); + xml::string value (x.scope ()); + + e.setAttribute (name.c_str (), value.c_str ()); + } + } + + std::ostream& + operator<< (std::ostream& os, const data& x) + { + // Use our base to print date and id. + // + const data_base& b (x); + os << b; + + if (x.scope_present ()) + os << std::endl << "scope: " << x.scope (); + + return os; + } +} diff --git a/examples/cxx/tree/custom/wildcard/wildcard-custom.hxx b/examples/cxx/tree/custom/wildcard/wildcard-custom.hxx new file mode 100644 index 0000000..2c58cc8 --- /dev/null +++ b/examples/cxx/tree/custom/wildcard/wildcard-custom.hxx @@ -0,0 +1,67 @@ +// file : examples/cxx/tree/custom/wildcard/wildcard-custom.hxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +// Do not include this file directly, use wildcard.hxx instead. This +// file is included into generated wildcard.hxx so we do not need to +// guard against multiple inclusions. +// + +namespace wildcard +{ + class data: public data_base + { + // Standard constructors. + // + public: + data (const xml_schema::string&); + + data (const xercesc::DOMElement&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + data (const data&, + xml_schema::flags = 0, + xml_schema::container* = 0); + + virtual data* + _clone (xml_schema::flags = 0, + xml_schema::container* = 0) const; + + // Our customizations. + // + public: + bool + scope_present () const + { + return scope_present_; + } + + const xml_schema::string& + scope () const + { + return scope_; + } + + void + scope (const xml_schema::string& s) + { + scope_present_ = true; + scope_ = s; + } + + private: + bool scope_present_; + xml_schema::string scope_; + }; + + // Serialization operator. + // + void + operator<< (xercesc::DOMElement&, const data&); + + // std::ostream insertion operator. + // + std::ostream& + operator<< (std::ostream&, const data&); +} diff --git a/examples/cxx/tree/custom/wildcard/wildcard.xml b/examples/cxx/tree/custom/wildcard/wildcard.xml new file mode 100644 index 0000000..c980193 --- /dev/null +++ b/examples/cxx/tree/custom/wildcard/wildcard.xml @@ -0,0 +1,15 @@ + + + + +abc123 diff --git a/examples/cxx/tree/custom/wildcard/wildcard.xsd b/examples/cxx/tree/custom/wildcard/wildcard.xsd new file mode 100644 index 0000000..ccce30b --- /dev/null +++ b/examples/cxx/tree/custom/wildcard/wildcard.xsd @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + -- cgit v1.1