4 #include <GdomeSmartDOM.hh>
6 namespace DOM = GdomeSmartDOM;
9 unsigned n_leaf_elements;
10 unsigned n_text_nodes;
11 unsigned n_blank_text_nodes;
12 unsigned n_attributes;
13 unsigned max_attributes;
15 unsigned max_children;
16 std::vector<unsigned> depths;
17 std::vector<unsigned> widths;
20 is_blank(const std::string& s)
22 for (int i = 0; i < s.length(); i++)
23 if (!isblank(s[i])) return false;
28 visit(DOM::Node node, unsigned depth)
32 max_depth = std::max(max_depth, depth);
34 switch (node.get_nodeType())
36 case DOM::Node::ELEMENT_NODE:
39 const unsigned n_attrs = node.get_attributes().get_length();
40 n_attributes += n_attrs;
41 max_attributes = std::max(max_attributes, n_attrs);
42 if (!node.get_firstChild()) n_leaf_elements++;
45 case DOM::Node::TEXT_NODE:
47 if (is_blank(node.get_nodeValue())) n_blank_text_nodes++;
49 case DOM::Node::ATTRIBUTE_NODE:
53 unsigned n_children = 0;
54 for (DOM::Node p = node.get_firstChild(); p; p = p.get_nextSibling())
59 max_children = std::max(max_children, n_children);
61 if (!node.get_firstChild())
62 depths.push_back(depth);
64 widths.push_back(n_children);
68 print_results(const std::string& URI)
70 unsigned tot_depth = 0;
71 for (std::vector<unsigned>::const_iterator p = depths.begin(); p != depths.end(); p++)
74 unsigned tot_width = 0;
75 for (std::vector<unsigned>::const_iterator p = widths.begin(); p != widths.end(); p++)
78 std::cout << "<stats for=\"" << URI << "\">" << std::endl;
79 std::cout << " <depth>" << std::endl;
80 std::cout << " <max>" << max_depth << "</max>" << std::endl;
81 std::cout << " <leaf-avg>" << tot_depth / ((double) depths.size()) << "</leaf-avg>" << std::endl;
82 std::cout << " </depth>" << std::endl;
83 std::cout << " <width>" << std::endl;
84 std::cout << " <max>" << max_children << "</max>" << std::endl;
85 std::cout << " <inner-avg>" << tot_width / ((double) widths.size()) << "</inner-avg>" << std::endl;
86 std::cout << " </width>" << std::endl;
87 std::cout << " <elements>" << std::endl;
88 std::cout << " <total>" << n_elements << "</total>" << std::endl;
89 std::cout << " <leaf>" << n_leaf_elements << "</leaf>" << std::endl;
90 std::cout << " </elements>" << std::endl;
91 std::cout << " <text-nodes>" << std::endl;
92 std::cout << " <total>" << n_text_nodes << "</total>" << std::endl;
93 std::cout << " <blank>" << n_blank_text_nodes << "</blank>" << std::endl;
94 std::cout << " </text-nodes>" << std::endl;
95 std::cout << " <attributes>" << std::endl;
96 std::cout << " <total>" << n_attributes << "</total>" << std::endl;
97 std::cout << " <max>" << max_attributes << "</max>" << std::endl;
98 std::cout << " </attributes>" << std::endl;
99 std::cout << "</stats>" << std::endl;
103 main(int argc, char* argv[])
106 std::cerr << "Usage: stats <URI>" << std::endl;
110 DOM::DOMImplementation di;
111 DOM::Document doc = di.createDocumentFromURI(argv[1]);
113 print_results(argv[1]);