pretty print for templates

master
Marc Wäckerlin 15 years ago
parent bbd3b9781d
commit 46065d3193
  1. 4
      src/xml-cxx/xml.hxx
  2. 57
      src/xml.cxx

@ -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<Node> read(std::istream& is)
throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch,
second_slash_in_tag, character_after_slash,

@ -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<<std::string(level, '\t')<<'<'<<node.name();
for (Attributes::const_iterator it(node.attributes().begin());
it!=node.attributes().end(); ++it)
os<<' '<<it->first<<"=\""<<it->second<<'"';
os<<'>';
if (dynamic_cast<const UnsignedInteger*>(&node))
os<<" (Unsigned Integer";
else if (dynamic_cast<const String*>(&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<<", "<<node.min()<<".."<<node.max()<<")";
os<<std::endl;
for (Node::size_type i(0); i<node.children(); ++i)
print(os, node[i], level+1);
os<<std::string(level, '\t')<<"</"<<node.name()<<'>';
} else {
os<<std::string(level, '\t')<<'<'<<node.name();
for (Attributes::const_iterator it(node.attributes().begin());
it!=node.attributes().end(); ++it)
os<<' '<<it->first<<"=\""<<it->second<<'"';
os<<"/>";
if (dynamic_cast<const UnsignedInteger*>(&node))
os<<" (Unsigned Integer";
else if (dynamic_cast<const String*>(&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<<", "<<node.min()<<".."<<node.max()<<")";
os<<std::endl;
}
return os;
}
std::auto_ptr<Node> Factory::read(std::istream& is)
throw(wrong_end_tag, wrong_start_tag, tag_expected, type_mismatch,
second_slash_in_tag,

Loading…
Cancel
Save