X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Fio%2FAbstractXMLObjectMarshaller.cpp;h=3ef702a5837f352eaa74d49697afaeec3e7b6af8;hb=5cb314df178f78c6fa7b9826c2c5a5298ec7a473;hp=0b09c3e631e1cc801075d49984e6b5ac2fcdd0e3;hpb=e963983abe628f963602015344ad8fd65e4fe406;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/io/AbstractXMLObjectMarshaller.cpp b/xmltooling/io/AbstractXMLObjectMarshaller.cpp index 0b09c3e..3ef702a 100644 --- a/xmltooling/io/AbstractXMLObjectMarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectMarshaller.cpp @@ -1,282 +1,307 @@ -/* -* Copyright 2001-2006 Internet2 - * -* Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * AbstractXMLObjectMarshaller.cpp - * - * A thread-safe abstract marshaller. - */ - -#include "internal.h" -#include "DOMCachingXMLObject.h" -#include "exceptions.h" -#include "io/AbstractXMLObjectMarshaller.h" -#include "util/NDC.h" -#include "util/XMLConstants.h" -#include "util/XMLHelper.h" - -#include -#include -#include -#include - -using namespace xmltooling; -using namespace log4cpp; -using namespace std; - -#define XT_log (*static_cast(m_log)) - -AbstractXMLObjectMarshaller::AbstractXMLObjectMarshaller(const XMLCh* targetNamespaceURI, const XMLCh* targetLocalName) - : m_targetQName(targetNamespaceURI, targetLocalName), - m_log(&Category::getInstance(XMLTOOLING_LOGCAT".Marshaller")) { - if (!targetLocalName || !*targetLocalName) - throw MarshallingException("targetLocalName cannot be null or empty"); -} - -DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMDocument* document) const -{ -#ifdef _DEBUG - xmltooling::NDC ndc("marshall"); -#endif - - if (XT_log.isDebugEnabled()) { - XT_log.debug("starting to marshalling %s", xmlObject->getElementQName().toString().c_str()); - } - - DOMCachingXMLObject* dc=dynamic_cast(xmlObject); - if (dc) { - DOMElement* cachedDOM=dc->getDOM(); - if (cachedDOM) { - if (!document || document==cachedDOM->getOwnerDocument()) { - XT_log.debug("XMLObject has a usable cached DOM, reusing it"); - if (document) - setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM); - dc->releaseParentDOM(true); - return cachedDOM; - } - - // We have a DOM but it doesn't match the document we were given. This both sucks and blows. - // Without an adoptNode option to maintain the child pointers, we have to either import the - // DOM while somehow reassigning all the nested references (which amounts to a complete - // *unmarshall* operation), or we just release the existing DOM and hope that we can get - // it back. This depends on all objects being able to preserve their DOM at all costs. - dc->releaseChildrenDOM(true); - dc->releaseDOM(); - } - } - - // If we get here, we didn't have a usable DOM (and/or we released the one we had). - // We may need to create our own document. - bool bindDocument=false; - if (!document) { - document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument(); - bindDocument=true; - } - - try { - XT_log.debug("creating root element to marshall"); - DOMElement* domElement = document->createElementNS( - xmlObject->getElementQName().getNamespaceURI(), xmlObject->getElementQName().getLocalPart() - ); - setDocumentElement(document, domElement); - marshallInto(xmlObject, domElement); - - //Recache the DOM. - if (dc) { - XT_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not "); - dc->setDOM(domElement, bindDocument); - dc->releaseParentDOM(true); - } - - return domElement; - } - catch (...) { - // Delete the document if need be, and rethrow. - if (bindDocument) { - document->release(); - } - throw; - } -} - -DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMElement* parentElement) const -{ -#ifdef _DEBUG - xmltooling::NDC ndc("marshall"); -#endif - - if (XT_log.isDebugEnabled()) { - XT_log.debug("starting to marshalling %s", xmlObject->getElementQName().toString().c_str()); - } - - DOMCachingXMLObject* dc=dynamic_cast(xmlObject); - if (dc) { - DOMElement* cachedDOM=dc->getDOM(); - if (cachedDOM) { - if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) { - XT_log.debug("XMLObject has a usable cached DOM, reusing it"); - if (parentElement!=cachedDOM->getParentNode()) { - parentElement->appendChild(cachedDOM); - dc->releaseParentDOM(true); - } - return cachedDOM; - } - - // We have a DOM but it doesn't match the document we were given. This both sucks and blows. - // Without an adoptNode option to maintain the child pointers, we have to either import the - // DOM while somehow reassigning all the nested references (which amounts to a complete - // *unmarshall* operation), or we just release the existing DOM and hope that we can get - // it back. This depends on all objects being able to preserve their DOM at all costs. - dc->releaseChildrenDOM(true); - dc->releaseDOM(); - } - } - - // If we get here, we didn't have a usable DOM (and/or we released the one we had). - XT_log.debug("creating root element to marshall"); - DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS( - xmlObject->getElementQName().getNamespaceURI(), xmlObject->getElementQName().getLocalPart() - ); - parentElement->appendChild(domElement); - marshallInto(xmlObject, domElement); - - //Recache the DOM. - if (dc) { - XT_log.debug("caching DOM for XMLObject"); - dc->setDOM(domElement, false); - dc->releaseParentDOM(true); - } - - return domElement; -} - -void AbstractXMLObjectMarshaller::marshallInto(XMLObject* xmlObject, DOMElement* targetElement) const -{ - targetElement->setPrefix(xmlObject->getElementQName().getPrefix()); - marshallElementType(xmlObject, targetElement); - marshallNamespaces(xmlObject, targetElement); - marshallAttributes(xmlObject, targetElement); - marshallChildElements(xmlObject, targetElement); - marshallElementContent(xmlObject, targetElement); - - /* TODO Signing/Encryption - if (xmlObject instanceof SignableXMLObject) { - signElement(targetElement, xmlObject); - } - - if (xmlObject instanceof EncryptableXMLObject) { - encryptElement(targetElement, xmlObject); - } - */ -} - -void AbstractXMLObjectMarshaller::marshallElementType(XMLObject* xmlObject, DOMElement* domElement) const -{ - const QName* type = xmlObject->getSchemaType(); - if (type) { - XT_log.debug("setting xsi:type attribute for XMLObject"); - - const XMLCh* typeLocalName = type->getLocalPart(); - if (!typeLocalName || !*typeLocalName) { - throw MarshallingException("Schema type of XMLObject may not have an empty local name."); - } - - static const XMLCh xsitype[] = { - chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull - }; - - XMLCh* xsivalue=const_cast(typeLocalName); - const XMLCh* prefix=type->getPrefix(); - if (prefix && *prefix) { - xsivalue=new XMLCh[XMLString::stringLen(typeLocalName) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)]; - *xsivalue=chNull; - XMLString::catString(xsivalue,prefix); - static const XMLCh colon[] = {chColon, chNull}; - XMLString::catString(xsivalue,colon); - XMLString::catString(xsivalue,typeLocalName); - } - domElement->setAttributeNS(XMLConstants::XSI_NS, xsitype, xsivalue); - if (xsivalue != typeLocalName) - XMLString::release(&xsivalue); - - XT_log.debug("Adding XSI namespace to list of namespaces used by XMLObject"); - xmlObject->addNamespace(Namespace(XMLConstants::XSI_NS, XMLConstants::XSI_PREFIX)); - } -} - -class _addns : public binary_function { -public: - void operator()(DOMElement* domElement, const Namespace& ns) const { - const XMLCh* prefix=ns.getNamespacePrefix(); - const XMLCh* uri=ns.getNamespaceURI(); - - // Check to see if the prefix is already declared properly above this node. - if (!ns.alwaysDeclare() && domElement->getParentNode() && - XMLString::equals(domElement->getParentNode()->lookupNamespaceURI(prefix),uri)) - return; - - if (prefix && *prefix) { - XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLConstants::XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)]; - *xmlns=chNull; - XMLString::catString(xmlns,XMLConstants::XMLNS_PREFIX); - static const XMLCh colon[] = {chColon, chNull}; - XMLString::catString(xmlns,colon); - XMLString::catString(xmlns,prefix); - domElement->setAttributeNS(XMLConstants::XMLNS_NS, xmlns, uri); - } - else { - domElement->setAttributeNS(XMLConstants::XMLNS_NS, XMLConstants::XMLNS_PREFIX, uri); - } - } -}; - -void AbstractXMLObjectMarshaller::marshallNamespaces(const XMLObject* xmlObject, DOMElement* domElement) const -{ - XT_log.debug("marshalling namespace attributes for XMLObject"); - const set& namespaces = xmlObject->getNamespaces(); - for_each(namespaces.begin(),namespaces.end(),bind1st(_addns(),domElement)); -} - -class _marshallchild : public binary_function { - void* m_log; -public: - _marshallchild(void* log) : m_log(log) {} - void operator()(XMLObject* obj, DOMElement* element) const { - if (XT_log.isDebugEnabled()) { - XT_log.debug("getting marshaller for child XMLObject: %s", obj->getElementQName().toString().c_str()); - } - - const Marshaller* marshaller = Marshaller::getMarshaller(obj); - if (!marshaller) { - XT_log.error( - "no default unmarshaller installed, unknown child object: %s", - obj->getElementQName().toString().c_str() - ); - throw MarshallingException("Marshaller found unknown child element, but no default marshaller was found."); - } - element->appendChild(marshaller->marshall(obj, element)); - } -}; - -void AbstractXMLObjectMarshaller::marshallChildElements(const XMLObject* xmlObject, DOMElement* domElement) const -{ - XT_log.debug("marshalling child elements for XMLObject"); - - vector children; - if (xmlObject->getOrderedChildren(children)) { - for_each(children.begin(),children.end(),bind2nd(_marshallchild(m_log),domElement)); - } -} +/* +* Copyright 2001-2007 Internet2 + * +* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * AbstractXMLObjectMarshaller.cpp + * + * A thread-safe abstract marshaller. + */ + +#include "internal.h" +#include "exceptions.h" +#include "io/AbstractXMLObjectMarshaller.h" +#ifndef XMLTOOLING_NO_XMLSEC + #include "signature/Signature.h" +#endif +#include "util/NDC.h" +#include "util/XMLConstants.h" +#include "util/XMLHelper.h" + +#include +#include +#include + +#ifndef XMLTOOLING_NO_XMLSEC + using namespace xmlsignature; +#endif +using namespace xmlconstants; +using namespace xmltooling; +using namespace std; + +DOMElement* AbstractXMLObjectMarshaller::marshall( + DOMDocument* document +#ifndef XMLTOOLING_NO_XMLSEC + ,const std::vector* sigs +#endif + ) const +{ +#ifdef _DEBUG + xmltooling::NDC ndc("marshall"); +#endif + + if (m_log.isDebugEnabled()) { + m_log.debug("starting to marshal %s", getElementQName().toString().c_str()); + } + + DOMElement* cachedDOM=getDOM(); + if (cachedDOM) { + if (!document || document==cachedDOM->getOwnerDocument()) { + m_log.debug("XMLObject has a usable cached DOM, reusing it"); + if (document) + setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM); + releaseParentDOM(true); + return cachedDOM; + } + + // We have a DOM but it doesn't match the document we were given. This both sucks and blows. + // Without an adoptNode option to maintain the child pointers, we have to either import the + // DOM while somehow reassigning all the nested references (which amounts to a complete + // *unmarshall* operation), or we just release the existing DOM and hope that we can get + // it back. This depends on all objects being able to preserve their DOM at all costs. + releaseChildrenDOM(true); + releaseDOM(); + } + + // If we get here, we didn't have a usable DOM (and/or we released the one we had). + // We may need to create our own document. + bool bindDocument=false; + if (!document) { + document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument(); + bindDocument=true; + } + + XercesJanitor janitor(bindDocument ? document : NULL); + + m_log.debug("creating root element to marshall"); + DOMElement* domElement = document->createElementNS( + getElementQName().getNamespaceURI(), getElementQName().getLocalPart() + ); + setDocumentElement(document, domElement); +#ifndef XMLTOOLING_NO_XMLSEC + marshallInto(domElement, sigs); +#else + marshallInto(domElement); +#endif + //Recache the DOM. + m_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not "); + setDOM(domElement, bindDocument); + janitor.release(); // safely transferred + releaseParentDOM(true); + + return domElement; +} + +DOMElement* AbstractXMLObjectMarshaller::marshall( + DOMElement* parentElement +#ifndef XMLTOOLING_NO_XMLSEC + ,const std::vector* sigs +#endif + ) const +{ +#ifdef _DEBUG + xmltooling::NDC ndc("marshall"); +#endif + + if (m_log.isDebugEnabled()) { + m_log.debug("starting to marshalling %s", getElementQName().toString().c_str()); + } + + DOMElement* cachedDOM=getDOM(); + if (cachedDOM) { + if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) { + m_log.debug("XMLObject has a usable cached DOM, reusing it"); + if (parentElement!=cachedDOM->getParentNode()) { + parentElement->appendChild(cachedDOM); + releaseParentDOM(true); + } + return cachedDOM; + } + + // We have a DOM but it doesn't match the document we were given. This both sucks and blows. + // Without an adoptNode option to maintain the child pointers, we have to either import the + // DOM while somehow reassigning all the nested references (which amounts to a complete + // *unmarshall* operation), or we just release the existing DOM and hope that we can get + // it back. This depends on all objects being able to preserve their DOM at all costs. + releaseChildrenDOM(true); + releaseDOM(); + } + + // If we get here, we didn't have a usable DOM (and/or we released the one we had). + m_log.debug("creating root element to marshall"); + DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS( + getElementQName().getNamespaceURI(), getElementQName().getLocalPart() + ); + parentElement->appendChild(domElement); +#ifndef XMLTOOLING_NO_XMLSEC + marshallInto(domElement, sigs); +#else + marshallInto(domElement); +#endif + + //Recache the DOM. + m_log.debug("caching DOM for XMLObject"); + setDOM(domElement, false); + releaseParentDOM(true); + + return domElement; +} + +void AbstractXMLObjectMarshaller::marshallInto( + DOMElement* targetElement +#ifndef XMLTOOLING_NO_XMLSEC + ,const std::vector* sigs +#endif + ) const +{ + if (getElementQName().hasPrefix()) + targetElement->setPrefix(getElementQName().getPrefix()); + + if (m_schemaLocation) { + static const XMLCh schemaLocation[]= UNICODE_LITERAL_14(s,c,h,e,m,a,L,o,c,a,t,i,o,n); + if (targetElement->getParentNode()==NULL || targetElement->getParentNode()->getNodeType()==DOMNode::DOCUMENT_NODE) + targetElement->setAttributeNS(XSI_NS,schemaLocation,m_schemaLocation); + } + + marshallElementType(targetElement); + marshallNamespaces(targetElement); + marshallAttributes(targetElement); + marshallContent(targetElement); + +#ifndef XMLTOOLING_NO_XMLSEC + if (sigs) { + for_each(sigs->begin(),sigs->end(),mem_fun(&Signature::sign)); + } +#endif +} + +void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) const +{ + const QName* type = getSchemaType(); + if (type) { + m_log.debug("setting xsi:type attribute for XMLObject"); + + const XMLCh* typeLocalName = type->getLocalPart(); + if (!typeLocalName || !*typeLocalName) { + throw MarshallingException("Schema type of XMLObject may not have an empty local name."); + } + + static const XMLCh xsitype[] = { + chLatin_x, chLatin_s, chLatin_i, chColon, chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull + }; + + XMLCh* xsivalue=const_cast(typeLocalName); + const XMLCh* prefix=type->getPrefix(); + if (prefix && *prefix) { + xsivalue=new XMLCh[XMLString::stringLen(typeLocalName) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)]; + *xsivalue=chNull; + XMLString::catString(xsivalue,prefix); + static const XMLCh colon[] = {chColon, chNull}; + XMLString::catString(xsivalue,colon); + XMLString::catString(xsivalue,typeLocalName); + } + domElement->setAttributeNS(XSI_NS, xsitype, xsivalue); + if (xsivalue != typeLocalName) + XMLString::release(&xsivalue); + + m_log.debug("Adding XSI namespace to list of namespaces used by XMLObject"); + addNamespace(Namespace(XSI_NS, XSI_PREFIX)); + } +} + +class _addns : public binary_function { +public: + void operator()(DOMElement* domElement, const Namespace& ns) const { + const XMLCh* prefix=ns.getNamespacePrefix(); + const XMLCh* uri=ns.getNamespaceURI(); + + // Check to see if the prefix is already declared properly above this node. + if (!ns.alwaysDeclare()) { + const XMLCh* declared=lookupNamespaceURI(domElement->getParentNode(),prefix); + if (declared && XMLString::equals(declared,uri)) + return; + } + + if (prefix && *prefix) { + XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)]; + *xmlns=chNull; + XMLString::catString(xmlns,XMLNS_PREFIX); + static const XMLCh colon[] = {chColon, chNull}; + XMLString::catString(xmlns,colon); + XMLString::catString(xmlns,prefix); + domElement->setAttributeNS(XMLNS_NS, xmlns, uri); + XMLString::release(&xmlns); + } + else { + domElement->setAttributeNS(XMLNS_NS, XMLNS_PREFIX, uri); + } + } + + const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const { + // Return NULL if no declaration in effect. The empty string signifies the null namespace. + if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE) { + // At the root, the default namespace is set to the null namespace. + if (!prefix || !*prefix) + return &chNull; + return NULL; // we're done + } + DOMNamedNodeMap* attributes = static_cast(n)->getAttributes(); + if (!attributes) + return lookupNamespaceURI(n->getParentNode(),prefix); // defer to parent + DOMNode* childNode; + DOMAttr* attribute; + for (XMLSize_t i=0; igetLength(); i++) { + childNode = attributes->item(i); + if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) // not an attribute? + continue; + attribute = static_cast(childNode); + if (!XMLString::equals(attribute->getNamespaceURI(),XMLNS_NS)) + continue; // not a namespace declaration + // Local name should be the prefix and the value would be the URI, except for the default namespace. + if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX)) + return attribute->getNodeValue(); + else if (XMLString::equals(prefix,attribute->getLocalName())) + return attribute->getNodeValue(); + } + // Defer to parent. + return lookupNamespaceURI(n->getParentNode(),prefix); + } +}; + +void AbstractXMLObjectMarshaller::marshallNamespaces(DOMElement* domElement) const +{ + m_log.debug("marshalling namespace attributes for XMLObject"); + const set& namespaces = getNamespaces(); + for_each(namespaces.begin(),namespaces.end(),bind1st(_addns(),domElement)); +} + +void AbstractXMLObjectMarshaller::marshallContent(DOMElement* domElement) const +{ + m_log.debug("marshalling text and child elements for XMLObject"); + + const XMLCh* val; + unsigned int pos=0; + const list& children=getOrderedChildren(); + for (list::const_iterator i=children.begin(); i!=children.end(); ++i, ++pos) { + val = getTextContent(pos); + if (val && *val) + domElement->appendChild(domElement->getOwnerDocument()->createTextNode(val)); + if (*i) + (*i)->marshall(domElement); + } + val = getTextContent(pos); + if (val && *val) + domElement->appendChild(domElement->getOwnerDocument()->createTextNode(val)); +}