001// Copyright 2004, 2005 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.tapestry.parse;
016
017import java.util.HashMap;
018import java.util.Map;
019
020import org.apache.hivemind.Location;
021import org.apache.hivemind.util.ToStringBuilder;
022
023/**
024 * Token representing the open tag for a component. Components may be either specified or implicit.
025 * Specified components (the traditional type, dating back to the origin of Tapestry) are matched by
026 * an entry in the containing component's specification. Implicit components specify their type in
027 * the component template and must not have an entry in the containing component's specification.
028 * 
029 * @see TokenType#OPEN
030 * @author Howard Lewis Ship
031 * @since 3.0
032 */
033
034public class OpenToken extends TemplateToken
035{
036    private String _tag;
037
038    private String _id;
039
040    private String _componentType;
041
042    private Map _attributes;
043
044    /**
045     * Creates a new token with the given tag, id and type
046     * 
047     * @param tag
048     *            the template tag which represents the component, typically "span"
049     * @param id
050     *            the id for the component, which may be assigned by the template parser for
051     *            implicit components
052     * @param componentType
053     *            the type of component, if an implicit component, or null for a specified component
054     * @param location
055     *            location of tag represented by this token
056     */
057
058    public OpenToken(String tag, String id, String componentType, Location location)
059    {
060        super(TokenType.OPEN, location);
061
062        _tag = tag;
063        _id = id;
064        _componentType = componentType;
065    }
066
067    /**
068     * Returns the id for the component.
069     */
070
071    public String getId()
072    {
073        return _id;
074    }
075
076    /**
077     * Returns the tag used to represent the component within the template.
078     */
079
080    public String getTag()
081    {
082        return _tag;
083    }
084
085    /**
086     * Returns the specified component type, or null for a component where the type is not defined
087     * in the template. The type may include a library id prefix.
088     */
089
090    public String getComponentType()
091    {
092        return _componentType;
093    }
094
095    public void addAttribute(String name, String value)
096    {
097        if (_attributes == null)
098            _attributes = new HashMap();
099
100        _attributes.put(name, value);
101    }
102
103    /**
104     * Returns a Map of attributes. Keys and values are strings.
105     */
106
107    public Map getAttributesMap()
108    {
109        return _attributes;
110    }
111
112    protected void extendDescription(ToStringBuilder builder)
113    {
114        builder.append("id", _id);
115        builder.append("componentType", _componentType);
116        builder.append("tag", _tag);
117        builder.append("attributes", _attributes);
118    }
119
120}