X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2FAbstractDOMCachingXMLObject.cpp;h=496615604d4245a00ae01531b95699e30da6edf9;hb=e7a65d784215bc04355f014141219b3e7ab4559a;hp=750e3f91f06ee104a526fc5efd35373a346a57ed;hpb=cc0bfa6146fb0a556e80ae5f83c330dbe2ddf3c9;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/AbstractDOMCachingXMLObject.cpp b/xmltooling/AbstractDOMCachingXMLObject.cpp index 750e3f9..4966156 100644 --- a/xmltooling/AbstractDOMCachingXMLObject.cpp +++ b/xmltooling/AbstractDOMCachingXMLObject.cpp @@ -1,122 +1,153 @@ -/* - * Copyright 2001-2006 Internet2 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * AbstractDOMCachingXMLObject.cpp - * - * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. - */ - -#include "internal.h" -#include "exceptions.h" -#include "AbstractDOMCachingXMLObject.h" - -#include -#include -#include - -using namespace xmltooling; -using namespace log4cpp; -using namespace std; - -AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject() -{ - if (m_document) - m_document->release(); -} - -void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) -{ - m_dom=dom; - if (dom) { - if (bindDocument) { - setDocument(dom->getOwnerDocument()); - } - } -} - -void AbstractDOMCachingXMLObject::releaseDOM() -{ - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM"); - if (log.isDebugEnabled()) { - string qname=getElementQName().toString(); - log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str()); - } - setDOM(NULL); -} - -void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) -{ - DOMCachingXMLObject* domCachingParent = dynamic_cast(getParent()); - if (domCachingParent) { - Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug( - "releasing cached DOM representation for parent object with propagation set to %s", - propagateRelease ? "true" : "false" - ); - domCachingParent->releaseDOM(); - if (propagateRelease) - domCachingParent->releaseParentDOM(propagateRelease); - } -} - -class _release : public binary_function { -public: - void operator()(XMLObject* obj, bool propagate) const { - DOMCachingXMLObject* domCaching = dynamic_cast(obj); - if (domCaching) { - domCaching->releaseDOM(); - if (propagate) - domCaching->releaseChildrenDOM(propagate); - } - } -}; - -void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) -{ - vector children; - if (getOrderedChildren(children)) { - Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug( - "releasing cached DOM representation for children with propagation set to %s", - propagateRelease ? "true" : "false" - ); - for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease)); - } -} - -XMLObject* AbstractDOMCachingXMLObject::prepareForAssignment(const XMLObject* oldValue, XMLObject* newValue) { - - if (newValue && newValue->hasParent()) - throw XMLObjectException("Child XMLObject cannot be added - it is already the child of another XMLObject"); - - if (!oldValue) { - if (newValue) { - releaseThisandParentDOM(); - newValue->setParent(this); - return newValue; - } - else { - return NULL; - } - } - - if (oldValue != newValue) { - delete oldValue; - releaseThisandParentDOM(); - newValue->setParent(this); - } - - return newValue; -} +/* + * Copyright 2001-2006 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * AbstractDOMCachingXMLObject.cpp + * + * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. + */ + +#include "internal.h" +#include "AbstractDOMCachingXMLObject.h" +#include "exceptions.h" +#include "XMLObjectBuilder.h" +#include "util/XMLHelper.h" + +#include +#include +#include + +using namespace xmltooling; +using namespace log4cpp; +using namespace std; + +AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject() +{ + if (m_document) + m_document->release(); +} + +void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const +{ + m_dom=dom; + if (dom) { + if (bindDocument) { + setDocument(dom->getOwnerDocument()); + } + } +} + +void AbstractDOMCachingXMLObject::releaseDOM() const +{ + if (m_dom) { + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM"); + if (log.isDebugEnabled()) { + string qname=getElementQName().toString(); + log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str()); + } + setDOM(NULL); + } +} + +void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const +{ + if (getParent() && getParent()->getDOM()) { + Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug( + "releasing cached DOM representation for parent object with propagation set to %s", + propagateRelease ? "true" : "false" + ); + getParent()->releaseDOM(); + if (propagateRelease) + getParent()->releaseParentDOM(propagateRelease); + } +} + +class _release : public binary_function { +public: + void operator()(XMLObject* obj, bool propagate) const { + if (obj) { + obj->releaseDOM(); + if (propagate) + obj->releaseChildrenDOM(propagate); + } + } +}; + +void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const +{ + if (hasChildren()) { + Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug( + "releasing cached DOM representation for children with propagation set to %s", + propagateRelease ? "true" : "false" + ); + const list& children=getOrderedChildren(); + for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease)); + } +} + +DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const +{ + if (getDOM()) { + if (!doc) + doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument(); + return static_cast(doc->importNode(getDOM(),true)); + } + return NULL; +} + +XMLObject* AbstractDOMCachingXMLObject::clone() const +{ + // See if we can clone via the DOM. + DOMElement* domCopy=cloneDOM(); + if (domCopy) { + // Seemed to work, so now we unmarshall the DOM to produce the clone. + const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy); + if (!b) { + auto_ptr q(XMLHelper::getNodeQName(domCopy)); + Category::getInstance(XMLTOOLING_LOGCAT".DOM").error( + "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str() + ); + domCopy->getOwnerDocument()->release(); + throw UnmarshallingException("Unable to locate builder for cloned element."); + } + XercesJanitor janitor(domCopy->getOwnerDocument()); + XMLObject* ret = b->buildFromElement(domCopy,true); // bind document + janitor.release(); // safely transferred + return ret; + } + return NULL; +} + +void AbstractDOMCachingXMLObject::detach() +{ + // This is an override that duplicates some of the checking in the base class but + // adds document management in preparation for deletion of the parent. + + if (!getParent()) + return; + + if (getParent()->hasParent()) + throw XMLObjectException("Cannot detach an object whose parent is itself a child."); + + AbstractDOMCachingXMLObject* parent = dynamic_cast(getParent()); + if (parent && parent->m_document) { + // Transfer control of document to me... + setDocument(parent->m_document); + parent->m_document = NULL; + } + // The rest is done by the base. + AbstractXMLObject::detach(); +}