diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2009-02-24 15:16:26 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2009-02-24 15:16:26 +0200 |
commit | 707cc94fe52463870a9c6c8e2e66eaaa389e601d (patch) | |
tree | 13e10ff28bf4455d915f9d59b401bdbb62a393cb /examples/cxx/parser/mixed |
Start tracking XSD/e with git after version 3.0.03.0.0
Diffstat (limited to 'examples/cxx/parser/mixed')
-rw-r--r-- | examples/cxx/parser/mixed/README | 54 | ||||
-rw-r--r-- | examples/cxx/parser/mixed/anchor.hxx | 34 | ||||
-rw-r--r-- | examples/cxx/parser/mixed/driver.cxx | 123 | ||||
-rw-r--r-- | examples/cxx/parser/mixed/makefile | 66 | ||||
-rw-r--r-- | examples/cxx/parser/mixed/text.map | 7 | ||||
-rw-r--r-- | examples/cxx/parser/mixed/text.xml | 18 | ||||
-rw-r--r-- | examples/cxx/parser/mixed/text.xsd | 29 |
7 files changed, 331 insertions, 0 deletions
diff --git a/examples/cxx/parser/mixed/README b/examples/cxx/parser/mixed/README new file mode 100644 index 0000000..69d2b07 --- /dev/null +++ b/examples/cxx/parser/mixed/README @@ -0,0 +1,54 @@ +This example shows how to handle "type-less content" such as +mixed content models, xsd:anyType/xsd:anySimpleType, and +xsd:any/xsd:anyAttribute in the Embedded C++/Parser mapping. + +In this example we use mixed content model to describe text +with embedded links, e.g., + + This paragraph talks about <a href="uri">time</a>. + +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. + +anchor.hxx + Anchor type that captures the information about a link. + +text.map + Type map. It maps XML Schema anchor types defined in + text.xsd to C++ anchor class defined in anchor.hxx. + +text-pskel.hxx +text-pskel.ixx +text-pskel.cxx + Parser skeletons generated by XSD/e from text.xsd and + text.map. + +driver.cxx + A parser implementation and a driver for the example. The + parser implementation prints the transformed text to STDOUT. + The driver first constructs a parser instance from the parser + implementation mentioned above and a couple of predefined + parsers for the XML Schema built-in types. In then invokes + this parser instance to parse the input file. + +To run the example on the sample XML instance document simply +execute: + +$ ./driver text.xml + +The example reads from STDIN if input file is not specified: + +$ ./driver <text.xml diff --git a/examples/cxx/parser/mixed/anchor.hxx b/examples/cxx/parser/mixed/anchor.hxx new file mode 100644 index 0000000..bc1b54d --- /dev/null +++ b/examples/cxx/parser/mixed/anchor.hxx @@ -0,0 +1,34 @@ +// file : examples/cxx/parser/mixed/anchor.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#ifndef ANCHOR_HXX +#define ANCHOR_HXX + +#include <string> + +struct anchor +{ + anchor (const std::string& text, const std::string& uri) + : uri_ (uri), text_ (text) + { + } + + const std::string& + text () const + { + return text_; + } + + const std::string& + uri () const + { + return uri_; + } + +private: + std::string uri_; + std::string text_; +}; + +#endif // ANCHOR_HXX diff --git a/examples/cxx/parser/mixed/driver.cxx b/examples/cxx/parser/mixed/driver.cxx new file mode 100644 index 0000000..13877b0 --- /dev/null +++ b/examples/cxx/parser/mixed/driver.cxx @@ -0,0 +1,123 @@ +// file : examples/cxx/parser/mixed/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : not copyrighted - public domain + +#include <string> +#include <vector> +#include <iostream> + +#include "anchor.hxx" +#include "text-pskel.hxx" + +#ifndef XSDE_REUSE_STYLE_TIEIN +# error this example requires the tiein parser reuse support +#endif + +using namespace std; + +struct anchor_pimpl: anchor_pskel +{ + anchor_pimpl () + : anchor_pskel (&base_impl_) + { + } + + virtual void + href (const std::string& uri) + { + uri_ = uri; + } + + virtual anchor + post_anchor () + { + return anchor (post_string (), uri_); + } + +private: + xml_schema::string_pimpl base_impl_; + std::string uri_; +}; + + +struct text_pimpl: text_pskel +{ + virtual void + a (const anchor& a) + { + cout << a.text () << "[" << anchors_.size () << "]"; + anchors_.push_back (a); + } + + virtual void + _any_characters (const xml_schema::ro_string& s) + { + cout << s; + } + + virtual void + post_text () + { + for (anchors::const_iterator i (anchors_.begin ()); + i != anchors_.end (); + ++i) + { + cout << "[" << i - anchors_.begin () << "] " << i->uri () << endl; + } + } + +private: + typedef vector<anchor> anchors; + anchors anchors_; +}; + + +int +main (int argc, char* argv[]) +{ + const char* input; + + if (argc < 2) + { + input = "STDIN"; + cerr << "XML file not specified, reading from STDIN" << endl; + } + else + input = argv[1]; + + try + { + // Construct the parser. + // + xml_schema::string_pimpl string_p; + anchor_pimpl anchor_p; + text_pimpl text_p; + + anchor_p.href_parser (string_p); + text_p.a_parser (anchor_p); + + xml_schema::document_pimpl doc_p (text_p, "text"); + + text_p.pre (); + + if (argc < 2) + doc_p.parse (cin); + else + doc_p.parse (argv[1]); + + text_p.post_text (); + } + catch (const xml_schema::parser_exception& e) + { + cerr << input << ":" << e.line () << ":" << e.column () << ": " + << e.text () << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << input << ": unable to open or read failure" << endl; + return 1; + } + + return 0; +} diff --git a/examples/cxx/parser/mixed/makefile b/examples/cxx/parser/mixed/makefile new file mode 100644 index 0000000..f41fae3 --- /dev/null +++ b/examples/cxx/parser/mixed/makefile @@ -0,0 +1,66 @@ +# file : examples/cxx/parser/mixed/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 := text.xsd +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-pskel.o)) +dep := $(obj:.o=.o.d) + +xsde.l := $(out_root)/libxsde/xsde/xsde.l +xsde.l.cpp-options := $(out_root)/libxsde/xsde/xsde.l.cpp-options + +driver := $(out_base)/driver +clean := $(out_base)/.clean + + +# Build. +# +$(driver): $(obj) $(xsde.l) + +$(obj) $(dep): cpp_options := -I$(out_base) -I$(src_base) +$(obj) $(dep): $(xsde.l.cpp-options) + +skel := $(out_base)/$(xsd:.xsd=-pskel.hxx) \ + $(out_base)/$(xsd:.xsd=-pskel.ixx) \ + $(out_base)/$(xsd:.xsd=-pskel.cxx) + +$(skel): xsde := $(out_root)/xsde/xsde +$(skel): xsde_options += --type-map $(src_base)/text.map + +$(skel): $(out_root)/xsde/xsde $(src_base)/text.map + +$(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=-pskel.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)/xsde/parser/xsd-cxx.make) + + +# Dependencies. +# +$(call import,$(src_root)/xsde/makefile) +$(call import,$(src_root)/libxsde/xsde/makefile) diff --git a/examples/cxx/parser/mixed/text.map b/examples/cxx/parser/mixed/text.map new file mode 100644 index 0000000..206d72e --- /dev/null +++ b/examples/cxx/parser/mixed/text.map @@ -0,0 +1,7 @@ +# file : examples/cxx/parser/mixed/text.map +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : not copyrighted - public domain + +include "anchor.hxx"; + +anchor anchor; diff --git a/examples/cxx/parser/mixed/text.xml b/examples/cxx/parser/mixed/text.xml new file mode 100644 index 0000000..97d4d21 --- /dev/null +++ b/examples/cxx/parser/mixed/text.xml @@ -0,0 +1,18 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/parser/text/text.xml +author : Boris Kolpackov <boris@codesynthesis.com> +copyright : not copyrighted - public domain + +--> + +<text xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="text.xsd"> + +The first paragraph of this text talks about <a href="http://en.wikipedia.org/wiki/time">time</a>. + +And this paragraph talks about <a href="http://en.wikipedia.org/wiki/space">space</a>. + +</text> diff --git a/examples/cxx/parser/mixed/text.xsd b/examples/cxx/parser/mixed/text.xsd new file mode 100644 index 0000000..4929964 --- /dev/null +++ b/examples/cxx/parser/mixed/text.xsd @@ -0,0 +1,29 @@ +<?xml version="1.0"?> + +<!-- + +file : examples/cxx/parser/mixed/text.xsd +author : Boris Kolpackov <boris@codesynthesis.com> +copyright : not copyrighted - public domain + +--> + +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> + + <xsd:complexType name="anchor"> + <xsd:simpleContent> + <xsd:extension base="xsd:string"> + <xsd:attribute name="href" type="xsd:string" use="required"/> + </xsd:extension> + </xsd:simpleContent> + </xsd:complexType> + + <xsd:complexType name="text" mixed="true"> + <xsd:sequence> + <xsd:element name="a" type="anchor" minOccurs="0" maxOccurs="unbounded"/> + </xsd:sequence> + </xsd:complexType> + + <xsd:element name="text" type="text"/> + +</xsd:schema> |