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/streaming/README | 22 ++++++++++ examples/cxx/tree/streaming/driver.cxx | 65 +++++++++++++++++++++++++++++ examples/cxx/tree/streaming/makefile | 74 +++++++++++++++++++++++++++++++++ examples/cxx/tree/streaming/records.xsd | 37 +++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 examples/cxx/tree/streaming/README create mode 100644 examples/cxx/tree/streaming/driver.cxx create mode 100644 examples/cxx/tree/streaming/makefile create mode 100644 examples/cxx/tree/streaming/records.xsd (limited to 'examples/cxx/tree/streaming') diff --git a/examples/cxx/tree/streaming/README b/examples/cxx/tree/streaming/README new file mode 100644 index 0000000..51e5c14 --- /dev/null +++ b/examples/cxx/tree/streaming/README @@ -0,0 +1,22 @@ +This example shows how to create an XML document by performing multiple +serializations of its smaller parts. This can be useful when the +document is too large to fit into memory or when the other end needs +to start processing without waiting for the whole document (streaming). + +The example consists of the following files: + +records.xsd + XML Schema which describes a collection of data records. + +records.hxx +records.cxx + C++ types that represent the given vocabulary as well as serialization + functions. These are generated by XSD from records.xsd. + +driver.cxx + Driver for the example. It progressively serializes one thousand data + record into a file (out.xml). + +To run the example simply execute: + +$ ./driver diff --git a/examples/cxx/tree/streaming/driver.cxx b/examples/cxx/tree/streaming/driver.cxx new file mode 100644 index 0000000..6afc1ce --- /dev/null +++ b/examples/cxx/tree/streaming/driver.cxx @@ -0,0 +1,65 @@ +// file : examples/cxx/tree/streaming/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include +#include + +#include "records.hxx" + +using std::cerr; +using std::endl; +using std::ios_base; + +int +main () +{ + try + { + std::ofstream ofs; + ofs.exceptions (ios_base::badbit | ios_base::failbit); + ofs.open ("out.xml"); + + // We will need to create XML declaration as well as open and close + // the root tag ourselves. + // + ofs << "" << endl + << "" << endl; + + // For performance reasons, we would like to initialize/terminate + // Xerces-C++ ourselves once instead of letting the serialization + // function do it for every record. + // + xercesc::XMLPlatformUtils::Initialize (); + + xml_schema::namespace_infomap map; + + for (unsigned long i (0); i < 1000; ++i) + { + // Create the next record. + // + record r ("data"); + + record_ (ofs, + r, + map, + "UTF-8", + xml_schema::flags::dont_initialize | + xml_schema::flags::no_xml_declaration); + } + + xercesc::XMLPlatformUtils::Terminate (); + + ofs << "" << endl; + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << "io failure" << endl; + return 1; + } +} diff --git a/examples/cxx/tree/streaming/makefile b/examples/cxx/tree/streaming/makefile new file mode 100644 index 0000000..0c478e0 --- /dev/null +++ b/examples/cxx/tree/streaming/makefile @@ -0,0 +1,74 @@ +# file : examples/cxx/tree/streaming/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 := records.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 := --generate-serialization \ + --suppress-parsing \ + --root-element-all + +$(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)/out.xml) + + +# 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/streaming/records.xsd b/examples/cxx/tree/streaming/records.xsd new file mode 100644 index 0000000..2b357e6 --- /dev/null +++ b/examples/cxx/tree/streaming/records.xsd @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1