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