1 package net.sourceforge.pmd.properties; 2 3 import net.sourceforge.pmd.util.StringUtil; 4 5 12 public class StringProperty extends AbstractPMDProperty { 13 14 private int preferredRowCount; 15 16 public static final char defaultDelimiter = '|'; 17 18 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 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 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 66 public Class type() { 67 return String .class; 68 } 69 70 76 public Object valueFrom(String valueString) { 77 78 if (maxValueCount() == 1) return valueString; 79 80 return StringUtil.substringsOf(valueString, multiValueDelimiter); 81 } 82 83 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 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 } 122 | Popular Tags |