1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.HashMap ; 19 import java.util.Map ; 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 37 public class InjectMetaWorker implements InjectEnhancementWorker 38 { 39 static final String SOURCE_NAME = "_$componentPropertySource"; 40 41 private ComponentPropertySource _source; 42 43 private ValueConverter _valueConverter; 44 45 private Map _primitiveParser = new HashMap (); 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 propertyName = spec.getProperty(); 59 String metaKey = spec.getObject(); 60 61 injectMetaValue(op, propertyName, metaKey); 62 } 63 64 public void injectMetaValue(EnhancementOperation op, String propertyName, String metaKey) 65 { 66 Defense.notNull(op, "op"); 67 Defense.notNull(propertyName, "propertyName"); 68 Defense.notNull(metaKey, "metaKey"); 69 70 Class propertyType = op.getPropertyType(propertyName); 71 72 op.claimProperty(propertyName); 73 74 String 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 parser = (String ) _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 metaKey, String propertyName, 98 MethodSignature sig, String sourceName, String 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 metaKey, 113 String propertyName, MethodSignature sig, String 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 metaKey, String propertyName, 128 Class propertyType, MethodSignature sig, String sourceName) 129 { 130 String valueConverterName = op.addInjectedField( 131 "_$valueConverter", 132 ValueConverter.class, 133 _valueConverter); 134 String 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 |