From 6e4d86618645c45d07c3a8113f4641cb9161309e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 6 Apr 2009 15:44:26 +0200 Subject: Add support for XML pretty-printing examples/cxx/hybrid/: examples/cxx/serializer/: turn on pretty-printing --- documentation/cxx/hybrid/guide/index.xhtml | 36 ++++++++++-------- documentation/cxx/serializer/guide/index.xhtml | 52 ++++++++++++++++++-------- 2 files changed, 57 insertions(+), 31 deletions(-) (limited to 'documentation') diff --git a/documentation/cxx/hybrid/guide/index.xhtml b/documentation/cxx/hybrid/guide/index.xhtml index d497a14..d2cc1ef 100644 --- a/documentation/cxx/hybrid/guide/index.xhtml +++ b/documentation/cxx/hybrid/guide/index.xhtml @@ -870,7 +870,7 @@ main (int argc, char* argv[]) xml_schema::document_simpl doc_s (hello_s.root_serializer (), hello_s.root_name ()); hello_s.pre (*h); - doc_s.serialize (cout); + doc_s.serialize (cout, xml_schema::document_simpl::pretty_print); hello_s.post (); delete h; @@ -893,15 +893,21 @@ main (int argc, char* argv[]) object model as in the previous example. Then it changes the greeting string and adds another entry to the list of names. Finally, it creates a document serializer and serializes the - object model back to XML. The resulting XML is written to - the standard output (cout) for us to inspect. - We could have also written the result to a file or memory - buffer by creating an instance of std::ofstream + object model back to XML. The hello_saggr class + is the serializer aggregate class we discussed earlier.

+ +

The resulting XML is written to the standard output (cout) + for us to inspect. We could have also written the result to a + file or memory buffer by creating an instance of std::ofstream or std::ostringstream and passing it to serialize() instead of cout. - The hello_saggr class is the serializer aggregate - class we discussed earlier. Serialization is covered in more - detail in Chapter 6, "Parsing and Serialization".

+ The second argument in the call to + serialize() is a flag that requests pretty-printing + of the resulting XML document. You would normally specify this flag + during testing to obtain easily-readable XML and remove it + in production to get faster serialization and smaller documents. + Serialization is covered in more detail in Chapter 6, + "Parsing and Serialization".

If we now compile and run this application (don't forget to compile and link hello-sskel.cxx and @@ -971,7 +977,7 @@ main (int argc, char* argv[]) ostringstream ostr; hello_s.pre (*h); - doc_s.serialize (ostr); + doc_s.serialize (ostr, xml_schema::document_simpl::pretty_print); hello_s.post (); delete h; @@ -1195,7 +1201,7 @@ main (int argc, char* argv[]) if (se = hello_s._error ()) break; - doc_s.serialize (w); + doc_s.serialize (w, xml_schema::document_simpl::pretty_print); if (se = doc_s._error ()) break; @@ -3248,7 +3254,7 @@ main () xml_schema::document_simpl doc_s (people_s.root_serializer (), people_s.root_name ()); people_s.pre (*ppl); - doc_s.serialize (cout); + doc_s.serialize (cout, xml_schema::document_simpl::pretty_print); people_s.post (); } @@ -3360,7 +3366,7 @@ main () xml_schema::document_simpl doc_s (people_s.root_serializer (), people_s.root_name ()); people_s.pre (ppl); - doc_s.serialize (cout); + doc_s.serialize (cout, xml_schema::document_simpl::pretty_print); people_s.post (); } @@ -3988,7 +3994,7 @@ xml_schema::document_simpl doc_s ( doc_s.add_no_namespace_schema ("supermen.xsd"); supermen_s.pre (*sm); -doc_s.serialize (cout); +doc_s.serialize (cout, xml_schema::document_simpl::pretty_print); supermen_s.post (); @@ -5566,7 +5572,7 @@ hello_saggr hello_s; xml_schema::document_simpl doc_s (hello_s.root_serializer (), hello_s.root_name ()); hello_s.pre (*h); -doc_s.serialize (std::cout); +doc_s.serialize (std::cout, xml_schema::document_simpl::pretty_print); hello_s.post (); @@ -5818,7 +5824,7 @@ main (int argc, char* argv[]) xml_schema::document_simpl doc_s (root_s, people_s.root_name ()); people_s.pre (*ppl); - doc_s.serialize (cout); + doc_s.serialize (cout, xml_schema::document_simpl::pretty_print); people_s.post (); } diff --git a/documentation/cxx/serializer/guide/index.xhtml b/documentation/cxx/serializer/guide/index.xhtml index bd681cc..dd3a8f3 100644 --- a/documentation/cxx/serializer/guide/index.xhtml +++ b/documentation/cxx/serializer/guide/index.xhtml @@ -757,7 +757,7 @@ main () xml_schema::document_simpl doc_s (hello_s, "hello"); hello_s.pre (); - doc_s.serialize (cout); + doc_s.serialize (cout, xml_schema::document_simpl::pretty_print); hello_s.post (); } catch (const xml_schema::serializer_exception& e) @@ -789,10 +789,14 @@ main ()

The final piece is the calls to pre(), serialize(), and post(). The call to serialize() performs the actual XML serialization - with the result written to std::cout. The calls - to pre() and post() make sure that - the serializer for the root element can perform proper - initialization and cleanup.

+ with the result written to std::cout. The second + argument in this call is a flag that requests pretty-printing + of the resulting XML document. You would normally specify this flag + during testing to obtain easily-readable XML and remove it + in production to get faster serialization and smaller documents. + The calls to pre() and + post() make sure that the serializer for the + root element can perform proper initialization and cleanup.

While our serializer implementation and test driver are pretty small and easy to write by hand, for bigger XML vocabularies it can be a @@ -1338,7 +1342,7 @@ xml_schema::document_simpl doc_s (people_s, "people"); std::ostringstream os; people_s.pre (); -doc_s.serialize (os); +doc_s.serialize (os, xml_schema::document_simpl::pretty_print); people_s.post (); cout << os.str (); @@ -1371,8 +1375,11 @@ namespace xml_schema const std::string& root_element_namespace, const std::string& root_element_name); + typedef unsigned short flags; + static const flags pretty_print; + void - serialize (std::ostream&); + serialize (std::ostream&, flags = 0); }; } @@ -2003,7 +2010,7 @@ main () xml_schema::document_simpl doc_s (people_s, "people"); people_s.pre (ppl); - doc_s.serialize (cout); + doc_s.serialize (cout, xml_schema::document_simpl::pretty_print); people_s.post (); } @@ -2652,7 +2659,7 @@ public:

 void
-serialize (std::ostream&);
+serialize (std::ostream&, flags);
   

See Section 8.1, "Document Serializer" @@ -3345,7 +3352,7 @@ main () xml_schema::document_simpl doc_s (supermen_s, "supermen", true); supermen_s.pre (sm); - doc_s.serialize (std::cout); + doc_s.serialize (std::cout, xml_schema::document_simpl::pretty_print); supermen_s.post (); } @@ -3856,7 +3863,7 @@ main () if (e = people_s._error ()) break; - doc_s.serialize (w); + doc_s.serialize (w, xml_schema::document_simpl::pretty_print); if (e = doc_s._error ()) break; @@ -5696,20 +5703,27 @@ namespace xml_schema add_no_namespace_schema (const std::string& location); public: + // Serialization flags. + // + typedef unsigned short flags; + + static const flags pretty_print; + + public: // Serialize to std::ostream. The std::ios_base::failure // exception is used to report io errors (badbit and failbit) // if C++ exceptions are enabled. Otherwise error codes are // used. // void - serialize (std::ostream&); + serialize (std::ostream&, flags = 0); public: // Serialize by calling writer::write() and writer::flush() to // output XML. // void - serialize (writer&); + serialize (writer&, flags = 0); // Serialize by calling the write and flush functions. If the // unbounded write function is not provided, the bounded version @@ -5722,11 +5736,17 @@ namespace xml_schema typedef void (*flush_func) (void*); void - serialize (write_bound_func, flush_func, void* user_data); + serialize (write_bound_func, + flush_func, + void* user_data, + flags = 0); void - serialize (write_func, write_bound_func, flush_func, void* user_data); - + serialize (write_func, + write_bound_func, + flush_func, + void* user_data, + flags = 0); public: // Low-level, genx-specific serialization. With this method // it is your responsibility to call genxStartDoc*() and -- cgit v1.1