2 * Copyright 2001-2007 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;
37 #ifndef XMLTOOLING_NO_XMLSEC
38 using xmlsignature::Signature;
41 void UnknownElementImpl::releaseDOM() const
44 xmltooling::NDC ndc("releaseDOM");
46 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
47 log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
49 // We're losing our DOM, so assuming we have one, we preserve it.
52 // This takes care of the generic housekeeping now that we've preserved things.
53 AbstractDOMCachingXMLObject::releaseDOM();
56 XMLObject* UnknownElementImpl::clone() const
58 UnknownElementImpl* ret=new UnknownElementImpl();
60 // If there's no XML locally, serialize this object into the new one.
61 // Otherwise just copy it over.
63 serialize(ret->m_xml);
70 void UnknownElementImpl::serialize(string& s) const
73 XMLHelper::serialize(getDOM(),s);
76 DOMElement* UnknownElementImpl::marshall(
78 #ifndef XMLTOOLING_NO_XMLSEC
79 ,const vector<Signature*>* sigs
80 ,const Credential* credential
85 xmltooling::NDC ndc("marshall");
88 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
89 log.debug("marshalling unknown content");
91 DOMElement* cachedDOM=getDOM();
93 if (!document || document==cachedDOM->getOwnerDocument()) {
94 log.debug("XMLObject has a usable cached DOM, reusing it");
96 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
97 releaseParentDOM(true);
101 // We have a DOM but it doesn't match the document we were given, so we import
102 // it into the new document.
103 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
106 setDocumentElement(document, cachedDOM);
107 log.debug("caching imported DOM for XMLObject");
108 setDOM(cachedDOM, false);
109 releaseParentDOM(true);
113 // If we get here, we didn't have a usable DOM.
114 // We need to reparse the XML we saved off into a new DOM.
115 bool bindDocument=false;
116 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
117 Wrapper4InputSource dsrc(&src,false);
118 log.debug("parsing XML back into DOM tree");
119 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
121 // The caller insists on using his own document, so we now have to import the thing
122 // into it. Then we're just dumping the one we built.
123 log.debug("reimporting new DOM into caller-supplied document");
124 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
125 internalDoc->release();
128 // We just bind the document we built to the object as the result.
129 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
130 document=internalDoc;
134 // Recache the DOM and clear the serialized copy.
135 setDocumentElement(document, cachedDOM);
136 log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
137 setDOM(cachedDOM, bindDocument);
138 releaseParentDOM(true);
144 DOMElement* UnknownElementImpl::marshall(
145 DOMElement* parentElement
146 #ifndef XMLTOOLING_NO_XMLSEC
147 ,const vector<Signature*>* sigs
148 ,const Credential* credential
153 xmltooling::NDC ndc("marshall");
156 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
157 log.debug("marshalling unknown content");
159 DOMElement* cachedDOM=getDOM();
161 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
162 log.debug("XMLObject has a usable cached DOM, reusing it");
163 parentElement->appendChild(cachedDOM);
164 releaseParentDOM(true);
168 // We have a DOM but it doesn't match the document we were given, so we import
169 // it into the new document.
170 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
173 parentElement->appendChild(cachedDOM);
174 log.debug("caching imported DOM for XMLObject");
175 setDOM(cachedDOM, false);
176 releaseParentDOM(true);
180 // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
181 // We need to reparse the XML we saved off into a new DOM.
182 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
183 Wrapper4InputSource dsrc(&src,false);
184 log.debug("parsing XML back into DOM tree");
185 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
187 log.debug("reimporting new DOM into caller-supplied document");
188 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
189 internalDoc->release();
191 // Recache the DOM and clear the serialized copy.
192 parentElement->appendChild(cachedDOM);
193 log.debug("caching DOM for XMLObject");
194 setDOM(cachedDOM, false);
195 releaseParentDOM(true);
200 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
202 setDOM(element, bindDocument);
206 XMLObject* UnknownElementBuilder::buildObject(
207 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
209 return new UnknownElementImpl(nsURI,localName,prefix);