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    
015    package org.apache.tapestry.parse;
016    
017    import org.apache.hivemind.Locatable;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.util.ToStringBuilder;
020    
021    /**
022     * Base class for a number of different types of tokens that can be extracted from a page/component
023     * template. This class defines the type of the token, subclasses provide interpretations on the
024     * token.
025     * 
026     * @author Howard Lewis Ship
027     */
028    
029    public abstract class TemplateToken implements Locatable
030    {
031        private TokenType _type;
032    
033        private Location _location;
034    
035        protected TemplateToken(TokenType type, Location location)
036        {
037            _type = type;
038            _location = location;
039        }
040    
041        public TokenType getType()
042        {
043            return _type;
044        }
045    
046        public Location getLocation()
047        {
048            return _location;
049        }
050    
051        public String toString()
052        {
053            ToStringBuilder builder = new ToStringBuilder(this);
054    
055            builder.append("type", _type.getName());
056            builder.append("location", _location);
057            
058            extendDescription(builder);
059    
060            return builder.toString();
061        }
062    
063        /**
064         * Overridden in subclasses to append additional fields (defined in the subclass) to the
065         * description. Subclasses may override this method without invoking this implementation, which
066         * is empty.
067         * 
068         * @since 3.0
069         */
070    
071        protected void extendDescription(ToStringBuilder builder)
072        {
073        }
074    }