// 
// 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.AttributeDefinition
//
// Author(s): Jeff Larson
//
// Description:
//
// Memory representation for an <attributeDefinition> element.
//
//

package org.openspml.message;

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

/**
 * A class representing an <code>attributeDefinition</code> element.
 */
public class AttributeDefinition {

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

    static final String ELEMENT = "attributeDefinition";

    /**
     * This is typed as a dsml:AttributeDescriptionValue which is a 
     * string restricted by the regular expression:
     * ((([0-2](\.[0-9]+)+)|([a-zA-Z]+([a-zA-Z0-9]|[-])*))(;([a-zA-Z0-9]|[-])+)*)
     */
    String _name;

    /**
     * Optional description.
     */
    String _description;

    /**
     * Optional type.
     * There must be some recommended names here?
     */
    String _type;

    /**
     * True if the attribute supports multiple values.
     */
    boolean _multi;

    /**
     * Arbitrary properties.  
     * <p>
     * The spec has these as dsml:DsmlAttr which are essentially
     * just name/value pairs, so implementing as a Map.
     * Could do the same for other attributes and get rid
     * of the Attribute class, but that provides some handy
     * XML handling methods.
     */
    Map _properties;

    // NOTE: Spec has:
    // <xsd:anyAttribute namespace="##other" processContents="lax" />
    // potentially need to maintain an arbitrary attribute map
    //
    // Spec has:
    // <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="lax" />
    // potentially need to maintain list of DOM elements?

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

    public AttributeDefinition() {
    }

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

    public void setName(String s) {
	_name = s;
    }

    public void setDescription(String s) {
	_description = s;
    }

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

    public void setMulti(boolean b) {
	_multi = b;
    }

    public void setProperties(Map props) {
	_properties = props;
    }
    
    public void setProperty(String name, Object value) {
	if (_properties == null)
	    _properties = new HashMap();
	_properties.put(name, value);
    }
    
    //////////////////////////////////////////////////////////////////////
    //
    // Accessors
    //
    //////////////////////////////////////////////////////////////////////
    
    public String getName() {
	return _name;
    }

    public String getDescription() {
	return _description;
    }

    public String getType() {
	return _type;
    }

    public boolean isMulti() {
	return _multi;
    }

    public Map getProperties() {
	return _properties;
    }
    
    public Object getProperty(String name) {
	return (_properties != null) ? _properties.get(name) : null;
    }

    static public AttributeDefinition find(List defs, String name) {

        AttributeDefinition found = null;
        if (defs != null && name != null) {
            for (int i = 0 ; i < defs.size() ; i++) {
                AttributeDefinition ad = (AttributeDefinition)defs.get(i);
                if (name.equals(ad.getName())) {
                    found = ad;
                    break;
                }
            }
        }

        return found;
    }

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

    String getElementName() {
        return ELEMENT;
    }

    void toXml(SpmlBuffer b) {

        b.addOpenStartTag(ELEMENT);
	b.addAttribute("name", _name);
	b.addAttribute("description", _description);
	b.addAttribute("type", _type);
	b.addAttribute("multivalued", _multi);

        if (_properties == null)
            b.append("/>\n");
        else {
            b.closeStartTag();
            b.incIndent();

            Attribute.toXml(b, "properties", _properties);

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

    void parseXml(XmlElement e) {

	// in theory, need to recognize and maintain 
	// other arbitrary elements

	_name = e.getAttribute("name");
	_description = e.getAttribute("description");
	_multi = e.getBooleanAttribute("multivalued");
	_type = e.getAttribute("type");

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

	    String tag = child.getLocalName();

	    if (tag.equals("properties"))
                _properties = Attribute.parseMap(child);

	    // in theory, need to recognize and maintain 
	    // other arbitrary elements

        }
    }

}
