]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/mathml_editor/src/TDocument.cc
* this is a large commit
[helm.git] / helm / DEVEL / mathml_editor / src / TDocument.cc
index a7b5c8d3a5d2cb57fcd5adc11f79eb6e61ad91ab..e578b79f46ae9d8411f3d00931c8c5dcbacac2e1 100644 (file)
@@ -34,16 +34,19 @@ TDocument::serialize(const char* filename) const
   di.saveDocumentToFile(doc, filename, GDOME_SAVE_LIBXML_INDENT);
 }
 
+std::string
+TDocument::makeId(unsigned id)
+{
+  ostringstream os;
+  os << "I" << id;
+  return os.str();
+}
+
 TNode
 TDocument::create(const std::string& name, unsigned id) const
 {
   DOM::Element elem = doc.createElementNS(TML_NS_URI, "tml:" + name);
-  if (id > 0)
-    {
-      ostringstream os;
-      os << "I" << id;
-      elem.setAttribute("id", os.str());
-    }
+  if (id > 0) elem.setAttribute("id", makeId(id));
   return elem;
 }
 
@@ -130,6 +133,24 @@ TDocument::findIdNode(const DOM::Node& node)
   return DOM::Node(0);
 }
 
+TNode
+TDocument::getNodeByIdAux(const TNode& node, const std::string& id)
+{
+  if (node.hasId(id)) return node;
+  else
+    for (TNode p = node.first(); p; p = p.next())
+      if (TNode res = getNodeByIdAux(p, id)) return res;
+  return TNode();
+}
+
+TNode
+TDocument::getNodeById(unsigned id) const
+{
+  DOM::Element root = doc.get_documentElement();
+  assert(root);
+  return getNodeByIdAux(root, makeId(id));
+}
+
 void
 TDocument::handleEvent(const DOM::Event& ev)
 {