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 * AbstractXMLObjectMarshaller.cpp
\r
20 * A thread-safe abstract marshaller.
\r
23 #include "internal.h"
\r
24 #include "DOMCachingXMLObject.h"
\r
25 #include "exceptions.h"
\r
26 #include "io/AbstractXMLObjectMarshaller.h"
\r
27 #include "util/NDC.h"
\r
28 #include "util/XMLConstants.h"
\r
29 #include "util/XMLHelper.h"
\r
31 #include <algorithm>
\r
32 #include <functional>
\r
33 #include <xercesc/util/XMLUniDefs.hpp>
\r
34 #include <log4cpp/Category.hh>
\r
36 using namespace xmltooling;
\r
37 using namespace log4cpp;
\r
38 using namespace std;
\r
40 #define XT_log (*static_cast<Category*>(m_log))
\r
42 AbstractXMLObjectMarshaller::AbstractXMLObjectMarshaller(const XMLCh* targetNamespaceURI, const XMLCh* targetLocalName)
\r
43 : m_targetQName(targetNamespaceURI, targetLocalName),
\r
44 m_log(&Category::getInstance(XMLTOOLING_LOGCAT".Marshaller")) {
\r
45 if (!targetLocalName || !*targetLocalName)
\r
46 throw MarshallingException("targetLocalName cannot be null or empty");
\r
49 DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMDocument* document) const
\r
52 xmltooling::NDC ndc("marshall");
\r
55 if (XT_log.isDebugEnabled()) {
\r
56 XT_log.debug("starting to marshalling %s", xmlObject->getElementQName().toString().c_str());
\r
59 DOMCachingXMLObject* dc=dynamic_cast<DOMCachingXMLObject*>(xmlObject);
\r
61 DOMElement* cachedDOM=dc->getDOM();
\r
63 if (!document || document==cachedDOM->getOwnerDocument()) {
\r
64 XT_log.debug("XMLObject has a usable cached DOM, reusing it");
\r
66 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
\r
67 dc->releaseParentDOM(true);
\r
71 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
\r
72 // Without an adoptNode option to maintain the child pointers, we have to either import the
\r
73 // DOM while somehow reassigning all the nested references (which amounts to a complete
\r
74 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
\r
75 // it back. This depends on all objects being able to preserve their DOM at all costs.
\r
76 dc->releaseChildrenDOM(true);
\r
81 // If we get here, we didn't have a usable DOM (and/or we released the one we had).
\r
82 // We may need to create our own document.
\r
83 bool bindDocument=false;
\r
85 document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
\r
90 XT_log.debug("creating root element to marshall");
\r
91 DOMElement* domElement = document->createElementNS(
\r
92 xmlObject->getElementQName().getNamespaceURI(), xmlObject->getElementQName().getLocalPart()
\r
94 setDocumentElement(document, domElement);
\r
95 marshallInto(*xmlObject, domElement);
\r
99 XT_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
\r
100 dc->setDOM(domElement, bindDocument);
\r
101 dc->releaseParentDOM(true);
\r
107 // Delete the document if need be, and rethrow.
\r
108 if (bindDocument) {
\r
109 document->release();
\r
115 DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMElement* parentElement) const
\r
118 xmltooling::NDC ndc("marshall");
\r
121 if (XT_log.isDebugEnabled()) {
\r
122 XT_log.debug("starting to marshalling %s", xmlObject->getElementQName().toString().c_str());
\r
125 DOMCachingXMLObject* dc=dynamic_cast<DOMCachingXMLObject*>(xmlObject);
\r
127 DOMElement* cachedDOM=dc->getDOM();
\r
129 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
\r
130 XT_log.debug("XMLObject has a usable cached DOM, reusing it");
\r
131 if (parentElement!=cachedDOM->getParentNode()) {
\r
132 parentElement->appendChild(cachedDOM);
\r
133 dc->releaseParentDOM(true);
\r
138 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
\r
139 // Without an adoptNode option to maintain the child pointers, we have to either import the
\r
140 // DOM while somehow reassigning all the nested references (which amounts to a complete
\r
141 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
\r
142 // it back. This depends on all objects being able to preserve their DOM at all costs.
\r
143 dc->releaseChildrenDOM(true);
\r
148 // If we get here, we didn't have a usable DOM (and/or we released the one we had).
\r
149 XT_log.debug("creating root element to marshall");
\r
150 DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS(
\r
151 xmlObject->getElementQName().getNamespaceURI(), xmlObject->getElementQName().getLocalPart()
\r
153 parentElement->appendChild(domElement);
\r
154 marshallInto(*xmlObject, domElement);
\r
158 XT_log.debug("caching DOM for XMLObject");
\r
159 dc->setDOM(domElement, false);
\r
160 dc->releaseParentDOM(true);
\r
166 void AbstractXMLObjectMarshaller::marshallInto(XMLObject& xmlObject, DOMElement* targetElement) const
\r
168 targetElement->setPrefix(xmlObject.getElementQName().getPrefix());
\r
169 marshallElementType(xmlObject, targetElement);
\r
170 marshallNamespaces(xmlObject, targetElement);
\r
171 marshallAttributes(xmlObject, targetElement);
\r
172 marshallChildElements(xmlObject, targetElement);
\r
173 marshallElementContent(xmlObject, targetElement);
\r
175 /* TODO Signing/Encryption
\r
176 if (xmlObject instanceof SignableXMLObject) {
\r
177 signElement(targetElement, xmlObject);
\r
180 if (xmlObject instanceof EncryptableXMLObject) {
\r
181 encryptElement(targetElement, xmlObject);
\r
186 void AbstractXMLObjectMarshaller::marshallElementType(XMLObject& xmlObject, DOMElement* domElement) const
\r
188 const QName* type = xmlObject.getSchemaType();
\r
190 XT_log.debug("setting xsi:type attribute for XMLObject");
\r
192 const XMLCh* typeLocalName = type->getLocalPart();
\r
193 if (!typeLocalName || !*typeLocalName) {
\r
194 throw MarshallingException("Schema type of XMLObject may not have an empty local name.");
\r
197 static const XMLCh xsitype[] = {
\r
198 chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull
\r
201 XMLCh* xsivalue=const_cast<XMLCh*>(typeLocalName);
\r
202 const XMLCh* prefix=type->getPrefix();
\r
203 if (prefix && *prefix) {
\r
204 xsivalue=new XMLCh[XMLString::stringLen(typeLocalName) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
\r
206 XMLString::catString(xsivalue,prefix);
\r
207 static const XMLCh colon[] = {chColon, chNull};
\r
208 XMLString::catString(xsivalue,colon);
\r
209 XMLString::catString(xsivalue,typeLocalName);
\r
211 domElement->setAttributeNS(XMLConstants::XSI_NS, xsitype, xsivalue);
\r
212 if (xsivalue != typeLocalName)
\r
213 XMLString::release(&xsivalue);
\r
215 XT_log.debug("Adding XSI namespace to list of namespaces used by XMLObject");
\r
216 xmlObject.addNamespace(Namespace(XMLConstants::XSI_NS, XMLConstants::XSI_PREFIX));
\r
220 class _addns : public binary_function<DOMElement*,Namespace,void> {
\r
222 void operator()(DOMElement* domElement, const Namespace& ns) const {
\r
223 const XMLCh* prefix=ns.getNamespacePrefix();
\r
224 const XMLCh* uri=ns.getNamespaceURI();
\r
226 // Check to see if the prefix is already declared properly above this node.
\r
227 if (!ns.alwaysDeclare()) {
\r
228 const XMLCh* declared=lookupNamespaceURI(domElement->getParentNode(),prefix);
\r
229 if (declared && XMLString::equals(declared,uri))
\r
233 if (prefix && *prefix) {
\r
234 XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLConstants::XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
\r
236 XMLString::catString(xmlns,XMLConstants::XMLNS_PREFIX);
\r
237 static const XMLCh colon[] = {chColon, chNull};
\r
238 XMLString::catString(xmlns,colon);
\r
239 XMLString::catString(xmlns,prefix);
\r
240 domElement->setAttributeNS(XMLConstants::XMLNS_NS, xmlns, uri);
\r
243 domElement->setAttributeNS(XMLConstants::XMLNS_NS, XMLConstants::XMLNS_PREFIX, uri);
\r
247 const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const {
\r
248 // Return NULL if no declaration in effect. The empty string signifies the null namespace.
\r
249 if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE)
\r
250 return NULL; // we're done
\r
251 DOMNamedNodeMap* attributes = static_cast<const DOMElement*>(n)->getAttributes();
\r
253 return lookupNamespaceURI(n->getParentNode(),prefix); // defer to parent
\r
254 DOMNode* childNode;
\r
255 DOMAttr* attribute;
\r
256 for (XMLSize_t i=0; i<attributes->getLength(); i++) {
\r
257 childNode = attributes->item(i);
\r
258 if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) // not an attribute?
\r
260 attribute = static_cast<DOMAttr*>(childNode);
\r
261 if (!XMLString::equals(attribute->getNamespaceURI(),XMLConstants::XMLNS_NS))
\r
262 continue; // not a namespace declaration
\r
263 // Local name should be the prefix and the value would be the URI, except for the default namespace.
\r
264 if (!prefix && XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX))
\r
265 return attribute->getNodeValue();
\r
266 else if (XMLString::equals(prefix,attribute->getLocalName()))
\r
267 return attribute->getNodeValue();
\r
269 // Defer to parent.
\r
270 return lookupNamespaceURI(n->getParentNode(),prefix);
\r
274 void AbstractXMLObjectMarshaller::marshallNamespaces(const XMLObject& xmlObject, DOMElement* domElement) const
\r
276 XT_log.debug("marshalling namespace attributes for XMLObject");
\r
277 const set<Namespace>& namespaces = xmlObject.getNamespaces();
\r
278 for_each(namespaces.begin(),namespaces.end(),bind1st(_addns(),domElement));
\r
281 class _marshallchild : public binary_function<XMLObject*,DOMElement*,void> {
\r
284 _marshallchild(void* log) : m_log(log) {}
\r
285 void operator()(XMLObject* obj, DOMElement* element) const {
\r
288 if (XT_log.isDebugEnabled()) {
\r
289 XT_log.debug("getting marshaller for child XMLObject: %s", obj->getElementQName().toString().c_str());
\r
292 const Marshaller* marshaller = Marshaller::getMarshaller(obj);
\r
295 "no default unmarshaller installed, unknown child object: %s",
\r
296 obj->getElementQName().toString().c_str()
\r
298 throw MarshallingException("Marshaller found unknown child element, but no default marshaller was found.");
\r
300 element->appendChild(marshaller->marshall(obj, element));
\r
304 void AbstractXMLObjectMarshaller::marshallChildElements(const XMLObject& xmlObject, DOMElement* domElement) const
\r
306 XT_log.debug("marshalling child elements for XMLObject");
\r
308 const list<XMLObject*>& children=xmlObject.getOrderedChildren();
\r
309 for_each(children.begin(),children.end(),bind2nd(_marshallchild(m_log),domElement));
\r