]> matita.cs.unibo.it Git - helm.git/blob - helm/papers/use_case/stats/stats.cc
* added .cc program (and equivalent .xsl stylesheet) for collecting
[helm.git] / helm / papers / use_case / stats / stats.cc
1
2 #include <cassert>
3 #include <GdomeSmartDOM.hh>
4
5 namespace DOM = GdomeSmartDOM;
6
7 unsigned n_elements;
8 unsigned n_leaf_elements;
9 unsigned n_text_nodes;
10 unsigned n_blank_text_nodes;
11 unsigned n_attributes;
12 unsigned max_attributes;
13 unsigned max_depth;
14 unsigned max_children;
15
16 bool
17 is_blank(const std::string& s)
18 {
19   for (int i = 0; i < s.length(); i++)
20     if (!isblank(s[i])) return false;
21   return true;
22 }
23
24 void
25 visit(DOM::Node node, unsigned depth)
26 {
27   assert(node);
28
29   max_depth = std::max(max_depth, depth);
30
31   switch (node.get_nodeType())
32     {
33     case DOM::Node::ELEMENT_NODE:
34       {
35         n_elements++;
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++;
40       }
41       break;
42     case DOM::Node::TEXT_NODE:
43       n_text_nodes++;
44       if (is_blank(node.get_nodeValue())) n_blank_text_nodes++;
45       break;
46     case DOM::Node::ATTRIBUTE_NODE:
47       break;
48     }
49
50   unsigned n_children = 0;
51   for (DOM::Node p = node.get_firstChild(); p; p = p.get_nextSibling())
52     {
53       visit(p, depth + 1);
54       n_children++;
55     }
56   max_children = std::max(max_children, n_children);
57 }
58
59 void
60 print_results(const std::string& URI)
61 {
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;
80 }
81
82 int
83 main(int argc, char* argv[])
84 {
85   DOM::DOMImplementation di;
86   DOM::Document doc = di.createDocumentFromURI(argv[1]);
87   visit(doc, 0);
88   print_results(argv[1]);
89 }