Coverage Report - org.apache.tapestry.enhance.InjectScriptWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectScriptWorker
100% 
100% 
1
 
 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  
 
 19  
 import org.apache.hivemind.Location;
 20  
 import org.apache.hivemind.Resource;
 21  
 import org.apache.hivemind.service.MethodSignature;
 22  
 import org.apache.hivemind.util.Defense;
 23  
 import org.apache.tapestry.IScript;
 24  
 import org.apache.tapestry.engine.IScriptSource;
 25  
 import org.apache.tapestry.spec.InjectSpecification;
 26  
 
 27  
 /**
 28  
  * Injects {@link org.apache.tapestry.IScript} instances directly into pages or components.
 29  
  * 
 30  
  * @author Howard M. Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  1
 public class InjectScriptWorker implements InjectEnhancementWorker
 34  
 {
 35  
     private IScriptSource _source;
 36  
 
 37  
     public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
 38  
     {
 39  1
         String propertyName = spec.getProperty();
 40  1
         String scriptName = spec.getObject();
 41  1
         Location location = spec.getLocation();
 42  
 
 43  1
         injectScript(op, propertyName, scriptName, location);
 44  1
     }
 45  
 
 46  
     /**
 47  
      * Injects a compiled script.
 48  
      * 
 49  
      * @param op
 50  
      *            the enhancement operation
 51  
      * @param propertyName
 52  
      *            the name of the property to inject
 53  
      * @param scriptName
 54  
      *            the name of the script (relative to the location)
 55  
      * @param location
 56  
      *            the location of the specification; primarily used as the base location for finding
 57  
      *            the script.
 58  
      */
 59  
 
 60  
     public void injectScript(EnhancementOperation op, String propertyName, String scriptName,
 61  
             Location location)
 62  
     {
 63  1
         Defense.notNull(op, "op");
 64  1
         Defense.notNull(propertyName, "propertyName");
 65  1
         Defense.notNull(scriptName, "scriptName");
 66  1
         Defense.notNull(location, "location");
 67  
 
 68  1
         op.claimReadonlyProperty(propertyName);
 69  
 
 70  3
         Class propertyType = EnhanceUtils.verifyPropertyType(op, propertyName, IScript.class);
 71  
 
 72  
         // PropertyType will likely be either java.lang.Object or IScript
 73  
 
 74  1
         String methodName = op.getAccessorMethodName(propertyName);
 75  
 
 76  1
         Resource resource = location.getResource().getRelativeResource(scriptName);
 77  
 
 78  1
         DeferredScript script = new DeferredScriptImpl(resource, _source, location);
 79  
 
 80  1
         String fieldName = op.addInjectedField("_$script", DeferredScript.class, script);
 81  
 
 82  1
         MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
 83  
 
 84  1
         op.addMethod(Modifier.PUBLIC, sig, "return " + fieldName + ".getScript();", location);
 85  1
     }
 86  
 
 87  
     public void setSource(IScriptSource source)
 88  
     {
 89  1
         _source = source;
 90  1
     }
 91  
 }