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;
38 void UnknownElementImpl::releaseDOM() const
41 xmltooling::NDC ndc("releaseDOM");
43 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
44 log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
46 // We're losing our DOM, so assuming we have one, we preserve it.
49 // This takes care of the generic housekeeping now that we've preserved things.
50 AbstractDOMCachingXMLObject::releaseDOM();
53 XMLObject* UnknownElementImpl::clone() const
55 UnknownElementImpl* ret=new UnknownElementImpl();
57 // If there's no XML locally, serialize this object into the new one.
58 // Otherwise just copy it over.
60 serialize(ret->m_xml);
67 void UnknownElementImpl::serialize(string& s) const
70 XMLHelper::serialize(getDOM(),s);
73 DOMElement* UnknownElementImpl::marshall(
75 #ifndef XMLTOOLING_NO_XMLSEC
76 ,const std::vector<xmlsignature::Signature*>* sigs
81 xmltooling::NDC ndc("marshall");
84 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
85 log.debug("marshalling unknown content");
87 DOMElement* cachedDOM=getDOM();
89 if (!document || document==cachedDOM->getOwnerDocument()) {
90 log.debug("XMLObject has a usable cached DOM, reusing it");
92 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
93 releaseParentDOM(true);
97 // We have a DOM but it doesn't match the document we were given, so we import
98 // it into the new document.
99 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
102 setDocumentElement(document, cachedDOM);
103 log.debug("caching imported DOM for XMLObject");
104 setDOM(cachedDOM, false);
105 releaseParentDOM(true);
109 // If we get here, we didn't have a usable DOM.
110 // We need to reparse the XML we saved off into a new DOM.
111 bool bindDocument=false;
112 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
113 Wrapper4InputSource dsrc(&src,false);
114 log.debug("parsing XML back into DOM tree");
115 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
117 // The caller insists on using his own document, so we now have to import the thing
118 // into it. Then we're just dumping the one we built.
119 log.debug("reimporting new DOM into caller-supplied document");
120 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
121 internalDoc->release();
124 // We just bind the document we built to the object as the result.
125 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
126 document=internalDoc;
130 // Recache the DOM and clear the serialized copy.
131 setDocumentElement(document, cachedDOM);
132 log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
133 setDOM(cachedDOM, bindDocument);
134 releaseParentDOM(true);
140 DOMElement* UnknownElementImpl::marshall(
141 DOMElement* parentElement
142 #ifndef XMLTOOLING_NO_XMLSEC
143 ,const std::vector<xmlsignature::Signature*>* sigs
148 xmltooling::NDC ndc("marshall");
151 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject");
152 log.debug("marshalling unknown content");
154 DOMElement* cachedDOM=getDOM();
156 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
157 log.debug("XMLObject has a usable cached DOM, reusing it");
158 parentElement->appendChild(cachedDOM);
159 releaseParentDOM(true);
163 // We have a DOM but it doesn't match the document we were given, so we import
164 // it into the new document.
165 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
168 parentElement->appendChild(cachedDOM);
169 log.debug("caching imported DOM for XMLObject");
170 setDOM(cachedDOM, false);
171 releaseParentDOM(true);
175 // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
176 // We need to reparse the XML we saved off into a new DOM.
177 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
178 Wrapper4InputSource dsrc(&src,false);
179 log.debug("parsing XML back into DOM tree");
180 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
182 log.debug("reimporting new DOM into caller-supplied document");
183 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
184 internalDoc->release();
186 // Recache the DOM and clear the serialized copy.
187 parentElement->appendChild(cachedDOM);
188 log.debug("caching DOM for XMLObject");
189 setDOM(cachedDOM, false);
190 releaseParentDOM(true);
195 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
197 setDOM(element, bindDocument);
201 XMLObject* UnknownElementBuilder::buildObject(
202 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
204 return new UnknownElementImpl(nsURI,localName,prefix);