KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > InjectMetaWorker


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.enhance;
16
17 import java.lang.reflect.Modifier JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.service.BodyBuilder;
22 import org.apache.hivemind.service.ClassFabUtils;
23 import org.apache.hivemind.service.MethodSignature;
24 import org.apache.hivemind.util.Defense;
25 import org.apache.tapestry.coerce.ValueConverter;
26 import org.apache.tapestry.services.ComponentPropertySource;
27 import org.apache.tapestry.spec.InjectSpecification;
28
29 /**
30  * Injects meta data obtained via {@link org.apache.tapestry.services.ComponentPropertySource}
31  * (meaning that meta-data is searched for in the component's specification, then it's namespace
32  * (library or application specification), then the global application properties.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class InjectMetaWorker implements InjectEnhancementWorker
38 {
39     static final String JavaDoc SOURCE_NAME = "_$componentPropertySource";
40
41     private ComponentPropertySource _source;
42
43     private ValueConverter _valueConverter;
44
45     private Map JavaDoc _primitiveParser = new HashMap JavaDoc();
46
47     {
48         _primitiveParser.put(boolean.class, "java.lang.Boolean.valueOf");
49         _primitiveParser.put(short.class, "java.lang.Short.parseShort");
50         _primitiveParser.put(int.class, "java.lang.Integer.parseInt");
51         _primitiveParser.put(long.class, "java.lang.Long.parseLong");
52         _primitiveParser.put(double.class, "java.lang.Double.parseDouble");
53         _primitiveParser.put(float.class, "java.lang.Float.parseFloat");
54     }
55
56     public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
57     {
58         String JavaDoc propertyName = spec.getProperty();
59         String JavaDoc metaKey = spec.getObject();
60
61         injectMetaValue(op, propertyName, metaKey);
62     }
63
64     public void injectMetaValue(EnhancementOperation op, String JavaDoc propertyName, String JavaDoc metaKey)
65     {
66         Defense.notNull(op, "op");
67         Defense.notNull(propertyName, "propertyName");
68         Defense.notNull(metaKey, "metaKey");
69
70         Class JavaDoc propertyType = op.getPropertyType(propertyName);
71
72         op.claimProperty(propertyName);
73
74         String JavaDoc sourceName = op
75                 .addInjectedField(SOURCE_NAME, ComponentPropertySource.class, _source);
76
77         MethodSignature sig = new MethodSignature(propertyType, op
78                 .getAccessorMethodName(propertyName), null, null);
79
80         String JavaDoc parser = (String JavaDoc) _primitiveParser.get(propertyType);
81
82         if (parser != null)
83         {
84             addPrimitive(op, metaKey, propertyName, sig, sourceName, parser);
85             return;
86         }
87
88         if (propertyType == char.class)
89         {
90             addCharacterPrimitive(op, metaKey, propertyName, sig, sourceName);
91             return;
92         }
93
94         addObject(op, metaKey, propertyName, propertyType, sig, sourceName);
95     }
96
97     private void addPrimitive(EnhancementOperation op, String JavaDoc metaKey, String JavaDoc propertyName,
98             MethodSignature sig, String JavaDoc sourceName, String JavaDoc parser)
99     {
100         BodyBuilder builder = new BodyBuilder();
101         builder.begin();
102         builder.addln(
103                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
104                 sourceName,
105                 metaKey);
106         builder.addln("return {0}(meta);", parser);
107         builder.end();
108
109         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
110     }
111
112     private void addCharacterPrimitive(EnhancementOperation op, String JavaDoc metaKey,
113             String JavaDoc propertyName, MethodSignature sig, String JavaDoc sourceName)
114     {
115         BodyBuilder builder = new BodyBuilder();
116         builder.begin();
117         builder.addln(
118                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
119                 sourceName,
120                 metaKey);
121         builder.addln("return meta.charAt(0);");
122         builder.end();
123
124         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
125     }
126
127     private void addObject(EnhancementOperation op, String JavaDoc metaKey, String JavaDoc propertyName,
128             Class JavaDoc propertyType, MethodSignature sig, String JavaDoc sourceName)
129     {
130         String JavaDoc valueConverterName = op.addInjectedField(
131                 "_$valueConverter",
132                 ValueConverter.class,
133                 _valueConverter);
134         String JavaDoc classRef = op.getClassReference(propertyType);
135
136         BodyBuilder builder = new BodyBuilder();
137         builder.begin();
138         builder.addln(
139                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
140                 sourceName,
141                 metaKey);
142         builder.addln("return ({0}) {1}.coerceValue(meta, {2});", ClassFabUtils
143                 .getJavaClassName(propertyType), valueConverterName, classRef);
144         builder.end();
145
146         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
147     }
148
149     public void setSource(ComponentPropertySource source)
150     {
151         _source = source;
152     }
153
154     public void setValueConverter(ValueConverter valueConverter)
155     {
156         _valueConverter = valueConverter;
157     }
158 }
159
Popular Tags