Brute-force XML ID lookup.
authorScott Cantor <cantor.2@osu.edu>
Mon, 27 Nov 2006 00:19:13 +0000 (00:19 +0000)
committerScott Cantor <cantor.2@osu.edu>
Mon, 27 Nov 2006 00:19:13 +0000 (00:19 +0000)
xmltooling/util/XMLHelper.cpp
xmltooling/util/XMLHelper.h

index 7932644..50f450e 100644 (file)
@@ -30,6 +30,7 @@
 
 using namespace xmltooling;
 using std::ostream;
+using std::list;
 
 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
     
@@ -86,6 +87,24 @@ DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
     return NULL;
 }
 
+const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh* id)
+{
+    if (XMLString::equals(id, tree.getXMLID()))
+        return &tree;
+    
+    const XMLObject* ret;
+    const list<XMLObject*>& children = tree.getOrderedChildren();
+    for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
+        if (*i) {
+            ret = getXMLObjectById(*(*i), id);
+            if (ret)
+                return ret;
+        }
+    }
+    
+    return NULL;
+}
+
 QName* XMLHelper::getNodeQName(const DOMNode* domNode)
 {
     if (domNode)
index 92d2f2f..eaa7832 100644 (file)
@@ -90,6 +90,17 @@ namespace xmltooling {
         static DOMAttr* getIdAttribute(const DOMElement* domElement);
 
         /**
+         * Attempts to locate an XMLObject from this point downward in the tree whose
+         * XML ID matches the supplied value.
+         * 
+         * @param tree  root of tree to search
+         * @param id    ID value to locate
+         * @return XMLObject in the tree with a matching ID value, or NULL
+         */
+        static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
+        
+
+        /**
          * Gets the QName for the given DOM node.
          * 
          * @param domNode the DOM node