001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 * 
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 * 
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 * 
019 */
020package org.apache.directory.api.dsmlv2;
021
022
023import org.apache.directory.api.util.Strings;
024
025
026/**
027 * This class represents a XML tag.
028 * <br>
029 * A XML tag is defined with :
030 * <ul>
031 *      <li>a name</li>
032 *      <li>a type (START tag or END tag)</li>
033 * </ul>
034 * 
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class Tag
038{
039    /** The name of the tag */
040    private String name;
041
042    /** The type of the tag */
043    private int type;
044
045    /** This int represents a START tag */
046    public static final int START = 0;
047
048    /** This int represents a END tag */
049    public static final int END = 1;
050
051
052    /**
053     * Creates a new instance of Tag.
054     *
055     * @param name the name of the tag
056     * @param type the type of the tag
057     */
058    public Tag( String name, int type )
059    {
060        setName( name );
061        setType( type );
062    }
063
064
065    /**
066     * Gets the name of the tag
067     *
068     * @return the name of the tag
069     */
070    public String getName()
071    {
072        return name;
073    }
074
075
076    /**
077     * Sets the name of the tag
078     *
079     * @param name the name to set
080     */
081    public void setName( String name )
082    {
083        this.name = Strings.toLowerCaseAscii( name );
084    }
085
086
087    /**
088     * Gets the type of the tag
089     *
090     * @return the type of the tag
091     */
092    public int getType()
093    {
094        return type;
095    }
096
097
098    /**
099     * Sets the type of the tag
100     *
101     * @param type the type to set
102     */
103    public void setType( int type )
104    {
105        this.type = type;
106    }
107
108
109    /**
110     * {@inheritDoc}
111     */
112    public boolean equals( Object obj )
113    {
114        if ( obj instanceof Tag )
115        {
116            Tag tag = ( Tag ) obj;
117            
118            return ( ( this.name.equals( tag.getName() ) ) && ( this.type == tag.getType() ) );
119
120        }
121        else
122        {
123            return false;
124        }
125    }
126
127
128    /**
129     * {@inheritDoc}
130     */
131    public int hashCode()
132    {
133        return name.hashCode() + type << 24;
134    }
135
136
137    /**
138     * {@inheritDoc}
139     */
140    public String toString()
141    {
142        if ( name != null )
143        {
144            return "<" + ( ( type == Tag.END ) ? "/" : "" ) + name + ">";
145        }
146        else
147        {
148            return "Unknown tag";
149        }
150    }
151}