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 *    https://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    @Override
113    public boolean equals( Object obj )
114    {
115        if ( obj instanceof Tag )
116        {
117            Tag tag = ( Tag ) obj;
118            
119            return ( this.name.equals( tag.getName() ) ) && ( this.type == tag.getType() );
120
121        }
122        else
123        {
124            return false;
125        }
126    }
127
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public int hashCode()
134    {
135        return name.hashCode() + type << 24;
136    }
137
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public String toString()
144    {
145        if ( name != null )
146        {
147            return "<" + ( ( type == Tag.END ) ? "/" : "" ) + name + ">";
148        }
149        else
150        {
151            return "Unknown tag";
152        }
153    }
154}