StringMultiProperty.java
001 /**
002  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003  */
004 package net.sourceforge.pmd.lang.rule.properties;
005 
006 import java.util.Map;
007 
008 import net.sourceforge.pmd.util.StringUtil;
009 
010 /**
011  * Defines a datatype that supports multiple String values.
012  * Note that all strings must be filtered by the delimiter character.
013  * 
014  * @author Brian Remedios
015  */
016 public class StringMultiProperty extends AbstractDelimitedProperty<String[]> {
017     
018   public static final char DEFAULT_DELIMITER = '|';
019     
020   /**
021    * Constructor for StringProperty.
022    * @param theName String
023    * @param theDescription String
024    * @param theDefaults String[]
025    * @param theUIOrder float
026    * @param delimiter String
027    * @throws IllegalArgumentException
028    */
029   public StringMultiProperty(String theName, String theDescription, String[] theDefaults, float theUIOrder, char delimiter) {
030     super(theName, theDescription, theDefaults, delimiter, theUIOrder);
031 
032     checkDefaults(theDefaults, delimiter);
033   }
034   
035   /**
036      * Constructor for CharacterProperty that accepts additional params from a map.
037      * 
038    * @param theName
039    * @param theDescription
040    * @param theDefaults
041    * @param otherParams
042    */
043   public StringMultiProperty(String theName, String theDescription, String theDefaults, Map<String, String> otherParams) {
044       this(theName, theDescription, StringUtil.substringsOf(theDefaults, delimiterIn(otherParams)), 0.0f, delimiterIn(otherParams));
045   }
046   
047   /**
048    * @param defaultValue
049    * @param delim
050    * @throws IllegalArgumentException
051    */
052   private static void checkDefaults(String[] defaultValue, char delim) {
053     
054     if (defaultValue == null) { return;  }
055     
056     for (int i=0; i<defaultValue.length; i++) {
057       if (defaultValue[i].indexOf(delim) >= 0) {
058         throw new IllegalArgumentException("Cannot include the delimiter in the set of defaults");
059       }
060     }
061   }
062   
063   /**
064    * @return Class
065    * @see net.sourceforge.pmd.PropertyDescriptor#type()
066    */
067   public Class<String[]> type() {
068     return String[].class;
069   }
070   
071   /**
072    * @param valueString String
073    * @return Object
074    * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
075    */
076   public String[] valueFrom(String valueString) {
077     return StringUtil.substringsOf(valueString, multiValueDelimiter());
078   }
079   
080   /**
081    * @param value String
082    * @return boolean
083    */
084   private boolean containsDelimiter(String value) {
085     return value.indexOf(multiValueDelimiter()) >= 0;
086   }
087   
088   /**
089    * @return String
090    */
091   private final String illegalCharMsg() {
092     return "Value cannot contain the '" + multiValueDelimiter() + "' character";
093   }
094   
095   /**
096    * 
097    * @param value Object
098    * @return String
099    */
100   protected String valueErrorFor(Object value) {
101 
102     if (value==null) { return "missing value"; }
103     
104     String testValue = (String)value;
105     if (containsDelimiter(testValue)) {
106         return illegalCharMsg();      
107     }
108     
109     // TODO - eval against regex checkers
110     
111     return null;    
112   }
113 }