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/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 ++++++++++ 9 files changed, 600 insertions(+) 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 (limited to 'examples/cxx/tree/custom/comments') 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&); +} -- cgit v1.1