Update copyright.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.cpp
index e5962d6..d0fa6c2 100644 (file)
-/*\r
- *  Copyright 2001-2006 Internet2\r
- * \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-/**\r
- * XMLHelper.cpp\r
- * \r
- * A helper class for working with W3C DOM objects. \r
- */\r
-\r
-#include "internal.h"\r
-#include "util/XMLHelper.h"\r
-#include "util/XMLConstants.h"\r
-\r
-#include <xercesc/util/XMLUniDefs.hpp>\r
-\r
-using namespace xmltooling;\r
-\r
-static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };\r
-    \r
-bool XMLHelper::hasXSIType(const DOMElement* e)\r
-{\r
-    if (e) {\r
-        if (e->hasAttributeNS(XMLConstants::XSI_NS, type)) {\r
-            return true;\r
-        }\r
-    }\r
-\r
-    return false;\r
-}\r
-\r
-QName* XMLHelper::getXSIType(const DOMElement* e)\r
-{\r
-    DOMAttr* attribute = e->getAttributeNodeNS(XMLConstants::XSI_NS, type);\r
-    if (attribute) {\r
-        int i;\r
-        const XMLCh* attributeValue = attribute->getTextContent();\r
-        if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {\r
-            XMLCh* prefix=new XMLCh[i+1];\r
-            XMLString::subString(prefix,attributeValue,0,i);\r
-            prefix[i]=chNull;\r
-            QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);\r
-            delete[] prefix;\r
-            return ret;\r
-        }\r
-    }\r
-\r
-    return NULL;\r
-}\r
-\r
-DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)\r
-{\r
-    if(!domElement->hasAttributes()) {\r
-        return NULL;\r
-    }\r
-    \r
-    DOMNamedNodeMap* attributes = domElement->getAttributes();\r
-    DOMAttr* attribute;\r
-    for(XMLSize_t i = 0; i < attributes->getLength(); i++) {\r
-        attribute = static_cast<DOMAttr*>(attributes->item(i));\r
-        if(attribute->isId()) {\r
-            return attribute;\r
-        }\r
-    }\r
-    \r
-    return NULL;\r
-}\r
-\r
-QName* XMLHelper::getNodeQName(const DOMNode* domNode)\r
-{\r
-    if (domNode)\r
-        return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());\r
-    return NULL; \r
-}\r
-\r
-QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)\r
-{\r
-    if (!attribute)\r
-        return NULL;\r
-    \r
-    int i;\r
-    const XMLCh* attributeValue=attribute->getTextContent();\r
-    if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {\r
-        XMLCh* prefix=new XMLCh[i+1];\r
-        XMLString::subString(prefix,attributeValue,0,i);\r
-        prefix[i]=chNull;\r
-        QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);\r
-        delete[] prefix;\r
-        return ret;\r
-    }\r
-    \r
-    return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);\r
-}\r
-\r
-DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)\r
-{\r
-    DOMDocument* parentDocument = parentElement->getOwnerDocument();\r
-    if (childElement->getOwnerDocument() != parentDocument) {\r
-        childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));\r
-    }\r
-\r
-    parentElement->appendChild(childElement);\r
-    return childElement;\r
-}\r
+/*
+ *  Copyright 2001-2007 Internet2
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * XMLHelper.cpp
+ * 
+ * A helper class for working with W3C DOM objects. 
+ */
+
+#include "internal.h"
+#include "exceptions.h"
+#include "util/XMLHelper.h"
+#include "util/XMLConstants.h"
+
+#include <xercesc/framework/MemBufFormatTarget.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
+
+using namespace xmltooling;
+using std::ostream;
+using std::list;
+
+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;
+}
+
+QName* XMLHelper::getXSIType(const DOMElement* e)
+{
+    DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
+    if (attribute) {
+        const XMLCh* attributeValue = attribute->getTextContent();
+        if (attributeValue && *attributeValue) {
+            int i;
+            if ((i=XMLString::indexOf(attributeValue,chColon))>0) {
+                XMLCh* prefix=new XMLCh[i+1];
+                XMLString::subString(prefix,attributeValue,0,i);
+                prefix[i]=chNull;
+                QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
+                delete[] prefix;
+                return ret;
+            }
+            else {
+                return new QName(e->lookupNamespaceURI(&chNull), attributeValue);
+            }
+        }
+    }
+
+    return NULL;
+}
+
+DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
+{
+    if(!domElement->hasAttributes()) {
+        return NULL;
+    }
+    
+    DOMNamedNodeMap* attributes = domElement->getAttributes();
+    DOMAttr* attribute;
+    for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
+        attribute = static_cast<DOMAttr*>(attributes->item(i));
+        if(attribute->isId()) {
+            return attribute;
+        }
+    }
+    
+    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)
+        return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
+    return NULL; 
+}
+
+QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
+{
+    if (!attribute)
+        return NULL;
+    
+    int i;
+    const XMLCh* attributeValue=attribute->getTextContent();
+    if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
+        XMLCh* prefix=new XMLCh[i+1];
+        XMLString::subString(prefix,attributeValue,0,i);
+        prefix[i]=chNull;
+        QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
+        delete[] prefix;
+        return ret;
+    }
+    
+    return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
+}
+
+DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
+{
+    DOMDocument* parentDocument = parentElement->getOwnerDocument();
+    if (childElement->getOwnerDocument() != parentDocument) {
+        childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
+    }
+
+    parentElement->appendChild(childElement);
+    return childElement;
+}
+
+const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
+{
+    DOMNode* child=e->getFirstChild();
+    while (child) {
+        if (child->getNodeType()==DOMNode::TEXT_NODE)
+            return child->getNodeValue();
+        child=child->getNextSibling();
+    }
+    return NULL;
+}
+
+DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* localName)
+{
+    DOMNode* child = n->getFirstChild();
+    while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
+        child = child->getNextSibling();
+    if (child && localName) {
+        if (!XMLString::equals(localName,child->getLocalName()))
+            return getNextSiblingElement(child, localName);
+    }
+    return static_cast<DOMElement*>(child);
+}    
+
+DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* localName)
+{
+    DOMNode* child = n->getLastChild();
+    while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
+        child = child->getPreviousSibling();
+    if (child && localName) {
+        if (!XMLString::equals(localName,child->getLocalName()))
+            return getPreviousSiblingElement(child, localName);
+    }
+    return static_cast<DOMElement*>(child);
+}    
+
+DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+    DOMElement* e = getFirstChildElement(n, localName);
+    while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+        e = getNextSiblingElement(e, localName);
+    return e;
+}
+
+DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+    DOMElement* e = getLastChildElement(n, localName);
+    while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+        e = getPreviousSiblingElement(e, localName);
+    return e;
+}
+
+DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* localName)
+{
+    DOMNode* sib = n->getNextSibling();
+    while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
+        sib = sib->getNextSibling();
+    if (sib && localName) {
+        if (!XMLString::equals(localName,sib->getLocalName()))
+            return getNextSiblingElement(sib, localName);
+    }   
+    return static_cast<DOMElement*>(sib);
+}
+
+DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName)
+{
+    DOMNode* sib = n->getPreviousSibling();
+    while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
+        sib = sib->getPreviousSibling();
+    if (sib && localName) {
+        if (!XMLString::equals(localName,sib->getLocalName()))
+            return getPreviousSiblingElement(sib, localName);
+    }   
+    return static_cast<DOMElement*>(sib);
+}
+
+DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+    DOMElement* e = getNextSiblingElement(n, localName);
+    while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+        e = getNextSiblingElement(e, localName);
+    return e;
+}
+
+DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+    DOMElement* e = getPreviousSiblingElement(n, localName);
+    while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+        e = getPreviousSiblingElement(e, localName);
+    return e;
+}
+
+void XMLHelper::serialize(const DOMNode* n, std::string& buf)
+{
+    static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
+    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
+    DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
+    DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
+    XercesJanitor<DOMWriter> janitor(serializer);
+    serializer->setEncoding(UTF8);
+    MemBufFormatTarget target;
+    if (!serializer->writeNode(&target,*n))
+        throw XMLParserException("unable to serialize XML");
+    buf.erase();
+    buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
+}
+
+namespace {
+    class StreamFormatTarget : public XMLFormatTarget
+    {
+    public:
+        StreamFormatTarget(std::ostream& out) : m_out(out) {}
+        ~StreamFormatTarget() {}
+
+        void writeChars(const XMLByte *const toWrite, const unsigned int count, XMLFormatter *const formatter) {
+            m_out.write(reinterpret_cast<const char*>(toWrite),count);
+        }
+
+        void flush() {
+            m_out.flush();
+        }
+
+    private:
+        std::ostream& m_out;
+    };
+};
+
+ostream& XMLHelper::serialize(const DOMNode* n, ostream& out)
+{
+    static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
+    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
+    DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
+    DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
+    XercesJanitor<DOMWriter> janitor(serializer);
+    serializer->setEncoding(UTF8);
+    StreamFormatTarget target(out);
+    if (!serializer->writeNode(&target,*n))
+        throw XMLParserException("unable to serialize XML");
+    return out;
+}
+
+ostream& xmltooling::operator<<(ostream& ostr, const DOMNode& node)
+{
+    return XMLHelper::serialize(&node, ostr);
+}
+
+ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
+{
+    return ostr << *(obj.marshall());
+}