/*
 * 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.
 */


/* QName.cpp - QName wrapper class

   Scott Cantor
   5/6/02

   $History:$
*/

#include "internal.h"

using namespace saml;
using namespace std;


saml::QName::QName(const XMLCh* nsURI, const XMLCh* localName)
{
    if (!localName || *localName==chNull)
        throw SAMLException("QName() requires local name");
    if (nsURI)
        m_namespace=nsURI;
    m_localName=localName;
}

saml::QName::~QName() {}

const XMLCh* saml::QName::getNamespaceURI() const
{
    return m_namespace.c_str();
}

const XMLCh* saml::QName::getLocalName() const
{
    return m_localName.c_str();
}

saml::QName* saml::QName::getQNameAttribute(DOMElement* e, const XMLCh* nsURI, const XMLCh* localName)
{
    const XMLCh* qval = e->getAttributeNS(nsURI,localName);
    if (!qval || !*qval)
        return NULL;
    return new saml::QName(saml::QName::getNamespaceForQName(qval,e),qval+XMLString::indexOf(qval,chColon) + 1);
}

saml::QName* saml::QName::getQNameTextNode(DOMText* t)
{
    const XMLCh* qval = t->getNodeValue();
    DOMNode* n = t->getParentNode();
    if (!qval || !*qval || !n || n->getNodeType()!=DOMNode::ELEMENT_NODE)
        return NULL;
    return new saml::QName(saml::QName::getNamespaceForQName(qval,static_cast<DOMElement*>(n)),
                            qval+XMLString::indexOf(qval,chColon) + 1);
}

const XMLCh* saml::QName::getNamespaceForQName(const XMLCh* qname, DOMElement* e)
{
    int i;
    if (qname && (i=XMLString::indexOf(qname,chColon))>0)
    {
        auto_ptr<XMLCh> prefix(new XMLCh[i+1]);
        XMLString::subString(prefix.get(),qname,0,i);
        (prefix.get())[i]=chNull;
        return getNamespaceForPrefix(prefix.get(),e);
    }
    return getNamespaceForPrefix(NULL,e);
}

const XMLCh* saml::QName::getNamespaceForPrefix(const XMLCh* prefix, DOMElement* e)
{
    DOMNode* n = e;
    const XMLCh* ns = NULL;

    if (prefix)
    {
        if (!XMLString::compareString(prefix,L(xml)))
            return XML::XML_NS;
        else if (!XMLString::compareString(prefix,L(xmlns)))
            return XML::XMLNS_NS;
    }

    while ((!ns || !*ns) && n && n->getNodeType() == DOMNode::ELEMENT_NODE)
    {
        ns = (static_cast<DOMElement*>(n))->getAttributeNS(XML::XMLNS_NS,prefix ? prefix : L(xmlns));
        n = n->getParentNode();
    }
    return ns;
}

bool saml::operator<(const saml::QName& op1, const saml::QName& op2)
{
    int i=XMLString::compareString(op1.getNamespaceURI(),op2.getNamespaceURI());
    if (i<0)
        return true;
    else if (i==0)
        return (XMLString::compareString(op1.getLocalName(),op2.getLocalName())<0);
    else
        return false;
}
