gcc const fix, converted linefeeds
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractDOMCachingXMLObject.cpp
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * AbstractDOMCachingXMLObject.cpp
19  * 
20  * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. 
21  */
22
23 #include "internal.h"
24 #include "AbstractDOMCachingXMLObject.h"
25 #include "exceptions.h"
26 #include "XMLObjectBuilder.h"
27 #include "util/XMLHelper.h"
28
29 #include <algorithm>
30 #include <functional>
31 #include <log4cpp/Category.hh>
32
33 using namespace xmltooling;
34 using namespace log4cpp;
35 using namespace std;
36
37 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
38 {
39     if (m_document)
40         m_document->release();
41 }
42
43 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
44 {
45     m_dom=dom;
46     if (dom) {
47         if (bindDocument) {
48             setDocument(dom->getOwnerDocument());
49         }
50     }
51 }
52
53 void AbstractDOMCachingXMLObject::releaseDOM() const
54 {
55     if (m_dom) {
56         Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM");
57         if (log.isDebugEnabled()) {
58             string qname=getElementQName().toString();
59             log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
60         }
61         setDOM(NULL);
62     }
63 }
64
65 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
66 {
67     if (getParent() && getParent()->getDOM()) {
68         Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(
69             "releasing cached DOM representation for parent object with propagation set to %s",
70             propagateRelease ? "true" : "false"
71             );
72         getParent()->releaseDOM();
73         if (propagateRelease)
74             getParent()->releaseParentDOM(propagateRelease);
75     }
76 }
77
78 class _release : public binary_function<XMLObject*,bool,void> {
79 public:
80     void operator()(XMLObject* obj, bool propagate) const {
81         if (obj) {
82             obj->releaseDOM();
83             if (propagate)
84                 obj->releaseChildrenDOM(propagate);
85         }
86     }
87 };
88
89 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
90 {
91     if (hasChildren()) {
92         Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(
93             "releasing cached DOM representation for children with propagation set to %s",
94             propagateRelease ? "true" : "false"
95             );
96         const list<XMLObject*>& children=getOrderedChildren();
97         for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
98     }
99 }
100
101 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
102 {
103     if (getDOM()) {
104         if (!doc)
105             doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
106         return static_cast<DOMElement*>(doc->importNode(getDOM(),true));
107     }
108     return NULL;
109 }
110
111 XMLObject* AbstractDOMCachingXMLObject::clone() const
112 {
113     // See if we can clone via the DOM.
114     DOMElement* domCopy=cloneDOM();
115     if (domCopy) {
116         // Seemed to work, so now we unmarshall the DOM to produce the clone.
117         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
118         if (!b) {
119             auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
120             Category::getInstance(XMLTOOLING_LOGCAT".DOM").error(
121                 "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
122                 );
123             domCopy->getOwnerDocument()->release();
124             throw UnmarshallingException("Unable to locate builder for cloned element.");
125         }
126         XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
127         XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
128         janitor.release(); // safely transferred
129         return ret;
130     }
131     return NULL;
132 }
133
134 void AbstractDOMCachingXMLObject::detach()
135 {
136     // This is an override that duplicates some of the checking in the base class but
137     // adds document management in preparation for deletion of the parent.
138
139     if (!getParent())
140         return;
141
142     if (getParent()->hasParent())
143         throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
144
145     AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());
146     if (parent && parent->m_document) {
147         // Transfer control of document to me...
148         setDocument(parent->m_document);
149         parent->m_document = NULL;
150     }
151     // The rest is done by the base.
152     AbstractXMLObject::detach();
153 }