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"
25 #include "impl/UnknownElement.h"
27 #include "util/XMLHelper.h"
29 #include <log4cpp/Category.hh>
30 #include <xercesc/framework/MemBufInputSource.hpp>
31 #include <xercesc/framework/Wrapper4InputSource.hpp>
32 #include <xercesc/util/XMLUniDefs.hpp>
34 using namespace xmltooling;
35 using namespace log4cpp;
37 using xmlsignature::Signature;
39 void UnknownElementImpl::releaseDOM() const
42 xmltooling::NDC ndc("releaseDOM");
44 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
45 log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
47 // We're losing our DOM, so assuming we have one, we preserve it.
50 // This takes care of the generic housekeeping now that we've preserved things.
51 AbstractDOMCachingXMLObject::releaseDOM();
54 XMLObject* UnknownElementImpl::clone() const
56 UnknownElementImpl* ret=new UnknownElementImpl();
58 // If there's no XML locally, serialize this object into the new one.
59 // Otherwise just copy it over.
61 serialize(ret->m_xml);
68 void UnknownElementImpl::serialize(string& s) const
71 XMLHelper::serialize(getDOM(),s);
74 DOMElement* UnknownElementImpl::marshall(
76 #ifndef XMLTOOLING_NO_XMLSEC
77 ,const vector<Signature*>* sigs
78 ,const Credential* credential
83 xmltooling::NDC ndc("marshall");
86 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
87 log.debug("marshalling unknown content");
89 DOMElement* cachedDOM=getDOM();
91 if (!document || document==cachedDOM->getOwnerDocument()) {
92 log.debug("XMLObject has a usable cached DOM, reusing it");
94 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
95 releaseParentDOM(true);
99 // We have a DOM but it doesn't match the document we were given, so we import
100 // it into the new document.
101 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
104 setDocumentElement(document, cachedDOM);
105 log.debug("caching imported DOM for XMLObject");
106 setDOM(cachedDOM, false);
107 releaseParentDOM(true);
111 // If we get here, we didn't have a usable DOM.
112 // We need to reparse the XML we saved off into a new DOM.
113 bool bindDocument=false;
114 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
115 Wrapper4InputSource dsrc(&src,false);
116 log.debug("parsing XML back into DOM tree");
117 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
119 // The caller insists on using his own document, so we now have to import the thing
120 // into it. Then we're just dumping the one we built.
121 log.debug("reimporting new DOM into caller-supplied document");
122 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
123 internalDoc->release();
126 // We just bind the document we built to the object as the result.
127 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
128 document=internalDoc;
132 // Recache the DOM and clear the serialized copy.
133 setDocumentElement(document, cachedDOM);
134 log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
135 setDOM(cachedDOM, bindDocument);
136 releaseParentDOM(true);
142 DOMElement* UnknownElementImpl::marshall(
143 DOMElement* parentElement
144 #ifndef XMLTOOLING_NO_XMLSEC
145 ,const vector<Signature*>* sigs
146 ,const Credential* credential
151 xmltooling::NDC ndc("marshall");
154 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
155 log.debug("marshalling unknown content");
157 DOMElement* cachedDOM=getDOM();
159 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
160 log.debug("XMLObject has a usable cached DOM, reusing it");
161 parentElement->appendChild(cachedDOM);
162 releaseParentDOM(true);
166 // We have a DOM but it doesn't match the document we were given, so we import
167 // it into the new document.
168 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
171 parentElement->appendChild(cachedDOM);
172 log.debug("caching imported DOM for XMLObject");
173 setDOM(cachedDOM, false);
174 releaseParentDOM(true);
178 // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
179 // We need to reparse the XML we saved off into a new DOM.
180 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
181 Wrapper4InputSource dsrc(&src,false);
182 log.debug("parsing XML back into DOM tree");
183 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
185 log.debug("reimporting new DOM into caller-supplied document");
186 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
187 internalDoc->release();
189 // Recache the DOM and clear the serialized copy.
190 parentElement->appendChild(cachedDOM);
191 log.debug("caching DOM for XMLObject");
192 setDOM(cachedDOM, false);
193 releaseParentDOM(true);
198 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
200 setDOM(element, bindDocument);
204 XMLObject* UnknownElementBuilder::buildObject(
205 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
207 return new UnknownElementImpl(nsURI,localName,prefix);