]> matita.cs.unibo.it Git - helm.git/blobdiff - 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
diff --git a/helm/papers/use_case/stats/stats.cc b/helm/papers/use_case/stats/stats.cc
new file mode 100644 (file)
index 0000000..9892e12
--- /dev/null
@@ -0,0 +1,89 @@
+
+#include <cassert>
+#include <GdomeSmartDOM.hh>
+
+namespace DOM = GdomeSmartDOM;
+
+unsigned n_elements;
+unsigned n_leaf_elements;
+unsigned n_text_nodes;
+unsigned n_blank_text_nodes;
+unsigned n_attributes;
+unsigned max_attributes;
+unsigned max_depth;
+unsigned max_children;
+
+bool
+is_blank(const std::string& s)
+{
+  for (int i = 0; i < s.length(); i++)
+    if (!isblank(s[i])) return false;
+  return true;
+}
+
+void
+visit(DOM::Node node, unsigned depth)
+{
+  assert(node);
+
+  max_depth = std::max(max_depth, depth);
+
+  switch (node.get_nodeType())
+    {
+    case DOM::Node::ELEMENT_NODE:
+      {
+       n_elements++;
+       const unsigned n_attrs = node.get_attributes().get_length();
+       n_attributes += n_attrs;
+       max_attributes = std::max(max_attributes, n_attrs);
+       if (!node.get_firstChild()) n_leaf_elements++;
+      }
+      break;
+    case DOM::Node::TEXT_NODE:
+      n_text_nodes++;
+      if (is_blank(node.get_nodeValue())) n_blank_text_nodes++;
+      break;
+    case DOM::Node::ATTRIBUTE_NODE:
+      break;
+    }
+
+  unsigned n_children = 0;
+  for (DOM::Node p = node.get_firstChild(); p; p = p.get_nextSibling())
+    {
+      visit(p, depth + 1);
+      n_children++;
+    }
+  max_children = std::max(max_children, n_children);
+}
+
+void
+print_results(const std::string& URI)
+{
+  std::cout << "<stats for=\"" << URI << "\">" << std::endl;
+  std::cout << "  <depth>" << max_depth << "</depth>" << std::endl;
+  std::cout << "  <elements>" << std::endl;
+  std::cout << "    <total>" << n_elements << "</total>" << std::endl;
+  std::cout << "    <leaf>" << n_leaf_elements << "</leaf>" << std::endl;
+  std::cout << "  </elements>" << std::endl;
+  std::cout << "  <text-nodes>" << std::endl;
+  std::cout << "    <total>" << n_text_nodes << "</total>" << std::endl;
+  std::cout << "    <blank>" << n_blank_text_nodes << "</blank>" << std::endl;
+  std::cout << "  </text-nodes>" << std::endl;
+  std::cout << "  <attributes>" << std::endl;
+  std::cout << "    <total>" << n_attributes << "</total>" << std::endl;
+  std::cout << "    <max>" << max_attributes << "</max>" << std::endl;
+  std::cout << "  </attributes>" << std::endl;
+  std::cout << "  <children>" << std::endl;
+  std::cout << "    <max>" << max_children << "</max>" << std::endl;
+  std::cout << "  </children>" << std::endl;  
+  std::cout << "</stats>" << std::endl;
+}
+
+int
+main(int argc, char* argv[])
+{
+  DOM::DOMImplementation di;
+  DOM::Document doc = di.createDocumentFromURI(argv[1]);
+  visit(doc, 0);
+  print_results(argv[1]);
+}