1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20 
21 /**
22  * AbstractDOMCachingXMLObject.cpp
23  *
24  * Extension of AbstractXMLObject that implements a DOMCachingXMLObject.
25  */
26 
27 #include "internal.h"
28 #include "AbstractDOMCachingXMLObject.h"
29 #include "exceptions.h"
30 #include "XMLObjectBuilder.h"
31 #include "util/XMLHelper.h"
32 
33 #include <algorithm>
34 #include <functional>
35 
36 using namespace xmltooling;
37 using namespace xercesc;
38 using namespace std;
39 
40 using boost::scoped_ptr;
41 
AbstractDOMCachingXMLObject()42 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject() : m_dom(nullptr), m_document(nullptr)
43 {
44 }
45 
AbstractDOMCachingXMLObject(const AbstractDOMCachingXMLObject & src)46 AbstractDOMCachingXMLObject::AbstractDOMCachingXMLObject(const AbstractDOMCachingXMLObject& src)
47     : AbstractXMLObject(src), m_dom(nullptr), m_document(nullptr)
48 {
49 }
50 
~AbstractDOMCachingXMLObject()51 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()
52 {
53     if (m_document)
54         m_document->release();
55 }
56 
getDOM() const57 DOMElement* AbstractDOMCachingXMLObject::getDOM() const
58 {
59     return m_dom;
60 }
61 
setDOM(DOMElement * dom,bool bindDocument) const62 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const
63 {
64     m_dom = dom;
65     if (dom && bindDocument) {
66         DOMDocument* doc = dom->getOwnerDocument();
67         setDocument(doc);
68         DOMElement* documentRoot = doc->getDocumentElement();
69         if (!documentRoot)
70             doc->appendChild(dom);
71         else if (documentRoot != dom)
72             doc->replaceChild(dom, documentRoot);
73     }
74 }
75 
setDocument(DOMDocument * doc) const76 void AbstractDOMCachingXMLObject::setDocument(DOMDocument* doc) const
77 {
78     if (m_document != doc) {
79         if (m_document)
80             m_document->release();
81         m_document=doc;
82     }
83 }
84 
releaseDOM() const85 void AbstractDOMCachingXMLObject::releaseDOM() const
86 {
87     if (m_dom) {
88         if (m_log.isDebugEnabled()) {
89             string qname=getElementQName().toString();
90             m_log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str());
91         }
92         setDOM(nullptr);
93     }
94 }
95 
releaseParentDOM(bool propagateRelease) const96 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const
97 {
98     if (getParent() && getParent()->getDOM()) {
99         m_log.debug(
100             "releasing cached DOM representation for parent object with propagation set to %s",
101             propagateRelease ? "true" : "false"
102             );
103         getParent()->releaseDOM();
104         if (propagateRelease)
105             getParent()->releaseParentDOM(propagateRelease);
106     }
107 }
108 
109 namespace {
110     class _release : public binary_function<XMLObject*,bool,void> {
111     public:
operator ()(XMLObject * obj,bool propagate) const112         void operator()(XMLObject* obj, bool propagate) const {
113             if (obj) {
114                 obj->releaseDOM();
115                 if (propagate)
116                     obj->releaseChildrenDOM(propagate);
117             }
118         }
119     };
120 };
121 
releaseChildrenDOM(bool propagateRelease) const122 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const
123 {
124     if (hasChildren()) {
125         m_log.debug(
126             "releasing cached DOM representation for children with propagation set to %s",
127             propagateRelease ? "true" : "false"
128             );
129         const list<XMLObject*>& children=getOrderedChildren();
130         for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));
131     }
132 }
133 
cloneDOM(DOMDocument * doc) const134 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const
135 {
136     if (getDOM()) {
137         DOMDocument* cloneDoc = doc;
138         if (!cloneDoc)
139             cloneDoc=DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
140         try {
141             return static_cast<DOMElement*>(cloneDoc->importNode(getDOM(),true));
142         }
143         catch (XMLException& ex) {
144             if (!doc)
145                 cloneDoc->release();
146             auto_ptr_char temp(ex.getMessage());
147             m_log.error("DOM clone failed: %s", temp.get());
148         }
149     }
150     return nullptr;
151 }
152 
clone() const153 XMLObject* AbstractDOMCachingXMLObject::clone() const
154 {
155     // See if we can clone via the DOM.
156     DOMElement* domCopy=cloneDOM();
157     if (domCopy) {
158         // Seemed to work, so now we unmarshall the DOM to produce the clone.
159         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);
160         if (!b) {
161             if (m_log.isErrorEnabled()) {
162                 scoped_ptr<QName> q(XMLHelper::getNodeQName(domCopy));
163                 m_log.error(
164                     "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()
165                    );
166             }
167             domCopy->getOwnerDocument()->release();
168             throw UnmarshallingException("Unable to locate builder for cloned element.");
169         }
170         XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());
171         XMLObject* ret = b->buildFromElement(domCopy,true); // bind document
172         janitor.release(); // safely transferred
173         return ret;
174     }
175     return nullptr;
176 }
177 
detach()178 void AbstractDOMCachingXMLObject::detach()
179 {
180     // This is an override that duplicates some of the checking in the base class but
181     // adds document management in preparation for deletion of the parent.
182 
183     if (!getParent())
184         return;
185 
186     if (getParent()->hasParent())
187         throw XMLObjectException("Cannot detach an object whose parent is itself a child.");
188 
189     AbstractDOMCachingXMLObject* parent = dynamic_cast<AbstractDOMCachingXMLObject*>(getParent());
190     if (parent && parent->m_document) {
191         // Transfer control of document to me...
192         setDocument(parent->m_document);
193         parent->m_document = nullptr;
194     }
195     // The rest is done by the base.
196     AbstractXMLObject::detach();
197 }
198