// 
// The Waveset SPML General License 
// 
// Version 0.1, April 2003
// Copyright (C) 2003 Waveset Technologies, Inc..
// 6034 West Courtyard Drive, Suite 210, Austin, Texas 78730
// All rights reserved.
// 
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// 
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions, and the disclaimers in Sections 6
//    and 7 below.
// 
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the disclaimers in Sections 6
//    and 7 below in the documentation and/or other materials provided
//    with the distribution.
// 
// 3. The end-user documentation included with the redistribution, if
//    any, must include the following acknowledgment:
// 
//    "This product includes software developed by 
//     Waveset Technologies, Inc. (www.waveset.com)."
// 
//    Alternately, this acknowledgment may appear in the software itself, if
//    and wherever such third-party acknowledgments normally appear.
//  
// 4. The names "Waveset" and "Waveset Technologies, Inc." must not be
//    used to endorse or promote products derived from this software
//    without the prior written permission of Waveset. For written
//    permission, please contact www.waveset.com.
//  
// 5. Products derived from this software may not be called "Waveset",
//    nor may "Waveset" appear in their name, without the prior written
//    permission of Waveset.
//  
// 6. NO WARRANTY 
// 
//    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED
//    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//    DISCLAIMED.
// 
// 7.  LIMITATION OF LIABILITY
// 
//    IN NO EVENT SHALL THE WAVESET OR ITS LICENSORS 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.
// 
// End of Terms and Conditions
// ====================================================================
//
// Component Name: org.openspml.message.AddRequest
//
// Author(s): Jeff Larson
//
// Description:
//
// Memory representation for an <identifier> element.
//
//

package org.openspml.message;

import java.util.*;
import org.openspml.util.*;

/**
 * A class used to represent an <code>identifier</code> element, 
 * found in several contexts within SPML messages.
 */
public class Identifier implements Constants {

    //////////////////////////////////////////////////////////////////////
    //
    // Fields
    //
    //////////////////////////////////////////////////////////////////////

    static final String ELEMENT = "identifier";

    /**
     * Valid values for the "identifierType" attribute.
     */
    public static final String TYPE_EMailAddress = 
    ENUM_PREFIX + "EMailAddress";

    public static final String TYPE_DN = 
    ENUM_PREFIX + "DN";

    public static final String TYPE_UserIDAndOrDomainName = 
    ENUM_PREFIX + "UserIDAndOrDomainName";

    public static final String TYPE_LibertyUniqueID = 
    ENUM_PREFIX + "LibertyUniqueID";

    public static final String TYPE_PassportUniqueID = 
    ENUM_PREFIX + "PassportUniqueID";

    public static final String TYPE_GUID = 
    ENUM_PREFIX + "GUID";

    public static final String TYPE_URN = 
    ENUM_PREFIX + "URN";

    public static final String TYPE_GenericString = 
    ENUM_PREFIX + "GenericString";

    public static final String TYPE_SAMLSubject = 
    ENUM_PREFIX + "SAMLSubject";

    public static final String TYPE_OID = 
    ENUM_PREFIX + "OID";

    /**
     * Identifier Type.  If null default to TYPE_GenericString.
     */
    String _type;

    /**
     * Text for simple identifiers.
     */
    String _id;

    /**
     * Optional attributes.
     */
    List _attributes;

    /**
     * Arbitrary DOM for complex identifiers.
     * Should have explicit support for SAMLSubjects.
     */
    // Element _identifierElement;

    //////////////////////////////////////////////////////////////////////
    //
    // Constructors
    //
    //////////////////////////////////////////////////////////////////////

    public Identifier() {
    }

    Identifier(XmlElement e) {
	parseXml(e);
    }

    public Identifier(String id) {
        _id = id;
    }

    public void setType(String s) {
        _type = s;
    }

    public void setId(String s) {
        _id = s;
    }

    public void setAttributes(List l) {
        _attributes = l;
    }

    public void setAttribute(Attribute a) {
        if (_attributes == null)
            _attributes = new ArrayList();
        _attributes.add(a);
    }

    //////////////////////////////////////////////////////////////////////
    //
    // Accessors
    //
    //////////////////////////////////////////////////////////////////////

    public String getId() {
        return _id;
    }

    public String getType() {
        return _type;
    }

    public List getAttributes() {
        return _attributes;
    }

    //////////////////////////////////////////////////////////////////////
    //
    // XML
    //
    //////////////////////////////////////////////////////////////////////

    void toXml(SpmlBuffer b) {
        toXml(b, ELEMENT);
    }

    void toXml(SpmlBuffer b, String wrapper) {

        String type = _type;
        if (type == null) {
	    // OpenNetworks defaults to GUID, use that
            // type = TYPE_GenericString;
            type = TYPE_GUID;
	}

        b.addOpenStartTag(wrapper);
        b.addAttribute("type", type);
        b.closeStartTag();
	b.incIndent();

        if (_id != null)
            b.addElement("id", _id);

        if (_attributes != null) {
            Iterator it = _attributes.iterator();
            while (it.hasNext()) {
                Attribute att = (Attribute)it.next();
                att.toXml(b);
            }
        }

        b.decIndent();
        b.addEndTag(wrapper);
    }

    void parseXml(XmlElement e) {
	
	_type = e.getAttribute("type");

	for (XmlElement child = e.getChildElement() ; 
	     child != null ; child = child.next()) {
	    String tag = child.getLocalName();

	    if (tag.equals("id"))
		_id = child.getContent();

	    else if (tag.equals(Attribute.ELEMENT))
		setAttribute(new Attribute(child));
	}
    }

}

