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