summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/streaming
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-09-17 07:15:29 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-09-17 07:15:29 +0200
commitf0510d2f90467de8e8f260b47d79a9baaf9bef17 (patch)
tree0b9929946f06a9cbe9b9e8f2a7600dae4e048f79 /examples/cxx/tree/streaming
Start tracking XSD with git
Diffstat (limited to 'examples/cxx/tree/streaming')
-rw-r--r--examples/cxx/tree/streaming/README22
-rw-r--r--examples/cxx/tree/streaming/driver.cxx65
-rw-r--r--examples/cxx/tree/streaming/makefile74
-rw-r--r--examples/cxx/tree/streaming/records.xsd37
4 files changed, 198 insertions, 0 deletions
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 <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <iostream>
+#include <fstream>
+
+#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 << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl
+ << "<records>" << 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 << "</records>" << 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 <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 := 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 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/streaming/records.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="record">
+ <xsd:sequence>
+ <xsd:element name="data" type="xsd:string"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <!--
+
+ We need this global element so that we get serialization functions
+ for record. Note that global elements are always qualified. As a
+ result, if you are using XML namespaces (this example is not), you
+ will need to make sure the record element inside the records type
+ is also qualified.
+
+ -->
+ <xsd:element name="record" type="record"/>
+
+ <xsd:complexType name="records">
+ <xsd:sequence>
+ <xsd:element name="record" type="record" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:element name="records" type="records"/>
+
+</xsd:schema>