001// Copyright 2011, 2012 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
015package org.apache.tapestry5.internal.pageload;
016
017import org.apache.tapestry5.commons.Resource;
018import org.apache.tapestry5.func.F;
019import org.apache.tapestry5.func.Mapper;
020import org.apache.tapestry5.ioc.util.LocalizedNameGenerator;
021import org.apache.tapestry5.model.ComponentModel;
022import org.apache.tapestry5.services.pageload.ComponentResourceLocator;
023import org.apache.tapestry5.services.pageload.ComponentResourceSelector;
024import org.apache.tapestry5.services.templates.ComponentTemplateLocator;
025
026import java.util.Arrays;
027import java.util.List;
028
029public class DefaultComponentResourceLocator implements ComponentResourceLocator
030{
031    private ComponentTemplateLocator componentTemplateLocator;
032
033    public DefaultComponentResourceLocator(ComponentTemplateLocator componentTemplateLocator)
034    {
035        this.componentTemplateLocator = componentTemplateLocator;
036    }
037
038    public Resource locateTemplate(ComponentModel model, ComponentResourceSelector selector)
039    {
040        // For 5.2 compatibility, this implementation delegates to the older
041        // ComponentTemplateLocator command chain. That may be removed in 5.4.
042
043        return componentTemplateLocator.locateTemplate(model, selector.locale);
044    }
045
046    public List<Resource> locateMessageCatalog(final Resource baseResource, ComponentResourceSelector selector)
047    {
048        String baseName = baseResource.getFile();
049
050        // This is the case for some of the "virtual resources" introduced in 5.4
051        if (baseName == null)
052        {
053            return Arrays.asList(baseResource.forLocale(selector.locale));
054        }
055
056        return F.flow(new LocalizedNameGenerator(baseName, selector.locale).iterator()).map(new Mapper<String, Resource>()
057        {
058            public Resource map(String element)
059            {
060                return baseResource.forFile(element);
061            }
062        }).toList();
063    }
064
065}