2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Basic implementation suitable for use as default for unrecognized content.
24 #include "exceptions.h"
26 #include "impl/UnknownElement.h"
28 #include "util/XMLHelper.h"
30 #include <xercesc/framework/MemBufInputSource.hpp>
31 #include <xercesc/framework/Wrapper4InputSource.hpp>
32 #include <xercesc/util/XMLUniDefs.hpp>
34 using namespace xmltooling::logging;
35 using namespace xmltooling;
36 using namespace xercesc;
38 #ifndef XMLTOOLING_NO_XMLSEC
39 using xmlsignature::Signature;
42 UnknownElementImpl::UnknownElementImpl(const XMLCh* namespaceURI, const XMLCh* elementLocalName, const XMLCh* namespacePrefix)
43 : AbstractXMLObject(namespaceURI, elementLocalName, namespacePrefix)
47 UnknownElementImpl::~UnknownElementImpl()
51 void UnknownElementImpl::releaseDOM() const
54 xmltooling::NDC ndc("releaseDOM");
56 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
57 log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
59 // We're losing our DOM, so assuming we have one, we preserve it.
62 // This takes care of the generic housekeeping now that we've preserved things.
63 AbstractDOMCachingXMLObject::releaseDOM();
66 XMLObject* UnknownElementImpl::clone() const
68 UnknownElementImpl* ret=new UnknownElementImpl();
70 // If there's no XML locally, serialize this object into the new one.
71 // Otherwise just copy it over.
73 serialize(ret->m_xml);
80 const XMLCh* UnknownElementImpl::getTextContent(unsigned int position) const
82 throw XMLObjectException("Direct access to content is not permitted.");
85 void UnknownElementImpl::setTextContent(const XMLCh*, unsigned int position)
87 throw XMLObjectException("Direct access to content is not permitted.");
90 void UnknownElementImpl::setDocumentElement(DOMDocument* document, DOMElement* element) const
92 DOMElement* documentRoot = document->getDocumentElement();
94 document->replaceChild(element, documentRoot);
96 document->appendChild(element);
99 void UnknownElementImpl::serialize(string& s) const
102 XMLHelper::serialize(getDOM(),s);
105 DOMElement* UnknownElementImpl::marshall(
106 DOMDocument* document
107 #ifndef XMLTOOLING_NO_XMLSEC
108 ,const vector<Signature*>* sigs
109 ,const Credential* credential
114 xmltooling::NDC ndc("marshall");
117 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
118 log.debug("marshalling unknown content");
120 DOMElement* cachedDOM=getDOM();
122 if (!document || document==cachedDOM->getOwnerDocument()) {
123 log.debug("XMLObject has a usable cached DOM, reusing it");
125 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
126 releaseParentDOM(true);
130 // We have a DOM but it doesn't match the document we were given, so we import
131 // it into the new document.
133 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
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")
143 setDocumentElement(document, cachedDOM);
144 log.debug("caching imported DOM for XMLObject");
145 setDOM(cachedDOM, false);
146 releaseParentDOM(true);
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);
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");
162 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
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")
171 internalDoc->release();
174 // We just bind the document we built to the object as the result.
175 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
176 document=internalDoc;
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);
190 DOMElement* UnknownElementImpl::marshall(
191 DOMElement* parentElement
192 #ifndef XMLTOOLING_NO_XMLSEC
193 ,const vector<Signature*>* sigs
194 ,const Credential* credential
199 xmltooling::NDC ndc("marshall");
202 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
203 log.debug("marshalling unknown content");
205 DOMElement* cachedDOM=getDOM();
207 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
208 log.debug("XMLObject has a usable cached DOM, reusing it");
209 parentElement->appendChild(cachedDOM);
210 releaseParentDOM(true);
214 // We have a DOM but it doesn't match the document we were given, so we import
215 // it into the new document.
217 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
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")
227 parentElement->appendChild(cachedDOM);
228 log.debug("caching imported DOM for XMLObject");
229 setDOM(cachedDOM, false);
230 releaseParentDOM(true);
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);
241 log.debug("reimporting new DOM into caller-supplied document");
243 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
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")
252 internalDoc->release();
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);
263 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
265 setDOM(element, bindDocument);
269 XMLObject* UnknownElementBuilder::buildObject(
270 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType
273 return new UnknownElementImpl(nsURI,localName,prefix);