First cut at signing support.
[shibboleth/cpp-xmltooling.git] / xmltooling / impl / UnknownElement.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  * UnknownElement.cpp\r
19  * \r
20  * Basic implementations suitable for use as defaults for unrecognized content\r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "exceptions.h"\r
25 #include "impl/UnknownElement.h"\r
26 #include "util/NDC.h"\r
27 #include "util/XMLHelper.h"\r
28 \r
29 #include <log4cpp/Category.hh>\r
30 #include <xercesc/framework/MemBufInputSource.hpp>\r
31 #include <xercesc/framework/Wrapper4InputSource.hpp>\r
32 #include <xercesc/util/XMLUniDefs.hpp>\r
33 \r
34 using namespace xmltooling;\r
35 using namespace log4cpp;\r
36 using namespace std;\r
37 \r
38 void UnknownElementImpl::releaseDOM()\r
39 {\r
40 #ifdef _DEBUG\r
41     xmltooling::NDC ndc("releaseDOM");\r
42 #endif\r
43     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".UnknownElementImpl");\r
44     log.debug("releasing DOM for unknown content, preserving current DOM in XML form");\r
45 \r
46     // We're losing our DOM, so assuming we have one, we preserve it.\r
47     serialize(m_xml);\r
48 \r
49     // This takes care of the generic housekeeping now that we've preserved things.\r
50     AbstractDOMCachingXMLObject::releaseDOM();\r
51 }\r
52 \r
53 XMLObject* UnknownElementImpl::clone() const\r
54 {\r
55     UnknownElementImpl* ret=new UnknownElementImpl();\r
56 \r
57     // If there's no XML locally, serialize this object into the new one.\r
58     // Otherwise just copy it over.\r
59     if (m_xml.empty())\r
60         serialize(ret->m_xml);\r
61     else\r
62         ret->m_xml=m_xml;\r
63 \r
64     return ret;\r
65 }\r
66 \r
67 void UnknownElementImpl::serialize(string& s) const\r
68 {\r
69     if (getDOM())\r
70         XMLHelper::serialize(getDOM(),s);\r
71 }\r
72 \r
73 DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMDocument* document, MarshallingContext* ctx) const\r
74 {\r
75 #ifdef _DEBUG\r
76     xmltooling::NDC ndc("marshall");\r
77 #endif\r
78     \r
79     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Marshaller");\r
80     log.debug("marshalling unknown content");\r
81 \r
82     UnknownElementImpl* unk=dynamic_cast<UnknownElementImpl*>(xmlObject);\r
83     if (!unk)\r
84         throw MarshallingException("Only objects of class UnknownElementImpl can be marshalled.");\r
85     \r
86     DOMElement* cachedDOM=unk->getDOM();\r
87     if (cachedDOM) {\r
88         if (!document || document==cachedDOM->getOwnerDocument()) {\r
89             log.debug("XMLObject has a usable cached DOM, reusing it");\r
90             if (document)\r
91                 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);\r
92             unk->releaseParentDOM(true);\r
93             return cachedDOM;\r
94         }\r
95         \r
96         // We have a DOM but it doesn't match the document we were given, so we import\r
97         // it into the new document.\r
98         cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));\r
99 \r
100         // Recache the DOM.\r
101         setDocumentElement(document, cachedDOM);\r
102         log.debug("caching imported DOM for XMLObject");\r
103         unk->setDOM(cachedDOM, false);\r
104         unk->releaseParentDOM(true);\r
105         return cachedDOM;\r
106     }\r
107     \r
108     // If we get here, we didn't have a usable DOM.\r
109     // We need to reparse the XML we saved off into a new DOM.\r
110     bool bindDocument=false;\r
111     MemBufInputSource src(reinterpret_cast<const XMLByte*>(unk->m_xml.c_str()),unk->m_xml.length(),"UnknownElementImpl");\r
112     Wrapper4InputSource dsrc(&src,false);\r
113     log.debug("parsing XML back into DOM tree");\r
114     DOMDocument* internalDoc=XMLToolingInternalConfig::getInternalConfig().m_parserPool->parse(dsrc);\r
115     if (document) {\r
116         // The caller insists on using his own document, so we now have to import the thing\r
117         // into it. Then we're just dumping the one we built.\r
118         log.debug("reimporting new DOM into caller-supplied document");\r
119         cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));\r
120         internalDoc->release();\r
121     }\r
122     else {\r
123         // We just bind the document we built to the object as the result.\r
124         cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());\r
125         document=internalDoc;\r
126         bindDocument=true;\r
127     }\r
128 \r
129     // Recache the DOM and clear the serialized copy.\r
130     setDocumentElement(document, cachedDOM);\r
131     log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");\r
132     unk->setDOM(cachedDOM, bindDocument);\r
133     unk->releaseParentDOM(true);\r
134     unk->m_xml.erase();\r
135     return cachedDOM;\r
136 }\r
137 \r
138 DOMElement* UnknownElementMarshaller::marshall(XMLObject* xmlObject, DOMElement* parentElement, MarshallingContext* ctx) const\r
139 {\r
140 #ifdef _DEBUG\r
141     xmltooling::NDC ndc("marshall");\r
142 #endif\r
143     \r
144     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Marshaller");\r
145     log.debug("marshalling unknown content");\r
146 \r
147     UnknownElementImpl* unk=dynamic_cast<UnknownElementImpl*>(xmlObject);\r
148     if (!unk)\r
149         throw MarshallingException("Only objects of class UnknownElementImpl can be marshalled.");\r
150     \r
151     DOMElement* cachedDOM=unk->getDOM();\r
152     if (cachedDOM) {\r
153         if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {\r
154             log.debug("XMLObject has a usable cached DOM, reusing it");\r
155             parentElement->appendChild(cachedDOM);\r
156             unk->releaseParentDOM(true);\r
157             return cachedDOM;\r
158         }\r
159         \r
160         // We have a DOM but it doesn't match the document we were given, so we import\r
161         // it into the new document.\r
162         cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));\r
163 \r
164         // Recache the DOM.\r
165         parentElement->appendChild(cachedDOM);\r
166         log.debug("caching imported DOM for XMLObject");\r
167         unk->setDOM(cachedDOM, false);\r
168         unk->releaseParentDOM(true);\r
169         return cachedDOM;\r
170     }\r
171     \r
172     // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).\r
173     // We need to reparse the XML we saved off into a new DOM.\r
174     MemBufInputSource src(reinterpret_cast<const XMLByte*>(unk->m_xml.c_str()),unk->m_xml.length(),"UnknownElementImpl");\r
175     Wrapper4InputSource dsrc(&src,false);\r
176     log.debug("parsing XML back into DOM tree");\r
177     DOMDocument* internalDoc=XMLToolingInternalConfig::getInternalConfig().m_parserPool->parse(dsrc);\r
178     \r
179     log.debug("reimporting new DOM into caller-supplied document");\r
180     cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));\r
181     internalDoc->release();\r
182 \r
183     // Recache the DOM and clear the serialized copy.\r
184     parentElement->appendChild(cachedDOM);\r
185     log.debug("caching DOM for XMLObject");\r
186     unk->setDOM(cachedDOM, false);\r
187     unk->releaseParentDOM(true);\r
188     unk->m_xml.erase();\r
189     return cachedDOM;\r
190 }\r
191 \r
192 XMLObject* UnknownElementUnmarshaller::unmarshall(DOMElement* element, bool bindDocument) const\r
193 {\r
194     UnknownElementImpl* ret=new UnknownElementImpl();\r
195     ret->setDOM(element, bindDocument);\r
196     return ret;\r
197 }\r