bee23c2e5375ed1aeda198018f05555c7f3116bc
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractDOMCachingXMLObject.cpp
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 /**\r
18  * AbstractDOMCachingXMLObject.cpp\r
19  * \r
20  * Extension of AbstractXMLObject that implements a DOMCachingXMLObject. \r
21  */\r
22 \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
28 \r
29 #include <algorithm>\r
30 #include <functional>\r
31 #include <log4cpp/Category.hh>\r
32 \r
33 using namespace xmltooling;\r
34 using namespace log4cpp;\r
35 using namespace std;\r
36 \r
37 AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject()\r
38 {\r
39     if (m_document)\r
40         m_document->release();\r
41 }\r
42 \r
43 void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) const\r
44 {\r
45     m_dom=dom;\r
46     if (dom) {\r
47         if (bindDocument) {\r
48             setDocument(dom->getOwnerDocument());\r
49         }\r
50     }\r
51 }\r
52 \r
53 void AbstractDOMCachingXMLObject::releaseDOM() const\r
54 {\r
55     if (m_dom) {\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
60         }\r
61         setDOM(NULL);\r
62     }\r
63 }\r
64 \r
65 void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const\r
66 {\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
71             );\r
72         getParent()->releaseDOM();\r
73         if (propagateRelease)\r
74             getParent()->releaseParentDOM(propagateRelease);\r
75     }\r
76 }\r
77 \r
78 class _release : public binary_function<XMLObject*,bool,void> {\r
79 public:\r
80     void operator()(XMLObject* obj, bool propagate) const {\r
81         if (obj) {\r
82             obj->releaseDOM();\r
83             if (propagate)\r
84                 obj->releaseChildrenDOM(propagate);\r
85         }\r
86     }\r
87 };\r
88 \r
89 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const\r
90 {\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
95             );\r
96         const list<XMLObject*>& children=getOrderedChildren();\r
97         for_each(children.begin(),children.end(),bind2nd(_release(),propagateRelease));\r
98     }\r
99 }\r
100 \r
101 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const\r
102 {\r
103     if (getDOM()) {\r
104         if (!doc)\r
105             doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();\r
106         return static_cast<DOMElement*>(doc->importNode(getDOM(),true));\r
107     }\r
108     return NULL;\r
109 }\r
110 \r
111 XMLObject* AbstractDOMCachingXMLObject::clone() const\r
112 {\r
113     // See if we can clone via the DOM.\r
114     DOMElement* domCopy=cloneDOM();\r
115     if (domCopy) {\r
116         // Seemed to work, so now we unmarshall the DOM to produce the clone.\r
117         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);\r
118         if (!b) {\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
122                 );\r
123             domCopy->getOwnerDocument()->release();\r
124             throw UnmarshallingException("Unable to locate builder for cloned element.");\r
125         }\r
126         XercesJanitor<DOMDocument> janitor(domCopy->getOwnerDocument());\r
127         XMLObject* ret = b->buildFromElement(domCopy,true); // bind document\r
128         janitor.release(); // safely transferred\r
129         return ret;\r
130     }\r
131     return NULL;\r
132 }\r
133 \r
134 void AbstractDOMCachingXMLObject::detach()\r
135 {\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
138 \r
139     if (!getParent())\r
140         return;\r
141 \r
142     if (getParent()->hasParent())\r
143         throw XMLObjectException("Cannot detach an object whose parent is itself a child.");\r
144 \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
150     }\r
151     // The rest is done by the base.\r
152     AbstractXMLObject::detach();\r
153 }\r