Add method to compute non-visibly used prefixes in a tree.
authorScott Cantor <cantor.2@osu.edu>
Tue, 19 Jan 2010 20:00:13 +0000 (20:00 +0000)
committerScott Cantor <cantor.2@osu.edu>
Tue, 19 Jan 2010 20:00:13 +0000 (20:00 +0000)
xmltooling/util/XMLHelper.cpp
xmltooling/util/XMLHelper.h

index 73e6681..ee502a4 100644 (file)
@@ -38,13 +38,7 @@ static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
     
 bool XMLHelper::hasXSIType(const DOMElement* e)
 {
-    if (e) {
-        if (e->hasAttributeNS(xmlconstants::XSI_NS, type)) {
-            return true;
-        }
-    }
-
-    return false;
+    return (e && e->hasAttributeNS(xmlconstants::XSI_NS, type));
 }
 
 xmltooling::QName* XMLHelper::getXSIType(const DOMElement* e)
@@ -125,6 +119,33 @@ XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id)
     return NULL;
 }
 
+void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, set<xstring>& prefixes)
+{
+    set<xstring> child_prefixes;
+    const list<XMLObject*>& children = tree.getOrderedChildren();
+    for (list<XMLObject*>::const_iterator i = children.begin(); i != children.end(); ++i) {
+        if (*i)
+            getNonVisiblyUsedPrefixes(*(*i), child_prefixes);
+    }
+    const set<Namespace>& nsset = tree.getNamespaces();
+    for (set<Namespace>::const_iterator ns = nsset.begin(); ns != nsset.end(); ++ns) {
+        // Check for xmlns:xml.
+        if (XMLString::equals(ns->getNamespacePrefix(), xmlconstants::XML_PREFIX) && XMLString::equals(ns->getNamespaceURI(), xmlconstants::XML_NS))
+            continue;
+        switch (ns->usage()) {
+            case Namespace::Indeterminate:
+                break;
+            case Namespace::VisiblyUsed:
+                child_prefixes.erase(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
+                break;
+            case Namespace::NonVisiblyUsed:
+                prefixes.insert(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
+                break;
+        }
+    }
+    prefixes.insert(child_prefixes.begin(), child_prefixes.end());
+}
+
 xmltooling::QName* XMLHelper::getNodeQName(const DOMNode* domNode)
 {
     if (domNode)
index 98dff81..9fe1e62 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2009 Internet2
+ *  Copyright 2001-2010 Internet2
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
 
 #include <xmltooling/base.h>
 
+#include <set>
 #include <iostream>
 #include <xercesc/dom/DOM.hpp>
 
@@ -134,6 +135,14 @@ namespace xmltooling {
         static XMLObject* getXMLObjectById(XMLObject& tree, const XMLCh* id);
 
         /**
+         * Returns a list of non-visibly-used namespace prefixes found in a tree.
+         *
+         * @param tree      root of tree to search
+         * @param prefixes  container to store prefix list
+         */
+        static void getNonVisiblyUsedPrefixes(const XMLObject& tree, std::set<xstring>& prefixes);
+
+        /**
          * Gets the QName for the given DOM node.
          *
          * @param domNode the DOM node