View Javadoc

    1   package net.sourceforge.pmd.properties;
    2
    3   import net.sourceforge.pmd.util.StringUtil;
    4
    5   /***
    6    * Defines a datatype that supports String values.
    7    * When capturing multiple values, all strings must be filtered by the delimiter character.
    8    *
    9    * @author Brian Remedios
    10   * @version $Revision$
    11   */
    12  public class StringProperty extends AbstractPMDProperty {
    13
    14  	private int preferredRowCount;
    15
    16  	public static final char defaultDelimiter = '|';
    17
    18  	/***
    19  	 * Constructor for StringProperty.
    20  	 * @param theName String
    21  	 * @param theDescription String
    22  	 * @param theDefaultValue String
    23  	 * @param theUIOrder float
    24  	 */
    25  	public StringProperty(String theName, String theDescription, String theDefaultValue, float theUIOrder) {
    26  		this(theName, theDescription, theDefaultValue, theUIOrder, defaultDelimiter);
    27
    28  		maxValueCount(1);
    29  	}
    30
    31  	/***
    32  	 * Constructor for StringProperty.
    33  	 * @param theName String
    34  	 * @param theDescription String
    35  	 * @param theValues String[]
    36  	 * @param theUIOrder float
    37  	 * @param aMultiValueDelimiter String
    38  	 */
    39  	public StringProperty(String theName, String theDescription, String[] theValues, float theUIOrder, char aMultiValueDelimiter) {
    40  		super(theName, theDescription, theValues, theUIOrder);
    41
    42  		maxValueCount(Integer.MAX_VALUE);
    43  		multiValueDelimiter(aMultiValueDelimiter);
    44  	}
    45
    46  	/***
    47  	 * Constructor for StringProperty.
    48  	 * @param theName String
    49  	 * @param theDescription String
    50  	 * @param theDefaultValue Object
    51  	 * @param theUIOrder float
    52  	 * @param aMultiValueDelimiter String
    53  	 */
    54  	protected StringProperty(String theName, String theDescription, Object theDefaultValue, float theUIOrder, char aMultiValueDelimiter) {
    55  		super(theName, theDescription, theDefaultValue, theUIOrder);
    56
    57  		maxValueCount(Integer.MAX_VALUE);
    58  		multiValueDelimiter(aMultiValueDelimiter);
    59  	}
    60
    61  	/***
    62  	 * Method type.
    63  	 * @return Class
    64  	 * @see net.sourceforge.pmd.PropertyDescriptor#type()
    65  	 */
    66  	public Class type() {
    67  		return String.class;
    68  	}
    69
    70  	/***
    71  	 * Method valueFrom.
    72  	 * @param valueString String
    73  	 * @return Object
    74  	 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
    75  	 */
    76  	public Object valueFrom(String valueString) {
    77
    78  		if (maxValueCount() == 1) return valueString;
    79
    80  		return StringUtil.substringsOf(valueString, multiValueDelimiter);
    81  	}
    82
    83  	/***
    84  	 * Method containsDelimiter.
    85  	 * @param value String
    86  	 * @return boolean
    87  	 */
    88  	private boolean containsDelimiter(String value) {
    89  		return value.indexOf(multiValueDelimiter) >= 0;
    90  	}
    91
    92  	private final String illegalCharMsg() {
    93  		return "Value cannot contain the \"" + multiValueDelimiter + "\" character";
    94  	}
    95
    96  	/***
    97  	 *
    98  	 * @param value Object
    99  	 * @return String
    100 	 */
    101 	protected String valueErrorFor(Object value) {
    102
    103 		if (maxValueCount() == 1) {
    104 			String testValue = (String)value;
    105 			if (!containsDelimiter(testValue)) return null;
    106 			return illegalCharMsg();
    107 		}
    108
    109 		String[] values = (String[])value;
    110 		for (int i=0; i<values.length; i++) {
    111 			if (!containsDelimiter(values[i])) continue;
    112 			return illegalCharMsg();
    113 			}
    114
    115 		return null;
    116 	}
    117
    118 	public int preferredRowCount() {
    119 		return preferredRowCount;
    120 	}
    121 }