aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/serializer/hello
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cxx/serializer/hello')
-rw-r--r--examples/cxx/serializer/hello/README25
-rw-r--r--examples/cxx/serializer/hello/driver.cxx90
-rw-r--r--examples/cxx/serializer/hello/hello.xsd22
-rw-r--r--examples/cxx/serializer/hello/makefile64
4 files changed, 201 insertions, 0 deletions
diff --git a/examples/cxx/serializer/hello/README b/examples/cxx/serializer/hello/README
new file mode 100644
index 0000000..a560fac
--- /dev/null
+++ b/examples/cxx/serializer/hello/README
@@ -0,0 +1,25 @@
+This is a "Hello, world!" example that shows how to use the
+Embedded C++/Serializer mapping to serialize XML documents.
+
+The example consists of the following files:
+
+hello.xsd
+ XML Schema which describes "hello" instance documents.
+
+hello-sskel.hxx
+hello-sskel.ixx
+hello-sskel.cxx
+ Serializer skeletons generated by XSD/e from hello.xsd.
+
+driver.cxx
+ A serializer implementation and a driver for the example.
+ The serializer implementation simply creates an XML
+ document with pre-defined data. The driver first constructs
+ a serializer instance from the serializer implementation
+ mentioned above and a couple of predefined serializers for
+ the XML Schema built-in types. In then invokes this serializer
+ instance to write the XML document to STDOUT.
+
+To run the example simply execute:
+
+$ ./driver
diff --git a/examples/cxx/serializer/hello/driver.cxx b/examples/cxx/serializer/hello/driver.cxx
new file mode 100644
index 0000000..e895add
--- /dev/null
+++ b/examples/cxx/serializer/hello/driver.cxx
@@ -0,0 +1,90 @@
+// file : examples/cxx/serializer/hello/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <string>
+#include <vector>
+#include <iostream>
+
+#include "hello-sskel.hxx"
+
+using namespace std;
+
+struct hello_simpl: hello_sskel
+{
+ hello_simpl ()
+ {
+ names_.push_back ("sun");
+ names_.push_back ("moon");
+ names_.push_back ("world");
+ }
+
+ virtual void
+ pre ()
+ {
+ i_ = names_.begin ();
+ }
+
+ virtual string
+ greeting ()
+ {
+ return "Hello";
+ }
+
+ virtual bool
+ name_next ()
+ {
+ return i_ != names_.end ();
+ }
+
+ virtual string
+ name ()
+ {
+ return *i_++;
+ }
+
+private:
+ typedef vector<string> names;
+
+ names names_;
+ names::iterator i_;
+};
+
+
+int
+main ()
+{
+ try
+ {
+ // Construct the serializer.
+ //
+ xml_schema::string_simpl string_s;
+ hello_simpl hello_s;
+
+ hello_s.greeting_serializer (string_s);
+ hello_s.name_serializer (string_s);
+
+
+ // Create the XML instance document. The second argument to the
+ // document's constructor is the document's root element name.
+ //
+ xml_schema::document_simpl doc_s (hello_s, "hello");
+ doc_s.add_no_namespace_schema ("hello.xsd");
+
+ hello_s.pre ();
+ doc_s.serialize (cout);
+ hello_s.post ();
+ }
+ catch (const xml_schema::serializer_exception& e)
+ {
+ cerr << "error: " << e.text () << endl;
+ return 1;
+ }
+ catch (const std::ios_base::failure&)
+ {
+ cerr << "error: write failure" << endl;
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/examples/cxx/serializer/hello/hello.xsd b/examples/cxx/serializer/hello/hello.xsd
new file mode 100644
index 0000000..d2affb5
--- /dev/null
+++ b/examples/cxx/serializer/hello/hello.xsd
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/serializer/hello/hello.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="hello">
+ <xsd:sequence>
+ <xsd:element name="greeting" type="xsd:string"/>
+ <xsd:element name="name" type="xsd:string" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:element name="hello" type="hello"/>
+
+</xsd:schema>
diff --git a/examples/cxx/serializer/hello/makefile b/examples/cxx/serializer/hello/makefile
new file mode 100644
index 0000000..c2721bb
--- /dev/null
+++ b/examples/cxx/serializer/hello/makefile
@@ -0,0 +1,64 @@
+# file : examples/cxx/serializer/hello/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 := hello.xsd
+cxx := driver.cxx
+
+obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=-sskel.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)
+$(obj) $(dep): $(xsde.l.cpp-options)
+
+skel := $(out_base)/$(xsd:.xsd=-sskel.hxx) \
+ $(out_base)/$(xsd:.xsd=-sskel.ixx) \
+ $(out_base)/$(xsd:.xsd=-sskel.cxx)
+
+$(skel): xsde := $(out_root)/xsde/xsde
+$(skel): $(out_root)/xsde/xsde
+
+$(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=-sskel.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/serializer/xsd-cxx.make)
+
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsde/makefile)
+$(call import,$(src_root)/libxsde/xsde/makefile)