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.IMarkupWriter;
025    import org.apache.tapestry.IRequestCycle;
026    import org.apache.tapestry.TapestryUtils;
027    import org.apache.tapestry.asset.AssetSource;
028    import org.apache.tapestry.util.DescribedLocation;
029    
030    /**
031     * An implementation that accepts a comma separated String for
032     * files, formFiles and widgetFiles. 
033     *
034     * @author Andreas Andreou
035     * @since 4.1.4
036     */
037    public class JavascriptManagerImpl implements JavascriptManager
038    {
039        private AssetSource _assetSource;
040        private List _files;
041        private List _formFiles;
042        private List _widgetFiles;
043        private IAsset _path;
044        private IAsset _tapestryFile;
045        private IAsset _tapestryPath;
046    
047        public JavascriptManagerImpl()
048        {
049            _files = new ArrayList();
050            _formFiles = new ArrayList();
051            _widgetFiles = new ArrayList();
052        }
053    
054        public IAsset getFirstAsset()
055        {
056            return findFirst(_files);
057        }
058    
059        public IAsset getFirstFormAsset()
060        {
061            return findFirst(_formFiles);
062        }
063    
064        public IAsset getFirstWidgetAsset()
065        {
066            return findFirst(_widgetFiles);
067        }
068    
069        public List getAssets()
070        {
071            return _files;
072        }
073    
074        public List getFormAssets()
075        {
076            return _formFiles;
077        }
078    
079        public List getWidgetAssets()
080        {
081            return _widgetFiles;
082        }
083    
084        public IAsset getPath()
085        {
086            return _path;
087        }
088    
089        public IAsset getTapestryAsset()
090        {
091            return _tapestryFile;
092        }
093    
094        public IAsset getTapestryPath()
095        {
096            return _tapestryPath;
097        }
098    
099        public void setFiles(String files)
100        {
101            _files = buildAssetList(files, "files");
102        }
103    
104        public void setFormFiles(String formFiles)
105        {
106            _formFiles = buildAssetList(formFiles, "formFiles");
107        }
108    
109        public void setWidgetFiles(String widgetFiles)
110        {
111            _widgetFiles = buildAssetList(widgetFiles, "widgetFiles");
112        }
113    
114        public void setFolder(String path)
115        {
116            _path = findAsset(path, "folder");
117        }
118    
119        public void setTapestryFile(String tapestryFile)
120        {
121            _tapestryFile = findAsset(tapestryFile, "tapestryFile");
122        }
123    
124        public void setTapestryFolder(String tapestryPath)
125        {
126            _tapestryPath = findAsset(tapestryPath, "tapestryFolder");
127        }
128    
129        public void setAssetSource(AssetSource assetSource)
130        {
131            _assetSource = assetSource;
132        }
133    
134            public void renderLibraryResources(IMarkupWriter writer,
135                            IRequestCycle cycle, boolean hasForm, boolean hasWidget) 
136            {
137            // include all the main js packages             
138            appendAssetsAsJavascript(writer, cycle, getAssets());        
139            
140            if (hasForm)
141            {
142                appendAssetsAsJavascript(writer, cycle, getFormAssets());
143            }
144            if (hasWidget)
145            {
146                appendAssetsAsJavascript(writer, cycle, getWidgetAssets());
147            }               
148                    
149            }    
150        
151        public void renderLibraryAdaptor(IMarkupWriter writer, IRequestCycle cycle) 
152        {
153            // include the tapestry js
154            IAsset tapestryAsset = getTapestryAsset();
155            if (tapestryAsset!=null)
156            {
157                appendAssetAsJavascript(writer, cycle, tapestryAsset);
158            }       
159            }
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            final String url = asset.buildURL();
172            
173            writer.begin("script");
174            writer.attribute("type", "text/javascript");
175            writer.attribute("src", url);
176            writer.end();
177            writer.println();
178        }        
179    
180        protected void appendAssetsAsJavascript(IMarkupWriter writer, IRequestCycle cycle, List jsAssets)
181        {
182            for (int i = 0; i < jsAssets.size(); i++)
183            {
184                appendAssetAsJavascript(writer, cycle, (IAsset) jsAssets.get(i));
185            }
186        }   
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            String[] js = TapestryUtils.split(files);
199            
200            List list = new ArrayList(js.length);
201            for (int i=0; i<js.length; i++) {
202                list.add(findAsset(js[i], name + i));
203            }
204    
205            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            IAsset asset = null;
218            if ( !HiveMind.isBlank(path) )
219            {
220                Location location = new DescribedLocation(new URLResource(path), description);
221                asset = _assetSource.findAsset(null, path, null, location);
222            }
223            return asset;
224        }
225    
226        private IAsset findFirst(List list)
227        {
228            if (list == null || list.isEmpty())
229                return null;
230            else
231                return (IAsset) list.get(0);
232        }
233    }