Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-xmltooling.git] / xmltooling / impl / UnknownElement.cpp
1 /*
2 *  Copyright 2001-2009 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         cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
133
134         // Recache the DOM.
135         setDocumentElement(document, cachedDOM);
136         log.debug("caching imported DOM for XMLObject");
137         setDOM(cachedDOM, false);
138         releaseParentDOM(true);
139         return cachedDOM;
140     }
141     
142     // If we get here, we didn't have a usable DOM.
143     // We need to reparse the XML we saved off into a new DOM.
144     bool bindDocument=false;
145     MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
146     Wrapper4InputSource dsrc(&src,false);
147     log.debug("parsing XML back into DOM tree");
148     DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
149     if (document) {
150         // The caller insists on using his own document, so we now have to import the thing
151         // into it. Then we're just dumping the one we built.
152         log.debug("reimporting new DOM into caller-supplied document");
153         cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
154         internalDoc->release();
155     }
156     else {
157         // We just bind the document we built to the object as the result.
158         cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
159         document=internalDoc;
160         bindDocument=true;
161     }
162
163     // Recache the DOM and clear the serialized copy.
164     setDocumentElement(document, cachedDOM);
165     log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
166     setDOM(cachedDOM, bindDocument);
167     releaseParentDOM(true);
168     m_xml.erase();
169     return cachedDOM;
170 }
171
172
173 DOMElement* UnknownElementImpl::marshall(
174     DOMElement* parentElement
175 #ifndef XMLTOOLING_NO_XMLSEC
176     ,const vector<Signature*>* sigs
177     ,const Credential* credential
178 #endif
179     ) const
180 {
181 #ifdef _DEBUG
182     xmltooling::NDC ndc("marshall");
183 #endif
184     
185     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
186     log.debug("marshalling unknown content");
187
188     DOMElement* cachedDOM=getDOM();
189     if (cachedDOM) {
190         if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
191             log.debug("XMLObject has a usable cached DOM, reusing it");
192             parentElement->appendChild(cachedDOM);
193             releaseParentDOM(true);
194             return cachedDOM;
195         }
196         
197         // We have a DOM but it doesn't match the document we were given, so we import
198         // it into the new document.
199         cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
200
201         // Recache the DOM.
202         parentElement->appendChild(cachedDOM);
203         log.debug("caching imported DOM for XMLObject");
204         setDOM(cachedDOM, false);
205         releaseParentDOM(true);
206         return cachedDOM;
207     }
208     
209     // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
210     // We need to reparse the XML we saved off into a new DOM.
211     MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
212     Wrapper4InputSource dsrc(&src,false);
213     log.debug("parsing XML back into DOM tree");
214     DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
215     
216     log.debug("reimporting new DOM into caller-supplied document");
217     cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
218     internalDoc->release();
219
220     // Recache the DOM and clear the serialized copy.
221     parentElement->appendChild(cachedDOM);
222     log.debug("caching DOM for XMLObject");
223     setDOM(cachedDOM, false);
224     releaseParentDOM(true);
225     m_xml.erase();
226     return cachedDOM;
227 }
228
229 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
230 {
231     setDOM(element, bindDocument);
232     return this;
233 }
234
235 XMLObject* UnknownElementBuilder::buildObject(
236     const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType
237     ) const
238 {
239     return new UnknownElementImpl(nsURI,localName,prefix);
240 }
241