summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/custom/comments
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-09-17 07:15:29 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-09-17 07:15:29 +0200
commitf0510d2f90467de8e8f260b47d79a9baaf9bef17 (patch)
tree0b9929946f06a9cbe9b9e8f2a7600dae4e048f79 /examples/cxx/tree/custom/comments
Start tracking XSD with git
Diffstat (limited to 'examples/cxx/tree/custom/comments')
-rw-r--r--examples/cxx/tree/custom/comments/README57
-rw-r--r--examples/cxx/tree/custom/comments/dom-parse.cxx118
-rw-r--r--examples/cxx/tree/custom/comments/dom-parse.hxx23
-rw-r--r--examples/cxx/tree/custom/comments/driver.cxx91
-rw-r--r--examples/cxx/tree/custom/comments/makefile84
-rw-r--r--examples/cxx/tree/custom/comments/people.xml21
-rw-r--r--examples/cxx/tree/custom/comments/people.xsd30
-rw-r--r--examples/cxx/tree/custom/comments/xml-schema-custom.cxx118
-rw-r--r--examples/cxx/tree/custom/comments/xml-schema-custom.hxx58
9 files changed, 600 insertions, 0 deletions
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 <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include "dom-parse.hxx"
+
+#include <istream>
+
+#include <xercesc/dom/DOM.hpp>
+#include <xercesc/util/XMLUniDefs.hpp> // chLatin_*
+#include <xercesc/framework/Wrapper4InputSource.hpp>
+
+#include <xsd/cxx/xml/sax/std-input-source.hxx>
+#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx>
+
+#include <xsd/cxx/tree/exceptions.hxx>
+#include <xsd/cxx/tree/error-handler.hxx>
+
+using namespace xercesc;
+namespace xml = xsd::cxx::xml;
+namespace tree = xsd::cxx::tree;
+
+xml::dom::auto_ptr<DOMDocument>
+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<DOMLSParser> 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<char> eh;
+ xml::dom::bits::error_handler_proxy<char> ehp (eh);
+ conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp);
+
+#else // _XERCES_VERSION >= 30000
+
+ // Same as above but for Xerces-C++ 2 series.
+ //
+ xml::dom::auto_ptr<DOMBuilder> 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<char> eh;
+ xml::dom::bits::error_handler_proxy<char> 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<DOMDocument> doc (parser->parse (&wrap));
+#else
+ xml::dom::auto_ptr<DOMDocument> doc (parser->parse (wrap));
+#endif
+
+ eh.throw_if_failed<tree::parsing<char> > ();
+
+ 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 <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef DOM_PARSE
+#define DOM_PARSE
+
+#include <string>
+#include <iosfwd>
+
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xsd/cxx/xml/dom/auto-ptr.hxx>
+
+// 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<xercesc::DOMDocument>
+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 <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <fstream>
+#include <iostream>
+
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+
+#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<xercesc::DOMDocument> doc (
+ parse (ifs, argv[1], true));
+
+ // Parse the DOM document to the object model.
+ //
+ std::auto_ptr<catalog> 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 <boris@codesynthesis.com>
+# 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 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/custom/comments/people.xml
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<ppl:catalog xmlns:ppl="http://www.codesynthesis.com/people"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.codesynthesis.com/people people.xsd">
+
+ <person>
+ <!--Record for Joe Dirt-->
+ <name>Joe Dirt</name>
+ <age>28</age>
+ </person>
+
+</ppl:catalog>
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 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/custom/comments/people.xsd
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:ppl="http://www.codesynthesis.com/people"
+ targetNamespace="http://www.codesynthesis.com/people">
+
+ <xsd:complexType name="person">
+ <xsd:sequence>
+ <xsd:element name="name" type="xsd:string"/>
+ <xsd:element name="age" type="xsd:unsignedShort"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="catalog">
+ <xsd:sequence>
+ <xsd:element name="person" type="ppl:person" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:element name="catalog" type="ppl:catalog"/>
+
+</xsd:schema>
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 <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+// Include xml-schema.hxx instead of xml-schema-custom.hxx here.
+//
+#include "xml-schema.hxx"
+
+#include <xercesc/dom/DOMComment.hpp>
+#include <xercesc/dom/DOMDocument.hpp>
+
+#include <xsd/cxx/xml/string.hxx> // 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<const DOMComment*> (n));
+ comment_ = xml::transcode<char> (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 <boris@codesynthesis.com>
+// 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 <string>
+
+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&);
+}