2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * AbstractDOMCachingXMLObject.cpp
\r
20 * Extension of AbstractXMLObject that implements a DOMCachingXMLObject.
\r
23 #include "internal.h"
\r
24 #include "AbstractDOMCachingXMLObject.h"
\r
25 #include "exceptions.h"
\r
26 #include "XMLObjectBuilder.h"
\r
27 #include "util/XMLHelper.h"
\r
29 #include <algorithm>
\r
30 #include <functional>
\r
31 #include <log4cpp/Category.hh>
\r
33 using namespace xmltooling;
\r
34 using namespace log4cpp;
\r
35 using namespace std;
\r
37 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
\r
40 m_document->release();
\r
43 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
\r
48 setDocument(dom->getOwnerDocument());
\r
53 void AbstractDOMCachingXMLObject::releaseDOM() const
\r
56 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM");
\r
57 if (log.isDebugEnabled()) {
\r
58 string qname=getElementQName().toString();
\r
59 log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
\r
65 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
\r
67 if (getParent() && getParent()->getDOM()) {
\r
68 Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(
\r
69 "releasing cached DOM representation for parent object with propagation set to %s",
\r
70 propagateRelease ? "true" : "false"
\r
72 getParent()->releaseDOM();
\r
73 if (propagateRelease)
\r
74 getParent()->releaseParentDOM(propagateRelease);
\r
78 class _release : public binary_function<XMLObject*,bool,void> {
\r
80 void operator()(XMLObject* obj, bool propagate) const {
\r
84 obj->releaseChildrenDOM(propagate);
\r
89 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
\r
91 if (hasChildren()) {
\r
92 Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(
\r
93 "releasing cached DOM representation for children with propagation set to %s",
\r
94 propagateRelease ? "true" : "false"
\r
96 const list<XMLObject*>& children=getOrderedChildren();
\r
97 for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
\r
101 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
\r
105 doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
\r
106 return static_cast<DOMElement*>(doc->importNode(getDOM(),true));
\r
111 XMLObject* AbstractDOMCachingXMLObject::clone() const
\r
113 // See if we can clone via the DOM.
\r
114 DOMElement* domCopy=cloneDOM();
\r
116 // Seemed to work, so now we unmarshall the DOM to produce the clone.
\r
117 const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
\r
119 auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
\r
120 Category::getInstance(XMLTOOLING_LOGCAT".DOM").error(
\r
121 "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
\r
123 domCopy->getOwnerDocument()->release();
\r
124 throw UnmarshallingException("Unable to locate builder for cloned element.");
\r
126 XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
\r
127 XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
\r
128 janitor.release(); // safely transferred
\r
134 void AbstractDOMCachingXMLObject::detach()
\r
136 // This is an override that duplicates some of the checking in the base class but
\r
137 // adds document management in preparation for deletion of the parent.
\r
142 if (getParent()->hasParent())
\r
143 throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
\r
145 AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());
\r
146 if (parent && parent->m_document) {
\r
147 // Transfer control of document to me...
\r
148 setDocument(parent->m_document);
\r
149 parent->m_document = NULL;
\r
151 // The rest is done by the base.
\r
152 AbstractXMLObject::detach();
\r