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