X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Fio%2FAbstractXMLObjectMarshaller.cpp;h=c715c26c3807e9c4a70d7b81ffeb50149999700b;hb=b79dc3af7d8eafeff3f119a4a67f734a83f29cb9;hp=d2cd392d10a26f89122c1ad9240adf7f41768f8b;hpb=e7a65d784215bc04355f014141219b3e7ab4559a;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/io/AbstractXMLObjectMarshaller.cpp b/xmltooling/io/AbstractXMLObjectMarshaller.cpp index d2cd392..c715c26 100644 --- a/xmltooling/io/AbstractXMLObjectMarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectMarshaller.cpp @@ -1,5 +1,5 @@ /* -* Copyright 2001-2006 Internet2 +* 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. @@ -24,6 +24,7 @@ #include "exceptions.h" #include "io/AbstractXMLObjectMarshaller.h" #ifndef XMLTOOLING_NO_XMLSEC + #include "security/Credential.h" #include "signature/Signature.h" #endif #include "util/NDC.h" @@ -33,21 +34,19 @@ #include #include #include -#include #ifndef XMLTOOLING_NO_XMLSEC using namespace xmlsignature; #endif +using namespace xmlconstants; using namespace xmltooling; -using namespace log4cpp; using namespace std; -#define XT_log (*static_cast(m_log)) - DOMElement* AbstractXMLObjectMarshaller::marshall( DOMDocument* document #ifndef XMLTOOLING_NO_XMLSEC - ,const std::vector* sigs + ,const vector* sigs + ,const Credential* credential #endif ) const { @@ -55,14 +54,14 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( xmltooling::NDC ndc("marshall"); #endif - if (XT_log.isDebugEnabled()) { - XT_log.debug("starting to marshal %s", getElementQName().toString().c_str()); + 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()) { - XT_log.debug("XMLObject has a usable cached DOM, reusing it"); + m_log.debug("XMLObject has a usable cached DOM, reusing it"); if (document) setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM); releaseParentDOM(true); @@ -88,18 +87,18 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( XercesJanitor janitor(bindDocument ? document : NULL); - XT_log.debug("creating root element to marshall"); + 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); + marshallInto(domElement, sigs, credential); #else marshallInto(domElement); #endif //Recache the DOM. - XT_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not "); + m_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not "); setDOM(domElement, bindDocument); janitor.release(); // safely transferred releaseParentDOM(true); @@ -110,7 +109,8 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( DOMElement* AbstractXMLObjectMarshaller::marshall( DOMElement* parentElement #ifndef XMLTOOLING_NO_XMLSEC - ,const std::vector* sigs + ,const vector* sigs + ,const Credential* credential #endif ) const { @@ -118,14 +118,14 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( xmltooling::NDC ndc("marshall"); #endif - if (XT_log.isDebugEnabled()) { - XT_log.debug("starting to marshalling %s", getElementQName().toString().c_str()); + 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()) { - XT_log.debug("XMLObject has a usable cached DOM, reusing it"); + m_log.debug("XMLObject has a usable cached DOM, reusing it"); if (parentElement!=cachedDOM->getParentNode()) { parentElement->appendChild(cachedDOM); releaseParentDOM(true); @@ -143,19 +143,19 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( } // 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"); + 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); + marshallInto(domElement, sigs, credential); #else marshallInto(domElement); #endif //Recache the DOM. - XT_log.debug("caching DOM for XMLObject"); + m_log.debug("caching DOM for XMLObject"); setDOM(domElement, false); releaseParentDOM(true); @@ -165,28 +165,45 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( void AbstractXMLObjectMarshaller::marshallInto( DOMElement* targetElement #ifndef XMLTOOLING_NO_XMLSEC - ,const std::vector* sigs + ,const vector* sigs + ,const Credential* credential #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(XMLConstants::XSI_NS,schemaLocation,m_schemaLocation); + if (m_schemaLocation || m_noNamespaceSchemaLocation) { + static const XMLCh schemaLocation[] = { + chLatin_x, chLatin_s, chLatin_i, chColon, + chLatin_s, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a, + chLatin_L, chLatin_o, chLatin_c, chLatin_a, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chNull + }; + static const XMLCh noNamespaceSchemaLocation[] = { + chLatin_x, chLatin_s, chLatin_i, chColon, + chLatin_n, chLatin_o, chLatin_N, chLatin_a, chLatin_m, chLatin_e, chLatin_s, chLatin_p, chLatin_a, chLatin_c, chLatin_e, + chLatin_S, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_a, + chLatin_L, chLatin_o, chLatin_c, chLatin_a, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chNull + }; + if (targetElement->getParentNode()==NULL || targetElement->getParentNode()->getNodeType()==DOMNode::DOCUMENT_NODE) { + if (m_schemaLocation) + targetElement->setAttributeNS(XSI_NS,schemaLocation,m_schemaLocation); + if (m_noNamespaceSchemaLocation) + targetElement->setAttributeNS(XSI_NS,noNamespaceSchemaLocation,m_noNamespaceSchemaLocation); + } } marshallElementType(targetElement); marshallNamespaces(targetElement); marshallAttributes(targetElement); - marshallContent(targetElement); #ifndef XMLTOOLING_NO_XMLSEC + marshallContent(targetElement,credential); if (sigs) { - for_each(sigs->begin(),sigs->end(),mem_fun(&Signature::sign)); + for_each(sigs->begin(),sigs->end(),bind2nd(mem_fun1_t(&Signature::sign),credential)); } +#else + marshallContent(targetElement); #endif } @@ -194,7 +211,7 @@ void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) co { const QName* type = getSchemaType(); if (type) { - XT_log.debug("setting xsi:type attribute for XMLObject"); + m_log.debug("setting xsi:type attribute for XMLObject"); const XMLCh* typeLocalName = type->getLocalPart(); if (!typeLocalName || !*typeLocalName) { @@ -215,12 +232,12 @@ void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) co XMLString::catString(xsivalue,colon); XMLString::catString(xsivalue,typeLocalName); } - domElement->setAttributeNS(XMLConstants::XSI_NS, xsitype, xsivalue); + domElement->setAttributeNS(XSI_NS, xsitype, xsivalue); if (xsivalue != typeLocalName) XMLString::release(&xsivalue); - XT_log.debug("Adding XSI namespace to list of namespaces used by XMLObject"); - addNamespace(Namespace(XMLConstants::XSI_NS, XMLConstants::XSI_PREFIX)); + m_log.debug("adding XSI namespace to list of namespaces used by XMLObject"); + addNamespace(Namespace(XSI_NS, XSI_PREFIX)); } } @@ -238,16 +255,17 @@ public: } if (prefix && *prefix) { - XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLConstants::XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)]; + XMLCh* xmlns=new XMLCh[XMLString::stringLen(XMLNS_PREFIX) + XMLString::stringLen(prefix) + 2*sizeof(XMLCh)]; *xmlns=chNull; - XMLString::catString(xmlns,XMLConstants::XMLNS_PREFIX); + XMLString::catString(xmlns,XMLNS_PREFIX); static const XMLCh colon[] = {chColon, chNull}; XMLString::catString(xmlns,colon); XMLString::catString(xmlns,prefix); - domElement->setAttributeNS(XMLConstants::XMLNS_NS, xmlns, uri); + domElement->setAttributeNS(XMLNS_NS, xmlns, uri); + XMLString::release(&xmlns); } else { - domElement->setAttributeNS(XMLConstants::XMLNS_NS, XMLConstants::XMLNS_PREFIX, uri); + domElement->setAttributeNS(XMLNS_NS, XMLNS_PREFIX, uri); } } @@ -269,10 +287,10 @@ public: if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) // not an attribute? continue; attribute = static_cast(childNode); - if (!XMLString::equals(attribute->getNamespaceURI(),XMLConstants::XMLNS_NS)) + 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(),XMLConstants::XMLNS_PREFIX)) + if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX)) return attribute->getNodeValue(); else if (XMLString::equals(prefix,attribute->getLocalName())) return attribute->getNodeValue(); @@ -284,14 +302,19 @@ public: void AbstractXMLObjectMarshaller::marshallNamespaces(DOMElement* domElement) const { - XT_log.debug("marshalling namespace attributes for XMLObject"); + 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 +void AbstractXMLObjectMarshaller::marshallContent( + DOMElement* domElement +#ifndef XMLTOOLING_NO_XMLSEC + ,const Credential* credential +#endif + ) const { - XT_log.debug("marshalling text and child elements for XMLObject"); + m_log.debug("marshalling text and child elements for XMLObject"); const XMLCh* val; unsigned int pos=0; @@ -300,8 +323,13 @@ void AbstractXMLObjectMarshaller::marshallContent(DOMElement* domElement) const val = getTextContent(pos); if (val && *val) domElement->appendChild(domElement->getOwnerDocument()->createTextNode(val)); - if (*i) + if (*i) { +#ifndef XMLTOOLING_NO_XMLSEC + (*i)->marshall(domElement,NULL,credential); +#else (*i)->marshall(domElement); +#endif + } } val = getTextContent(pos); if (val && *val)