gcc const fix, converted linefeeds
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractDOMCachingXMLObject.cpp
index bee23c2..4966156 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 "AbstractDOMCachingXMLObject.h"\r
-#include "exceptions.h"\r
-#include "XMLObjectBuilder.h"\r
-#include "util/XMLHelper.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::setDOM(DOMElement* dom, bool bindDocument) const\r
-{\r
-    m_dom=dom;\r
-    if (dom) {\r
-        if (bindDocument) {\r
-            setDocument(dom->getOwnerDocument());\r
-        }\r
-    }\r
-}\r
-\r
-void AbstractDOMCachingXMLObject::releaseDOM() const\r
-{\r
-    if (m_dom) {\r
-        Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM");\r
-        if (log.isDebugEnabled()) {\r
-            string qname=getElementQName().toString();\r
-            log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());\r
-        }\r
-        setDOM(NULL);\r
-    }\r
-}\r
-\r
-void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const\r
-{\r
-    if (getParent() && getParent()->getDOM()) {\r
-        Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(\r
-            "releasing cached DOM representation for parent object with propagation set to %s",\r
-            propagateRelease ? "true" : "false"\r
-            );\r
-        getParent()->releaseDOM();\r
-        if (propagateRelease)\r
-            getParent()->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
-        if (obj) {\r
-            obj->releaseDOM();\r
-            if (propagate)\r
-                obj->releaseChildrenDOM(propagate);\r
-        }\r
-    }\r
-};\r
-\r
-void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const\r
-{\r
-    if (hasChildren()) {\r
-        Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(\r
-            "releasing cached DOM representation for children with propagation set to %s",\r
-            propagateRelease ? "true" : "false"\r
-            );\r
-        const list<XMLObject*>& children=getOrderedChildren();\r
-        for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));\r
-    }\r
-}\r
-\r
-DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const\r
-{\r
-    if (getDOM()) {\r
-        if (!doc)\r
-            doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();\r
-        return static_cast<DOMElement*>(doc->importNode(getDOM(),true));\r
-    }\r
-    return NULL;\r
-}\r
-\r
-XMLObject* AbstractDOMCachingXMLObject::clone() const\r
-{\r
-    // See if we can clone via the DOM.\r
-    DOMElement* domCopy=cloneDOM();\r
-    if (domCopy) {\r
-        // Seemed to work, so now we unmarshall the DOM to produce the clone.\r
-        const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);\r
-        if (!b) {\r
-            auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));\r
-            Category::getInstance(XMLTOOLING_LOGCAT".DOM").error(\r
-                "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()\r
-                );\r
-            domCopy->getOwnerDocument()->release();\r
-            throw UnmarshallingException("Unable to locate builder for cloned element.");\r
-        }\r
-        XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());\r
-        XMLObject* ret = b->buildFromElement(domCopy,true); // bind document\r
-        janitor.release(); // safely transferred\r
-        return ret;\r
-    }\r
-    return NULL;\r
-}\r
-\r
-void AbstractDOMCachingXMLObject::detach()\r
-{\r
-    // This is an override that duplicates some of the checking in the base class but\r
-    // adds document management in preparation for deletion of the parent.\r
-\r
-    if (!getParent())\r
-        return;\r
-\r
-    if (getParent()->hasParent())\r
-        throw XMLObjectException("Cannot detach an object whose parent is itself a child.");\r
-\r
-    AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());\r
-    if (parent && parent->m_document) {\r
-        // Transfer control of document to me...\r
-        setDocument(parent->m_document);\r
-        parent->m_document = NULL;\r
-    }\r
-    // The rest is done by the base.\r
-    AbstractXMLObject::detach();\r
-}\r
+/*
+ *  Copyright 2001-2006 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.
+ */
+
+/**
+ * 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>
+#include <log4cpp/Category.hh>
+
+using namespace xmltooling;
+using namespace log4cpp;
+using namespace std;
+
+AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
+{
+    if (m_document)
+        m_document->release();
+}
+
+void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
+{
+    m_dom=dom;
+    if (dom) {
+        if (bindDocument) {
+            setDocument(dom->getOwnerDocument());
+        }
+    }
+}
+
+void AbstractDOMCachingXMLObject::releaseDOM() const
+{
+    if (m_dom) {
+        Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM");
+        if (log.isDebugEnabled()) {
+            string qname=getElementQName().toString();
+            log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
+        }
+        setDOM(NULL);
+    }
+}
+
+void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
+{
+    if (getParent() && getParent()->getDOM()) {
+        Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(
+            "releasing cached DOM representation for parent object with propagation set to %s",
+            propagateRelease ? "true" : "false"
+            );
+        getParent()->releaseDOM();
+        if (propagateRelease)
+            getParent()->releaseParentDOM(propagateRelease);
+    }
+}
+
+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()) {
+        Category::getInstance(XMLTOOLING_LOGCAT".DOM").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()) {
+        if (!doc)
+            doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
+        return static_cast<DOMElement*>(doc->importNode(getDOM(),true));
+    }
+    return NULL;
+}
+
+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));
+            Category::getInstance(XMLTOOLING_LOGCAT".DOM").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 NULL;
+}
+
+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 = NULL;
+    }
+    // The rest is done by the base.
+    AbstractXMLObject::detach();
+}