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     * Define a transition between two states of a grammar. It stores the next
026     * state, and the action to execute while transiting.
027     * 
028     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
029     * @version $Rev$, $Date$
030     */
031    public class GrammarTransition
032    {
033        /** The next state in the grammar */
034        private int nextState;
035    
036        /** The action associated to the transition */
037        private GrammarAction action;
038    
039        /** The current state */
040        private int currentState;
041    
042        
043        /**
044         * Creates a new GrammarTransition object.
045         * 
046         * @param currentState
047         *      The current transition
048         * @param nextState
049         *      The target state
050         * @param action
051         *      The action to execute. It could be null.
052         */
053        public GrammarTransition( int currentState, int nextState, GrammarAction action )
054        {
055            this.currentState = currentState;
056            this.nextState = nextState;
057            this.action = action;
058        }
059    
060        /**
061         * Gets the target state
062         * 
063         * @return
064         *      the target state.
065         */
066        public int getNextState()
067        {
068            return nextState;
069        }
070    
071    
072        /**
073         * Tells if the transition has an associated action.
074         * 
075         * @return 
076         *      <code>true</code> if an action has been asociated to the
077         *         transition
078         */
079        public boolean hasAction()
080        {
081            return action != null;
082        }
083    
084    
085        /**
086         * Gets the action associated with the transition
087         * 
088         * @return
089         *      the action associated with the transition
090         */
091        public GrammarAction getAction()
092        {
093            return action;
094        }
095    
096    
097        /**
098         * Returns a representation of the transition as a string
099         * 
100         * @param grammar
101         *      the grammar which state we want a String from
102         * @param statesEnum
103         *      the states enum that contains the states' names
104         * @return 
105         *      a representation of the transition as a string.
106         */
107        public String toString( int grammar, IStates statesEnum )
108        {
109    
110            StringBuffer sb = new StringBuffer();
111    
112            sb.append( "Transition from <" ).append( statesEnum.getState( currentState ) ).append( "> to <" ).append(
113                statesEnum.getState( nextState ) ).append( ">, action : " ).append(
114                ( ( action == null ) ? "no action" : action.toString() ) ).append( ">" );
115    
116            return sb.toString();
117        }
118    }