Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / impl / UnknownElement.cpp
1 /*
2 *  Copyright 2001-2007 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 std;
37 #ifndef XMLTOOLING_NO_XMLSEC
38 using xmlsignature::Signature;
39 #endif
40
41 void UnknownElementImpl::releaseDOM() const
42 {
43 #ifdef _DEBUG
44     xmltooling::NDC ndc("releaseDOM");
45 #endif
46     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
47     log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
48
49     // We're losing our DOM, so assuming we have one, we preserve it.
50     serialize(m_xml);
51
52     // This takes care of the generic housekeeping now that we've preserved things.
53     AbstractDOMCachingXMLObject::releaseDOM();
54 }
55
56 XMLObject* UnknownElementImpl::clone() const
57 {
58     UnknownElementImpl* ret=new UnknownElementImpl();
59
60     // If there's no XML locally, serialize this object into the new one.
61     // Otherwise just copy it over.
62     if (m_xml.empty())
63         serialize(ret->m_xml);
64     else
65         ret->m_xml=m_xml;
66
67     return ret;
68 }
69
70 void UnknownElementImpl::serialize(string& s) const
71 {
72     if (getDOM())
73         XMLHelper::serialize(getDOM(),s);
74 }
75
76 DOMElement* UnknownElementImpl::marshall(
77     DOMDocument* document
78 #ifndef XMLTOOLING_NO_XMLSEC
79     ,const vector<Signature*>* sigs
80     ,const Credential* credential
81 #endif
82     ) const
83 {
84 #ifdef _DEBUG
85     xmltooling::NDC ndc("marshall");
86 #endif
87     
88     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
89     log.debug("marshalling unknown content");
90
91     DOMElement* cachedDOM=getDOM();
92     if (cachedDOM) {
93         if (!document || document==cachedDOM->getOwnerDocument()) {
94             log.debug("XMLObject has a usable cached DOM, reusing it");
95             if (document)
96                 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
97             releaseParentDOM(true);
98             return cachedDOM;
99         }
100         
101         // We have a DOM but it doesn't match the document we were given, so we import
102         // it into the new document.
103         cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
104
105         // Recache the DOM.
106         setDocumentElement(document, cachedDOM);
107         log.debug("caching imported DOM for XMLObject");
108         setDOM(cachedDOM, false);
109         releaseParentDOM(true);
110         return cachedDOM;
111     }
112     
113     // If we get here, we didn't have a usable DOM.
114     // We need to reparse the XML we saved off into a new DOM.
115     bool bindDocument=false;
116     MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
117     Wrapper4InputSource dsrc(&src,false);
118     log.debug("parsing XML back into DOM tree");
119     DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
120     if (document) {
121         // The caller insists on using his own document, so we now have to import the thing
122         // into it. Then we're just dumping the one we built.
123         log.debug("reimporting new DOM into caller-supplied document");
124         cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
125         internalDoc->release();
126     }
127     else {
128         // We just bind the document we built to the object as the result.
129         cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
130         document=internalDoc;
131         bindDocument=true;
132     }
133
134     // Recache the DOM and clear the serialized copy.
135     setDocumentElement(document, cachedDOM);
136     log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
137     setDOM(cachedDOM, bindDocument);
138     releaseParentDOM(true);
139     m_xml.erase();
140     return cachedDOM;
141 }
142
143
144 DOMElement* UnknownElementImpl::marshall(
145     DOMElement* parentElement
146 #ifndef XMLTOOLING_NO_XMLSEC
147     ,const vector<Signature*>* sigs
148     ,const Credential* credential
149 #endif
150     ) const
151 {
152 #ifdef _DEBUG
153     xmltooling::NDC ndc("marshall");
154 #endif
155     
156     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
157     log.debug("marshalling unknown content");
158
159     DOMElement* cachedDOM=getDOM();
160     if (cachedDOM) {
161         if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
162             log.debug("XMLObject has a usable cached DOM, reusing it");
163             parentElement->appendChild(cachedDOM);
164             releaseParentDOM(true);
165             return cachedDOM;
166         }
167         
168         // We have a DOM but it doesn't match the document we were given, so we import
169         // it into the new document.
170         cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
171
172         // Recache the DOM.
173         parentElement->appendChild(cachedDOM);
174         log.debug("caching imported DOM for XMLObject");
175         setDOM(cachedDOM, false);
176         releaseParentDOM(true);
177         return cachedDOM;
178     }
179     
180     // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
181     // We need to reparse the XML we saved off into a new DOM.
182     MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
183     Wrapper4InputSource dsrc(&src,false);
184     log.debug("parsing XML back into DOM tree");
185     DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
186     
187     log.debug("reimporting new DOM into caller-supplied document");
188     cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
189     internalDoc->release();
190
191     // Recache the DOM and clear the serialized copy.
192     parentElement->appendChild(cachedDOM);
193     log.debug("caching DOM for XMLObject");
194     setDOM(cachedDOM, false);
195     releaseParentDOM(true);
196     m_xml.erase();
197     return cachedDOM;
198 }
199
200 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
201 {
202     setDOM(element, bindDocument);
203     return this;
204 }
205
206 XMLObject* UnknownElementBuilder::buildObject(
207     const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
208     ) const {
209     return new UnknownElementImpl(nsURI,localName,prefix);
210 }
211