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