Allow control over thread stack size.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.cpp
index 77f8d0f..ee502a4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2007 Internet2
+ *  Copyright 2001-2009 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
 
 #include "internal.h"
 #include "exceptions.h"
+#include "QName.h"
+#include "XMLObject.h"
 #include "util/XMLHelper.h"
 #include "util/XMLConstants.h"
 
@@ -36,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)
@@ -123,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)
@@ -132,21 +155,26 @@ xmltooling::QName* XMLHelper::getNodeQName(const DOMNode* domNode)
 
 xmltooling::QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
 {
-    if (!attribute)
+    return getNodeValueAsQName(attribute);
+}
+
+xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode)
+{
+    if (!domNode)
         return NULL;
     
     int i;
-    const XMLCh* attributeValue=attribute->getTextContent();
-    if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
+    const XMLCh* value=domNode->getTextContent();
+    if (value && (i=XMLString::indexOf(value,chColon))>0) {
         XMLCh* prefix=new XMLCh[i+1];
-        XMLString::subString(prefix,attributeValue,0,i);
+        XMLString::subString(prefix,value,0,i);
         prefix[i]=chNull;
-        xmltooling::QName* ret=new xmltooling::QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
+        xmltooling::QName* ret=new xmltooling::QName(domNode->lookupNamespaceURI(prefix), value + i + 1, prefix);
         delete[] prefix;
         return ret;
     }
     
-    return new xmltooling::QName(attribute->lookupNamespaceURI(NULL), attributeValue);
+    return new xmltooling::QName(domNode->lookupNamespaceURI(NULL), value);
 }
 
 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
@@ -160,6 +188,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();
@@ -254,7 +287,7 @@ DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh*
 void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
-    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
+    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, chDigit_8, chNull };
 
     MemBufFormatTarget target;
     DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
@@ -264,9 +297,9 @@ void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty)
     XercesJanitor<DOMLSSerializer> janitor(serializer);
     if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
         serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
-    DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();\r
-    XercesJanitor<DOMLSOutput> j_theOutput(theOutput);\r
-    theOutput->setEncoding(UTF8);\r
+    DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();
+    XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
+    theOutput->setEncoding(UTF8);
     theOutput->setByteStream(&target);
     if (!serializer->write(n, theOutput))
         throw XMLParserException("unable to serialize XML");
@@ -307,7 +340,7 @@ namespace {
 ostream& XMLHelper::serialize(const DOMNode* n, ostream& out, bool pretty)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
-    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
+    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, chDigit_8, chNull };
 
     StreamFormatTarget target(out);
     DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
@@ -317,9 +350,9 @@ ostream& XMLHelper::serialize(const DOMNode* n, ostream& out, bool pretty)
     XercesJanitor<DOMLSSerializer> janitor(serializer);
     if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
         serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
-    DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();\r
-    XercesJanitor<DOMLSOutput> j_theOutput(theOutput);\r
-    theOutput->setEncoding(UTF8);\r
+    DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();
+    XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
+    theOutput->setEncoding(UTF8);
     theOutput->setByteStream(&target);
     if (!serializer->write(n, theOutput))
         throw XMLParserException("unable to serialize XML");