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