Coverage Report - org.apache.tapestry.annotations.ComponentAnnotationWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentAnnotationWorker
0%
0/60
0%
0/22
3.6
 
 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.annotations;
 16  
 
 17  
 import java.lang.reflect.Method;
 18  
 
 19  
 import org.apache.hivemind.ApplicationRuntimeException;
 20  
 import org.apache.hivemind.HiveMind;
 21  
 import org.apache.hivemind.Location;
 22  
 import org.apache.tapestry.enhance.EnhancementOperation;
 23  
 import org.apache.tapestry.spec.BindingSpecification;
 24  
 import org.apache.tapestry.spec.BindingType;
 25  
 import org.apache.tapestry.spec.ContainedComponent;
 26  
 import org.apache.tapestry.spec.IBindingSpecification;
 27  
 import org.apache.tapestry.spec.IComponentSpecification;
 28  
 import org.apache.tapestry.spec.IContainedComponent;
 29  
 
 30  
 /**
 31  
  * Adds a {@link org.apache.tapestry.spec.IContainedComponent} to the
 32  
  * {@link org.apache.tapestry.spec.IComponentSpecification}.
 33  
  * 
 34  
  * @author Howard Lewis Ship
 35  
  * @since 4.0
 36  
  * @see Component
 37  
  */
 38  0
 public class ComponentAnnotationWorker implements MethodAnnotationEnhancementWorker
 39  
 {
 40  
 
 41  
     public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
 42  
             Method method, Location location)
 43  
     {
 44  0
         Component component = method.getAnnotation(Component.class);
 45  
 
 46  0
         String propertyName = AnnotationUtils.getPropertyName(method);
 47  0
         String type = component.type();
 48  0
         String copyOf = component.copyOf();
 49  0
         boolean hasCopyOf = HiveMind.isNonBlank(copyOf);
 50  
         
 51  0
         if (hasCopyOf)
 52  
         {
 53  0
             if (HiveMind.isNonBlank(type))
 54  0
                 throw new ApplicationRuntimeException(AnnotationMessages.bothTypeAndCopyOf(propertyName));
 55  0
             type = null;
 56  
         }
 57  
         else
 58  
         {
 59  0
             if (type.equals(""))
 60  
             {
 61  0
                 Class retTypeClazz = method.getReturnType();
 62  0
                 type = resolveComponentType(retTypeClazz);
 63  
             }
 64  0
             copyOf = null;
 65  
         }
 66  
         
 67  0
         IContainedComponent cc = new ContainedComponent();
 68  
 
 69  0
         cc.setInheritInformalParameters(component.inheritInformalParameters());
 70  0
         cc.setType(type);
 71  0
         cc.setCopyOf(copyOf);
 72  0
         cc.setPropertyName(propertyName);
 73  0
         cc.setLocation(location);
 74  
 
 75  0
         for (String binding : component.bindings())
 76  
         {
 77  0
             addBinding(cc, binding, location);
 78  
         }
 79  
 
 80  0
         for (String binding : component.inheritedBindings())
 81  
         {
 82  0
             addInheritedBinding(cc, binding, location);
 83  
         }
 84  
 
 85  0
         String id = component.id();
 86  
 
 87  0
         if (id.equals(""))
 88  0
             id = propertyName;
 89  
         
 90  0
         spec.addComponent(id, cc);
 91  
         
 92  0
         if (hasCopyOf)
 93  
         {
 94  0
             IContainedComponent source = spec.getComponent(copyOf);
 95  0
             if (source != null)                
 96  0
                 AnnotationUtils.copyBindings(source, cc);
 97  
         }
 98  0
     }
 99  
     
 100  
     protected String resolveComponentType(Class retTypeClass)
 101  
     {
 102  0
         return retTypeClass.getSimpleName();
 103  
     }
 104  
 
 105  
     void addBinding(IContainedComponent component, String binding, Location location)
 106  
     {
 107  0
         int equalsx = binding.indexOf('=');
 108  
 
 109  0
         if (equalsx < 1)
 110  0
             invalidBinding(binding);
 111  
 
 112  0
         if (equalsx + 1 >= binding.length())
 113  0
             invalidBinding(binding);
 114  
 
 115  0
         String name = binding.substring(0, equalsx).trim();
 116  0
         String value = binding.substring(equalsx + 1).trim();
 117  
 
 118  0
         IBindingSpecification bs = new BindingSpecification();
 119  0
         bs.setType(BindingType.PREFIXED);
 120  0
         bs.setValue(value);
 121  0
         bs.setLocation(location);
 122  
 
 123  0
         component.setBinding(name, bs);
 124  0
     }
 125  
 
 126  
     /**
 127  
      * @since 4.1.4
 128  
      */
 129  
     void addInheritedBinding(IContainedComponent component, String binding, Location location)
 130  
     {
 131  0
         int equalsx = binding.indexOf('=');
 132  
         String name;
 133  
         String containerName;
 134  
 
 135  0
         if (equalsx < 0)
 136  
         {
 137  0
             name = binding.trim();
 138  0
             containerName = name;
 139  
         }
 140  
         else
 141  
         {
 142  0
             name = binding.substring(0, equalsx).trim();
 143  0
             containerName = binding.substring(equalsx + 1).trim();
 144  
         }
 145  
 
 146  0
         IBindingSpecification bs = new BindingSpecification();
 147  0
         bs.setType(BindingType.INHERITED);
 148  0
         bs.setValue(containerName);
 149  0
         bs.setLocation(location);
 150  
 
 151  0
         component.setBinding(name, bs);
 152  0
     }
 153  
 
 154  
     protected void invalidBinding(String binding)
 155  
     {
 156  0
         throw new ApplicationRuntimeException(AnnotationMessages.bindingWrongFormat(binding));
 157  
     }
 158  
 }