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