summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/parser/hello
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-12-18 18:48:46 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2021-02-25 13:45:48 +0300
commit5e527213a2430bb3018e5eebd909aef294edf9b5 (patch)
tree94de33c82080b53d9a9e300170f6d221d89078f4 /xsd-examples/cxx/parser/hello
parent7420f85ea19b0562ffdd8123442f32bc8bac1267 (diff)
Switch to build2
Diffstat (limited to 'xsd-examples/cxx/parser/hello')
-rw-r--r--xsd-examples/cxx/parser/hello/README28
-rw-r--r--xsd-examples/cxx/parser/hello/buildfile24
-rw-r--r--xsd-examples/cxx/parser/hello/driver.cxx67
-rw-r--r--xsd-examples/cxx/parser/hello/hello.xml19
-rw-r--r--xsd-examples/cxx/parser/hello/hello.xsd21
5 files changed, 159 insertions, 0 deletions
diff --git a/xsd-examples/cxx/parser/hello/README b/xsd-examples/cxx/parser/hello/README
new file mode 100644
index 0000000..97449de
--- /dev/null
+++ b/xsd-examples/cxx/parser/hello/README
@@ -0,0 +1,28 @@
+This is a "Hello, world!" example that shows how to use the
+C++/Parser mapping to parse XML instance documents.
+
+The example consists of the following files:
+
+hello.xsd
+ XML Schema which describes "hello" instance documents.
+
+hello.xml
+ Sample XML instance document.
+
+hello-pskel.hxx
+hello-pskel.cxx
+ Parser skeletons generated by XSD from hello.xsd.
+
+driver.cxx
+ A parser implementation and a driver for the example. The
+ parser implementation simply prints the data to STDERR.
+ 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 hello.xml
diff --git a/xsd-examples/cxx/parser/hello/buildfile b/xsd-examples/cxx/parser/hello/buildfile
new file mode 100644
index 0000000..2df4a7f
--- /dev/null
+++ b/xsd-examples/cxx/parser/hello/buildfile
@@ -0,0 +1,24 @@
+# file : cxx/parser/hello/buildfile
+# license : not copyrighted - public domain
+
+import libs = libxsd%lib{xsd}
+import libs += libxerces-c%lib{xerces-c}
+
+./: exe{driver} doc{README}
+
+exe{driver}: {hxx cxx}{* -hello-pskel} {hxx ixx cxx}{hello-pskel} $libs
+
+exe{driver}: xml{hello}: test.input = true
+
+<{hxx ixx cxx}{hello-pskel}>: xsd{hello} $xsd
+{{
+ diag xsd ($<[0]) # @@ TMP
+
+ $xsd cxx-parser --std c++11 \
+ --generate-inline \
+ --skel-file-suffix -pskel \
+ --output-dir $out_base \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base"
diff --git a/xsd-examples/cxx/parser/hello/driver.cxx b/xsd-examples/cxx/parser/hello/driver.cxx
new file mode 100644
index 0000000..024d2fc
--- /dev/null
+++ b/xsd-examples/cxx/parser/hello/driver.cxx
@@ -0,0 +1,67 @@
+// file : cxx/parser/hello/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <string>
+#include <iostream>
+
+#include "hello-pskel.hxx"
+
+using namespace std;
+
+struct hello_pimpl: hello_pskel
+{
+ virtual void
+ greeting (const string& greeting)
+ {
+ greeting_ = greeting;
+ }
+
+ virtual void
+ name (const string& name)
+ {
+ cout << greeting_ << ", " << name << "!" << endl;
+ }
+
+private:
+ string greeting_;
+};
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " hello.xml" << endl;
+ return 1;
+ }
+
+ try
+ {
+ // Construct the parser.
+ //
+ xml_schema::string_pimpl string_p;
+ hello_pimpl hello_p;
+
+ hello_p.greeting_parser (string_p);
+ hello_p.name_parser (string_p);
+
+ // Parse the XML instance document. The second argument to the
+ // document's constructor is the document's root element name.
+ //
+ xml_schema::document doc_p (hello_p, "hello");
+
+ hello_p.pre ();
+ doc_p.parse (argv[1]);
+ hello_p.post_hello ();
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ return 1;
+ }
+ catch (const std::ios_base::failure&)
+ {
+ cerr << argv[1] << ": unable to open or read failure" << endl;
+ return 1;
+ }
+}
diff --git a/xsd-examples/cxx/parser/hello/hello.xml b/xsd-examples/cxx/parser/hello/hello.xml
new file mode 100644
index 0000000..1f5adad
--- /dev/null
+++ b/xsd-examples/cxx/parser/hello/hello.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/parser/hello/hello.xml
+copyright : not copyrighted - public domain
+
+-->
+
+<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="hello.xsd">
+
+ <greeting>Hello</greeting>
+
+ <name>sun</name>
+ <name>moon</name>
+ <name>world</name>
+
+</hello>
diff --git a/xsd-examples/cxx/parser/hello/hello.xsd b/xsd-examples/cxx/parser/hello/hello.xsd
new file mode 100644
index 0000000..81907d6
--- /dev/null
+++ b/xsd-examples/cxx/parser/hello/hello.xsd
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/parser/hello/hello.xsd
+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>