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/mixed/README | 38 ++++++++++++ examples/cxx/tree/mixed/driver.cxx | 121 +++++++++++++++++++++++++++++++++++++ examples/cxx/tree/mixed/makefile | 73 ++++++++++++++++++++++ examples/cxx/tree/mixed/text.xml | 18 ++++++ examples/cxx/tree/mixed/text.xsd | 29 +++++++++ 5 files changed, 279 insertions(+) create mode 100644 examples/cxx/tree/mixed/README create mode 100644 examples/cxx/tree/mixed/driver.cxx create mode 100644 examples/cxx/tree/mixed/makefile create mode 100644 examples/cxx/tree/mixed/text.xml create mode 100644 examples/cxx/tree/mixed/text.xsd (limited to 'examples/cxx/tree/mixed') diff --git a/examples/cxx/tree/mixed/README b/examples/cxx/tree/mixed/README new file mode 100644 index 0000000..c6f8be7 --- /dev/null +++ b/examples/cxx/tree/mixed/README @@ -0,0 +1,38 @@ +This example shows how to access the underlying DOM nodes in the +C++/Tree mapping in order to handle raw, "type-less content" such +as mixed content models, anyType/anySimpleType, and any/anyAttribute. + +In this example we use mixed content model to describe text with +embedded links, e.g., + + This paragraph talks about time. + +The example transforms such text into plain text with references, e.g., + + This paragraph talks about time[0]. + + [0] uri + +The example consists of the following files: + +text.xsd + XML Schema which describes "text with links" instance documents. + +text.xml + Sample XML instance document. + +text.hxx +text.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 text.xsd. + +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 uses + both the underlying DOM and statically-typed mapping to perform the + transformation. + +To run the example on the sample XML instance document simply execute: + +$ ./driver text.xml diff --git a/examples/cxx/tree/mixed/driver.cxx b/examples/cxx/tree/mixed/driver.cxx new file mode 100644 index 0000000..d905421 --- /dev/null +++ b/examples/cxx/tree/mixed/driver.cxx @@ -0,0 +1,121 @@ +// file : examples/cxx/tree/mixed/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include + +#include + +#include "text.hxx" + +// The following transcode() utility function is handy when working with +// Xerces. Include it after the generated header in order to get only char +// or wchar_t version depending on how you compiled your schemas. +// +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::auto_ptr; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " text.xml" << endl; + return 1; + } + + using namespace xercesc; + + int r (0); + + // The Xerces-C++ DOM objects that will be associated with the + // document tree "out-live" the call to the parsing function. + // Therefore we need to initialize the Xerces-C++ runtime + // ourselves. + // + XMLPlatformUtils::Initialize (); + + try + { + auto_ptr t ( + text_ (argv[1], + xml_schema::flags::keep_dom | + xml_schema::flags::dont_initialize)); + + // Note that DOM association is preserved in copies but only if they + // are "complete", i.e., made from the root of the tree. + // + text copy (*t); + + // Print text. + // + { + namespace xml = xsd::cxx::xml; + + unsigned long ref (0); + DOMNode* root (copy._node ()); + + for (DOMNode* n (root->getFirstChild ()); + n != 0; + n = n->getNextSibling ()) + { + switch (n->getNodeType ()) + { + case DOMNode::TEXT_NODE: + { + cout << xml::transcode (n->getTextContent ()); + break; + } + case DOMNode::ELEMENT_NODE: + { + // Let's get back to a tree node from this DOM node. + // + xml_schema::type& t ( + *reinterpret_cast ( + n->getUserData (xml_schema::dom::tree_node_key))); + + anchor& a (dynamic_cast (t)); + + cout << a << "[" << ref << "]"; + + // Or we could continue using DOM interface: + // + //cout << xml::transcode (n->getTextContent ()) + // << "[" << ref << "]"; + + ++ref; + break; + } + default: + break; // Ignore all other nodes (e.g., comments, etc). + } + } + } + + // Print references. + // + { + unsigned long r (0); + + for (text::a_const_iterator i (copy.a ().begin ()); + i != copy.a ().end (); + ++i, ++r) + { + cout << "[" << r << "] " << i->href () << endl; + } + } + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + r = 1; + } + + XMLPlatformUtils::Terminate (); + return r; +} diff --git a/examples/cxx/tree/mixed/makefile b/examples/cxx/tree/mixed/makefile new file mode 100644 index 0000000..cc81bca --- /dev/null +++ b/examples/cxx/tree/mixed/makefile @@ -0,0 +1,73 @@ +# file : examples/cxx/tree/mixed/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 := text.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) + + +# 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 + +$(out_base)/$(xsd:.xsd=.hxx) \ +$(out_base)/$(xsd:.xsd=.ixx) \ +$(out_base)/$(xsd:.xsd=.cxx): xsd_options := + +$(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/mixed/text.xml b/examples/cxx/tree/mixed/text.xml new file mode 100644 index 0000000..69abe8f --- /dev/null +++ b/examples/cxx/tree/mixed/text.xml @@ -0,0 +1,18 @@ + + + + + + +The first paragraph of this text talks about time. + +And this paragraph talks about space. + + diff --git a/examples/cxx/tree/mixed/text.xsd b/examples/cxx/tree/mixed/text.xsd new file mode 100644 index 0000000..8aa8280 --- /dev/null +++ b/examples/cxx/tree/mixed/text.xsd @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1