001: package net.sourceforge.pmd.properties;
002:
003: import net.sourceforge.pmd.util.StringUtil;
004:
005: /**
006: * Defines a datatype that supports String values.
007: * When capturing multiple values, all strings must be filtered by the delimiter character.
008: *
009: * @author Brian Remedios
010: * @version $Revision$
011: */
012: public class StringProperty extends AbstractPMDProperty {
013:
014: private int preferredRowCount;
015:
016: public static final char defaultDelimiter = '|';
017:
018: /**
019: * Constructor for StringProperty.
020: * @param theName String
021: * @param theDescription String
022: * @param theDefaultValue String
023: * @param theUIOrder float
024: */
025: public StringProperty(String theName, String theDescription,
026: String theDefaultValue, float theUIOrder) {
027: this (theName, theDescription, theDefaultValue, theUIOrder,
028: defaultDelimiter);
029:
030: maxValueCount(1);
031: }
032:
033: /**
034: * Constructor for StringProperty.
035: * @param theName String
036: * @param theDescription String
037: * @param theValues String[]
038: * @param theUIOrder float
039: * @param aMultiValueDelimiter String
040: */
041: public StringProperty(String theName, String theDescription,
042: String[] theValues, float theUIOrder,
043: char aMultiValueDelimiter) {
044: super (theName, theDescription, theValues, theUIOrder);
045:
046: maxValueCount(Integer.MAX_VALUE);
047: multiValueDelimiter(aMultiValueDelimiter);
048: }
049:
050: /**
051: * Constructor for StringProperty.
052: * @param theName String
053: * @param theDescription String
054: * @param theDefaultValue Object
055: * @param theUIOrder float
056: * @param aMultiValueDelimiter String
057: */
058: protected StringProperty(String theName, String theDescription,
059: Object theDefaultValue, float theUIOrder,
060: char aMultiValueDelimiter) {
061: super (theName, theDescription, theDefaultValue, theUIOrder);
062:
063: maxValueCount(Integer.MAX_VALUE);
064: multiValueDelimiter(aMultiValueDelimiter);
065: }
066:
067: /**
068: * Method type.
069: * @return Class
070: * @see net.sourceforge.pmd.PropertyDescriptor#type()
071: */
072: public Class<?> type() {
073: return String.class;
074: }
075:
076: /**
077: * Method valueFrom.
078: * @param valueString String
079: * @return Object
080: * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
081: */
082: public Object valueFrom(String valueString) {
083:
084: if (maxValueCount() == 1)
085: return valueString;
086:
087: return StringUtil
088: .substringsOf(valueString, multiValueDelimiter);
089: }
090:
091: /**
092: * Method containsDelimiter.
093: * @param value String
094: * @return boolean
095: */
096: private boolean containsDelimiter(String value) {
097: return value.indexOf(multiValueDelimiter) >= 0;
098: }
099:
100: private final String illegalCharMsg() {
101: return "Value cannot contain the \"" + multiValueDelimiter
102: + "\" character";
103: }
104:
105: /**
106: *
107: * @param value Object
108: * @return String
109: */
110: protected String valueErrorFor(Object value) {
111:
112: if (maxValueCount() == 1) {
113: String testValue = (String) value;
114: if (!containsDelimiter(testValue))
115: return null;
116: return illegalCharMsg();
117: }
118:
119: String[] values = (String[]) value;
120: for (int i = 0; i < values.length; i++) {
121: if (!containsDelimiter(values[i]))
122: continue;
123: return illegalCharMsg();
124: }
125:
126: return null;
127: }
128:
129: public int preferredRowCount() {
130: return preferredRowCount;
131: }
132: }
|