package edu.stanford.rt.credential; import java.util.*; import edu.stanford.rt.util.*; import edu.stanford.rt.datatype.*; /** * @author Ninghui Li, Sandra Qiu
* * Implementation of RoleTerm element. Each RoleTerm object * has a reference to its corresponding RoleDeclaration object and stores * its parameter values, if any. * */ public class RoleTerm { // The corresponding role declaration. private RoleDeclaration roleDeclaration; // Stores parameter values according to the prefix positions in role declaration. private ValueSet[] paramValues; /** * Constructor for RoleTerm. * * @param roleDeclaration * corresponding role declaration. */ public RoleTerm(RoleDeclaration roleDeclaration) { this.roleDeclaration = roleDeclaration; this.paramValues = new ValueSet[roleDeclaration.getTotalPrefixes() + 1]; this.paramValues[0] = null; } /** * Returns the roleDeclaration. * @return RoleDeclaration * The corresonding role decalration for this role term. */ public RoleDeclaration getRoleDeclaration() { return roleDeclaration; } /** * Method putParameterValue. * puts parameter value according to the position of the * its prefix. * @param prefix * the parameter prefix * @param parameterValue * the parameter value */ public void putParameterValue( String prefix, ValueSet parameterValue) { //RTUtil.debugInfo("RoleTerm.putParameterValue().roleDeclaration = " // +roleDeclaration.getName()); RTUtil.debugInfo( "RoleTerm.putParameterValue().prefix = " + prefix); int index = roleDeclaration.getPosition(prefix); RTUtil.debugInfo( "RoleTerm.putParameterValue().index = " + index); // empty element for index 0 paramValues[index] = parameterValue; } /** * Method getParameterValue. * @param prefix * @return ValueSet */ public ValueSet getParameterValue(String prefix) { int index = roleDeclaration.getPosition(prefix); return paramValues[index]; } /** * Method getParameterValueAt. * @param index * @return ValueSet */ public ValueSet getParameterValueAt(int index) { return paramValues[index]; } /** * Method getParameterType. * @param parameterName * @return DataType * @throws DomainSpecException */ public DataType getParameterType(String parameterName) throws DomainSpecException { return roleDeclaration.getParameterType(parameterName); } public String toString(String indent) { String thisIndent = indent + " "; StringBuffer sb = new StringBuffer(); sb.append(thisIndent).append("RoleTerm: \n"); sb.append(thisIndent + " ").append( "Corresponding role declaration: "); sb.append(roleDeclaration.getName()).append("\n"); sb.append(thisIndent + " ").append("Parameters: \n"); for (int i = 1; i < paramValues.length; i++) { if (paramValues[i] != null && !isParameterValueNull(paramValues[i])) sb .append(thisIndent + " ") .append(paramValues[i].toString(thisIndent)) .append("\n"); } return sb.toString(); } /** * Method isParameterValueNull. * checks if the parameter value is null. It can be null when * the parameter has Equals or Special Principal * child element. * @param paramValue * @return boolean */ public boolean isParameterValueNull(ValueSet paramValue) { if (paramValue == null) return true; if (!(paramValue instanceof TreeValueSet) && !(paramValue instanceof IntervalValueSet) && !(paramValue instanceof SetValueSet) && !(paramValue instanceof SingletonValueSet)) return true; else return false; } }