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 net.sourceforge.pmd.PropertyDescriptor;
7   import net.sourceforge.pmd.util.StringUtil;
8   
9   public class PropertyDescriptorFactory {
10      /**
11       * Returns the String type of the PropertyDescriptor for use in XML
12       * serialization. If the value is <code>null</code> the type cannot be
13       * serialized.
14       */
15      public static String getPropertyDescriptorType(PropertyDescriptor<?> propertyDescriptor) {
16          Class<?> type = propertyDescriptor.type();
17          String typeName = null;
18          if (propertyDescriptor instanceof EnumeratedProperty || propertyDescriptor instanceof MethodProperty // TODO
19                                                                                                               // -
20                                                                                                               // yes
21                                                                                                               // we
22                                                                                                               // can,
23                                                                                                               // investigate
24                  || propertyDescriptor instanceof TypeProperty) {
25              // Cannot serialize these kinds of PropertyDescriptors
26          } else if ("java.lang".equals(type.getPackage().getName())) {
27              typeName = type.getSimpleName();
28          }
29          if (typeName == null) {
30              throw new IllegalArgumentException("Cannot encode type for PropertyDescriptor class: " + type.getName());
31          }
32          return typeName;
33      }
34  
35      public static PropertyDescriptor<?> createPropertyDescriptor(String name, String description, String type,
36              String delimiter, String min, String max, String value) {
37          return new PropertyDescriptorWrapper(createRawPropertyDescriptor(name, description, type, delimiter, min, max,
38                  value));
39      }
40  
41      private static PropertyDescriptor<?> createRawPropertyDescriptor(String name, String description, String type,
42              String delimiter, String min, String max, String value) {
43          if ("Boolean".equals(type)) {
44              return new BooleanProperty(name, description, value, 0.0f);
45          } else if ("Boolean[]".equals(type)) {
46              BooleanMultiProperty property = new BooleanMultiProperty(name, description, null, 0.0f);
47              return new BooleanMultiProperty(name, description, property.valueFrom(value), 0.0f);
48          } else if ("Character".equals(type)) {
49              return new CharacterProperty(name, description, CharacterProperty.charFrom(value), 0.0f);
50          } else if ("Character[]".equals(type)) {
51              checkDelimiter(name, type, delimiter);
52              char delim = delimiter.charAt(0);
53              CharacterMultiProperty property = new CharacterMultiProperty(name, description, null, 0.0f, delim);
54              return new CharacterMultiProperty(name, description, property.valueFrom(value), 0.0f, delim);
55          } else if ("Double".equals(type)) {
56              checkMinMax(name, type, min, max);
57              return new DoubleProperty(name, description, min, max, value, 0.0f);
58          } else if ("Double[]".equals(type)) {
59              checkMinMax(name, type, min, max);
60              DoubleMultiProperty property = new DoubleMultiProperty(name, description, 0d, 0d, null, 0.0f);
61              return new DoubleMultiProperty(name, description, DoubleProperty.doubleFrom(min),
62                      DoubleProperty.doubleFrom(max), property.valueFrom(value), 0.0f);
63          } else if ("Float".equals(type)) {
64              checkMinMax(name, type, min, max);
65              return new FloatProperty(name, description, min, max, value, 0.0f);
66          } else if ("Float[]".equals(type)) {
67              checkMinMax(name, type, min, max);
68              FloatMultiProperty property = new FloatMultiProperty(name, description, 0f, 0f, null, 0.0f);
69              return new FloatMultiProperty(name, description, FloatProperty.floatFrom(min),
70                      FloatProperty.floatFrom(max), property.valueFrom(value), 0.0f);
71          } else if ("Integer".equals(type)) {
72              checkMinMax(name, type, min, max);
73              return new IntegerProperty(name, description, min, max, value, 0.0f);
74          } else if ("Integer[]".equals(type)) {
75              checkMinMax(name, type, min, max);
76              IntegerMultiProperty property = new IntegerMultiProperty(name, description, 0, 0, null, 0.0f);
77              return new IntegerMultiProperty(name, description, IntegerProperty.intFrom(min),
78                      IntegerProperty.intFrom(max), property.valueFrom(value), 0.0f);
79          } else if ("Long".equals(type)) {
80              checkMinMax(name, type, min, max);
81              return new LongProperty(name, description, min, max, value, 0.0f);
82          } else if ("Long[]".equals(type)) {
83              checkMinMax(name, type, min, max);
84              LongMultiProperty property = new LongMultiProperty(name, description, 0L, 0L, null, 0.0f);
85              return new LongMultiProperty(name, description, LongProperty.longFrom(min), LongProperty.longFrom(max),
86                      property.valueFrom(value), 0.0f);
87  
88              // TODO - include legal package names for next four types
89          } else if ("Type".equals(type)) {
90              return new TypeProperty(name, description, value, (String[]) null, 0.0f);
91          } else if ("Type[]".equals(type)) {
92              return new TypeMultiProperty(name, description, value, (String[]) null, 0.0f);
93          } else if ("Method".equals(type)) {
94              return new MethodProperty(name, description, value, (String[]) null, 0.0f);
95          } else if ("Method[]".equals(type)) {
96              return new MethodMultiProperty(name, description, value, (String[]) null, 0.0f);
97  
98          } else if ("String".equals(type)) {
99              return new StringProperty(name, description, value, 0.0f);
100         } else if ("String[]".equals(type)) {
101             checkDelimiter(name, type, delimiter);
102             char delim = delimiter.charAt(0);
103             StringMultiProperty property = new StringMultiProperty(name, description, null, 0.0f, delim);
104             return new StringMultiProperty(name, description, property.valueFrom(value), 0.0f, delim);
105         } else {
106             throw new IllegalArgumentException("Cannot define property type '" + type + "'.");
107         }
108     }
109 
110     private static void checkDelimiter(String name, String type, String delimiter) {
111         if (delimiter == null || delimiter.length() == 0) {
112             throw new IllegalArgumentException("Delimiter must be provided to create PropertyDescriptor for " + name
113                     + " of type " + type + ".");
114         }
115     }
116 
117     private static void checkMinMax(String name, String type, String min, String max) {
118         if (StringUtil.isEmpty(min)) {
119             throw new IllegalArgumentException("Min must be provided to create PropertyDescriptor for " + name
120                     + " of type " + type + ".");
121         }
122         if (StringUtil.isEmpty(max)) {
123             throw new IllegalArgumentException("Max must be provided to create PropertyDescriptor for " + name
124                     + " of type " + type + ".");
125         }
126     }
127 }