From 5e527213a2430bb3018e5eebd909aef294edf9b5 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 18 Dec 2020 18:48:46 +0300 Subject: Switch to build2 --- xsd-examples/cxx/tree/wildcard/.gitignore | 1 + xsd-examples/cxx/tree/wildcard/README | 34 +++++++ xsd-examples/cxx/tree/wildcard/buildfile | 30 ++++++ xsd-examples/cxx/tree/wildcard/driver.cxx | 159 ++++++++++++++++++++++++++++++ xsd-examples/cxx/tree/wildcard/email.xml | 31 ++++++ xsd-examples/cxx/tree/wildcard/email.xsd | 50 ++++++++++ 6 files changed, 305 insertions(+) create mode 100644 xsd-examples/cxx/tree/wildcard/.gitignore create mode 100644 xsd-examples/cxx/tree/wildcard/README create mode 100644 xsd-examples/cxx/tree/wildcard/buildfile create mode 100644 xsd-examples/cxx/tree/wildcard/driver.cxx create mode 100644 xsd-examples/cxx/tree/wildcard/email.xml create mode 100644 xsd-examples/cxx/tree/wildcard/email.xsd (limited to 'xsd-examples/cxx/tree/wildcard') diff --git a/xsd-examples/cxx/tree/wildcard/.gitignore b/xsd-examples/cxx/tree/wildcard/.gitignore new file mode 100644 index 0000000..234645e --- /dev/null +++ b/xsd-examples/cxx/tree/wildcard/.gitignore @@ -0,0 +1 @@ +email.?xx diff --git a/xsd-examples/cxx/tree/wildcard/README b/xsd-examples/cxx/tree/wildcard/README new file mode 100644 index 0000000..d451509 --- /dev/null +++ b/xsd-examples/cxx/tree/wildcard/README @@ -0,0 +1,34 @@ +This example shows how to use the optional wildcard mapping provided +by C++/Tree to parse, access, modify, and serialize the XML data +matched by XML Schema wildcards (any and anyAttribute). For an +alternative approach that employes type customization see the +custom/wildcard example. + +The example consists of the following files: + +email.xsd + XML Schema which describes a simple email format with the + extensible envelope type. + +email.xml + Sample email message. + +email.hxx +email.ixx +email.cxx + C++ types that represent the given vocabulary, a set of parsing + functions that convert XML instance 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 email.xsd. + Note that the --generate-wildcard option is used to request the + wildcard mapping. + +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. Next the driver creates a + reply email which is then serialized to XML. + +To run the example on the sample XML instance document simply execute: + +$ ./driver email.xml diff --git a/xsd-examples/cxx/tree/wildcard/buildfile b/xsd-examples/cxx/tree/wildcard/buildfile new file mode 100644 index 0000000..5bc63c5 --- /dev/null +++ b/xsd-examples/cxx/tree/wildcard/buildfile @@ -0,0 +1,30 @@ +# file : cxx/tree/wildcard/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}{* -email} {hxx ixx cxx}{email} $libs + +exe{driver}: xml{email}: test.input = true + +<{hxx ixx cxx}{email}>: xsd{email} $xsd +{{ + diag xsd ($<[0]) # @@ TMP + + $xsd cxx-tree --std c++11 \ + --generate-inline \ + --generate-wildcard \ + --generate-serialization \ + --root-element message \ + --output-dir $out_base \ + $path($<[0]) +}} + +cxx.poptions =+ "-I$out_base" + +# Define XSD_CXX11 since we include libxsd headers directly. +# +cxx.poptions += -DXSD_CXX11 diff --git a/xsd-examples/cxx/tree/wildcard/driver.cxx b/xsd-examples/cxx/tree/wildcard/driver.cxx new file mode 100644 index 0000000..146a2d9 --- /dev/null +++ b/xsd-examples/cxx/tree/wildcard/driver.cxx @@ -0,0 +1,159 @@ +// file : cxx/tree/wildcard/driver.cxx +// copyright : not copyrighted - public domain + +#include +#include // std::unique_ptr +#include // std::memcpy +#include + +#include +#include + +#include "email.hxx" + +// The following string class keeps us sane when working with Xerces. +// Include it after the generated header in order to get only char or +// wchar_t version depending on how you compiled your schemas. +// +#include + +using std::cerr; +using std::endl; +using std::string; + +int +main (int argc, char* argv[]) +{ + if (argc != 2) + { + cerr << "usage: " << argv[0] << " email.xml" << endl; + return 1; + } + + using namespace xercesc; + + int r (0); + + // The Xerces-C++ DOM objects that will be used to store the + // content matched by wildcards "out-live" the call to the + // parsing function. Therefore we need to initialize the + // Xerces-C++ runtime ourselves. + // + XMLPlatformUtils::Initialize (); + + try + { + using namespace email; + namespace xml = xsd::cxx::xml; + + // Read in the message. + // + std::unique_ptr msg ( + message (argv[1], xml_schema::flags::dont_initialize)); + + // Print what we've got. + // + cerr << "To: " << msg->to () << endl + << "From: " << msg->from () << endl + << "Subject: " << msg->subject () << endl; + + envelope::any_sequence& body (msg->any ()); + + for (envelope::any_iterator i (body.begin ()); i != body.end (); ++i) + { + DOMElement& e (*i); + string name (xml::transcode (e.getLocalName ())); + + if (name == "text") + { + // Create object representation for the text element. + // + xml_schema::string text (e); + + cerr << text << endl + << endl; + } + else if (name == "binary") + { + // Create object representation for the binary element. + // + binary bin (e); + + cerr << "binary: " << bin.name () << " type: " << bin.mime () << endl + << endl; + } + else + { + cerr << "unknown body type: " << name << endl; + } + } + + // Create a reply message. + // + envelope reply (msg->from (), msg->to (), "Re: " + msg->subject ()); + + // Copy the thread-id attribute from the original message if any. + // + envelope::any_attribute_set& as (msg->any_attribute ()); + envelope::any_attribute_iterator ti ( + as.find ("http://www.codesynthesis.com/email", "thread-id")); + + if (ti != as.end ()) + reply.any_attribute ().insert (*ti); + + // Add a text body. + // + DOMDocument& doc (reply.dom_document ()); + envelope::any_sequence& rbody (reply.any ()); + + xml_schema::string text ("Hi!\n\n" + "Indeed nice pictures. Check out mine.\n\n" + "Jane"); + + DOMElement* e ( + doc.createElementNS ( + xml::string ("http://www.codesynthesis.com/email").c_str (), + xml::string ("eml:text").c_str ())); + + *e << text; + rbody.push_back (e); + + // Add a (fake) image. + // + binary pic ("pic.jpg", "image/jpeg"); + pic.size (3); + std::memcpy (pic.data (), "123", 3); + + e = doc.createElementNS ( + xml::string ("http://www.codesynthesis.com/email").c_str (), + xml::string ("eml:binary").c_str ()); + + *e << pic; + rbody.push_back (e); + + + // Prepare namespace mapping and schema location information for + // serialization. + // + xml_schema::namespace_infomap map; + + map["eml"].name = "http://www.codesynthesis.com/email"; + map["eml"].schema = "email.xsd"; + + // Write it out. + // + message (std::cout, + reply, + map, + "UTF-8", + xml_schema::flags::dont_initialize); + } + catch (const xml_schema::exception& e) + { + cerr << e << endl; + r = 1; + } + + XMLPlatformUtils::Terminate (); + return r; +} diff --git a/xsd-examples/cxx/tree/wildcard/email.xml b/xsd-examples/cxx/tree/wildcard/email.xml new file mode 100644 index 0000000..a9331e5 --- /dev/null +++ b/xsd-examples/cxx/tree/wildcard/email.xml @@ -0,0 +1,31 @@ + + + + + + + Jane Doe <jane@doe.com> + John Doe <john@doe.com> + Surfing pictures + + +Hi Jane, + +Here are cool pictures of me surfing. + +Cheers, +John + + + YmFzZTY0IGJpbmFyeQ== + YmFzZTY0IGJpbmFyeQ== + + diff --git a/xsd-examples/cxx/tree/wildcard/email.xsd b/xsd-examples/cxx/tree/wildcard/email.xsd new file mode 100644 index 0000000..c051e85 --- /dev/null +++ b/xsd-examples/cxx/tree/wildcard/email.xsd @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.1