From d80d096ee8743fd6f7382d274272b0b6d7faf9bf Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 18 Oct 2010 11:17:51 +0200 Subject: Support for schema evolution using substitution groups New examples: hybrid/evolution/ignore and hybrid/evolution/passthrough. --- examples/cxx/hybrid/evolution/ignore/driver.cxx | 131 ++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 examples/cxx/hybrid/evolution/ignore/driver.cxx (limited to 'examples/cxx/hybrid/evolution/ignore/driver.cxx') diff --git a/examples/cxx/hybrid/evolution/ignore/driver.cxx b/examples/cxx/hybrid/evolution/ignore/driver.cxx new file mode 100644 index 0000000..6c1c45f --- /dev/null +++ b/examples/cxx/hybrid/evolution/ignore/driver.cxx @@ -0,0 +1,131 @@ +// file : examples/cxx/hybrid/evolution/ignore/driver.cxx +// author : Boris Kolpackov +// copyright : not copyrighted - public domain + +#include // std::auto_ptr +#include +#include + +#include "transform.hxx" +#include "transform-pimpl.hxx" + +using namespace std; +using namespace transform; + +// Parser substitution map callback. This callback is called when the +// parser needs to check whether an element belongs to a substitution +// group. We use this callback to return a special ignored type marker +// for unknown transformations. +// +static bool +parser_callback (const xml_schema::ro_string& root_ns, + const xml_schema::ro_string& root_name, + const xml_schema::ro_string& /*member_ns*/, + const xml_schema::ro_string& member_name, + const char*& type) +{ + // Check that the root of the substitution group is 'transformation'. + // + if (root_name == "transformation" && + root_ns == "http://www.codesynthesis.com/transform") + { + // Check that the element's name has the '-transformation' suffix. This + // is the convention we use in our schema to minimize the chance of + // treating any unknown elements as a transformation. + // + // + string s ("-transformation"); + string n (member_name); + + if (n.size () > s.size () && + string (n, n.size () - s.size (), s.size ()) == s) + { + // Set type to the special ignore marker. + // + type = "*"; + return true; + } + } + + return false; +} + +int +main (int argc, char* argv[]) +{ + const char* input; + + if (argc < 2) + { + input = "STDIN"; + cerr << "XML file not specified, reading from STDIN" << endl; + } + else + input = argv[1]; + + try + { + // Set the substitution map callback. + // + xml_schema::parser_smap_callback (&parser_callback); + + // Parse. + // + transformations_paggr transformations_p; + + // The last argument to the document's constructor indicates that we + // are parsing polymorphic XML documents. + // + xml_schema::document_pimpl doc_p ( + transformations_p.root_parser (), + transformations_p.root_namespace (), + transformations_p.root_name (), + true); + + transformations_p.pre (); + + if (argc < 2) + doc_p.parse (cin); + else + doc_p.parse (argv[1]); + + auto_ptr tf (transformations_p.post ()); + + // Print what we've got. + // + for (transformations_type::transformation_iterator i = + tf->transformation ().begin (); + i != tf->transformation ().end (); + ++i) + { + transformation_type& t = *i; + + if (move_type* m = dynamic_cast (&t)) + { + cout << m->name () << ": " << m->description () << endl + << "\tx: " << m->x () << endl + << "\ty: " << m->y () << endl + << endl; + } + else if (rotate_type* r = dynamic_cast (&t)) + { + cout << r->name () << ": " << r->description () << endl + << "\tangle: " << r->angle () << endl + << endl; + } + } + } + catch (const xml_schema::parser_exception& e) + { + cerr << input << ":" << e.line () << ":" << e.column () << ": " + << e.text () << endl; + return 1; + } + catch (const std::ios_base::failure&) + { + cerr << input << ": unable to open or read/write failure" << endl; + return 1; + } + + return 0; +} -- cgit v1.1