2 * Copyright 2001-2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * AbstractDOMCachingXMLObject.cpp
20 * Extension of AbstractXMLObject that implements a DOMCachingXMLObject.
24 #include "AbstractDOMCachingXMLObject.h"
25 #include "exceptions.h"
26 #include "XMLObjectBuilder.h"
27 #include "util/XMLHelper.h"
32 using namespace xmltooling;
33 using namespace xercesc;
36 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject() : m_dom(NULL), m_document(NULL)
40 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject(const AbstractDOMCachingXMLObject& src)
41 : AbstractXMLObject(src), m_dom(NULL), m_document(NULL)
45 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
48 m_document->release();
51 DOMElement* AbstractDOMCachingXMLObject::getDOM() const
56 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
61 setDocument(dom->getOwnerDocument());
66 void AbstractDOMCachingXMLObject::setDocument(DOMDocument* doc) const
68 if (m_document != doc) {
70 m_document->release();
75 void AbstractDOMCachingXMLObject::releaseDOM() const
78 if (m_log.isDebugEnabled()) {
79 string qname=getElementQName().toString();
80 m_log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
86 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
88 if (getParent() && getParent()->getDOM()) {
90 "releasing cached DOM representation for parent object with propagation set to %s",
91 propagateRelease ? "true" : "false"
93 getParent()->releaseDOM();
95 getParent()->releaseParentDOM(propagateRelease);
100 class _release : public binary_function<XMLObject*,bool,void> {
102 void operator()(XMLObject* obj, bool propagate) const {
106 obj->releaseChildrenDOM(propagate);
112 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
116 "releasing cached DOM representation for children with propagation set to %s",
117 propagateRelease ? "true" : "false"
119 const list<XMLObject*>& children=getOrderedChildren();
120 for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
124 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
127 DOMDocument* cloneDoc = doc;
129 cloneDoc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
131 return static_cast<DOMElement*>(cloneDoc->importNode(getDOM(),true));
133 catch (XMLException& ex) {
136 auto_ptr_char temp(ex.getMessage());
137 m_log.error("DOM clone failed: %s", temp.get());
143 XMLObject* AbstractDOMCachingXMLObject::clone() const
145 // See if we can clone via the DOM.
146 DOMElement* domCopy=cloneDOM();
148 // Seemed to work, so now we unmarshall the DOM to produce the clone.
149 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
151 auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
153 "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
155 domCopy->getOwnerDocument()->release();
156 throw UnmarshallingException("Unable to locate builder for cloned element.");
158 XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
159 XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
160 janitor.release(); // safely transferred
166 void AbstractDOMCachingXMLObject::detach()
168 // This is an override that duplicates some of the checking in the base class but
169 // adds document management in preparation for deletion of the parent.
174 if (getParent()->hasParent())
175 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
177 AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());
178 if (parent && parent->m_document) {
179 // Transfer control of document to me...
180 setDocument(parent->m_document);
181 parent->m_document = NULL;
183 // The rest is done by the base.
184 AbstractXMLObject::detach();