View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.rule.properties;
5   
6   import java.util.Map;
7   
8   import net.sourceforge.pmd.PropertyDescriptorFactory;
9   import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
10  import net.sourceforge.pmd.util.StringUtil;
11  
12  /**
13   * Defines a datatype that supports multiple String values. Note that all
14   * strings must be filtered by the delimiter character.
15   * 
16   * @author Brian Remedios
17   */
18  public class StringMultiProperty extends AbstractProperty<String[]> {
19  
20      public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<StringMultiProperty>(
21              String[].class) {
22  
23          public StringMultiProperty createWith(Map<String, String> valuesById) {
24              char delimiter = delimiterIn(valuesById);
25              return new StringMultiProperty(nameIn(valuesById), descriptionIn(valuesById), StringUtil.substringsOf(
26                      defaultValueIn(valuesById), delimiter), 0.0f, delimiter);
27          }
28      };
29  
30      /**
31       * Constructor for StringProperty.
32       * 
33       * @param theName String
34       * @param theDescription String
35       * @param theDefaults String[]
36       * @param theUIOrder float
37       * @param delimiter String
38       * @throws IllegalArgumentException
39       */
40      public StringMultiProperty(String theName, String theDescription, String[] theDefaults, float theUIOrder,
41              char delimiter) {
42          super(theName, theDescription, theDefaults, theUIOrder, delimiter);
43  
44          checkDefaults(theDefaults, delimiter);
45      }
46  
47      /**
48       * @param defaultValue
49       * @param delim
50       * @throws IllegalArgumentException
51       */
52      private static void checkDefaults(String[] defaultValue, char delim) {
53  
54          if (defaultValue == null) {
55              return;
56          }
57  
58          for (int i = 0; i < defaultValue.length; i++) {
59              if (defaultValue[i].indexOf(delim) >= 0) {
60                  throw new IllegalArgumentException("Cannot include the delimiter in the set of defaults");
61              }
62          }
63      }
64  
65      /**
66       * @return Class
67       * @see net.sourceforge.pmd.PropertyDescriptor#type()
68       */
69      public Class<String[]> type() {
70          return String[].class;
71      }
72  
73      /**
74       * @param valueString String
75       * @return Object
76       * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
77       */
78      public String[] valueFrom(String valueString) {
79          return StringUtil.substringsOf(valueString, multiValueDelimiter());
80      }
81  
82      /**
83       * @param value String
84       * @return boolean
85       */
86      private boolean containsDelimiter(String value) {
87          return value.indexOf(multiValueDelimiter()) >= 0;
88      }
89  
90      /**
91       * @return String
92       */
93      private final String illegalCharMsg() {
94          return "Value cannot contain the '" + multiValueDelimiter() + "' character";
95      }
96  
97      /**
98       * 
99       * @param value Object
100      * @return String
101      */
102     protected String valueErrorFor(Object value) {
103 
104         if (value == null) {
105             return "missing value";
106         }
107 
108         String testValue = (String) value;
109         if (containsDelimiter(testValue)) {
110             return illegalCharMsg();
111         }
112 
113         // TODO - eval against regex checkers
114 
115         return null;
116     }
117 
118     /**
119      * @return boolean
120      * @see net.sourceforge.pmd.PropertyDescriptor#isMultiValue()
121      */
122     @Override
123     public boolean isMultiValue() {
124         return true;
125     }
126 }