2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * UnknownElement.cpp
\r
20 * Basic implementation suitable for use as default for unrecognized content
\r
23 #include "internal.h"
\r
24 #include "exceptions.h"
\r
25 #include "impl/UnknownElement.h"
\r
26 #include "util/NDC.h"
\r
27 #include "util/XMLHelper.h"
\r
29 #include <log4cpp/Category.hh>
\r
30 #include <xercesc/framework/MemBufInputSource.hpp>
\r
31 #include <xercesc/framework/Wrapper4InputSource.hpp>
\r
32 #include <xercesc/util/XMLUniDefs.hpp>
\r
34 using namespace xmltooling;
\r
35 using namespace log4cpp;
\r
36 using namespace std;
\r
38 void UnknownElementImpl::releaseDOM()
\r
41 xmltooling::NDC ndc("releaseDOM");
\r
43 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".UnknownElementImpl");
\r
44 log.debug("releasing DOM for unknown content, preserving current DOM in XML form");
\r
46 // We're losing our DOM, so assuming we have one, we preserve it.
\r
49 // This takes care of the generic housekeeping now that we've preserved things.
\r
50 AbstractDOMCachingXMLObject::releaseDOM();
\r
53 XMLObject* UnknownElementImpl::clone() const
\r
55 UnknownElementImpl* ret=new UnknownElementImpl();
\r
57 // If there's no XML locally, serialize this object into the new one.
\r
58 // Otherwise just copy it over.
\r
60 serialize(ret->m_xml);
\r
67 void UnknownElementImpl::serialize(string& s) const
\r
70 XMLHelper::serialize(getDOM(),s);
\r
73 DOMElement* UnknownElementImpl::marshall(DOMDocument* document, MarshallingContext* ctx) const
\r
76 xmltooling::NDC ndc("marshall");
\r
79 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Marshaller");
\r
80 log.debug("marshalling unknown content");
\r
82 DOMElement* cachedDOM=getDOM();
\r
84 if (!document || document==cachedDOM->getOwnerDocument()) {
\r
85 log.debug("XMLObject has a usable cached DOM, reusing it");
\r
87 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
\r
88 releaseParentDOM(true);
\r
92 // We have a DOM but it doesn't match the document we were given, so we import
\r
93 // it into the new document.
\r
94 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
\r
97 setDocumentElement(document, cachedDOM);
\r
98 log.debug("caching imported DOM for XMLObject");
\r
99 setDOM(cachedDOM, false);
\r
100 releaseParentDOM(true);
\r
104 // If we get here, we didn't have a usable DOM.
\r
105 // We need to reparse the XML we saved off into a new DOM.
\r
106 bool bindDocument=false;
\r
107 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
\r
108 Wrapper4InputSource dsrc(&src,false);
\r
109 log.debug("parsing XML back into DOM tree");
\r
110 DOMDocument* internalDoc=XMLToolingInternalConfig::getInternalConfig().m_parserPool->parse(dsrc);
\r
112 // The caller insists on using his own document, so we now have to import the thing
\r
113 // into it. Then we're just dumping the one we built.
\r
114 log.debug("reimporting new DOM into caller-supplied document");
\r
115 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
\r
116 internalDoc->release();
\r
119 // We just bind the document we built to the object as the result.
\r
120 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
\r
121 document=internalDoc;
\r
125 // Recache the DOM and clear the serialized copy.
\r
126 setDocumentElement(document, cachedDOM);
\r
127 log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
\r
128 setDOM(cachedDOM, bindDocument);
\r
129 releaseParentDOM(true);
\r
134 DOMElement* UnknownElementImpl::marshall(DOMElement* parentElement, MarshallingContext* ctx) const
\r
137 xmltooling::NDC ndc("marshall");
\r
140 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Marshaller");
\r
141 log.debug("marshalling unknown content");
\r
143 DOMElement* cachedDOM=getDOM();
\r
145 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
\r
146 log.debug("XMLObject has a usable cached DOM, reusing it");
\r
147 parentElement->appendChild(cachedDOM);
\r
148 releaseParentDOM(true);
\r
152 // We have a DOM but it doesn't match the document we were given, so we import
\r
153 // it into the new document.
\r
154 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
\r
156 // Recache the DOM.
\r
157 parentElement->appendChild(cachedDOM);
\r
158 log.debug("caching imported DOM for XMLObject");
\r
159 setDOM(cachedDOM, false);
\r
160 releaseParentDOM(true);
\r
164 // If we get here, we didn't have a usable DOM (and/or we flushed the one we had).
\r
165 // We need to reparse the XML we saved off into a new DOM.
\r
166 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"UnknownElementImpl");
\r
167 Wrapper4InputSource dsrc(&src,false);
\r
168 log.debug("parsing XML back into DOM tree");
\r
169 DOMDocument* internalDoc=XMLToolingInternalConfig::getInternalConfig().m_parserPool->parse(dsrc);
\r
171 log.debug("reimporting new DOM into caller-supplied document");
\r
172 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(), true));
\r
173 internalDoc->release();
\r
175 // Recache the DOM and clear the serialized copy.
\r
176 parentElement->appendChild(cachedDOM);
\r
177 log.debug("caching DOM for XMLObject");
\r
178 setDOM(cachedDOM, false);
\r
179 releaseParentDOM(true);
\r
184 XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument)
\r
186 setDOM(element, bindDocument);
\r
190 XMLObject* UnknownElementBuilder::buildObject(
\r
191 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
\r
193 return new UnknownElementImpl(nsURI,localName,prefix);
\r