First cut at signing support.
[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 "exceptions.h"\r
25 #include "AbstractDOMCachingXMLObject.h"\r
26 #include "io/Unmarshaller.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)\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()\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)\r
66 {\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
73                 );\r
74             domCachingParent->releaseDOM();\r
75             if (propagateRelease)\r
76                 domCachingParent->releaseParentDOM(propagateRelease);\r
77         }\r
78     }\r
79 }\r
80 \r
81 class _release : public binary_function<XMLObject*,bool,void> {\r
82 public:\r
83     void operator()(XMLObject* obj, bool propagate) const {\r
84         DOMCachingXMLObject* domCaching = dynamic_cast<DOMCachingXMLObject*>(obj);\r
85         if (domCaching) {\r
86             domCaching->releaseDOM();\r
87             if (propagate)\r
88                 domCaching->releaseChildrenDOM(propagate);\r
89         }\r
90     }\r
91 };\r
92 \r
93 void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease)\r
94 {\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
99             );\r
100         for_each(m_children.begin(),m_children.end(),bind2nd(_release(),propagateRelease));\r
101     }\r
102 }\r
103 \r
104 DOMElement* AbstractDOMCachingXMLObject::cloneDOM(DOMDocument* doc) const\r
105 {\r
106     if (getDOM()) {\r
107         if (!doc)\r
108             doc=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();\r
109         return static_cast<DOMElement*>(doc->importNode(getDOM(),true));\r
110     }\r
111     return NULL;\r
112 }\r
113 \r
114 XMLObject* AbstractDOMCachingXMLObject::clone() const\r
115 {\r
116     // See if we can clone via the DOM.\r
117     DOMElement* domCopy=cloneDOM();\r
118     if (domCopy) {\r
119         // Seemed to work, so now we unmarshall the DOM to produce the clone.\r
120         const Unmarshaller* u=Unmarshaller::getUnmarshaller(domCopy);\r
121         if (!u) {\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
125                 );\r
126             domCopy->getOwnerDocument()->release();\r
127             throw UnmarshallingException("Unable to locate unmarshaller for cloned element.");\r
128         }\r
129         try {\r
130             return u->unmarshall(domCopy, true);    // bind document\r
131         }\r
132         catch (...) {\r
133             domCopy->getOwnerDocument()->release();\r
134         }\r
135     }\r
136     return NULL;\r
137 }\r
138 \r
139 XMLObject* AbstractDOMCachingXMLObject::prepareForAssignment(XMLObject* oldValue, XMLObject* newValue) {\r
140 \r
141     if (newValue && newValue->hasParent())\r
142         throw XMLObjectException("child XMLObject cannot be added - it is already the child of another XMLObject");\r
143 \r
144     if (!oldValue) {\r
145         if (newValue) {\r
146             releaseThisandParentDOM();\r
147             newValue->setParent(this);\r
148             return newValue;\r
149         }\r
150         else {\r
151             return NULL;\r
152         }\r
153     }\r
154 \r
155     if (oldValue != newValue) {\r
156         delete oldValue;\r
157         releaseThisandParentDOM();\r
158         newValue->setParent(this);\r
159     }\r
160 \r
161     return newValue;\r
162 }\r