001 // Copyright 2007 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.javascript;
016
017 import java.util.ArrayList;
018 import java.util.List;
019
020 import org.apache.hivemind.HiveMind;
021 import org.apache.hivemind.Location;
022 import org.apache.hivemind.util.URLResource;
023 import org.apache.tapestry.IAsset;
024 import org.apache.tapestry.TapestryUtils;
025 import org.apache.tapestry.asset.AssetSource;
026 import org.apache.tapestry.util.DescribedLocation;
027
028 /**
029 * An implementation that accepts a comma separated String for
030 * files, formFiles and widgetFiles.
031 *
032 * @author Andreas Andreou
033 * @since 4.1.4
034 */
035 public class JavascriptManagerImpl implements JavascriptManager
036 {
037 private AssetSource _assetSource;
038 private List _files;
039 private List _formFiles;
040 private List _widgetFiles;
041 private IAsset _path;
042 private IAsset _tapestryFile;
043 private IAsset _tapestryPath;
044
045 public JavascriptManagerImpl()
046 {
047 _files = new ArrayList();
048 _formFiles = new ArrayList();
049 _widgetFiles = new ArrayList();
050 }
051
052 public IAsset getFirstAsset()
053 {
054 return findFirst(_files);
055 }
056
057 public IAsset getFirstFormAsset()
058 {
059 return findFirst(_formFiles);
060 }
061
062 public IAsset getFirstWidgetAsset()
063 {
064 return findFirst(_widgetFiles);
065 }
066
067 public List getAssets()
068 {
069 return _files;
070 }
071
072 public List getFormAssets()
073 {
074 return _formFiles;
075 }
076
077 public List getWidgetAssets()
078 {
079 return _widgetFiles;
080 }
081
082 public IAsset getPath()
083 {
084 return _path;
085 }
086
087 public IAsset getTapestryAsset()
088 {
089 return _tapestryFile;
090 }
091
092 public IAsset getTapestryPath()
093 {
094 return _tapestryPath;
095 }
096
097 public void setFiles(String files)
098 {
099 _files = buildAssetList(files, "files");
100 }
101
102 public void setFormFiles(String formFiles)
103 {
104 _formFiles = buildAssetList(formFiles, "formFiles");
105 }
106
107 public void setWidgetFiles(String widgetFiles)
108 {
109 _widgetFiles = buildAssetList(widgetFiles, "widgetFiles");
110 }
111
112 public void setFolder(String path)
113 {
114 _path = findAsset(path, "folder");
115 }
116
117 public void setTapestryFile(String tapestryFile)
118 {
119 _tapestryFile = findAsset(tapestryFile, "tapestryFile");
120 }
121
122 public void setTapestryFolder(String tapestryPath)
123 {
124 _tapestryPath = findAsset(tapestryPath, "tapestryFolder");
125 }
126
127 public void setAssetSource(AssetSource assetSource)
128 {
129 _assetSource = assetSource;
130 }
131
132 private List buildAssetList(String files, String name)
133 {
134 String[] js = TapestryUtils.split(files);
135
136 List list = new ArrayList(js.length);
137 for (int i=0; i<js.length; i++) {
138 list.add(findAsset(js[i], name + i));
139 }
140
141 return list;
142 }
143
144 private IAsset findAsset(String path, String description)
145 {
146 IAsset asset = null;
147 if ( !HiveMind.isBlank(path) )
148 {
149 Location location = new DescribedLocation(new URLResource(path), description);
150 asset = _assetSource.findAsset(null, path, null, location);
151 }
152 return asset;
153 }
154
155 private IAsset findFirst(List list)
156 {
157 if (list == null || list.isEmpty())
158 return null;
159 else
160 return (IAsset) list.get(0);
161 }
162 }