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/dbxml/README | 45 ++++++++++ examples/cxx/tree/dbxml/driver.cxx | 175 ++++++++++++++++++++++++++++++++++++ examples/cxx/tree/dbxml/library.xsd | 75 ++++++++++++++++ examples/cxx/tree/dbxml/makefile | 76 ++++++++++++++++ 4 files changed, 371 insertions(+) create mode 100644 examples/cxx/tree/dbxml/README create mode 100644 examples/cxx/tree/dbxml/driver.cxx create mode 100644 examples/cxx/tree/dbxml/library.xsd create mode 100644 examples/cxx/tree/dbxml/makefile (limited to 'examples/cxx/tree/dbxml') diff --git a/examples/cxx/tree/dbxml/README b/examples/cxx/tree/dbxml/README new file mode 100644 index 0000000..59e67d0 --- /dev/null +++ b/examples/cxx/tree/dbxml/README @@ -0,0 +1,45 @@ +This example shows how to use the C++/Tree mapping on top of the Berkeley +DB XML embedded XML database. This example is described in detail in the +"C++/Tree Mapping and Berkeley DB XML Integration Guide" which can be +found in the documentation/cxx/tree/dbxml directory of the XSD +distribution. + +You will need the Berkeley DB XML libraries[1] installed in order to build +and run this example. The makefiles and project files for this example link +to the DB XML library names corresponding to version 2.3.X. Earlier and later +versions can also be used but may require adjustments to the library names +being linked to. + +Note that due to the incomplete DOM API implementation provided by DB +XML (as of version 2.3.10), the generated code and your application +should be compiled with the DBXML_DOM macro defined in order to avoid +using unsupported parts of the API. + +The example consists of the following files: + +library.xsd + XML Schema which describes a library of books. + +library.hxx +library.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 library.xsd. + +driver.cxx + Driver for the example. It performs the following four operations on + the database: + + * Create a new document in DB from an object model + * Create an object model from a document in DB + * Create an object model from a document fragment in DB + * Update a document fragment in DB from an object model + + +To run the example simply execute: + +$ ./driver + + +[1] http://www.sleepycat.com/products/bdbxml.html diff --git a/examples/cxx/tree/dbxml/driver.cxx b/examples/cxx/tree/dbxml/driver.cxx new file mode 100644 index 0000000..56cbb4a --- /dev/null +++ b/examples/cxx/tree/dbxml/driver.cxx @@ -0,0 +1,175 @@ +// file : examples/cxx/tree/dbxml/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include +#include +#include + +#include + +#include "library.hxx" + +using std::cerr; +using std::endl; +using std::string; +using std::auto_ptr; + +using namespace DbXml; +using namespace xsd::cxx; // for xml::string + +void +print_document (const string& desc, + XmlContainer container, + const string& name) +{ + XmlDocument doc (container.getDocument (name)); + + string content; + doc.getContent (content); + + cerr << endl + << desc << endl + << content << endl; +} + +int +main () +{ + try + { + using namespace library; + using xml_schema::date; + + XmlManager manager; + + { + XmlContainer container (manager.createContainer ("new.bdbxml")); + + XmlUpdateContext update_context (manager.createUpdateContext ()); + + XmlQueryContext context (manager.createQueryContext ()); + context.setNamespace ("lib", "http://www.codesynthesis.com/library"); + + + // Create a new document from an object model. + // + { + // Create a new catalog with one book. + // + catalog c; + + book b (20530902, // ISBN + "The Elements of Style", // Title + genre::reference, // Genre + "ES"); // ID + + author strunk ("William Strunk, Jr.", date (1869, 7, 1)); + strunk.died (date (1946, 9, 26)); + + b.author ().push_back (strunk); + c.book ().push_back (b); + + + // Create a new XML document. + // + XmlDocument doc (manager.createDocument ()); + doc.setName ("new.xml"); + + + // Obtain its DOM representation and add the root element. + // + xercesc::DOMDocument& dom_doc (*doc.getContentAsDOM ()); + + dom_doc.appendChild ( + dom_doc.createElementNS ( + xml::string ("http://www.codesynthesis.com/library").c_str (), + xml::string ("lib:catalog").c_str ())); + + + // Serialize the object model to the XML document. Also avoid + // re-initializing the Xerces-C++ runtime since XmlManager has + // it initialized. + // + catalog_ (dom_doc, c, xml_schema::flags::dont_initialize); + + + // Place the document into the container. + // + container.putDocument (doc, update_context); + + print_document ("after create:", container, "new.xml"); + } + + // Create an object model from a document in DB. + // + { + // Resolve the document in the container. + // + XmlDocument doc (container.getDocument ("new.xml")); + + + // Create the object model from the document's DOM. Also avoid + // re-initializing the Xerces-C++ runtime since XmlManager has + // it initialized. + // + auto_ptr c (catalog_ (*doc.getContentAsDOM (), + xml_schema::flags::dont_initialize)); + + cerr << *c << endl; + } + + + // Lookup a document fragment. + // + + string query ("collection('new.bdbxml')/lib:catalog/book[@id='ES']"); + + // Find "The Elements of Style". + // + XmlValue v; + XmlResults results (manager.query (query, context)); + + if (results.next (v)) + { + // Create an object model from the document fragment. + // + auto_ptr b ( + new book ( + *static_cast (v.asNode ()))); + + cerr << *b << endl; + + + // Add another author, change the availability status. + // + author white ("E.B. White", date (1899, 7, 11)); + white.died (date (1985, 10, 1)); + + b->author ().push_back (white); + b->available (false); + + + // Update the document fragment from the object model. + // + *static_cast (v.asNode ()) << *b; + + + // Update the document in the container. + // + XmlDocument doc (v.asDocument ()); + container.updateDocument (doc, update_context); + } + + print_document ("after update:", container, "new.xml"); + } + + manager.removeContainer ("new.bdbxml"); + } + catch (const std::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/examples/cxx/tree/dbxml/library.xsd b/examples/cxx/tree/dbxml/library.xsd new file mode 100644 index 0000000..c71c312 --- /dev/null +++ b/examples/cxx/tree/dbxml/library.xsd @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/cxx/tree/dbxml/makefile b/examples/cxx/tree/dbxml/makefile new file mode 100644 index 0000000..2a5ba4e --- /dev/null +++ b/examples/cxx/tree/dbxml/makefile @@ -0,0 +1,76 @@ +# file : examples/cxx/tree/dbxml/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make + +xsd := library.xsd +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=.o)) +dep := $(obj:.o=.o.d) + +driver := $(out_base)/driver +clean := $(out_base)/.clean + + +# Import. +# +$(call import,\ + $(scf_root)/import/libxerces-c/stub.make,\ + l: xerces_c.l,cpp-options: xerces_c.l.cpp-options) + +$(call import,\ + $(scf_root)/import/libdbxml/stub.make,\ + l: dbxml.l,cpp-options: dbxml.l.cpp-options) + + +# Build. +# +$(driver): $(obj) $(xerces_c.l) $(dbxml.l) + +$(obj) $(dep): cpp_options := -I$(src_root)/libxsd -DDBXML_DOM +$(obj) $(dep): $(xerces_c.l.cpp-options) $(dbxml.l.cpp-options) + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := --generate-serialization \ + --generate-ostream + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean)) + + +# How to. +# +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(scf_root)/xsd/tree/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) -- cgit v1.1