Coverage Report - org.apache.tapestry.javascript.JavascriptManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JavascriptManagerImpl
0%
0/61
0%
0/16
1.375
 
 1  
 // Copyright 2007 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.javascript;
 16  
 
 17  
 import java.util.ArrayList;
 18  
 import java.util.List;
 19  
 
 20  
 import org.apache.hivemind.HiveMind;
 21  
 import org.apache.hivemind.Location;
 22  
 import org.apache.hivemind.util.URLResource;
 23  
 import org.apache.tapestry.IAsset;
 24  
 import org.apache.tapestry.IMarkupWriter;
 25  
 import org.apache.tapestry.IRequestCycle;
 26  
 import org.apache.tapestry.TapestryUtils;
 27  
 import org.apache.tapestry.asset.AssetSource;
 28  
 import org.apache.tapestry.util.DescribedLocation;
 29  
 
 30  
 /**
 31  
  * An implementation that accepts a comma separated String for
 32  
  * files, formFiles and widgetFiles. 
 33  
  *
 34  
  * @author Andreas Andreou
 35  
  * @since 4.1.4
 36  
  */
 37  
 public class JavascriptManagerImpl implements JavascriptManager
 38  
 {
 39  
     private AssetSource _assetSource;
 40  
     private List _files;
 41  
     private List _formFiles;
 42  
     private List _widgetFiles;
 43  
     private IAsset _path;
 44  
     private IAsset _tapestryFile;
 45  
     private IAsset _tapestryPath;
 46  
 
 47  
     public JavascriptManagerImpl()
 48  0
     {
 49  0
         _files = new ArrayList();
 50  0
         _formFiles = new ArrayList();
 51  0
         _widgetFiles = new ArrayList();
 52  0
     }
 53  
 
 54  
     public IAsset getFirstAsset()
 55  
     {
 56  0
         return findFirst(_files);
 57  
     }
 58  
 
 59  
     public IAsset getFirstFormAsset()
 60  
     {
 61  0
         return findFirst(_formFiles);
 62  
     }
 63  
 
 64  
     public IAsset getFirstWidgetAsset()
 65  
     {
 66  0
         return findFirst(_widgetFiles);
 67  
     }
 68  
 
 69  
     public List getAssets()
 70  
     {
 71  0
         return _files;
 72  
     }
 73  
 
 74  
     public List getFormAssets()
 75  
     {
 76  0
         return _formFiles;
 77  
     }
 78  
 
 79  
     public List getWidgetAssets()
 80  
     {
 81  0
         return _widgetFiles;
 82  
     }
 83  
 
 84  
     public IAsset getPath()
 85  
     {
 86  0
         return _path;
 87  
     }
 88  
 
 89  
     public IAsset getTapestryAsset()
 90  
     {
 91  0
         return _tapestryFile;
 92  
     }
 93  
 
 94  
     public IAsset getTapestryPath()
 95  
     {
 96  0
         return _tapestryPath;
 97  
     }
 98  
 
 99  
     public void setFiles(String files)
 100  
     {
 101  0
         _files = buildAssetList(files, "files");
 102  0
     }
 103  
 
 104  
     public void setFormFiles(String formFiles)
 105  
     {
 106  0
         _formFiles = buildAssetList(formFiles, "formFiles");
 107  0
     }
 108  
 
 109  
     public void setWidgetFiles(String widgetFiles)
 110  
     {
 111  0
         _widgetFiles = buildAssetList(widgetFiles, "widgetFiles");
 112  0
     }
 113  
 
 114  
     public void setFolder(String path)
 115  
     {
 116  0
         _path = findAsset(path, "folder");
 117  0
     }
 118  
 
 119  
     public void setTapestryFile(String tapestryFile)
 120  
     {
 121  0
         _tapestryFile = findAsset(tapestryFile, "tapestryFile");
 122  0
     }
 123  
 
 124  
     public void setTapestryFolder(String tapestryPath)
 125  
     {
 126  0
         _tapestryPath = findAsset(tapestryPath, "tapestryFolder");
 127  0
     }
 128  
 
 129  
     public void setAssetSource(AssetSource assetSource)
 130  
     {
 131  0
         _assetSource = assetSource;
 132  0
     }
 133  
 
 134  
         public void renderLibraryResources(IMarkupWriter writer,
 135  
                         IRequestCycle cycle, boolean hasForm, boolean hasWidget) 
 136  
         {
 137  
         // include all the main js packages                
 138  0
         appendAssetsAsJavascript(writer, cycle, getAssets());        
 139  
         
 140  0
         if (hasForm)
 141  
         {
 142  0
             appendAssetsAsJavascript(writer, cycle, getFormAssets());
 143  
         }
 144  0
         if (hasWidget)
 145  
         {
 146  0
             appendAssetsAsJavascript(writer, cycle, getWidgetAssets());
 147  
         }                
 148  
                 
 149  0
         }    
 150  
     
 151  
     public void renderLibraryAdaptor(IMarkupWriter writer, IRequestCycle cycle) 
 152  
     {
 153  
             // include the tapestry js
 154  0
         IAsset tapestryAsset = getTapestryAsset();
 155  0
         if (tapestryAsset!=null)
 156  
         {
 157  0
             appendAssetAsJavascript(writer, cycle, tapestryAsset);
 158  
         }            
 159  0
         }
 160  
 
 161  
 
 162  
         
 163  
     /**
 164  
      * Appends a script tag to include the given asset. 
 165  
      * @param writer
 166  
      * @param cycle
 167  
      * @param asset
 168  
      */
 169  
     protected void appendAssetAsJavascript(IMarkupWriter writer, IRequestCycle cycle, IAsset asset)
 170  
     {
 171  0
         final String url = asset.buildURL();
 172  
         
 173  0
         writer.begin("script");
 174  0
         writer.attribute("type", "text/javascript");
 175  0
         writer.attribute("src", url);
 176  0
         writer.end();
 177  0
         writer.println();
 178  0
     }        
 179  
 
 180  
     protected void appendAssetsAsJavascript(IMarkupWriter writer, IRequestCycle cycle, List jsAssets)
 181  
     {
 182  0
         for (int i = 0; i < jsAssets.size(); i++)
 183  
         {
 184  0
             appendAssetAsJavascript(writer, cycle, (IAsset) jsAssets.get(i));
 185  
         }
 186  0
     }        
 187  
 
 188  
         /**
 189  
      * Builds a {@link List} of {@link IAsset} from a comma
 190  
      * separated input string.
 191  
      * 
 192  
      * @param files A comma separated string.
 193  
      * @param name Description of assets.
 194  
      * @return
 195  
      */
 196  
     protected List buildAssetList(String files, String name)
 197  
     {
 198  0
         String[] js = TapestryUtils.split(files);
 199  
         
 200  0
         List list = new ArrayList(js.length);
 201  0
         for (int i=0; i<js.length; i++) {
 202  0
             list.add(findAsset(js[i], name + i));
 203  
         }
 204  
 
 205  0
         return list;
 206  
     }
 207  
 
 208  
     /**
 209  
      * Finds the given asset (in classpath, context, e.t.c.).
 210  
      * 
 211  
      * @param path
 212  
      * @param description
 213  
      * @return
 214  
      */
 215  
     protected IAsset findAsset(String path, String description)
 216  
     {
 217  0
         IAsset asset = null;
 218  0
         if ( !HiveMind.isBlank(path) )
 219  
         {
 220  0
             Location location = new DescribedLocation(new URLResource(path), description);
 221  0
             asset = _assetSource.findAsset(null, path, null, location);
 222  
         }
 223  0
         return asset;
 224  
     }
 225  
 
 226  
     private IAsset findFirst(List list)
 227  
     {
 228  0
         if (list == null || list.isEmpty())
 229  0
             return null;
 230  
         else
 231  0
             return (IAsset) list.get(0);
 232  
     }
 233  
 }