2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * AbstractDOMCachingXMLObject.cpp
24 * Extension of AbstractXMLObject that implements a DOMCachingXMLObject.
28 #include "AbstractDOMCachingXMLObject.h"
29 #include "exceptions.h"
30 #include "XMLObjectBuilder.h"
31 #include "util/XMLHelper.h"
36 using namespace xmltooling;
37 using namespace xercesc;
40 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject() : m_dom(nullptr), m_document(nullptr)
44 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject(const AbstractDOMCachingXMLObject& src)
45 : AbstractXMLObject(src), m_dom(nullptr), m_document(nullptr)
49 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
52 m_document->release();
55 DOMElement* AbstractDOMCachingXMLObject::getDOM() const
60 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
63 if (dom && bindDocument) {
64 DOMDocument* doc = dom->getOwnerDocument();
66 DOMElement* documentRoot = doc->getDocumentElement();
68 doc->appendChild(dom);
69 else if (documentRoot != dom)
70 doc->replaceChild(dom, documentRoot);
74 void AbstractDOMCachingXMLObject::setDocument(DOMDocument* doc) const
76 if (m_document != doc) {
78 m_document->release();
83 void AbstractDOMCachingXMLObject::releaseDOM() const
86 if (m_log.isDebugEnabled()) {
87 string qname=getElementQName().toString();
88 m_log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
94 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
96 if (getParent() && getParent()->getDOM()) {
98 "releasing cached DOM representation for parent object with propagation set to %s",
99 propagateRelease ? "true" : "false"
101 getParent()->releaseDOM();
102 if (propagateRelease)
103 getParent()->releaseParentDOM(propagateRelease);
108 class _release : public binary_function<XMLObject*,bool,void> {
110 void operator()(XMLObject* obj, bool propagate) const {
114 obj->releaseChildrenDOM(propagate);
120 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
124 "releasing cached DOM representation for children with propagation set to %s",
125 propagateRelease ? "true" : "false"
127 const list<XMLObject*>& children=getOrderedChildren();
128 for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
132 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
135 DOMDocument* cloneDoc = doc;
137 cloneDoc=DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
139 return static_cast<DOMElement*>(cloneDoc->importNode(getDOM(),true));
141 catch (XMLException& ex) {
144 auto_ptr_char temp(ex.getMessage());
145 m_log.error("DOM clone failed: %s", temp.get());
151 XMLObject* AbstractDOMCachingXMLObject::clone() const
153 // See if we can clone via the DOM.
154 DOMElement* domCopy=cloneDOM();
156 // Seemed to work, so now we unmarshall the DOM to produce the clone.
157 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
159 auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
161 "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
163 domCopy->getOwnerDocument()->release();
164 throw UnmarshallingException("Unable to locate builder for cloned element.");
166 XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
167 XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
168 janitor.release(); // safely transferred
174 void AbstractDOMCachingXMLObject::detach()
176 // This is an override that duplicates some of the checking in the base class but
177 // adds document management in preparation for deletion of the parent.
182 if (getParent()->hasParent())
183 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
185 AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());
186 if (parent && parent->m_document) {
187 // Transfer control of document to me...
188 setDocument(parent->m_document);
189 parent->m_document = nullptr;
191 // The rest is done by the base.
192 AbstractXMLObject::detach();