https://issues.shibboleth.net/jira/browse/CPPXT-61
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractDOMCachingXMLObject.cpp
1 /*
2  *  Copyright 2001-2010 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
32 using namespace xmltooling;
33 using namespace xercesc;
34 using namespace std;
35
36 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject() : m_dom(nullptr), m_document(nullptr)
37 {
38 }
39
40 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject(const AbstractDOMCachingXMLObject& src)
41     : AbstractXMLObject(src), m_dom(nullptr), m_document(nullptr)
42 {
43 }
44
45 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
46 {
47     if (m_document)
48         m_document->release();
49 }
50
51 DOMElement* AbstractDOMCachingXMLObject::getDOM() const
52 {
53     return m_dom;
54 }
55
56 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
57 {
58     m_dom = dom;
59     if (dom) {
60         if (bindDocument) {
61             DOMDocument* doc = dom->getOwnerDocument();
62             setDocument(doc);
63             if (dom) {
64                 DOMElement* documentRoot = doc->getDocumentElement();
65                 if (!documentRoot)
66                     doc->appendChild(dom);
67                 else if (documentRoot != dom)
68                     doc->replaceChild(dom, documentRoot);
69             }
70         }
71     }
72 }
73
74 void AbstractDOMCachingXMLObject::setDocument(DOMDocument* doc) const
75 {
76     if (m_document != doc) {
77         if (m_document)
78             m_document->release();
79         m_document=doc;
80     }
81 }
82
83 void AbstractDOMCachingXMLObject::releaseDOM() const
84 {
85     if (m_dom) {
86         if (m_log.isDebugEnabled()) {
87             string qname=getElementQName().toString();
88             m_log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
89         }
90         setDOM(nullptr);
91     }
92 }
93
94 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
95 {
96     if (getParent() && getParent()->getDOM()) {
97         m_log.debug(
98             "releasing cached DOM representation for parent object with propagation set to %s",
99             propagateRelease ? "true" : "false"
100             );
101         getParent()->releaseDOM();
102         if (propagateRelease)
103             getParent()->releaseParentDOM(propagateRelease);
104     }
105 }
106
107 namespace {
108     class _release : public binary_function<XMLObject*,bool,void> {
109     public:
110         void operator()(XMLObject* obj, bool propagate) const {
111             if (obj) {
112                 obj->releaseDOM();
113                 if (propagate)
114                     obj->releaseChildrenDOM(propagate);
115             }
116         }
117     };
118 };
119
120 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
121 {
122     if (hasChildren()) {
123         m_log.debug(
124             "releasing cached DOM representation for children with propagation set to %s",
125             propagateRelease ? "true" : "false"
126             );
127         const list<XMLObject*>& children=getOrderedChildren();
128         for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
129     }
130 }
131
132 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
133 {
134     if (getDOM()) {
135         DOMDocument* cloneDoc = doc;
136         if (!cloneDoc)
137             cloneDoc=DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
138         try {
139             return static_cast<DOMElement*>(cloneDoc->importNode(getDOM(),true));
140         }
141         catch (XMLException& ex) {
142             if (!doc)
143                 cloneDoc->release();
144             auto_ptr_char temp(ex.getMessage());
145             m_log.error("DOM clone failed: %s", temp.get());
146         }
147     }
148     return nullptr;
149 }
150
151 XMLObject* AbstractDOMCachingXMLObject::clone() const
152 {
153     // See if we can clone via the DOM.
154     DOMElement* domCopy=cloneDOM();
155     if (domCopy) {
156         // Seemed to work, so now we unmarshall the DOM to produce the clone.
157         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
158         if (!b) {
159             auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
160             m_log.error(
161                 "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
162                 );
163             domCopy->getOwnerDocument()->release();
164             throw UnmarshallingException("Unable to locate builder for cloned element.");
165         }
166         XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
167         XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
168         janitor.release(); // safely transferred
169         return ret;
170     }
171     return nullptr;
172 }
173
174 void AbstractDOMCachingXMLObject::detach()
175 {
176     // This is an override that duplicates some of the checking in the base class but
177     // adds document management in preparation for deletion of the parent.
178
179     if (!getParent())
180         return;
181
182     if (getParent()->hasParent())
183         throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
184
185     AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());
186     if (parent && parent->m_document) {
187         // Transfer control of document to me...
188         setDocument(parent->m_document);
189         parent->m_document = nullptr;
190     }
191     // The rest is done by the base.
192     AbstractXMLObject::detach();
193 }