2 * Copyright 2001-2009 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.
132 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
135 setDocumentElement(document, cachedDOM);
136 log.debug("caching imported DOM for XMLObject");
137 setDOM(cachedDOM, false);
138 releaseParentDOM(true);
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);
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();
157 // We just bind the document we built to the object as the result.
158 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
159 document=internalDoc;
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);
173 DOMElement* UnknownElementImpl::marshall(
174 DOMElement* parentElement
175 #ifndef XMLTOOLING_NO_XMLSEC
176 ,const vector<Signature*>* sigs
177 ,const Credential* credential
182 xmltooling::NDC ndc("marshall");
185 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
186 log.debug("marshalling unknown content");
188 DOMElement* cachedDOM=getDOM();
190 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
191 log.debug("XMLObject has a usable cached DOM, reusing it");
192 parentElement->appendChild(cachedDOM);
193 releaseParentDOM(true);
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));
202 parentElement->appendChild(cachedDOM);
203 log.debug("caching imported DOM for XMLObject");
204 setDOM(cachedDOM, false);
205 releaseParentDOM(true);
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);
216 log.debug("reimporting new DOM into caller-supplied document");
217 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
218 internalDoc->release();
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);
229 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
231 setDOM(element, bindDocument);
235 XMLObject* UnknownElementBuilder::buildObject(
236 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType
239 return new UnknownElementImpl(nsURI,localName,prefix);