Add same-doc KeyInfoReference support, with option to disable.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.cpp
index 9ebde32..bbeb383 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.
@@ -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)
@@ -63,18 +57,18 @@ xmltooling::QName* XMLHelper::getXSIType(const DOMElement* e)
                 return ret;
             }
             else {
-                return new xmltooling::QName(e->lookupNamespaceURI(NULL), attributeValue);
+                return new xmltooling::QName(e->lookupNamespaceURI(nullptr), attributeValue);
             }
         }
     }
 
-    return NULL;
+    return nullptr;
 }
 
 DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
 {
     if(!domElement->hasAttributes()) {
-        return NULL;
+        return nullptr;
     }
     
     DOMNamedNodeMap* attributes = domElement->getAttributes();
@@ -86,7 +80,7 @@ DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
         }
     }
     
-    return NULL;
+    return nullptr;
 }
 
 const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh* id)
@@ -104,7 +98,7 @@ const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh*
         }
     }
     
-    return NULL;
+    return nullptr;
 }
 
 XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id)
@@ -122,14 +116,41 @@ XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id)
         }
     }
     
-    return NULL;
+    return nullptr;
+}
+
+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)
         return new xmltooling::QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
-    return NULL
+    return nullptr
 }
 
 xmltooling::QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
@@ -140,7 +161,7 @@ xmltooling::QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
 xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode)
 {
     if (!domNode)
-        return NULL;
+        return nullptr;
     
     int i;
     const XMLCh* value=domNode->getTextContent();
@@ -153,7 +174,21 @@ xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode)
         return ret;
     }
     
-    return new xmltooling::QName(domNode->lookupNamespaceURI(NULL), value);
+    return new xmltooling::QName(domNode->lookupNamespaceURI(nullptr), value);
+}
+
+bool XMLHelper::getNodeValueAsBool(const xercesc::DOMNode* domNode, bool def)
+{
+    if (!domNode)
+        return def;
+    const XMLCh* value = domNode->getNodeValue();
+    if (!value || !*value)
+        return def;
+    if (*value == chLatin_t || *value == chDigit_1)
+        return true;
+    else if (*value == chLatin_f || *value == chDigit_0)
+        return false;
+    return def;
 }
 
 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
@@ -167,6 +202,11 @@ DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement*
     return childElement;
 }
 
+bool XMLHelper::isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local)
+{
+    return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));
+}
+
 const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
 {
     DOMNode* child=e->getFirstChild();
@@ -175,7 +215,7 @@ const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
             return child->getNodeValue();
         child=child->getNextSibling();
     }
-    return NULL;
+    return nullptr;
 }
 
 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* localName)