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 org.apache.hivemind.Location;
 18  
 import org.apache.hivemind.service.BodyBuilder;
 19  
 import org.apache.hivemind.service.ClassFabUtils;
 20  
 import org.apache.hivemind.service.MethodSignature;
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.coerce.ValueConverter;
 23  
 import org.apache.tapestry.services.ComponentPropertySource;
 24  
 import org.apache.tapestry.spec.InjectSpecification;
 25  
 
 26  
 import java.lang.reflect.Modifier;
 27  
 import java.util.HashMap;
 28  
 import java.util.Map;
 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("java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 112  
                       sourceName,
 113  
                       metaKey);
 114  1
         builder.addln("return {0}(meta);", parser);
 115  1
         builder.end();
 116  
 
 117  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 118  1
     }
 119  
 
 120  
     private void addBoolean(EnhancementOperation op, String metaKey, String propertyName,
 121  
                             MethodSignature sig, String sourceName, Location location)
 122  
     {
 123  1
         BodyBuilder builder = new BodyBuilder();
 124  1
         builder.begin();
 125  1
         builder.addln(
 126  
           "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 127  
           sourceName,
 128  
           metaKey);
 129  1
         builder.addln("return java.lang.Boolean.valueOf(meta).booleanValue();");
 130  1
         builder.end();
 131  
 
 132  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 133  1
     }
 134  
 
 135  
     private void addCharacterPrimitive(EnhancementOperation op, String metaKey,
 136  
                                        String propertyName, MethodSignature sig, String sourceName, Location location)
 137  
     {
 138  1
         BodyBuilder builder = new BodyBuilder();
 139  1
         builder.begin();
 140  1
         builder.addln(
 141  
           "java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 142  
           sourceName,
 143  
           metaKey);
 144  1
         builder.addln("return meta.charAt(0);");
 145  1
         builder.end();
 146  
 
 147  1
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 148  1
     }
 149  
 
 150  
     private void addObject(EnhancementOperation op, String metaKey, String propertyName,
 151  
                            Class propertyType, MethodSignature sig, String sourceName, Location location)
 152  
     {
 153  2
         String valueConverterName = op.addInjectedField("_$valueConverter", ValueConverter.class, _valueConverter);
 154  
 
 155  2
         String classRef = op.getClassReference(propertyType);
 156  
 
 157  2
         BodyBuilder builder = new BodyBuilder();
 158  2
         builder.begin();
 159  2
         builder.addln("java.lang.String meta = {0}.getComponentProperty(this, \"{1}\");",
 160  
                       sourceName,
 161  
                       metaKey);
 162  2
         builder.addln("return ({0}) {1}.coerceValue(meta, {2});", ClassFabUtils
 163  
           .getJavaClassName(propertyType), valueConverterName, classRef);
 164  2
         builder.end();
 165  
 
 166  2
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 167  2
     }
 168  
 
 169  
     public void setSource(ComponentPropertySource source)
 170  
     {
 171  5
         _source = source;
 172  5
     }
 173  
 
 174  
     public void setValueConverter(ValueConverter valueConverter)
 175  
     {
 176  2
         _valueConverter = valueConverter;
 177  2
     }
 178  
 }