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     */
020    
021    package org.apache.directory.shared.dsmlv2;
022    
023    
024    /**
025     * This class represents a XML tag.
026     * A XML tag is defined with :
027     * <ul>
028     *      <li>a name</li>
029     *      <li>a type (START tag or END tag)</li>
030     * </ul>
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev$, $Date$
034     */
035    public class Tag
036    {
037        /** The name of the tag */
038        private String name;
039    
040        /** The type of the tag */
041        private int type;
042    
043        /** This int represents a START tag */
044        public static int START = 0;
045    
046        /** This int represents a END tag */
047        public static int END = 1;
048    
049    
050        /**
051         * Creates a new instance of Tag.
052         *
053         * @param name
054         *      the name of the tag
055         * @param type
056         *      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
069         *      the name of the tag
070         */
071        public String getName()
072        {
073            return name;
074        }
075    
076    
077        /**
078         * Sets the name of the tag
079         *
080         * @param name
081         *      the name to set
082         */
083        public void setName( String name )
084        {
085            this.name = name.toLowerCase();
086        }
087    
088    
089        /**
090         * Gets the type of the tag
091         *
092         * @return
093         *      the type of the tag
094         */
095        public int getType()
096        {
097            return type;
098        }
099    
100    
101        /**
102         * Sets the type of the tag
103         *
104         * @param type
105         *      the type to set
106         */
107        public void setType( int type )
108        {
109            this.type = type;
110        }
111    
112    
113        /* (non-Javadoc)
114         * @see java.lang.Object#equals(java.lang.Object)
115         */
116        @Override
117        public boolean equals( Object obj )
118        {
119            if ( obj instanceof Tag )
120            {
121                Tag tag = ( Tag ) obj;
122                return ( ( this.name.equals( tag.getName() ) ) && ( this.type == tag.getType() ) );
123    
124            }
125            else
126            {
127                return false;
128            }
129        }
130    
131    
132        /* (non-Javadoc)
133         * @see java.lang.Object#hashCode()
134         */
135        @Override
136        public int hashCode()
137        {
138            return name.hashCode() + type << 24;
139        }
140    
141    
142        /* (non-Javadoc)
143         * @see java.lang.Object#toString()
144         */
145        @Override
146        public String toString()
147        {
148            if ( name != null )
149            {
150                return "<" + ( ( type == Tag.END ) ? "/" : "" ) + name + ">";
151            }
152            else
153            {
154                return "Unknown tag";
155            }
156        }
157    }