Moved DOM methods up the tree, add copy c'tors, KeyInfo sample
[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         for_each(m_children.begin(),m_children.end(),bind2nd(_release(),propagateRelease));\r
97     }\r
98 }\r
99 \r
100 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const\r
101 {\r
102     if (getDOM()) {\r
103         if (!doc)\r
104             doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();\r
105         return static_cast<DOMElement*>(doc->importNode(getDOM(),true));\r
106     }\r
107     return NULL;\r
108 }\r
109 \r
110 XMLObject* AbstractDOMCachingXMLObject::clone() const\r
111 {\r
112     // See if we can clone via the DOM.\r
113     DOMElement* domCopy=cloneDOM();\r
114     if (domCopy) {\r
115         // Seemed to work, so now we unmarshall the DOM to produce the clone.\r
116         const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy);\r
117         if (!b) {\r
118             auto_ptr<QName> q(XMLHelper::getNodeQName(domCopy));\r
119             Category::getInstance(XMLTOOLING_LOGCAT".DOM").error(\r
120                 "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str()\r
121                 );\r
122             domCopy->getOwnerDocument()->release();\r
123             throw UnmarshallingException("Unable to locate builder for cloned element.");\r
124         }\r
125         try {\r
126             return b->buildFromElement(domCopy,true); // bind document\r
127         }\r
128         catch (...) {\r
129             domCopy->getOwnerDocument()->release();\r
130             throw;\r
131         }\r
132     }\r
133     return NULL;\r
134 }\r