1cf0373503ef60febbf6bedd305ffd6bb4ae7e90
[shibboleth/cpp-xmltooling.git] / xmltooling / impl / UnknownElement.cpp
1 /*
2 *  Copyright 2001-2010 Internet2
3  * 
4 * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * UnknownElement.cpp
19  * 
20  * Basic implementation suitable for use as default for unrecognized content.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "logging.h"
26 #include "impl/UnknownElement.h"
27 #include "util/NDC.h"
28 #include "util/XMLHelper.h"
29
30 #include <xercesc/framework/MemBufInputSource.hpp>
31 #include <xercesc/framework/Wrapper4InputSource.hpp>
32 #include <xercesc/util/XMLUniDefs.hpp>
33
34 using namespace xmltooling::logging;
35 using namespace xmltooling;
36 using namespace xercesc;
37 using namespace std;
38 #ifndef XMLTOOLING_NO_XMLSEC
39 using xmlsignature::Signature;
40 #endif
41
42 UnknownElementImpl::UnknownElementImpl(const XMLCh* namespaceURI, const XMLCh* elementLocalName, const XMLCh* namespacePrefix)
43     : AbstractXMLObject(namespaceURI, elementLocalName, namespacePrefix)
44 {
45 }
46
47 UnknownElementImpl::~UnknownElementImpl()
48 {
49 }
50
51 void UnknownElementImpl::releaseDOM() const
52 {
53 #ifdef _DEBUG
54     xmltooling::NDC ndc("releaseDOM");
55 #endif
56     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
57     log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
58
59     // We're losing our DOM, so assuming we have one, we preserve it.
60     serialize(m_xml);
61
62     // This takes care of the generic housekeeping now that we've preserved things.
63     AbstractDOMCachingXMLObject::releaseDOM();
64 }
65
66 XMLObject* UnknownElementImpl::clone() const
67 {
68     UnknownElementImpl* ret=new UnknownElementImpl();
69
70     // If there's no XML locally, serialize this object into the new one.
71     // Otherwise just copy it over.
72     if (m_xml.empty())
73         serialize(ret->m_xml);
74     else
75         ret->m_xml=m_xml;
76
77     return ret;
78 }
79
80 const XMLCh* UnknownElementImpl::getTextContent(unsigned int position) const
81 {
82     throw XMLObjectException("Direct access to content is not permitted.");
83 }
84
85 void UnknownElementImpl::setTextContent(const XMLCh*, unsigned int position)
86 {
87     throw XMLObjectException("Direct access to content is not permitted.");
88 }
89
90 void UnknownElementImpl::setDocumentElement(DOMDocument* document, DOMElement* element) const
91 {
92     DOMElement* documentRoot = document->getDocumentElement();
93     if (documentRoot)
94         document->replaceChild(element, documentRoot);
95     else
96         document->appendChild(element);
97 }
98
99 void UnknownElementImpl::serialize(string& s) const
100 {
101     if (getDOM())
102         XMLHelper::serialize(getDOM(),s);
103 }
104
105 DOMElement* UnknownElementImpl::marshall(
106     DOMDocument* document
107 #ifndef XMLTOOLING_NO_XMLSEC
108     ,const vector<Signature*>* sigs
109     ,const Credential* credential
110 #endif
111     ) const
112 {
113 #ifdef _DEBUG
114     xmltooling::NDC ndc("marshall");
115 #endif
116     
117     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
118     log.debug("marshalling unknown content");
119
120     DOMElement* cachedDOM=getDOM();
121     if (cachedDOM) {
122         if (!document || document==cachedDOM->getOwnerDocument()) {
123             log.debug("XMLObject has a usable cached DOM, reusing it");
124             if (document)
125                 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
126             releaseParentDOM(true);
127             return cachedDOM;
128         }
129         
130         // We have a DOM but it doesn't match the document we were given, so we import
131         // it into the new document.
132         try {
133             cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
134         }
135         catch (XMLException& ex) {
136             auto_ptr_char temp(ex.getMessage());
137             throw XMLParserException(
138                 string("Error importing DOM into caller-supplied document: ") + (temp.get() ? temp.get() : "no message")
139                 );
140         }
141
142         // Recache the DOM.
143         setDocumentElement(document, cachedDOM);
144         log.debug("caching imported DOM for XMLObject");
145         setDOM(cachedDOM, false);
146         releaseParentDOM(true);
147         return cachedDOM;
148     }
149     
150     // If we get here, we didn't have a usable DOM.
151     // We need to reparse the XML we saved off into a new DOM.
152     bool bindDocument=false;
153     MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
154     Wrapper4InputSource dsrc(&src,false);
155     log.debug("parsing XML back into DOM tree");
156     DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
157     if (document) {
158         // The caller insists on using his own document, so we now have to import the thing
159         // into it. Then we're just dumping the one we built.
160         log.debug("reimporting new DOM into caller-supplied document");
161         try {
162             cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
163         }
164         catch (XMLException& ex) {
165             internalDoc->release();
166             auto_ptr_char temp(ex.getMessage());
167             throw XMLParserException(
168                 string("Error importing DOM into caller-supplied document: ") + (temp.get() ? temp.get() : "no message")
169                 );
170         }
171         internalDoc->release();
172     }
173     else {
174         // We just bind the document we built to the object as the result.
175         cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
176         document=internalDoc;
177         bindDocument=true;
178     }
179
180     // Recache the DOM and clear the serialized copy.
181     setDocumentElement(document, cachedDOM);
182     log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
183     setDOM(cachedDOM, bindDocument);
184     releaseParentDOM(true);
185     m_xml.erase();
186     return cachedDOM;
187 }
188
189
190 DOMElement* UnknownElementImpl::marshall(
191     DOMElement* parentElement
192 #ifndef XMLTOOLING_NO_XMLSEC
193     ,const vector<Signature*>* sigs
194     ,const Credential* credential
195 #endif
196     ) const
197 {
198 #ifdef _DEBUG
199     xmltooling::NDC ndc("marshall");
200 #endif
201     
202     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
203     log.debug("marshalling unknown content");
204
205     DOMElement* cachedDOM=getDOM();
206     if (cachedDOM) {
207         if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
208             log.debug("XMLObject has a usable cached DOM, reusing it");
209             parentElement->appendChild(cachedDOM);
210             releaseParentDOM(true);
211             return cachedDOM;
212         }
213         
214         // We have a DOM but it doesn't match the document we were given, so we import
215         // it into the new document.
216         try {
217             cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
218         }
219         catch (XMLException& ex) {
220             auto_ptr_char temp(ex.getMessage());
221             throw XMLParserException(
222                 string("Error importing DOM into caller-supplied document: ") + (temp.get() ? temp.get() : "no message")
223                 );
224         }
225
226         // Recache the DOM.
227         parentElement->appendChild(cachedDOM);
228         log.debug("caching imported DOM for XMLObject");
229         setDOM(cachedDOM, false);
230         releaseParentDOM(true);
231         return cachedDOM;
232     }
233     
234     // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
235     // We need to reparse the XML we saved off into a new DOM.
236     MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
237     Wrapper4InputSource dsrc(&src,false);
238     log.debug("parsing XML back into DOM tree");
239     DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
240     
241     log.debug("reimporting new DOM into caller-supplied document");
242     try {
243         cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
244     }
245     catch (XMLException& ex) {
246         internalDoc->release();
247         auto_ptr_char temp(ex.getMessage());
248         throw XMLParserException(
249             string("Error importing DOM into caller-supplied document: ") + (temp.get() ? temp.get() : "no message")
250             );
251     }
252     internalDoc->release();
253
254     // Recache the DOM and clear the serialized copy.
255     parentElement->appendChild(cachedDOM);
256     log.debug("caching DOM for XMLObject");
257     setDOM(cachedDOM, false);
258     releaseParentDOM(true);
259     m_xml.erase();
260     return cachedDOM;
261 }
262
263 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
264 {
265     setDOM(element, bindDocument);
266     return this;
267 }
268
269 XMLObject* UnknownElementBuilder::buildObject(
270     const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType
271     ) const
272 {
273     return new UnknownElementImpl(nsURI,localName,prefix);
274 }
275