001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.internal.parser;
014
015import org.apache.tapestry5.commons.Location;
016import org.apache.tapestry5.commons.Resource;
017import org.apache.tapestry5.commons.util.CollectionFactory;
018import org.apache.tapestry5.ioc.internal.util.InternalUtils;
019
020import static org.apache.tapestry5.commons.util.CollectionFactory.newList;
021
022import java.util.Collections;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026
027public class ComponentTemplateImpl implements ComponentTemplate
028{
029    private final Resource resource;
030
031    private final List<TemplateToken> tokens;
032
033    private final Map<String, Location> componentIds;
034
035    private final boolean extension, strictMixinParameters;
036
037    private final Map<String, List<TemplateToken>> overrides;
038
039    /**
040     * @param resource
041     *         the resource from which the template was parsed
042     * @param tokens
043     *         the tokens of the template, a copy of this list will be made
044     * @param componentIds
045     *         ids of components defined in the template
046     * @param extension
047     *         if this template is an extension of a parent-class template
048     * @param strictMixinParameters
049     *         if the template was parsed with the 5.4 DTD and is strict
050     *         about mixin parameters being fully qualified
051     * @param overrides
052     *         id to list of tokens for that override
053     */
054    public ComponentTemplateImpl(Resource resource, List<TemplateToken> tokens,
055                                 Map<String, Location> componentIds, boolean extension,
056                                 boolean strictMixinParameters, Map<String, List<TemplateToken>> overrides)
057    {
058        this.resource = resource;
059        this.extension = extension;
060        this.strictMixinParameters = strictMixinParameters;
061        this.overrides = overrides;
062        this.tokens = newList(tokens);
063        this.componentIds = CollectionFactory.newMap(componentIds);
064    }
065
066    public Resource getResource()
067    {
068        return resource;
069    }
070
071    public List<TemplateToken> getTokens()
072    {
073        return tokens;
074    }
075
076    public Map<String, Location> getComponentIds()
077    {
078        return componentIds;
079    }
080
081    public boolean usesStrictMixinParameters()
082    {
083        return strictMixinParameters;
084    }
085
086    /**
087     * Returns false.
088     */
089    public boolean isMissing()
090    {
091        return false;
092    }
093
094    public List<TemplateToken> getExtensionPointTokens(String extensionPointId)
095    {
096        return InternalUtils.get(overrides, extensionPointId);
097    }
098    
099    public Set<String> getExtensionPointIds()
100    {
101        return overrides != null ? overrides.keySet() : Collections.emptySet();
102    }
103
104    public boolean isExtension()
105    {
106        return extension;
107    }
108}