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 "exceptions.h"
\r
25 #include "AbstractDOMCachingXMLObject.h"
\r
26 #include "io/Unmarshaller.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)
\r
48 setDocument(dom->getOwnerDocument());
\r
53 void AbstractDOMCachingXMLObject::releaseDOM()
\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)
\r
67 DOMCachingXMLObject* domCachingParent = dynamic_cast<DOMCachingXMLObject*>(getParent());
\r
68 if (domCachingParent) {
\r
69 if (domCachingParent->getDOM()) {
\r
70 Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(
\r
71 "releasing cached DOM representation for parent object with propagation set to %s",
\r
72 propagateRelease ? "true" : "false"
\r
74 domCachingParent->releaseDOM();
\r
75 if (propagateRelease)
\r
76 domCachingParent->releaseParentDOM(propagateRelease);
\r
81 class _release : public binary_function<XMLObject*,bool,void> {
\r
83 void operator()(XMLObject* obj, bool propagate) const {
\r
84 DOMCachingXMLObject* domCaching = dynamic_cast<DOMCachingXMLObject*>(obj);
\r
86 domCaching->releaseDOM();
\r
88 domCaching->releaseChildrenDOM(propagate);
\r
93 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease)
\r
95 if (hasChildren()) {
\r
96 Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug(
\r
97 "releasing cached DOM representation for children with propagation set to %s",
\r
98 propagateRelease ? "true" : "false"
\r
100 for_each(m_children.begin(),m_children.end(),bind2nd(_release(),propagateRelease));
\r
104 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
\r
108 doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
\r
109 return static_cast<DOMElement*>(doc->importNode(getDOM(),true));
\r
114 XMLObject* AbstractDOMCachingXMLObject::clone() const
\r
116 // See if we can clone via the DOM.
\r
117 DOMElement* domCopy=cloneDOM();
\r
119 // Seemed to work, so now we unmarshall the DOM to produce the clone.
\r
120 const Unmarshaller* u=Unmarshaller::getUnmarshaller(domCopy);
\r
122 auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
\r
123 Category::getInstance(XMLTOOLING_LOGCAT".DOM").error(
\r
124 "DOM clone failed, unable to locate unmarshaller for element (%s)", q->toString().c_str()
\r
128 return u->unmarshall(domCopy, true); // bind document
\r
131 domCopy->getOwnerDocument()->release();
\r