summaryrefslogtreecommitdiff
path: root/xsd-examples/cxx/tree/library
diff options
context:
space:
mode:
Diffstat (limited to 'xsd-examples/cxx/tree/library')
-rw-r--r--xsd-examples/cxx/tree/library/.gitignore1
-rw-r--r--xsd-examples/cxx/tree/library/README32
-rw-r--r--xsd-examples/cxx/tree/library/buildfile25
-rw-r--r--xsd-examples/cxx/tree/library/driver.cxx130
-rw-r--r--xsd-examples/cxx/tree/library/library.xml52
-rw-r--r--xsd-examples/cxx/tree/library/library.xsd72
6 files changed, 312 insertions, 0 deletions
diff --git a/xsd-examples/cxx/tree/library/.gitignore b/xsd-examples/cxx/tree/library/.gitignore
new file mode 100644
index 0000000..c116ec1
--- /dev/null
+++ b/xsd-examples/cxx/tree/library/.gitignore
@@ -0,0 +1 @@
+library.?xx
diff --git a/xsd-examples/cxx/tree/library/README b/xsd-examples/cxx/tree/library/README
new file mode 100644
index 0000000..0b8638c
--- /dev/null
+++ b/xsd-examples/cxx/tree/library/README
@@ -0,0 +1,32 @@
+This example shows how to use the C++/Tree mapping to parse XML documents
+into a tree-like in-memory object model, modify this object model, and
+finally serialize it back to XML.
+
+The example consists of the following files:
+
+library.xsd
+ XML Schema which describes a library of books.
+
+library.xml
+ Sample XML instance document.
+
+library.hxx
+library.ixx
+library.cxx
+ C++ types that represent the given vocabulary, a set of parsing
+ functions that convert XML documents to a tree-like in-memory object
+ model, and a set of serialization functions that convert the object
+ model back to XML. These are generated by XSD from library.xsd.
+
+driver.cxx
+ Driver for the example. It first calls one of the parsing functions
+ that constructs the object model from the input file. It then prints
+ the content of the object model to STDERR. Finally, the driver modifies
+ the object model and serializes it back to XML.
+
+To run the example on the sample XML instance document simply execute:
+
+$ ./driver library.xml
+
+This example also shows how to use the ID/IDREF cross-referencing
+mechanism and the xsd:enumeration to C++ enum mapping.
diff --git a/xsd-examples/cxx/tree/library/buildfile b/xsd-examples/cxx/tree/library/buildfile
new file mode 100644
index 0000000..4ab023b
--- /dev/null
+++ b/xsd-examples/cxx/tree/library/buildfile
@@ -0,0 +1,25 @@
+# file : cxx/tree/library/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}{* -library} {hxx ixx cxx}{library} $libs
+
+exe{driver}: xml{library}: test.input = true
+
+<{hxx ixx cxx}{library}>: xsd{library} $xsd
+{{
+ diag xsd ($<[0]) # @@ TMP
+
+ $xsd cxx-tree --std c++11 \
+ --generate-inline \
+ --generate-ostream \
+ --generate-serialization \
+ --output-dir $out_base \
+ $path($<[0])
+}}
+
+cxx.poptions =+ "-I$out_base"
diff --git a/xsd-examples/cxx/tree/library/driver.cxx b/xsd-examples/cxx/tree/library/driver.cxx
new file mode 100644
index 0000000..9086891
--- /dev/null
+++ b/xsd-examples/cxx/tree/library/driver.cxx
@@ -0,0 +1,130 @@
+// file : cxx/tree/library/driver.cxx
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include "library.hxx"
+
+using std::cerr;
+using std::endl;
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " library.xml" << endl;
+ return 1;
+ }
+
+ try
+ {
+ using namespace library;
+
+ // Read in the XML file and obtain its object model.
+ //
+ std::unique_ptr<catalog> c (catalog_ (argv[1]));
+
+
+ // Let's print what we've got.
+ //
+ for (catalog::book_const_iterator bi (c->book ().begin ());
+ bi != c->book ().end ();
+ ++bi)
+ {
+ cerr << endl
+ << "ID : " << bi->id () << endl
+ << "ISBN : " << bi->isbn () << endl
+ << "Title : " << bi->title () << endl
+ << "Genre : " << bi->genre () << endl;
+
+ for (book::author_const_iterator ai (bi->author ().begin ());
+ ai != bi->author ().end ();
+ ++ai)
+ {
+ cerr << "Author : " << ai->name () << endl;
+ cerr << " Born : " << ai->born () << endl;
+
+ if (ai->died ())
+ cerr << " Died : " << *ai->died () << endl;
+
+ if (ai->recommends ())
+ cerr << " Recommends : " << (*ai->recommends ())->title () << endl;
+ }
+
+ cerr << "Available : " << std::boolalpha << bi->available () << endl;
+ }
+
+
+ // Now we are going to modify the object model and serialize it
+ // back to XML.
+ //
+
+ catalog::book_sequence& books (c->book ());
+
+
+ // Get rid of all unavailable books.
+ //
+ for (catalog::book_iterator bi (books.begin ()); bi != books.end ();)
+ {
+ if (!bi->available ())
+ bi = books.erase (bi);
+ else
+ ++bi;
+ }
+
+
+ // Insert a new book.
+ //
+ book b (679776443, // ISBN
+ "Dead Souls", // Title
+ genre::philosophy, // Genre
+ "DS"); // ID
+
+ b.author ().push_back (author ("Nikolai Gogol",
+ xml_schema::date (1809, 3, 31)));
+
+ books.insert (books.begin (), b);
+
+
+ // Because we removed all unavailable books, some IDREFs might be
+ // broken. Let's fix this.
+ //
+ for (catalog::book_iterator bi (books.begin ()); bi != books.end (); ++bi)
+ {
+ for (book::author_iterator ai (bi->author ().begin ());
+ ai != bi->author ().end ();
+ ++ai)
+ {
+ author::recommends_optional& c (ai->recommends ());
+
+ if (c.present ())
+ {
+ author::recommends_type& ref (c.get ());
+
+ if (!ref)
+ c.reset ();
+ }
+ }
+ }
+
+
+ // Prepare namespace mapping and schema location information.
+ //
+ xml_schema::namespace_infomap map;
+
+ map["lib"].name = "http://www.codesynthesis.com/library";
+ map["lib"].schema = "library.xsd";
+
+
+ // Write it out.
+ //
+ catalog_ (std::cout, *c, map);
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ return 1;
+ }
+}
diff --git a/xsd-examples/cxx/tree/library/library.xml b/xsd-examples/cxx/tree/library/library.xml
new file mode 100644
index 0000000..d048aa4
--- /dev/null
+++ b/xsd-examples/cxx/tree/library/library.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/library/library.xml
+copyright : not copyrighted - public domain
+
+-->
+
+<lib:catalog xmlns:lib="http://www.codesynthesis.com/library"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.codesynthesis.com/library library.xsd">
+
+ <book id="MM" available="false">
+ <isbn>0679760806</isbn>
+ <title>The Master and Margarita</title>
+ <genre>fiction</genre>
+
+ <author recommends="WP">
+ <name>Mikhail Bulgakov</name>
+ <born>1891-05-15</born>
+ <died>1940-03-10</died>
+ </author>
+ </book>
+
+
+ <book id="WP">
+ <isbn>0679600841</isbn>
+ <title>War and Peace</title>
+ <genre>history</genre>
+
+ <author recommends="CP">
+ <name>Leo Tolstoy</name>
+ <born>1828-09-09</born>
+ <died>1910-11-20</died>
+ </author>
+ </book>
+
+
+ <book id="CP" available="false">
+ <isbn>0679420290</isbn>
+ <title>Crime and Punishment</title>
+ <genre>philosophy</genre>
+
+ <author>
+ <name>Fyodor Dostoevsky</name>
+ <born>1821-11-11</born>
+ <died>1881-02-09</died>
+ </author>
+ </book>
+
+</lib:catalog>
diff --git a/xsd-examples/cxx/tree/library/library.xsd b/xsd-examples/cxx/tree/library/library.xsd
new file mode 100644
index 0000000..4db07ef
--- /dev/null
+++ b/xsd-examples/cxx/tree/library/library.xsd
@@ -0,0 +1,72 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : cxx/tree/library/library.xsd
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:xse="http://www.codesynthesis.com/xmlns/xml-schema-extension"
+ xmlns:lib="http://www.codesynthesis.com/library"
+ targetNamespace="http://www.codesynthesis.com/library">
+
+ <xsd:simpleType name="isbn">
+ <xsd:restriction base="xsd:unsignedInt"/>
+ </xsd:simpleType>
+
+ <xsd:complexType name="title">
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string">
+ <xsd:attribute name="lang" type="xsd:language"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+
+ <xsd:simpleType name="genre">
+ <xsd:restriction base="xsd:string">
+ <xsd:enumeration value="romance"/>
+ <xsd:enumeration value="fiction"/>
+ <xsd:enumeration value="horror"/>
+ <xsd:enumeration value="history"/>
+ <xsd:enumeration value="philosophy"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+
+ <xsd:complexType name="person">
+ <xsd:sequence>
+ <xsd:element name="name" type="xsd:string"/>
+ <xsd:element name="born" type="xsd:date"/>
+ <xsd:element name="died" type="xsd:date" minOccurs="0"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="author">
+ <xsd:complexContent>
+ <xsd:extension base="lib:person">
+ <xsd:attribute name="recommends" type="xsd:IDREF" xse:refType="lib:book"/>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+
+ <xsd:complexType name="book">
+ <xsd:sequence>
+ <xsd:element name="isbn" type="lib:isbn"/>
+ <xsd:element name="title" type="lib:title"/>
+ <xsd:element name="genre" type="lib:genre"/>
+ <xsd:element name="author" type="lib:author" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="available" type="xsd:boolean" default="true"/>
+ <xsd:attribute name="id" type="xsd:ID" use="required"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="catalog">
+ <xsd:sequence>
+ <xsd:element name="book" type="lib:book" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:element name="catalog" type="lib:catalog"/>
+
+</xsd:schema>