KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > attribute > ClassPropertiesHelper


1 package com.tirsen.nanning.attribute;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.lang.reflect.Field JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 public class ClassPropertiesHelper {
15
16     private static Map JavaDoc methodSignatureCache = new HashMap JavaDoc();
17
18     private static final String JavaDoc CLASS_PREFIX = "class";
19     private static final String JavaDoc FIELD_PREFIX = "field";
20     private static final String JavaDoc METHOD_PREFIX = "method";
21
22     Properties JavaDoc properties;
23     private String JavaDoc className;
24     private String JavaDoc packageName;
25     private ClassAttributes targetClassAttributes;
26     private Class JavaDoc attributeClass;
27
28     public ClassPropertiesHelper() {
29         properties = new Properties JavaDoc();
30     }
31
32     public ClassPropertiesHelper(ClassAttributes classAttributes) {
33         this();
34         setTargetClassAttributes(classAttributes);
35     }
36
37     public String JavaDoc getClassName() {
38         return className;
39     }
40
41     public void setClassName(String JavaDoc className) {
42         this.className = className;
43     }
44
45     public void setTargetClassAttributes(ClassAttributes classAttributes) {
46         targetClassAttributes = classAttributes;
47         setAttributeClass(classAttributes.getAttributeClass());
48     }
49
50     private void setAttributeClass(Class JavaDoc attributeClass) {
51         this.attributeClass = attributeClass;
52         if (attributeClass != null) {
53             className = className(attributeClass);
54             packageName = packageName(attributeClass);
55         }
56     }
57
58     void loadAttributes(Properties JavaDoc properties) {
59         assert targetClassAttributes != null;
60
61         this.properties = properties;
62
63         for (Iterator JavaDoc iterator = properties.entrySet().iterator(); iterator.hasNext();) {
64             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
65             String JavaDoc propertyName = (String JavaDoc) entry.getKey();
66             String JavaDoc attributeValue = (String JavaDoc) entry.getValue();
67
68             String JavaDoc[] parts = propertyName.split("\\.");
69             if (CLASS_PREFIX.equals(parts[0])) {
70                 String JavaDoc attributeName = joinTail(parts, 1);
71                 loadClassAttribute(attributeName, attributeValue);
72             } else if (FIELD_PREFIX.equals(parts[0])) {
73                 String JavaDoc attributeName = joinTail(parts, 2);
74                 String JavaDoc fieldName = parts[1];
75                 loadFieldAttribute(fieldName, attributeName, attributeValue);
76             } else if (METHOD_PREFIX.equals(parts[0])) {
77                 String JavaDoc attributeName = joinTail(parts, 2);
78                 String JavaDoc methodSignature = parts[1];
79                 loadMethodAttribute(methodSignature, attributeName, attributeValue);
80             } else {
81                 throw new AttributeException("Invalid property " + propertyName);
82             }
83         }
84     }
85
86     void loadMethodAttribute(String JavaDoc methodSignature, String JavaDoc attributeName, String JavaDoc attributeValue) {
87         assert methodSignature.indexOf('(') != -1 && methodSignature.indexOf(')') != -1:
88                 "method signature of wrong format: " + methodSignature;
89         if (targetClassAttributes != null) {
90             Method JavaDoc method = findMethod(methodSignature);
91             if (method == null) {
92                 throw new AttributeException("Error while loading attributes, could not find method: " +
93                                              methodSignature + " for " + attributeClass);
94             }
95
96             targetClassAttributes.setMethodAttribute(method, attributeName, attributeValue);
97         }
98         properties.put(METHOD_PREFIX + "." + methodSignature + "." + attributeName, attributeValue);
99     }
100
101     void loadFieldAttribute(String JavaDoc fieldName, String JavaDoc attributeName, String JavaDoc attributeValue) {
102         if (targetClassAttributes != null) {
103             try {
104                 Field JavaDoc field = attributeClass.getDeclaredField(fieldName);
105                 targetClassAttributes.setFieldAttribute(field, attributeName, attributeValue);
106             } catch (Exception JavaDoc e) {
107                 throw new AttributeException("Error while loading attributes, could not find field: " +
108                                              fieldName + " for " + attributeClass, e);
109             }
110         }
111         properties.put(FIELD_PREFIX + "." + fieldName + "." + attributeName, attributeValue);
112     }
113
114     void loadClassAttribute(String JavaDoc attributeName, String JavaDoc attributeValue) {
115         if (targetClassAttributes != null) {
116             if (attributeClass != null) {
117                 targetClassAttributes.setClassAttribute(attributeName, attributeValue);
118             }
119         }
120         properties.put(CLASS_PREFIX + "." + attributeName, attributeValue);
121     }
122
123     private Method JavaDoc findMethod(String JavaDoc methodSignature) {
124         Method JavaDoc[] declaredMethods = attributeClass.getDeclaredMethods();
125         for (int i = 0; i < declaredMethods.length; i++) {
126             Method JavaDoc method = declaredMethods[i];
127             if (methodSignature(method).equals(methodSignature)) {
128                 return method;
129             }
130         }
131         return null;
132     }
133
134     public static String JavaDoc joinTail(String JavaDoc[] parts, int firstIndex) {
135         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
136         for (int i = firstIndex; i < parts.length; i++) {
137             String JavaDoc part = parts[i];
138             result.append(part);
139             if (i < parts.length - 1) {
140                 result.append(".");
141             }
142         }
143         return result.toString();
144     }
145
146     static String JavaDoc methodSignature(Method JavaDoc method) {
147         String JavaDoc signature = (String JavaDoc) methodSignatureCache.get(method);
148         if (signature == null) {
149             StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
150             stringBuffer.append(method.getName());
151             stringBuffer.append('(');
152             Class JavaDoc[] parameterTypes = method.getParameterTypes();
153             for (int i = 0; i < parameterTypes.length; i++) {
154                 Class JavaDoc parameterType = parameterTypes[i];
155                 if (parameterType.isArray()) {
156                     parameterType = parameterType.getComponentType();
157                 }
158                 String JavaDoc type = parameterType.getName();
159                 type = type.substring(type.lastIndexOf('.') + 1);
160                 stringBuffer.append(type);
161                 if (i + 1 < parameterTypes.length) {
162                     stringBuffer.append(',');
163                 }
164             }
165             stringBuffer.append(')');
166             signature = stringBuffer.toString();
167             methodSignatureCache.put(method, signature);
168         }
169         return signature;
170     }
171
172     public String JavaDoc getPackageName() {
173         return packageName;
174     }
175
176     public void setPackageName(String JavaDoc packageName) {
177         this.packageName = packageName;
178     }
179
180     static String JavaDoc className(Class JavaDoc klass) {
181         String JavaDoc className = klass.getName();
182         return className.substring(className.lastIndexOf('.') + 1);
183     }
184
185     static String JavaDoc packageName(Class JavaDoc klass) {
186         String JavaDoc className = klass.getName();
187         int lastDot = className.lastIndexOf('.');
188         if (lastDot == -1) {
189             return "";
190         }
191         return className.substring(0, lastDot);
192     }
193
194     public void transferAttributesToTarget() {
195         loadAttributes(properties);
196     }
197
198     public File JavaDoc getAttributeFile(File JavaDoc baseDir) {
199         return new File JavaDoc(baseDir, getPackageName().replace('.', File.separatorChar) + File.separator + getClassName() +
200                                  PropertyFileAttributeLoader.ATTRIBUTE_FILE_SUFFIX);
201     }
202
203     public void store(File JavaDoc dest) throws IOException JavaDoc {
204         OutputStream JavaDoc output = new FileOutputStream JavaDoc(getAttributeFile(dest));
205         try {
206             properties.store(output, getPackageName() + "." + getClassName());
207         } finally {
208             output.close();
209         }
210     }
211 }
212
Popular Tags