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 net.sourceforge.pmd.PropertyDescriptor;
007 import net.sourceforge.pmd.util.StringUtil;
008
009 public class PropertyDescriptorFactory {
010 /**
011 * Returns the String type of the PropertyDescriptor for use in XML serialization.
012 * If the value is <code>null</code> the type cannot be serialized.
013 */
014 public static String getPropertyDescriptorType(PropertyDescriptor<?> propertyDescriptor) {
015 Class<?> type = propertyDescriptor.type();
016 String typeName = null;
017 if (propertyDescriptor instanceof EnumeratedProperty ||
018 propertyDescriptor instanceof MethodProperty // TODO - yes we can, investigate
019 || propertyDescriptor instanceof TypeProperty) {
020 // Cannot serialize these kinds of PropertyDescriptors
021 } else if ("java.lang".equals(type.getPackage().getName())) {
022 typeName = type.getSimpleName();
023 }
024 if (typeName == null) {
025 throw new IllegalArgumentException("Cannot encode type for PropertyDescriptor class: " + type.getName());
026 }
027 return typeName;
028 }
029
030 public static PropertyDescriptor<?> createPropertyDescriptor(String name, String description, String type,
031 String delimiter, String min, String max, String value) {
032 return new PropertyDescriptorWrapper(createRawPropertyDescriptor(name, description, type, delimiter, min, max,
033 value));
034 }
035
036 private static PropertyDescriptor<?> createRawPropertyDescriptor(String name, String description, String type,
037 String delimiter, String min, String max, String value) {
038 if ("Boolean".equals(type)) {
039 return new BooleanProperty(name, description, value, 0.0f);
040 } else if ("Boolean[]".equals(type)) {
041 BooleanMultiProperty property = new BooleanMultiProperty(name, description, null, 0.0f);
042 return new BooleanMultiProperty(name, description, property.valueFrom(value), 0.0f);
043 } else if ("Character".equals(type)) {
044 return new CharacterProperty(name, description, CharacterProperty.charFrom(value), 0.0f);
045 } else if ("Character[]".equals(type)) {
046 checkDelimiter(name, type, delimiter);
047 char delim = delimiter.charAt(0);
048 CharacterMultiProperty property = new CharacterMultiProperty(name, description, null, 0.0f, delim);
049 return new CharacterMultiProperty(name, description, property.valueFrom(value), 0.0f, delim);
050 } else if ("Double".equals(type)) {
051 checkMinMax(name, type, min, max);
052 return new DoubleProperty(name, description, min, max, value, 0.0f);
053 } else if ("Double[]".equals(type)) {
054 checkMinMax(name, type, min, max);
055 DoubleMultiProperty property = new DoubleMultiProperty(name, description, 0d, 0d, null, 0.0f);
056 return new DoubleMultiProperty(name, description, DoubleProperty.doubleFrom(min), DoubleProperty.doubleFrom(max), property.valueFrom(value), 0.0f);
057 } else if ("Float".equals(type)) {
058 checkMinMax(name, type, min, max);
059 return new FloatProperty(name, description, min, max, value, 0.0f);
060 } else if ("Float[]".equals(type)) {
061 checkMinMax(name, type, min, max);
062 FloatMultiProperty property = new FloatMultiProperty(name, description, 0f, 0f, null, 0.0f);
063 return new FloatMultiProperty(name, description, FloatProperty.floatFrom(min), FloatProperty.floatFrom(max), property.valueFrom(value), 0.0f);
064 } else if ("Integer".equals(type)) {
065 checkMinMax(name, type, min, max);
066 return new IntegerProperty(name, description, min, max, value, 0.0f);
067 } else if ("Integer[]".equals(type)) {
068 checkMinMax(name, type, min, max);
069 IntegerMultiProperty property = new IntegerMultiProperty(name, description, 0, 0, null, 0.0f);
070 return new IntegerMultiProperty(name, description, IntegerProperty.intFrom(min), IntegerProperty.intFrom(max), property.valueFrom(value), 0.0f);
071 } else if ("Long".equals(type)) {
072 checkMinMax(name, type, min, max);
073 return new LongProperty(name, description, min, max, value, 0.0f);
074 } else if ("Long[]".equals(type)) {
075 checkMinMax(name, type, min, max);
076 LongMultiProperty property = new LongMultiProperty(name, description, 0l, 0l, null, 0.0f);
077 return new LongMultiProperty(name, description, LongProperty.longFrom(min), LongProperty.longFrom(max), property.valueFrom(value), 0.0f);
078
079 // TODO - include legal package names for next four types
080 } else if ("Type".equals(type)) {
081 return new TypeProperty(name, description, value, null, 0.0f);
082 } else if ("Type[]".equals(type)) {
083 return new TypeMultiProperty(name, description, value, null, 0.0f);
084 } else if ("Method".equals(type)) {
085 return new MethodProperty(name, description, value, null, 0.0f);
086 } else if ("Method[]".equals(type)) {
087 return new MethodMultiProperty(name, description, value, null, 0.0f);
088
089 } else if ("String".equals(type)) {
090 return new StringProperty(name, description, value, 0.0f);
091 } else if ("String[]".equals(type)) {
092 checkDelimiter(name, type, delimiter);
093 char delim = delimiter.charAt(0);
094 StringMultiProperty property = new StringMultiProperty(name, description, null, 0.0f, delim);
095 return new StringMultiProperty(name, description, property.valueFrom(value), 0.0f, delim);
096 } else {
097 throw new IllegalArgumentException("Cannot define property type '" + type + "'.");
098 }
099 }
100
101 private static void checkDelimiter(String name, String type, String delimiter) {
102 if (delimiter == null || delimiter.length() == 0) {
103 throw new IllegalArgumentException("Delimiter must be provided to create PropertyDescriptor for " + name
104 + " of type " + type + ".");
105 }
106 }
107
108 private static void checkMinMax(String name, String type, String min, String max) {
109 if (StringUtil.isEmpty(min)) {
110 throw new IllegalArgumentException("Min must be provided to create PropertyDescriptor for " + name
111 + " of type " + type + ".");
112 }
113 if (StringUtil.isEmpty(max)) {
114 throw new IllegalArgumentException("Max must be provided to create PropertyDescriptor for " + name
115 + " of type " + type + ".");
116 }
117 }
118 }
|