diff options
Diffstat (limited to 'xsd/examples/cxx/tree/dbxml')
-rw-r--r-- | xsd/examples/cxx/tree/dbxml/README | 45 | ||||
-rw-r--r-- | xsd/examples/cxx/tree/dbxml/driver.cxx | 175 | ||||
-rw-r--r-- | xsd/examples/cxx/tree/dbxml/library.xsd | 75 | ||||
-rw-r--r-- | xsd/examples/cxx/tree/dbxml/makefile | 107 |
4 files changed, 402 insertions, 0 deletions
diff --git a/xsd/examples/cxx/tree/dbxml/README b/xsd/examples/cxx/tree/dbxml/README new file mode 100644 index 0000000..59e67d0 --- /dev/null +++ b/xsd/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/xsd/examples/cxx/tree/dbxml/driver.cxx b/xsd/examples/cxx/tree/dbxml/driver.cxx new file mode 100644 index 0000000..56cbb4a --- /dev/null +++ b/xsd/examples/cxx/tree/dbxml/driver.cxx @@ -0,0 +1,175 @@ +// file : examples/cxx/tree/dbxml/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <memory> // std::auto_ptr +#include <string> +#include <cassert> +#include <iostream> + +#include <dbxml/DbXml.hpp> + +#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<catalog> 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<book> b ( + new book ( + *static_cast<xercesc::DOMElement*> (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<xercesc::DOMElement*> (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/xsd/examples/cxx/tree/dbxml/library.xsd b/xsd/examples/cxx/tree/dbxml/library.xsd new file mode 100644 index 0000000..c71c312 --- /dev/null +++ b/xsd/examples/cxx/tree/dbxml/library.xsd @@ -0,0 +1,75 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/tree/dbxml/library.xsd +author : Boris Kolpackov <boris@codesynthesis.com> +copyright : not copyrighted - public domain + +--> + +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xse="http://www.codesynthesis.com/xmlns/xml-schema-extension" + xmlns:lib="http://www.codesynthesis.com/library" + targetNamespace="http://www.codesynthesis.com/library"> + + <xsd:simpleType name="isbn"> + <xsd:restriction base="xsd:unsignedInt"/> + </xsd:simpleType> + + + <xsd:complexType name="title"> + <xsd:simpleContent> + <xsd:extension base="xsd:string"> + <xsd:attribute name="lang" type="xsd:language"/> + </xsd:extension> + </xsd:simpleContent> + </xsd:complexType> + + <xsd:simpleType name="genre"> + <xsd:restriction base="xsd:string"> + <xsd:enumeration value="romance"/> + <xsd:enumeration value="fiction"/> + <xsd:enumeration value="horror"/> + <xsd:enumeration value="history"/> + <xsd:enumeration value="philosophy"/> + <xsd:enumeration value="reference"/> + </xsd:restriction> + </xsd:simpleType> + + <xsd:complexType name="person"> + <xsd:sequence> + <xsd:element name="name" type="xsd:string"/> + <xsd:element name="born" type="xsd:date"/> + <xsd:element name="died" type="xsd:date" minOccurs="0"/> + </xsd:sequence> + </xsd:complexType> + + <xsd:complexType name="author"> + <xsd:complexContent> + <xsd:extension base="lib:person"> + <xsd:attribute name="recommends" type="xsd:IDREF" xse:refType="lib:book"/> + </xsd:extension> + </xsd:complexContent> + </xsd:complexType> + + <xsd:complexType name="book"> + <xsd:sequence> + <xsd:element name="isbn" type="lib:isbn"/> + <xsd:element name="title" type="lib:title"/> + <xsd:element name="genre" type="lib:genre"/> + <xsd:element name="author" type="lib:author" maxOccurs="unbounded"/> + </xsd:sequence> + <xsd:attribute name="available" type="xsd:boolean" default="true"/> + <xsd:attribute name="id" type="xsd:ID" use="required"/> + </xsd:complexType> + + <xsd:complexType name="catalog"> + <xsd:sequence> + <xsd:element name="book" type="lib:book" maxOccurs="unbounded"/> + </xsd:sequence> + </xsd:complexType> + + <xsd:element name="catalog" type="lib:catalog"/> + +</xsd:schema> diff --git a/xsd/examples/cxx/tree/dbxml/makefile b/xsd/examples/cxx/tree/dbxml/makefile new file mode 100644 index 0000000..fe10d6d --- /dev/null +++ b/xsd/examples/cxx/tree/dbxml/makefile @@ -0,0 +1,107 @@ +# file : examples/cxx/tree/dbxml/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2005-2010 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 +install := $(out_base)/.install +dist := $(out_base)/.dist +dist-win := $(out_base)/.dist-win +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) + +ifeq ($(filter $(MAKECMDGOALS),dist dist-win install),) +$(call import,\ + $(scf_root)/import/libdbxml/stub.make,\ + l: dbxml.l,cpp-options: dbxml.l.cpp-options) +endif + + +# 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) + +genf := $(xsd:.xsd=.hxx) $(xsd:.xsd=.ixx) $(xsd:.xsd=.cxx) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): xsd := $(out_root)/xsd/xsd +$(gen): xsd_options := --generate-serialization --generate-ostream +$(gen): $(out_root)/xsd/xsd + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +$(out_base)/: $(driver) + + +# Install & Dist. +# +dist-common := $(out_base)/.dist-common + +$(install) $(dist) $(dist-win) $(dist-common): path := $(subst $(src_root)/,,$(src_base)) + +$(install): + $(call install-data,$(src_base)/README,$(install_doc_dir)/xsd/$(path)/README) + $(call install-data,$(src_base)/driver.cxx,$(install_doc_dir)/xsd/$(path)/driver.cxx) + $(call install-data,$(src_base)/library.xsd,$(install_doc_dir)/xsd/$(path)/library.xsd) + +$(dist-common): + $(call install-data,$(src_base)/driver.cxx,$(dist_prefix)/$(path)/driver.cxx) + $(call install-data,$(src_base)/library.xsd,$(dist_prefix)/$(path)/library.xsd) + +$(dist): $(dist-common) + $(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README) + +$(dist-win): $(dist-common) + $(call install-data,$(src_base)/README,$(dist_prefix)/$(path)/README.txt) + $(call message,,unix2dos $(dist_prefix)/$(path)/README.txt) + + +# Clean. +# +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(gen): | $(out_base)/.gitignore +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# 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,$(bld_root)/install.make) +$(call include,$(scf_root)/xsd/tree/xsd-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/xsd/makefile) |