3 #include <GdomeSmartDOM.hh>
5 namespace DOM = GdomeSmartDOM;
8 unsigned n_leaf_elements;
10 unsigned n_blank_text_nodes;
11 unsigned n_attributes;
12 unsigned max_attributes;
14 unsigned max_children;
17 is_blank(const std::string& s)
19 for (int i = 0; i < s.length(); i++)
20 if (!isblank(s[i])) return false;
25 visit(DOM::Node node, unsigned depth)
29 max_depth = std::max(max_depth, depth);
31 switch (node.get_nodeType())
33 case DOM::Node::ELEMENT_NODE:
36 const unsigned n_attrs = node.get_attributes().get_length();
37 n_attributes += n_attrs;
38 max_attributes = std::max(max_attributes, n_attrs);
39 if (!node.get_firstChild()) n_leaf_elements++;
42 case DOM::Node::TEXT_NODE:
44 if (is_blank(node.get_nodeValue())) n_blank_text_nodes++;
46 case DOM::Node::ATTRIBUTE_NODE:
50 unsigned n_children = 0;
51 for (DOM::Node p = node.get_firstChild(); p; p = p.get_nextSibling())
56 max_children = std::max(max_children, n_children);
60 print_results(const std::string& URI)
62 std::cout << "<stats for=\"" << URI << "\">" << std::endl;
63 std::cout << " <depth>" << max_depth << "</depth>" << std::endl;
64 std::cout << " <elements>" << std::endl;
65 std::cout << " <total>" << n_elements << "</total>" << std::endl;
66 std::cout << " <leaf>" << n_leaf_elements << "</leaf>" << std::endl;
67 std::cout << " </elements>" << std::endl;
68 std::cout << " <text-nodes>" << std::endl;
69 std::cout << " <total>" << n_text_nodes << "</total>" << std::endl;
70 std::cout << " <blank>" << n_blank_text_nodes << "</blank>" << std::endl;
71 std::cout << " </text-nodes>" << std::endl;
72 std::cout << " <attributes>" << std::endl;
73 std::cout << " <total>" << n_attributes << "</total>" << std::endl;
74 std::cout << " <max>" << max_attributes << "</max>" << std::endl;
75 std::cout << " </attributes>" << std::endl;
76 std::cout << " <children>" << std::endl;
77 std::cout << " <max>" << max_children << "</max>" << std::endl;
78 std::cout << " </children>" << std::endl;
79 std::cout << "</stats>" << std::endl;
83 main(int argc, char* argv[])
85 DOM::DOMImplementation di;
86 DOM::Document doc = di.createDocumentFromURI(argv[1]);
88 print_results(argv[1]);