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 "exceptions.h"
\r
25 #include "io/AbstractXMLObjectMarshaller.h"
\r
26 #ifndef XMLTOOLING_NO_XMLSEC
\r
27 #include "signature/Signature.h"
\r
29 #include "util/NDC.h"
\r
30 #include "util/XMLConstants.h"
\r
31 #include "util/XMLHelper.h"
\r
33 #include <algorithm>
\r
34 #include <functional>
\r
35 #include <xercesc/util/XMLUniDefs.hpp>
\r
36 #include <log4cpp/Category.hh>
\r
38 #ifndef XMLTOOLING_NO_XMLSEC
\r
39 using namespace xmlsignature;
\r
41 using namespace xmltooling;
\r
42 using namespace log4cpp;
\r
43 using namespace std;
\r
45 #define XT_log (*static_cast<Category*>(m_log))
\r
47 DOMElement* AbstractXMLObjectMarshaller::marshall(
\r
48 DOMDocument* document
\r
49 #ifndef XMLTOOLING_NO_XMLSEC
\r
50 ,const std::vector<xmlsignature::Signature*>* sigs
\r
55 xmltooling::NDC ndc("marshall");
\r
58 if (XT_log.isDebugEnabled()) {
\r
59 XT_log.debug("starting to marshal %s", getElementQName().toString().c_str());
\r
62 DOMElement* cachedDOM=getDOM();
\r
64 if (!document || document==cachedDOM->getOwnerDocument()) {
\r
65 XT_log.debug("XMLObject has a usable cached DOM, reusing it");
\r
67 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
\r
68 releaseParentDOM(true);
\r
72 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
\r
73 // Without an adoptNode option to maintain the child pointers, we have to either import the
\r
74 // DOM while somehow reassigning all the nested references (which amounts to a complete
\r
75 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
\r
76 // it back. This depends on all objects being able to preserve their DOM at all costs.
\r
77 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
89 XercesJanitor<DOMDocument> janitor(bindDocument ? document : NULL);
\r
91 XT_log.debug("creating root element to marshall");
\r
92 DOMElement* domElement = document->createElementNS(
\r
93 getElementQName().getNamespaceURI(), getElementQName().getLocalPart()
\r
95 setDocumentElement(document, domElement);
\r
96 #ifndef XMLTOOLING_NO_XMLSEC
\r
97 marshallInto(domElement, sigs);
\r
99 marshallInto(domElement);
\r
102 XT_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not ");
\r
103 setDOM(domElement, bindDocument);
\r
104 janitor.release(); // safely transferred
\r
105 releaseParentDOM(true);
\r
110 DOMElement* AbstractXMLObjectMarshaller::marshall(
\r
111 DOMElement* parentElement
\r
112 #ifndef XMLTOOLING_NO_XMLSEC
\r
113 ,const std::vector<xmlsignature::Signature*>* sigs
\r
118 xmltooling::NDC ndc("marshall");
\r
121 if (XT_log.isDebugEnabled()) {
\r
122 XT_log.debug("starting to marshalling %s", getElementQName().toString().c_str());
\r
125 DOMElement* cachedDOM=getDOM();
\r
127 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
\r
128 XT_log.debug("XMLObject has a usable cached DOM, reusing it");
\r
129 if (parentElement!=cachedDOM->getParentNode()) {
\r
130 parentElement->appendChild(cachedDOM);
\r
131 releaseParentDOM(true);
\r
136 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
\r
137 // Without an adoptNode option to maintain the child pointers, we have to either import the
\r
138 // DOM while somehow reassigning all the nested references (which amounts to a complete
\r
139 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
\r
140 // it back. This depends on all objects being able to preserve their DOM at all costs.
\r
141 releaseChildrenDOM(true);
\r
145 // If we get here, we didn't have a usable DOM (and/or we released the one we had).
\r
146 XT_log.debug("creating root element to marshall");
\r
147 DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS(
\r
148 getElementQName().getNamespaceURI(), getElementQName().getLocalPart()
\r
150 parentElement->appendChild(domElement);
\r
151 #ifndef XMLTOOLING_NO_XMLSEC
\r
152 marshallInto(domElement, sigs);
\r
154 marshallInto(domElement);
\r
158 XT_log.debug("caching DOM for XMLObject");
\r
159 setDOM(domElement, false);
\r
160 releaseParentDOM(true);
\r
165 void AbstractXMLObjectMarshaller::marshallInto(
\r
166 DOMElement* targetElement
\r
167 #ifndef XMLTOOLING_NO_XMLSEC
\r
168 ,const std::vector<xmlsignature::Signature*>* sigs
\r
172 if (getElementQName().hasPrefix())
\r
173 targetElement->setPrefix(getElementQName().getPrefix());
\r
175 if (m_schemaLocation) {
\r
176 static const XMLCh schemaLocation[]= UNICODE_LITERAL_14(s,c,h,e,m,a,L,o,c,a,t,i,o,n);
\r
177 if (targetElement->getParentNode()==NULL || targetElement->getParentNode()->getNodeType()==DOMNode::DOCUMENT_NODE)
\r
178 targetElement->setAttributeNS(XMLConstants::XSI_NS,schemaLocation,m_schemaLocation);
\r
181 marshallElementType(targetElement);
\r
182 marshallNamespaces(targetElement);
\r
183 marshallAttributes(targetElement);
\r
184 marshallChildElements(targetElement);
\r
185 marshallElementContent(targetElement);
\r
187 #ifndef XMLTOOLING_NO_XMLSEC
\r
189 for_each(sigs->begin(),sigs->end(),mem_fun<void,Signature>(&Signature::sign));
\r
194 void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) const
\r
196 const QName* type = getSchemaType();
\r
198 XT_log.debug("setting xsi:type attribute for XMLObject");
\r
200 const XMLCh* typeLocalName = type->getLocalPart();
\r
201 if (!typeLocalName || !*typeLocalName) {
\r
202 throw MarshallingException("Schema type of XMLObject may not have an empty local name.");
\r
205 static const XMLCh xsitype[] = {
\r
206 chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull
\r
209 XMLCh* xsivalue=const_cast<XMLCh*>(typeLocalName);
\r
210 const XMLCh* prefix=type->getPrefix();
\r
211 if (prefix && *prefix) {
\r
212 xsivalue=new XMLCh[XMLString::stringLen(typeLocalName) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
\r
214 XMLString::catString(xsivalue,prefix);
\r
215 static const XMLCh colon[] = {chColon, chNull};
\r
216 XMLString::catString(xsivalue,colon);
\r
217 XMLString::catString(xsivalue,typeLocalName);
\r
219 domElement->setAttributeNS(XMLConstants::XSI_NS, xsitype, xsivalue);
\r
220 if (xsivalue != typeLocalName)
\r
221 XMLString::release(&xsivalue);
\r
223 XT_log.debug("Adding XSI namespace to list of namespaces used by XMLObject");
\r
224 addNamespace(Namespace(XMLConstants::XSI_NS, XMLConstants::XSI_PREFIX));
\r
228 class _addns : public binary_function<DOMElement*,Namespace,void> {
\r
230 void operator()(DOMElement* domElement, const Namespace& ns) const {
\r
231 const XMLCh* prefix=ns.getNamespacePrefix();
\r
232 const XMLCh* uri=ns.getNamespaceURI();
\r
234 // Check to see if the prefix is already declared properly above this node.
\r
235 if (!ns.alwaysDeclare()) {
\r
236 const XMLCh* declared=lookupNamespaceURI(domElement->getParentNode(),prefix);
\r
237 if (declared && XMLString::equals(declared,uri))
\r
241 if (prefix && *prefix) {
\r
242 XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLConstants::XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)];
\r
244 XMLString::catString(xmlns,XMLConstants::XMLNS_PREFIX);
\r
245 static const XMLCh colon[] = {chColon, chNull};
\r
246 XMLString::catString(xmlns,colon);
\r
247 XMLString::catString(xmlns,prefix);
\r
248 domElement->setAttributeNS(XMLConstants::XMLNS_NS, xmlns, uri);
\r
251 domElement->setAttributeNS(XMLConstants::XMLNS_NS, XMLConstants::XMLNS_PREFIX, uri);
\r
255 const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const {
\r
256 // Return NULL if no declaration in effect. The empty string signifies the null namespace.
\r
257 if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE) {
\r
258 // At the root, the default namespace is set to the null namespace.
\r
259 if (!prefix || !*prefix)
\r
261 return NULL; // we're done
\r
263 DOMNamedNodeMap* attributes = static_cast<const DOMElement*>(n)->getAttributes();
\r
265 return lookupNamespaceURI(n->getParentNode(),prefix); // defer to parent
\r
266 DOMNode* childNode;
\r
267 DOMAttr* attribute;
\r
268 for (XMLSize_t i=0; i<attributes->getLength(); i++) {
\r
269 childNode = attributes->item(i);
\r
270 if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) // not an attribute?
\r
272 attribute = static_cast<DOMAttr*>(childNode);
\r
273 if (!XMLString::equals(attribute->getNamespaceURI(),XMLConstants::XMLNS_NS))
\r
274 continue; // not a namespace declaration
\r
275 // Local name should be the prefix and the value would be the URI, except for the default namespace.
\r
276 if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX))
\r
277 return attribute->getNodeValue();
\r
278 else if (XMLString::equals(prefix,attribute->getLocalName()))
\r
279 return attribute->getNodeValue();
\r
281 // Defer to parent.
\r
282 return lookupNamespaceURI(n->getParentNode(),prefix);
\r
286 void AbstractXMLObjectMarshaller::marshallNamespaces(DOMElement* domElement) const
\r
288 XT_log.debug("marshalling namespace attributes for XMLObject");
\r
289 const set<Namespace>& namespaces = getNamespaces();
\r
290 for_each(namespaces.begin(),namespaces.end(),bind1st(_addns(),domElement));
\r
293 class _marshallit : public binary_function<const XMLObject*,DOMElement*,void> {
\r
295 void operator()(const XMLObject* xo, DOMElement* e) const {
\r
296 if (xo) xo->marshall(e);
\r
300 void AbstractXMLObjectMarshaller::marshallChildElements(DOMElement* domElement) const
\r
302 XT_log.debug("marshalling child elements for XMLObject");
\r
304 const list<XMLObject*>& children=getOrderedChildren();
\r
305 for_each(children.begin(),children.end(),bind2nd(_marshallit(),domElement));
\r