From 46065d31931ba76577e0036fbbb8dda4ef88b5d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Thu, 9 Apr 2009 13:32:15 +0000 Subject: [PATCH] pretty print for templates --- src/xml-cxx/xml.hxx | 4 ++++ src/xml.cxx | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/xml-cxx/xml.hxx b/src/xml-cxx/xml.hxx index 30c9f4b..32d5a71 100644 --- a/src/xml-cxx/xml.hxx +++ b/src/xml-cxx/xml.hxx @@ -458,6 +458,10 @@ namespace xml { public: Factory(const Node& t) throw(); const Node& operator*() const throw(); + friend std::ostream& operator<<(std::ostream& os, + const Factory& factory) throw(); + static std::ostream& print(std::ostream& os, const Node& node, + unsigned int level=0) throw(); std::auto_ptr read(std::istream& is) throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch, second_slash_in_tag, character_after_slash, diff --git a/src/xml.cxx b/src/xml.cxx index f38a72f..96c65d4 100644 --- a/src/xml.cxx +++ b/src/xml.cxx @@ -525,6 +525,63 @@ namespace xml { const Node& Factory::operator*() const throw() { return _template[0]; } + std::ostream& operator<<(std::ostream& os, const Factory& factory) throw() { + return factory.print(os, *factory); + } + std::ostream& Factory::print(std::ostream& os, const Node& node, + unsigned int level) throw() { + if (node.children()) { + os<first<<"=\""<second<<'"'; + os<<'>'; + if (dynamic_cast(&node)) + os<<" (Unsigned Integer"; + else if (dynamic_cast(&node)) + os<<" (String"; + else + os<<" (Node"; + if (node.min()==1 && node.max()==1) + os<<", required exactly once)"; + else if (node.min()==1 && node.max()==0) + os<<", required at least once)"; + else if (node.min()==0 && node.max()==0) + os<<", 0..n)"; + else if (node.min()==0 && node.max()==1) + os<<", optional)"; + else + os<<", "<'; + } else { + os<first<<"=\""<second<<'"'; + os<<"/>"; + if (dynamic_cast(&node)) + os<<" (Unsigned Integer"; + else if (dynamic_cast(&node)) + os<<" (String"; + else + os<<" (Node"; + if (node.min()==1 && node.max()==1) + os<<", required exactly once)"; + else if (node.min()==1 && node.max()==0) + os<<", required at least once)"; + else if (node.min()==0 && node.max()==0) + os<<", 0..n)"; + else if (node.min()==0 && node.max()==1) + os<<", optional)"; + else + os<<", "< Factory::read(std::istream& is) throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch, second_slash_in_tag,