Set fourth file version digit to signify rebuild.
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractDOMCachingXMLObject.cpp
index 0b9f064..061df1a 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
- * AbstractDOMCachingXMLObject.cpp\r
- * \r
- * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. \r
- */\r
-\r
-#include "internal.h"\r
-#include "exceptions.h"\r
-#include "AbstractDOMCachingXMLObject.h"\r
-\r
-#include <algorithm>\r
-#include <functional>\r
-#include <log4cpp/Category.hh>\r
-\r
-using namespace xmltooling;\r
-using namespace log4cpp;\r
-using namespace std;\r
-\r
-AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()\r
-{\r
-    if (m_document)\r
-        m_document->release();\r
-}\r
-\r
-void AbstractDOMCachingXMLObject::releaseDOM()\r
-{\r
-    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM");\r
-    if (log.isDebugEnabled())\r
-        log.debug("Releasing cached DOM reprsentation for %s", getElementQName().toString().c_str());\r
-\r
-    // We don't get rid of the document we're holding, if any.\r
-    // The marshalling process deals with that.\r
-    setDOM(NULL);\r
-}\r
-\r
-void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease)\r
-{\r
-    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM");\r
-    if (log.isDebugEnabled()) {\r
-        log.debug(\r
-            "Releasing cached DOM representation for parent of %s with propagation set to %s",\r
-            getElementQName().toString().c_str(), propagateRelease ? "true" : "false"\r
-            );\r
-    }\r
-\r
-    DOMCachingXMLObject* domCachingParent = dynamic_cast<DOMCachingXMLObject*>(getParent());\r
-    if (domCachingParent) {\r
-        domCachingParent->releaseDOM();\r
-        if (propagateRelease)\r
-            domCachingParent->releaseParentDOM(propagateRelease);\r
-    }\r
-}\r
-\r
-class _release : public binary_function<XMLObject*,bool,void> {\r
-public:\r
-    void operator()(XMLObject* obj, bool propagate) const {\r
-        DOMCachingXMLObject* domCachingParent = dynamic_cast<DOMCachingXMLObject*>(obj);\r
-        if (domCachingParent) {\r
-            domCachingParent->releaseDOM();\r
-            if (propagate)\r
-                domCachingParent->releaseChildrenDOM(propagate);\r
-        }\r
-    }\r
-};\r
-\r
-void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease)\r
-{\r
-    Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM");\r
-    if (log.isDebugEnabled()) {\r
-        log.debug(\r
-            "Releasing cached DOM representation for children of %s with propagation set to %s",\r
-            getElementQName().toString().c_str(), propagateRelease ? "true" : "false"\r
-            );\r
-    }\r
-    \r
-    vector<XMLObject*> children;\r
-    if (getOrderedChildren(children))\r
-        for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));\r
-}\r
-\r
-XMLObject* AbstractDOMCachingXMLObject::prepareForAssignment(const XMLObject* oldValue, XMLObject* newValue) {\r
-\r
-    if (newValue && newValue->hasParent())\r
-        throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");\r
-\r
-    if (!oldValue) {\r
-        if (newValue) {\r
-            releaseThisandParentDOM();\r
-            newValue->setParent(this);\r
-            return newValue;\r
-        }\r
-        else {\r
-            return NULL;\r
-        }\r
-    }\r
-\r
-    if (oldValue != newValue) {\r
-        delete oldValue;\r
-        releaseThisandParentDOM();\r
-        newValue->setParent(this);\r
-    }\r
-\r
-    return newValue;\r
-}\r
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you 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.
+ */
+
+/**
+ * AbstractDOMCachingXMLObject.cpp
+ * 
+ * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. 
+ */
+
+#include "internal.h"
+#include "AbstractDOMCachingXMLObject.h"
+#include "exceptions.h"
+#include "XMLObjectBuilder.h"
+#include "util/XMLHelper.h"
+
+#include <algorithm>
+#include <functional>
+
+using namespace xmltooling;
+using namespace xercesc;
+using namespace std;
+
+AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject() : m_dom(nullptr), m_document(nullptr)
+{
+}
+
+AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject(const AbstractDOMCachingXMLObject& src)
+    : AbstractXMLObject(src), m_dom(nullptr), m_document(nullptr)
+{
+}
+
+AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
+{
+    if (m_document)
+        m_document->release();
+}
+
+DOMElement* AbstractDOMCachingXMLObject::getDOM() const
+{
+    return m_dom;
+}
+
+void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
+{
+    m_dom = dom;
+    if (dom && bindDocument) {
+        DOMDocument* doc = dom->getOwnerDocument();
+        setDocument(doc);
+        DOMElement* documentRoot = doc->getDocumentElement();
+        if (!documentRoot)
+            doc->appendChild(dom);
+        else if (documentRoot != dom)
+            doc->replaceChild(dom, documentRoot);
+    }
+}
+
+void AbstractDOMCachingXMLObject::setDocument(DOMDocument* doc) const
+{
+    if (m_document != doc) {
+        if (m_document)
+            m_document->release();
+        m_document=doc;
+    }
+}
+
+void AbstractDOMCachingXMLObject::releaseDOM() const
+{
+    if (m_dom) {
+        if (m_log.isDebugEnabled()) {
+            string qname=getElementQName().toString();
+            m_log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
+        }
+        setDOM(nullptr);
+    }
+}
+
+void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
+{
+    if (getParent() && getParent()->getDOM()) {
+        m_log.debug(
+            "releasing cached DOM representation for parent object with propagation set to %s",
+            propagateRelease ? "true" : "false"
+            );
+        getParent()->releaseDOM();
+        if (propagateRelease)
+            getParent()->releaseParentDOM(propagateRelease);
+    }
+}
+
+namespace {
+    class _release : public binary_function<XMLObject*,bool,void> {
+    public:
+        void operator()(XMLObject* obj, bool propagate) const {
+            if (obj) {
+                obj->releaseDOM();
+                if (propagate)
+                    obj->releaseChildrenDOM(propagate);
+            }
+        }
+    };
+};
+
+void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
+{
+    if (hasChildren()) {
+        m_log.debug(
+            "releasing cached DOM representation for children with propagation set to %s",
+            propagateRelease ? "true" : "false"
+            );
+        const list<XMLObject*>& children=getOrderedChildren();
+        for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
+    }
+}
+
+DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
+{
+    if (getDOM()) {
+        DOMDocument* cloneDoc = doc;
+        if (!cloneDoc)
+            cloneDoc=DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
+        try {
+            return static_cast<DOMElement*>(cloneDoc->importNode(getDOM(),true));
+        }
+        catch (XMLException& ex) {
+            if (!doc)
+                cloneDoc->release();
+            auto_ptr_char temp(ex.getMessage());
+            m_log.error("DOM clone failed: %s", temp.get());
+        }
+    }
+    return nullptr;
+}
+
+XMLObject* AbstractDOMCachingXMLObject::clone() const
+{
+    // See if we can clone via the DOM.
+    DOMElement* domCopy=cloneDOM();
+    if (domCopy) {
+        // Seemed to work, so now we unmarshall the DOM to produce the clone.
+        const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
+        if (!b) {
+            auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
+            m_log.error(
+                "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
+                );
+            domCopy->getOwnerDocument()->release();
+            throw UnmarshallingException("Unable to locate builder for cloned element.");
+        }
+        XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
+        XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
+        janitor.release(); // safely transferred
+        return ret;
+    }
+    return nullptr;
+}
+
+void AbstractDOMCachingXMLObject::detach()
+{
+    // This is an override that duplicates some of the checking in the base class but
+    // adds document management in preparation for deletion of the parent.
+
+    if (!getParent())
+        return;
+
+    if (getParent()->hasParent())
+        throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
+
+    AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());
+    if (parent && parent->m_document) {
+        // Transfer control of document to me...
+        setDocument(parent->m_document);
+        parent->m_document = nullptr;
+    }
+    // The rest is done by the base.
+    AbstractXMLObject::detach();
+}