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;
36 using namespace xercesc;
38 #ifndef XMLTOOLING_NO_XMLSEC
39 using xmlsignature::Signature;
42 void UnknownElementImpl::releaseDOM() const
45 xmltooling::NDC ndc("releaseDOM");
47 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
48 log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
50 // We're losing our DOM, so assuming we have one, we preserve it.
53 // This takes care of the generic housekeeping now that we've preserved things.
54 AbstractDOMCachingXMLObject::releaseDOM();
57 XMLObject* UnknownElementImpl::clone() const
59 UnknownElementImpl* ret=new UnknownElementImpl();
61 // If there's no XML locally, serialize this object into the new one.
62 // Otherwise just copy it over.
64 serialize(ret->m_xml);
71 void UnknownElementImpl::serialize(string& s) const
74 XMLHelper::serialize(getDOM(),s);
77 DOMElement* UnknownElementImpl::marshall(
79 #ifndef XMLTOOLING_NO_XMLSEC
80 ,const vector<Signature*>* sigs
81 ,const Credential* credential
86 xmltooling::NDC ndc("marshall");
89 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
90 log.debug("marshalling unknown content");
92 DOMElement* cachedDOM=getDOM();
94 if (!document || document==cachedDOM->getOwnerDocument()) {
95 log.debug("XMLObject has a usable cached DOM, reusing it");
97 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
98 releaseParentDOM(true);
102 // We have a DOM but it doesn't match the document we were given, so we import
103 // it into the new document.
104 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
107 setDocumentElement(document, cachedDOM);
108 log.debug("caching imported DOM for XMLObject");
109 setDOM(cachedDOM, false);
110 releaseParentDOM(true);
114 // If we get here, we didn't have a usable DOM.
115 // We need to reparse the XML we saved off into a new DOM.
116 bool bindDocument=false;
117 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
118 Wrapper4InputSource dsrc(&src,false);
119 log.debug("parsing XML back into DOM tree");
120 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
122 // The caller insists on using his own document, so we now have to import the thing
123 // into it. Then we're just dumping the one we built.
124 log.debug("reimporting new DOM into caller-supplied document");
125 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
126 internalDoc->release();
129 // We just bind the document we built to the object as the result.
130 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
131 document=internalDoc;
135 // Recache the DOM and clear the serialized copy.
136 setDocumentElement(document, cachedDOM);
137 log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
138 setDOM(cachedDOM, bindDocument);
139 releaseParentDOM(true);
145 DOMElement* UnknownElementImpl::marshall(
146 DOMElement* parentElement
147 #ifndef XMLTOOLING_NO_XMLSEC
148 ,const vector<Signature*>* sigs
149 ,const Credential* credential
154 xmltooling::NDC ndc("marshall");
157 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
158 log.debug("marshalling unknown content");
160 DOMElement* cachedDOM=getDOM();
162 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
163 log.debug("XMLObject has a usable cached DOM, reusing it");
164 parentElement->appendChild(cachedDOM);
165 releaseParentDOM(true);
169 // We have a DOM but it doesn't match the document we were given, so we import
170 // it into the new document.
171 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
174 parentElement->appendChild(cachedDOM);
175 log.debug("caching imported DOM for XMLObject");
176 setDOM(cachedDOM, false);
177 releaseParentDOM(true);
181 // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
182 // We need to reparse the XML we saved off into a new DOM.
183 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
184 Wrapper4InputSource dsrc(&src,false);
185 log.debug("parsing XML back into DOM tree");
186 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
188 log.debug("reimporting new DOM into caller-supplied document");
189 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
190 internalDoc->release();
192 // Recache the DOM and clear the serialized copy.
193 parentElement->appendChild(cachedDOM);
194 log.debug("caching DOM for XMLObject");
195 setDOM(cachedDOM, false);
196 releaseParentDOM(true);
201 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
203 setDOM(element, bindDocument);
207 XMLObject* UnknownElementBuilder::buildObject(
208 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const xmltooling::QName* schemaType
210 return new UnknownElementImpl(nsURI,localName,prefix);