    001    // Copyright 2010 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.tapestry5.internal.services.javascript;
    016
    017    import java.util.List;
    018
    019    import org.apache.tapestry5.Asset;
    020    import org.apache.tapestry5.SymbolConstants;
    021    import org.apache.tapestry5.func.F;
    022    import org.apache.tapestry5.func.Mapper;
    023    import org.apache.tapestry5.internal.services.RequestConstants;
    024    import org.apache.tapestry5.ioc.annotations.Symbol;
    025    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    026    import org.apache.tapestry5.ioc.services.ThreadLocale;
    027    import org.apache.tapestry5.services.assets.AssetPathConstructor;
    028    import org.apache.tapestry5.services.javascript.JavaScriptStack;
    029    import org.apache.tapestry5.services.javascript.JavaScriptStackSource;
    030
    031    public class JavaScriptStackPathConstructorImpl implements JavaScriptStackPathConstructor
    032    {
    033        private final ThreadLocale threadLocale;
    034
    035        private final AssetPathConstructor assetPathConstructor;
    036
    037        private final JavaScriptStackSource javascriptStackSource;
    038
    039        private final boolean combineScripts;
    040
    041        private final Mapper<Asset, String> toPath = new Mapper<Asset, String>()
    042        {
    043            public String map(Asset input)
    044            {
    045                return input.toClientURL();
    046            }
    047        };
    048
    049        public JavaScriptStackPathConstructorImpl(ThreadLocale threadLocale, AssetPathConstructor assetPathConstructor,
    050                JavaScriptStackSource javascriptStackSource,
    051
    052                @Symbol(SymbolConstants.COMBINE_SCRIPTS)
    053                boolean combineScripts)
    054        {
    055            this.threadLocale = threadLocale;
    056            this.assetPathConstructor = assetPathConstructor;
    057            this.javascriptStackSource = javascriptStackSource;
    058            this.combineScripts = combineScripts;
    059        }
    060
    061        public List<String> constructPathsForJavaScriptStack(String stackName)
    062        {
    063            JavaScriptStack stack = javascriptStackSource.getStack(stackName);
    064
    065            List<Asset> assets = stack.getJavaScriptLibraries();
    066
    067            if (assets.size() > 1 && combineScripts)
    068                return combinedStackURL(stackName);
    069
    070            return toPaths(assets);
    071        }
    072
    073        private List<String> toPaths(List<Asset> assets)
    074        {
    075            assert assets != null;
    076            return F.flow(assets).map(toPath).toList();
    077        }
    078
    079        private List<String> combinedStackURL(String stackName)
    080        {
    081            String path = String.format("%s/%s.js", threadLocale.getLocale().toString(), stackName);
    082
    083            String stackURL = assetPathConstructor.constructAssetPath(RequestConstants.STACK_FOLDER, path);
    084
    085            return CollectionFactory.newList(stackURL);
    086        }
    087
    088    }