2 * Copyright 2001-2010 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(nullptr), m_document(nullptr)
40 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject(const AbstractDOMCachingXMLObject& src)
41 : AbstractXMLObject(src), m_dom(nullptr), m_document(nullptr)
45 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
48 m_document->release();
51 DOMElement* AbstractDOMCachingXMLObject::getDOM() const
56 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
59 if (dom && bindDocument) {
60 DOMDocument* doc = dom->getOwnerDocument();
62 DOMElement* documentRoot = doc->getDocumentElement();
64 doc->appendChild(dom);
65 else if (documentRoot != dom)
66 doc->replaceChild(dom, documentRoot);
70 void AbstractDOMCachingXMLObject::setDocument(DOMDocument* doc) const
72 if (m_document != doc) {
74 m_document->release();
79 void AbstractDOMCachingXMLObject::releaseDOM() const
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());
90 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
92 if (getParent() && getParent()->getDOM()) {
94 "releasing cached DOM representation for parent object with propagation set to %s",
95 propagateRelease ? "true" : "false"
97 getParent()->releaseDOM();
99 getParent()->releaseParentDOM(propagateRelease);
104 class _release : public binary_function<XMLObject*,bool,void> {
106 void operator()(XMLObject* obj, bool propagate) const {
110 obj->releaseChildrenDOM(propagate);
116 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
120 "releasing cached DOM representation for children with propagation set to %s",
121 propagateRelease ? "true" : "false"
123 const list<XMLObject*>& children=getOrderedChildren();
124 for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
128 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
131 DOMDocument* cloneDoc = doc;
133 cloneDoc=DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
135 return static_cast<DOMElement*>(cloneDoc->importNode(getDOM(),true));
137 catch (XMLException& ex) {
140 auto_ptr_char temp(ex.getMessage());
141 m_log.error("DOM clone failed: %s", temp.get());
147 XMLObject* AbstractDOMCachingXMLObject::clone() const
149 // See if we can clone via the DOM.
150 DOMElement* domCopy=cloneDOM();
152 // Seemed to work, so now we unmarshall the DOM to produce the clone.
153 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
155 auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
157 "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
159 domCopy->getOwnerDocument()->release();
160 throw UnmarshallingException("Unable to locate builder for cloned element.");
162 XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
163 XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
164 janitor.release(); // safely transferred
170 void AbstractDOMCachingXMLObject::detach()
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.
178 if (getParent()->hasParent())
179 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
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;
187 // The rest is done by the base.
188 AbstractXMLObject::detach();