/*
 * The OpenSAML License, Version 1.
 * Copyright (c) 2002
 * University Corporation for Advanced Internet Development, Inc.
 * All rights reserved
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution, if any, must include
 * the following acknowledgment: "This product includes software developed by
 * the University Corporation for Advanced Internet Development
 * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
 * may appear in the software itself, if and wherever such third-party
 * acknowledgments normally appear.
 *
 * Neither the name of OpenSAML nor the names of its contributors, nor
 * Internet2, nor the University Corporation for Advanced Internet Development,
 * Inc., nor UCAID may be used to endorse or promote products derived from this
 * software without specific prior written permission. For written permission,
 * please contact opensaml@opensaml.org
 *
 * Products derived from this software may not be called OpenSAML, Internet2,
 * UCAID, or the University Corporation for Advanced Internet Development, nor
 * may OpenSAML appear in their name, without prior written permission of the
 * University Corporation for Advanced Internet Development.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
 * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
 * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/* SAMLAttributeStatement.cpp - SAML attribute statement implementation

   Scott Cantor
   5/23/02

   $History:$
*/

#include "internal.h"

using namespace saml;
using namespace std;


SAMLAttributeStatement::SAMLAttributeStatement(SAMLSubject* subject, const Iterator<SAMLAttribute*>& attributes)
{
    RTTI(SAMLAttributeStatement);
    if (!subject || attributes.size()==0)
        throw SAMLException(SAMLException::RESPONDER,"SAMLAttributeStatement() requires subject and at least one attribute");
    m_subject=subject;
    while (attributes.hasNext())
        m_attributes.push_back(attributes.next());
}

SAMLAttributeStatement::SAMLAttributeStatement(DOMElement* e) : m_subject(NULL)
{
    RTTI(SAMLAttributeStatement);
    fromDOM(e);
}

SAMLAttributeStatement::SAMLAttributeStatement(istream& in) : SAMLStatement(in), m_subject(NULL)
{
    RTTI(SAMLAttributeStatement);
    fromDOM(m_document->getDocumentElement());
}

SAMLAttributeStatement::~SAMLAttributeStatement()
{
    delete m_subject;
    for (vector<SAMLAttribute*>::const_iterator i=m_attributes.begin(); i!=m_attributes.end(); i++)
        delete (*i);
}

SAMLObject* SAMLAttributeStatement::clone() const
{
    return new SAMLAttributeStatement(static_cast<SAMLSubject*>(m_subject->clone()),getAttributes().clone());
}

void SAMLAttributeStatement::fromDOM(DOMElement* e)
{
    NDC ndc("fromDOM");
    SAMLObject::fromDOM(e);

    if (SAMLConfig::getConfig().strict_dom_checking)
    {
        if (XMLString::compareString(XML::SAML_NS,e->getNamespaceURI()))
            throw MalformedException(SAMLException::RESPONDER,"SAMLAttributeStatement::fromDOM() missing saml namespace on root element");

        if (XMLString::compareString(L(AttributeStatement),e->getLocalName()))
        {
            auto_ptr<saml::QName> type(saml::QName::getQNameAttribute(e,XML::XSI_NS,L(type)));
            if ((XMLString::compareString(L(Statement),e->getLocalName()) && XMLString::compareString(L(SubjectStatement),e->getLocalName())) ||
                    !type.get() || XMLString::compareString(XML::SAML_NS,type->getNamespaceURI()) ||
                    XMLString::compareString(L(AttributeStatementType),type->getLocalName()))
                throw MalformedException(SAMLException::RESPONDER, "SAMLAttributeStatement::fromDOM() requires saml:AttributeStatement at root");
        }
    }
    m_root=e;
    m_bOwnStrings=false;

    DOMNode* n=e->getFirstChild();
    while (n && n->getNodeType()!=DOMNode::ELEMENT_NODE)
        n=n->getNextSibling();

    m_subject=new SAMLSubject(static_cast<DOMElement*>(n));

    DOMNodeList* nlist = e->getElementsByTagNameNS(XML::SAML_NS,L(Attribute));
    for (int i=0; nlist && i<nlist->getLength(); i++)
    {
        DOMElement* a=static_cast<DOMElement*>(nlist->item(i));
        try
        {
            m_attributes.push_back(SAMLAttribute::getInstance(a));
        }
        catch (SAMLException& e)
        {
            SAML_log.warn("exception while instantiating a SAMLAttribute: %s",e.what());
        }
    }
}

DOMNode* SAMLAttributeStatement::toDOM(DOMDocument* doc, bool xmlns) const
{
    SAMLObject::toDOM(doc,xmlns);
    if (m_root)
    {
        if (xmlns)
        {
            static_cast<DOMElement*>(m_root)->setAttributeNS(XML::XMLNS_NS,L(xmlns),XML::SAML_NS);
            static_cast<DOMElement*>(m_root)->setAttributeNS(XML::XMLNS_NS,L_QNAME(xmlns,xsi),XML::XSI_NS);
            static_cast<DOMElement*>(m_root)->setAttributeNS(XML::XMLNS_NS,L_QNAME(xmlns,xsd),XML::XSD_NS);
        }
        return m_root;
    }

    if (m_attributes.empty())
        throw MalformedException(SAMLException::RESPONDER,"SAMLAttributeStatement::toDOM() requires at least one attribute");

    if (!doc)
        doc=m_document;

    // Construct an AttributeStatement.
    DOMElement* s=doc->createElementNS(XML::SAML_NS,L(AttributeStatement));
    if (xmlns)
        s->setAttributeNS(XML::XMLNS_NS,L(xmlns),XML::SAML_NS);
    s->appendChild(m_subject->toDOM(doc,false));

    for (vector<SAMLAttribute*>::const_iterator i=m_attributes.begin(); i!=m_attributes.end(); i++)
        s->appendChild((*i)->toDOM(doc,false));

    return m_root=s;
}
