Chris PeBenito 36e54b
/* Copyright (C) 2005 Tresys Technology, LLC
Chris PeBenito 36e54b
 * License: refer to COPYING file for license information.
Chris PeBenito 36e54b
 * Authors: Spencer Shimko <sshimko@tresys.com>
Chris PeBenito 36e54b
 * 
Chris PeBenito 36e54b
 * PolicyElement.java: The reference policy base class		
Chris PeBenito 36e54b
 * Version: @version@ 
Chris PeBenito 36e54b
 */
Chris PeBenito 36e54b
package policy;
Chris PeBenito 36e54b
Chris PeBenito 36e54b
import java.util.Map;
Chris PeBenito 36e54b
import java.util.TreeMap;
Chris PeBenito 36e54b
Chris PeBenito 36e54b
/**
Chris PeBenito 36e54b
 * Reference policy elements have certain similarties which
Chris PeBenito 36e54b
 * should be inherited from a base class.  This includes a name
Chris PeBenito 36e54b
 * attribute that will be used for hashing, sorting, and converting
Chris PeBenito 36e54b
 * to a string.  
Chris PeBenito 36e54b
 * 
Chris PeBenito 36e54b
 * @see Layer
Chris PeBenito 36e54b
 * @see Module
Chris PeBenito 36e54b
 * @see Interface
Chris PeBenito 36e54b
 * @see Parameter
Chris PeBenito 36e54b
 */
Chris PeBenito 36e54b
public abstract class PolicyElement {
Chris PeBenito 36e54b
	/** the string identifying the element */
Chris PeBenito 36e54b
	public final String Name;
Chris PeBenito 36e54b
	/** the parent component */
Chris PeBenito 36e54b
	public final PolicyElement Parent;
Chris PeBenito 36e54b
Chris PeBenito 36e54b
	/** the attributes of this element */
Chris PeBenito 36e54b
	private final Map<String,String> attributes;
Chris PeBenito 36e54b
	
Chris PeBenito 36e54b
	public String PCDATA;
Chris PeBenito 36e54b
Chris PeBenito 36e54b
	/**
Chris PeBenito 36e54b
	 * Default constructor assigns name.
Chris PeBenito 36e54b
	 * 
Chris PeBenito 36e54b
	 * @param _name		The name of the element.
Chris PeBenito 36e54b
	 * @param _Parent 	The reference to the parent element.
Chris PeBenito 36e54b
	 */
Chris PeBenito 36e54b
	public PolicyElement(String _name, PolicyElement _Parent){
Chris PeBenito 36e54b
		Name = _name;
Chris PeBenito 36e54b
		Parent = _Parent;
Chris PeBenito 36e54b
		attributes = new TreeMap<String,String>();
Chris PeBenito 36e54b
	}
Chris PeBenito 36e54b
	
Chris PeBenito 36e54b
	/**
Chris PeBenito 36e54b
	 * Add attribute to policy element.
Chris PeBenito 36e54b
	 * 
Chris PeBenito 36e54b
	 * @param aName		String to identify attribute
Chris PeBenito 36e54b
	 * @param aString 	Value of attribute identified by string aName
Chris PeBenito 36e54b
	 * @return 			true when attribute added to element,
Chris PeBenito 36e54b
	 * 					false when attribute already defined for element 
Chris PeBenito 36e54b
	 */
Chris PeBenito 36e54b
	public boolean AddAttribute(String aName, String aString){
Chris PeBenito 36e54b
		if (attributes.containsKey(aName)){
Chris PeBenito 36e54b
			return false;
Chris PeBenito 36e54b
		}
Chris PeBenito 36e54b
		attributes.put(aName,aString);
Chris PeBenito 36e54b
		return true;
Chris PeBenito 36e54b
	}
Chris PeBenito 36e54b
	
Chris PeBenito 36e54b
	/**
Chris PeBenito 36e54b
	 * Get attribute from policy element.
Chris PeBenito 36e54b
	 * 
Chris PeBenito 36e54b
	 * @param aName		String to identify attribute
Chris PeBenito 36e54b
	 * @return 			String value associated with named attribute
Chris PeBenito 36e54b
	 * 					or null if no attribute defined
Chris PeBenito 36e54b
	 * 					for aName. 
Chris PeBenito 36e54b
	 */
Chris PeBenito 36e54b
	public String GetAttribute(String aName){
Chris PeBenito 36e54b
		return attributes.get(aName);
Chris PeBenito 36e54b
	}
Chris PeBenito 36e54b
Chris PeBenito 36e54b
	/**
Chris PeBenito 36e54b
	 * Overridden equals method
Chris PeBenito 36e54b
	 * 
Chris PeBenito 36e54b
	 * @param obj	Object for comparison
Chris PeBenito 36e54b
	 */
Chris PeBenito 36e54b
	@Override public boolean equals(Object obj){
Chris PeBenito 36e54b
		return obj.toString().equals(this.Name);
Chris PeBenito 36e54b
	}
Chris PeBenito 36e54b
Chris PeBenito 36e54b
	/**
Chris PeBenito 36e54b
	 * Return a hashcode for the element. Currently implemented as a 
Chris PeBenito 36e54b
	 * call to the String hashCode() method.
Chris PeBenito 36e54b
	 * 
Chris PeBenito 36e54b
	 * @return integer hashCode of this instance assuring two objects that
Chris PeBenito 36e54b
	 * 		   are == will return same hashcode.
Chris PeBenito 36e54b
	 */
Chris PeBenito 36e54b
	@Override public int hashCode() {
Chris PeBenito 36e54b
		return this.toString().hashCode();
Chris PeBenito 36e54b
	}
Chris PeBenito 36e54b
	
Chris PeBenito 36e54b
	/**
Chris PeBenito 36e54b
	 * Overridden toString() method.
Chris PeBenito 36e54b
	 * 
Chris PeBenito 36e54b
	 * @return String representation of instance (it's name).
Chris PeBenito 36e54b
	 */
Chris PeBenito 36e54b
	@Override public String toString(){
Chris PeBenito 36e54b
			return Name;
Chris PeBenito 36e54b
	}		
Chris PeBenito 36e54b
	
Chris PeBenito 36e54b
	/**
Chris PeBenito 36e54b
	 * Since Component might be used as a key in some type of sort Compare
Chris PeBenito 36e54b
	 * is implemented.
Chris PeBenito 36e54b
	 * 
Chris PeBenito 36e54b
	 * @return	0 if this==that, <0 if this<that, >0 if this>that
Chris PeBenito 36e54b
	 * @see String.compareTo
Chris PeBenito 36e54b
	 */
Chris PeBenito 36e54b
	public int compareTo(Layer that){
Chris PeBenito 36e54b
		return Name.compareTo(that.Name);
Chris PeBenito 36e54b
	}
Chris PeBenito 36e54b
}