2 * Copyright 2001-2009 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.
18 * AbstractXMLObjectMarshaller.cpp
20 * A thread-safe abstract marshaller.
24 #include "exceptions.h"
25 #include "io/AbstractXMLObjectMarshaller.h"
26 #ifndef XMLTOOLING_NO_XMLSEC
27 #include "security/Credential.h"
28 #include "signature/Signature.h"
31 #include "util/XMLConstants.h"
32 #include "util/XMLHelper.h"
36 #include <xercesc/util/XMLUniDefs.hpp>
38 #ifndef XMLTOOLING_NO_XMLSEC
39 using namespace xmlsignature;
41 using namespace xmlconstants;
42 using namespace xmltooling;
43 using namespace xercesc;
46 AbstractXMLObjectMarshaller::AbstractXMLObjectMarshaller()
50 AbstractXMLObjectMarshaller::~AbstractXMLObjectMarshaller()
54 void AbstractXMLObjectMarshaller::setDocumentElement(DOMDocument* document, DOMElement* element) const
56 DOMElement* documentRoot = document->getDocumentElement();
58 document->replaceChild(element, documentRoot);
60 document->appendChild(element);
63 DOMElement* AbstractXMLObjectMarshaller::marshall(
65 #ifndef XMLTOOLING_NO_XMLSEC
66 ,const vector<Signature*>* sigs
67 ,const Credential* credential
72 xmltooling::NDC ndc("marshall");
75 if (m_log.isDebugEnabled()) {
76 m_log.debug("starting to marshal %s", getElementQName().toString().c_str());
79 DOMElement* cachedDOM=getDOM();
81 if (!document || document==cachedDOM->getOwnerDocument()) {
82 m_log.debug("XMLObject has a usable cached DOM, reusing it");
84 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
85 releaseParentDOM(true);
89 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
90 // Without an adoptNode option to maintain the child pointers, we have to either import the
91 // DOM while somehow reassigning all the nested references (which amounts to a complete
92 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
93 // it back. This depends on all objects being able to preserve their DOM at all costs.
94 releaseChildrenDOM(true);
98 // If we get here, we didn't have a usable DOM (and/or we released the one we had).
99 // We may need to create our own document.
100 bool bindDocument=false;
102 document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
106 XercesJanitor<DOMDocument> janitor(bindDocument ? document : NULL);
108 m_log.debug("creating root element to marshall");
109 DOMElement* domElement = document->createElementNS(
110 getElementQName().getNamespaceURI(), getElementQName().getLocalPart()
112 setDocumentElement(document, domElement);
113 #ifndef XMLTOOLING_NO_XMLSEC
114 marshallInto(domElement, sigs, credential);
116 marshallInto(domElement);
119 m_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
120 setDOM(domElement, bindDocument);
121 janitor.release(); // safely transferred
122 releaseParentDOM(true);
127 DOMElement* AbstractXMLObjectMarshaller::marshall(
128 DOMElement* parentElement
129 #ifndef XMLTOOLING_NO_XMLSEC
130 ,const vector<Signature*>* sigs
131 ,const Credential* credential
136 xmltooling::NDC ndc("marshall");
139 if (m_log.isDebugEnabled()) {
140 m_log.debug("starting to marshalling %s", getElementQName().toString().c_str());
143 DOMElement* cachedDOM=getDOM();
145 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
146 m_log.debug("XMLObject has a usable cached DOM, reusing it");
147 if (parentElement!=cachedDOM->getParentNode()) {
148 parentElement->appendChild(cachedDOM);
149 releaseParentDOM(true);
154 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
155 // Without an adoptNode option to maintain the child pointers, we have to either import the
156 // DOM while somehow reassigning all the nested references (which amounts to a complete
157 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
158 // it back. This depends on all objects being able to preserve their DOM at all costs.
159 releaseChildrenDOM(true);
163 // If we get here, we didn't have a usable DOM (and/or we released the one we had).
164 m_log.debug("creating root element to marshall");
165 DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS(
166 getElementQName().getNamespaceURI(), getElementQName().getLocalPart()
168 parentElement->appendChild(domElement);
169 #ifndef XMLTOOLING_NO_XMLSEC
170 marshallInto(domElement, sigs, credential);
172 marshallInto(domElement);
176 m_log.debug("caching DOM for XMLObject");
177 setDOM(domElement, false);
178 releaseParentDOM(true);
183 void AbstractXMLObjectMarshaller::marshallInto(
184 DOMElement* targetElement
185 #ifndef XMLTOOLING_NO_XMLSEC
186 ,const vector<Signature*>* sigs
187 ,const Credential* credential
191 if (getElementQName().hasPrefix())
192 targetElement->setPrefix(getElementQName().getPrefix());
194 if (m_schemaLocation || m_noNamespaceSchemaLocation) {
195 static const XMLCh schemaLocation[] = {
196 chLatin_x, chLatin_s, chLatin_i, chColon,
197 chLatin_s, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a,
198 chLatin_L, chLatin_o, chLatin_c, chLatin_a, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chNull
200 static const XMLCh noNamespaceSchemaLocation[] = {
201 chLatin_x, chLatin_s, chLatin_i, chColon,
202 chLatin_n, chLatin_o, chLatin_N, chLatin_a, chLatin_m, chLatin_e, chLatin_s, chLatin_p, chLatin_a, chLatin_c, chLatin_e,
203 chLatin_S, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a,
204 chLatin_L, chLatin_o, chLatin_c, chLatin_a, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chNull
206 if (targetElement->getParentNode()==NULL || targetElement->getParentNode()->getNodeType()==DOMNode::DOCUMENT_NODE) {
207 if (m_schemaLocation)
208 targetElement->setAttributeNS(XSI_NS,schemaLocation,m_schemaLocation);
209 if (m_noNamespaceSchemaLocation)
210 targetElement->setAttributeNS(XSI_NS,noNamespaceSchemaLocation,m_noNamespaceSchemaLocation);
214 static const XMLCh _nil[] = { chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_n, chLatin_i, chLatin_l, chNull };
216 if (m_nil != xmlconstants::XML_BOOL_NULL) {
218 case xmlconstants::XML_BOOL_TRUE:
219 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_TRUE);
221 case xmlconstants::XML_BOOL_ONE:
222 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_ONE);
224 case xmlconstants::XML_BOOL_FALSE:
225 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_FALSE);
227 case xmlconstants::XML_BOOL_ZERO:
228 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_ZERO);
231 m_log.debug("adding XSI namespace to list of namespaces visibly used by XMLObject");
232 addNamespace(Namespace(XSI_NS, XSI_PREFIX, false, Namespace::VisiblyUsed));
235 marshallElementType(targetElement);
236 marshallNamespaces(targetElement);
237 marshallAttributes(targetElement);
239 #ifndef XMLTOOLING_NO_XMLSEC
240 marshallContent(targetElement,credential);
242 for_each(sigs->begin(),sigs->end(),bind2nd(mem_fun1_t<void,Signature,const Credential*>(&Signature::sign),credential));
245 marshallContent(targetElement);
249 void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) const
251 const QName* type = getSchemaType();
253 m_log.debug("setting xsi:type attribute for XMLObject");
255 const XMLCh* typeLocalName = type->getLocalPart();
256 if (!typeLocalName || !*typeLocalName) {
257 throw MarshallingException("Schema type of XMLObject may not have an empty local name.");
260 static const XMLCh xsitype[] = {
261 chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull
264 XMLCh* xsivalue=const_cast<XMLCh*>(typeLocalName);
265 const XMLCh* prefix=type->getPrefix();
266 if (prefix && *prefix) {
267 xsivalue=new XMLCh[XMLString::stringLen(typeLocalName) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
269 XMLString::catString(xsivalue,prefix);
270 static const XMLCh colon[] = {chColon, chNull};
271 XMLString::catString(xsivalue,colon);
272 XMLString::catString(xsivalue,typeLocalName);
274 domElement->setAttributeNS(XSI_NS, xsitype, xsivalue);
275 if (xsivalue != typeLocalName)
276 XMLString::release(&xsivalue);
278 m_log.debug("adding XSI namespace to list of namespaces visibly used by XMLObject");
279 addNamespace(Namespace(XSI_NS, XSI_PREFIX, false, Namespace::VisiblyUsed));
283 class _addns : public binary_function<DOMElement*,Namespace,void> {
285 void operator()(DOMElement* domElement, const Namespace& ns) const {
286 const XMLCh* prefix=ns.getNamespacePrefix();
287 const XMLCh* uri=ns.getNamespaceURI();
289 // Check for xmlns:xml.
290 if (XMLString::equals(prefix, XML_PREFIX) && XMLString::equals(uri, XML_NS))
293 // Check to see if the prefix is already declared properly above this node.
294 if (!ns.alwaysDeclare()) {
295 const XMLCh* declared=lookupNamespaceURI(domElement->getParentNode(),prefix);
296 if (declared && XMLString::equals(declared,uri))
300 if (prefix && *prefix) {
301 XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
303 XMLString::catString(xmlns,XMLNS_PREFIX);
304 static const XMLCh colon[] = {chColon, chNull};
305 XMLString::catString(xmlns,colon);
306 XMLString::catString(xmlns,prefix);
307 domElement->setAttributeNS(XMLNS_NS, xmlns, uri);
311 domElement->setAttributeNS(XMLNS_NS, XMLNS_PREFIX, uri);
315 const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const {
316 // Return NULL if no declaration in effect. The empty string signifies the null namespace.
317 if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE) {
318 // At the root, the default namespace is set to the null namespace.
319 if (!prefix || !*prefix)
321 return NULL; // we're done
323 DOMNamedNodeMap* attributes = static_cast<const DOMElement*>(n)->getAttributes();
325 return lookupNamespaceURI(n->getParentNode(),prefix); // defer to parent
328 for (XMLSize_t i=0; i<attributes->getLength(); i++) {
329 childNode = attributes->item(i);
330 if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) // not an attribute?
332 attribute = static_cast<DOMAttr*>(childNode);
333 if (!XMLString::equals(attribute->getNamespaceURI(),XMLNS_NS))
334 continue; // not a namespace declaration
335 // Local name should be the prefix and the value would be the URI, except for the default namespace.
336 if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX))
337 return attribute->getNodeValue();
338 else if (XMLString::equals(prefix,attribute->getLocalName()))
339 return attribute->getNodeValue();
342 return lookupNamespaceURI(n->getParentNode(),prefix);
346 void AbstractXMLObjectMarshaller::marshallNamespaces(DOMElement* domElement) const
348 m_log.debug("marshalling namespace attributes for XMLObject");
349 const set<Namespace>& namespaces = getNamespaces();
350 for_each(namespaces.begin(),namespaces.end(),bind1st(_addns(),domElement));
353 void AbstractXMLObjectMarshaller::marshallContent(
354 DOMElement* domElement
355 #ifndef XMLTOOLING_NO_XMLSEC
356 ,const Credential* credential
360 m_log.debug("marshalling text and child elements for XMLObject");
363 const XMLCh* val = getTextContent(pos);
365 domElement->appendChild(domElement->getOwnerDocument()->createTextNode(val));
367 const list<XMLObject*>& children=getOrderedChildren();
368 for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
370 #ifndef XMLTOOLING_NO_XMLSEC
371 (*i)->marshall(domElement,NULL,credential);
373 (*i)->marshall(domElement);
375 val = getTextContent(++pos);
377 domElement->appendChild(domElement->getOwnerDocument()->createTextNode(val));
382 void AbstractXMLObjectMarshaller::marshallAttributes(DOMElement* domElement) const