Coverage Report - org.apache.tapestry.enhance.InjectMetaWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectMetaWorker
100% 
100% 
1.875
 
 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;
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.hivemind.Location;
 22  
 import org.apache.hivemind.service.BodyBuilder;
 23  
 import org.apache.hivemind.service.ClassFabUtils;
 24  
 import org.apache.hivemind.service.MethodSignature;
 25  
 import org.apache.hivemind.util.Defense;
 26  
 import org.apache.tapestry.coerce.ValueConverter;
 27  
 import org.apache.tapestry.services.ComponentPropertySource;
 28  
 import org.apache.tapestry.spec.InjectSpecification;
 29  
 
 30  
 /**
 31  
  * Injects meta data obtained via {@link org.apache.tapestry.services.ComponentPropertySource}
 32  
  * (meaning that meta-data is searched for in the component's specification, then it's namespace
 33  
  * (library or application specification), then the global application properties.
 34  
  * 
 35  
  * @author Howard M. Lewis Ship
 36  
  * @since 4.0
 37  
  */
 38  5
 public class InjectMetaWorker implements InjectEnhancementWorker
 39  
 {
 40  
     static final String SOURCE_NAME = "_$componentPropertySource";
 41  
 
 42  
     private ComponentPropertySource _source;
 43  
 
 44  
     private ValueConverter _valueConverter;
 45  
     
 46  5
     private Map _primitiveParser = new HashMap();
 47  
     {
 48  5
         _primitiveParser.put(short.class, "java.lang.Short.parseShort");
 49  5
         _primitiveParser.put(int.class, "java.lang.Integer.parseInt");
 50  5
         _primitiveParser.put(long.class, "java.lang.Long.parseLong");
 51  5
         _primitiveParser.put(double.class, "java.lang.Double.parseDouble");
 52  5
         _primitiveParser.put(float.class, "java.lang.Float.parseFloat");
 53  5
     }
 54  
 
 55  
     public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
 56  
     {
 57  5
         String propertyName = spec.getProperty();
 58  5
         String metaKey = spec.getObject();
 59  
 
 60  5
         injectMetaValue(op, propertyName, metaKey, spec.getLocation());
 61  5
     }
 62  
 
 63  
     public void injectMetaValue(EnhancementOperation op, String propertyName, String metaKey,
 64  
             Location location)
 65  
     {
 66  5
         Defense.notNull(op, "op");
 67  5
         Defense.notNull(propertyName, "propertyName");
 68  5
         Defense.notNull(metaKey, "metaKey");
 69  
 
 70  5
         Class propertyType = op.getPropertyType(propertyName);
 71  
         
 72  
         // Default to object if not specified
 73  
         
 74  5
         if (propertyType == null) {
 75  
             
 76  4
             propertyType = Object.class;
 77  
         }
 78  
         
 79  5
         op.claimReadonlyProperty(propertyName);
 80  
 
 81  5
         String sourceName = op.addInjectedField(SOURCE_NAME, ComponentPropertySource.class, _source);
 82  
 
 83  5
         MethodSignature sig = new MethodSignature(propertyType, op.getAccessorMethodName(propertyName), null, null);
 84  
 
 85  5
         String parser = (String) _primitiveParser.get(propertyType);
 86  
 
 87  5
         if (parser != null)
 88  
         {
 89  1
             addPrimitive(op, metaKey, propertyName, sig, sourceName, parser, location);
 90  1
             return;
 91  4
         } else if (propertyType == boolean.class) 
 92  
         {
 93  1
             addBoolean(op, metaKey, propertyName, sig, sourceName, location);
 94  1
             return;
 95  
         }
 96  
         
 97  3
         if (propertyType == char.class)
 98  
         {
 99  1
             addCharacterPrimitive(op, metaKey, propertyName, sig, sourceName, location);
 100  1
             return;
 101  
         }
 102  
 
 103  2
         addObject(op, metaKey, propertyName, propertyType, sig, sourceName, location);
 104  2
     }
 105  
 
 106  
     private void addPrimitive(EnhancementOperation op, String metaKey, String propertyName,
 107  
             MethodSignature sig, String sourceName, String parser, Location location)
 108  
     {
 109  1
         BodyBuilder builder = new BodyBuilder();
 110  1
         builder.begin();
 111  1
         builder.addln(
 112  
                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 113  
                 sourceName,
 114  
                 metaKey);
 115  1
         builder.addln("return {0}(meta);", parser);
 116  1
         builder.end();
 117  
 
 118  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 119  1
     }
 120  
     
 121  
     private void addBoolean(EnhancementOperation op, String metaKey, String propertyName,
 122  
             MethodSignature sig, String sourceName, Location location)
 123  
     {
 124  1
         BodyBuilder builder = new BodyBuilder();
 125  1
         builder.begin();
 126  1
         builder.addln(
 127  
                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 128  
                 sourceName,
 129  
                 metaKey);
 130  1
         builder.addln("return java.lang.Boolean.valueOf(meta).booleanValue();");
 131  1
         builder.end();
 132  
         
 133  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 134  1
     }
 135  
     
 136  
     private void addCharacterPrimitive(EnhancementOperation op, String metaKey,
 137  
             String propertyName, MethodSignature sig, String sourceName, Location location)
 138  
     {
 139  1
         BodyBuilder builder = new BodyBuilder();
 140  1
         builder.begin();
 141  1
         builder.addln(
 142  
                 "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 143  
                 sourceName,
 144  
                 metaKey);
 145  1
         builder.addln("return meta.charAt(0);");
 146  1
         builder.end();
 147  
 
 148  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 149  1
     }
 150  
 
 151  
     private void addObject(EnhancementOperation op, String metaKey, String propertyName,
 152  
             Class propertyType, MethodSignature sig, String sourceName, Location location)
 153  
     {
 154  2
         String valueConverterName = op.addInjectedField("_$valueConverter", ValueConverter.class, _valueConverter);
 155  
         
 156  2
         String classRef = op.getClassReference(propertyType);
 157  
         
 158  2
         BodyBuilder builder = new BodyBuilder();
 159  2
         builder.begin();
 160  2
         builder.addln("java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 161  
                 sourceName,
 162  
                 metaKey);
 163  2
         builder.addln("return ({0}) {1}.coerceValue(meta, {2});", ClassFabUtils
 164  
                 .getJavaClassName(propertyType), valueConverterName, classRef);
 165  2
         builder.end();
 166  
         
 167  2
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 168  2
     }
 169  
 
 170  
     public void setSource(ComponentPropertySource source)
 171  
     {
 172  5
         _source = source;
 173  5
     }
 174  
 
 175  
     public void setValueConverter(ValueConverter valueConverter)
 176  
     {
 177  2
         _valueConverter = valueConverter;
 178  2
     }
 179  
 }