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