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 DOMElement* AbstractXMLObjectMarshaller::marshall(
48 #ifndef XMLTOOLING_NO_XMLSEC
49 ,const vector<Signature*>* sigs
50 ,const Credential* credential
55 xmltooling::NDC ndc("marshall");
58 if (m_log.isDebugEnabled()) {
59 m_log.debug("starting to marshal %s", getElementQName().toString().c_str());
62 DOMElement* cachedDOM=getDOM();
64 if (!document || document==cachedDOM->getOwnerDocument()) {
65 m_log.debug("XMLObject has a usable cached DOM, reusing it");
67 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
68 releaseParentDOM(true);
72 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
73 // Without an adoptNode option to maintain the child pointers, we have to either import the
74 // DOM while somehow reassigning all the nested references (which amounts to a complete
75 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
76 // it back. This depends on all objects being able to preserve their DOM at all costs.
77 releaseChildrenDOM(true);
81 // If we get here, we didn't have a usable DOM (and/or we released the one we had).
82 // We may need to create our own document.
83 bool bindDocument=false;
85 document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
89 XercesJanitor<DOMDocument> janitor(bindDocument ? document : NULL);
91 m_log.debug("creating root element to marshall");
92 DOMElement* domElement = document->createElementNS(
93 getElementQName().getNamespaceURI(), getElementQName().getLocalPart()
95 setDocumentElement(document, domElement);
96 #ifndef XMLTOOLING_NO_XMLSEC
97 marshallInto(domElement, sigs, credential);
99 marshallInto(domElement);
102 m_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
103 setDOM(domElement, bindDocument);
104 janitor.release(); // safely transferred
105 releaseParentDOM(true);
110 DOMElement* AbstractXMLObjectMarshaller::marshall(
111 DOMElement* parentElement
112 #ifndef XMLTOOLING_NO_XMLSEC
113 ,const vector<Signature*>* sigs
114 ,const Credential* credential
119 xmltooling::NDC ndc("marshall");
122 if (m_log.isDebugEnabled()) {
123 m_log.debug("starting to marshalling %s", getElementQName().toString().c_str());
126 DOMElement* cachedDOM=getDOM();
128 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
129 m_log.debug("XMLObject has a usable cached DOM, reusing it");
130 if (parentElement!=cachedDOM->getParentNode()) {
131 parentElement->appendChild(cachedDOM);
132 releaseParentDOM(true);
137 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
138 // Without an adoptNode option to maintain the child pointers, we have to either import the
139 // DOM while somehow reassigning all the nested references (which amounts to a complete
140 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
141 // it back. This depends on all objects being able to preserve their DOM at all costs.
142 releaseChildrenDOM(true);
146 // If we get here, we didn't have a usable DOM (and/or we released the one we had).
147 m_log.debug("creating root element to marshall");
148 DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS(
149 getElementQName().getNamespaceURI(), getElementQName().getLocalPart()
151 parentElement->appendChild(domElement);
152 #ifndef XMLTOOLING_NO_XMLSEC
153 marshallInto(domElement, sigs, credential);
155 marshallInto(domElement);
159 m_log.debug("caching DOM for XMLObject");
160 setDOM(domElement, false);
161 releaseParentDOM(true);
166 void AbstractXMLObjectMarshaller::marshallInto(
167 DOMElement* targetElement
168 #ifndef XMLTOOLING_NO_XMLSEC
169 ,const vector<Signature*>* sigs
170 ,const Credential* credential
174 if (getElementQName().hasPrefix())
175 targetElement->setPrefix(getElementQName().getPrefix());
177 if (m_schemaLocation || m_noNamespaceSchemaLocation) {
178 static const XMLCh schemaLocation[] = {
179 chLatin_x, chLatin_s, chLatin_i, chColon,
180 chLatin_s, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a,
181 chLatin_L, chLatin_o, chLatin_c, chLatin_a, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chNull
183 static const XMLCh noNamespaceSchemaLocation[] = {
184 chLatin_x, chLatin_s, chLatin_i, chColon,
185 chLatin_n, chLatin_o, chLatin_N, chLatin_a, chLatin_m, chLatin_e, chLatin_s, chLatin_p, chLatin_a, chLatin_c, chLatin_e,
186 chLatin_S, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a,
187 chLatin_L, chLatin_o, chLatin_c, chLatin_a, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chNull
189 if (targetElement->getParentNode()==NULL || targetElement->getParentNode()->getNodeType()==DOMNode::DOCUMENT_NODE) {
190 if (m_schemaLocation)
191 targetElement->setAttributeNS(XSI_NS,schemaLocation,m_schemaLocation);
192 if (m_noNamespaceSchemaLocation)
193 targetElement->setAttributeNS(XSI_NS,noNamespaceSchemaLocation,m_noNamespaceSchemaLocation);
197 static const XMLCh _nil[] = { chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_n, chLatin_i, chLatin_l, chNull };
199 if (m_nil != xmlconstants::XML_BOOL_NULL) {
201 case xmlconstants::XML_BOOL_TRUE:
202 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_TRUE);
204 case xmlconstants::XML_BOOL_ONE:
205 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_ONE);
207 case xmlconstants::XML_BOOL_FALSE:
208 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_FALSE);
210 case xmlconstants::XML_BOOL_ZERO:
211 targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_ZERO);
214 m_log.debug("adding XSI namespace to list of namespaces used by XMLObject");
215 addNamespace(Namespace(XSI_NS, XSI_PREFIX));
218 marshallElementType(targetElement);
219 marshallNamespaces(targetElement);
220 marshallAttributes(targetElement);
222 #ifndef XMLTOOLING_NO_XMLSEC
223 marshallContent(targetElement,credential);
225 for_each(sigs->begin(),sigs->end(),bind2nd(mem_fun1_t<void,Signature,const Credential*>(&Signature::sign),credential));
228 marshallContent(targetElement);
232 void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) const
234 const QName* type = getSchemaType();
236 m_log.debug("setting xsi:type attribute for XMLObject");
238 const XMLCh* typeLocalName = type->getLocalPart();
239 if (!typeLocalName || !*typeLocalName) {
240 throw MarshallingException("Schema type of XMLObject may not have an empty local name.");
243 static const XMLCh xsitype[] = {
244 chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull
247 XMLCh* xsivalue=const_cast<XMLCh*>(typeLocalName);
248 const XMLCh* prefix=type->getPrefix();
249 if (prefix && *prefix) {
250 xsivalue=new XMLCh[XMLString::stringLen(typeLocalName) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
252 XMLString::catString(xsivalue,prefix);
253 static const XMLCh colon[] = {chColon, chNull};
254 XMLString::catString(xsivalue,colon);
255 XMLString::catString(xsivalue,typeLocalName);
257 domElement->setAttributeNS(XSI_NS, xsitype, xsivalue);
258 if (xsivalue != typeLocalName)
259 XMLString::release(&xsivalue);
261 m_log.debug("adding XSI namespace to list of namespaces used by XMLObject");
262 addNamespace(Namespace(XSI_NS, XSI_PREFIX));
266 class _addns : public binary_function<DOMElement*,Namespace,void> {
268 void operator()(DOMElement* domElement, const Namespace& ns) const {
269 const XMLCh* prefix=ns.getNamespacePrefix();
270 const XMLCh* uri=ns.getNamespaceURI();
272 // Check for xmlns:xml.
273 if (XMLString::equals(prefix, XML_PREFIX) && XMLString::equals(uri, XML_NS))
276 // Check to see if the prefix is already declared properly above this node.
277 if (!ns.alwaysDeclare()) {
278 const XMLCh* declared=lookupNamespaceURI(domElement->getParentNode(),prefix);
279 if (declared && XMLString::equals(declared,uri))
283 if (prefix && *prefix) {
284 XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
286 XMLString::catString(xmlns,XMLNS_PREFIX);
287 static const XMLCh colon[] = {chColon, chNull};
288 XMLString::catString(xmlns,colon);
289 XMLString::catString(xmlns,prefix);
290 domElement->setAttributeNS(XMLNS_NS, xmlns, uri);
294 domElement->setAttributeNS(XMLNS_NS, XMLNS_PREFIX, uri);
298 const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const {
299 // Return NULL if no declaration in effect. The empty string signifies the null namespace.
300 if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE) {
301 // At the root, the default namespace is set to the null namespace.
302 if (!prefix || !*prefix)
304 return NULL; // we're done
306 DOMNamedNodeMap* attributes = static_cast<const DOMElement*>(n)->getAttributes();
308 return lookupNamespaceURI(n->getParentNode(),prefix); // defer to parent
311 for (XMLSize_t i=0; i<attributes->getLength(); i++) {
312 childNode = attributes->item(i);
313 if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) // not an attribute?
315 attribute = static_cast<DOMAttr*>(childNode);
316 if (!XMLString::equals(attribute->getNamespaceURI(),XMLNS_NS))
317 continue; // not a namespace declaration
318 // Local name should be the prefix and the value would be the URI, except for the default namespace.
319 if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX))
320 return attribute->getNodeValue();
321 else if (XMLString::equals(prefix,attribute->getLocalName()))
322 return attribute->getNodeValue();
325 return lookupNamespaceURI(n->getParentNode(),prefix);
329 void AbstractXMLObjectMarshaller::marshallNamespaces(DOMElement* domElement) const
331 m_log.debug("marshalling namespace attributes for XMLObject");
332 const set<Namespace>& namespaces = getNamespaces();
333 for_each(namespaces.begin(),namespaces.end(),bind1st(_addns(),domElement));
336 void AbstractXMLObjectMarshaller::marshallContent(
337 DOMElement* domElement
338 #ifndef XMLTOOLING_NO_XMLSEC
339 ,const Credential* credential
343 m_log.debug("marshalling text and child elements for XMLObject");
346 const XMLCh* val = getTextContent(pos);
348 domElement->appendChild(domElement->getOwnerDocument()->createTextNode(val));
350 const list<XMLObject*>& children=getOrderedChildren();
351 for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
353 #ifndef XMLTOOLING_NO_XMLSEC
354 (*i)->marshall(domElement,NULL,credential);
356 (*i)->marshall(domElement);
358 val = getTextContent(++pos);
360 domElement->appendChild(domElement->getOwnerDocument()->createTextNode(val));