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