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.asn1.ber.grammar;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.Asn1Container;
025import org.apache.directory.api.asn1.util.Asn1StringUtils;
026import org.apache.directory.api.i18n.I18n;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * The abstract Grammar which is the Mother of all the grammars. It contains
033 * the transitions table.
034 *
035 * @param <C> The container type
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public abstract class AbstractGrammar<C extends Asn1Container> implements Grammar<C>
040{
041    /** The logger */
042    private static final Logger LOG = LoggerFactory.getLogger( AbstractGrammar.class );
043
044    /** Speedup for logs */
045    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
046
047    /**
048     * Table of transitions. It's a two dimension array, the first dimension
049     * indices the states, the second dimension indices the Tag value, so it is
050     * 256 wide.
051     */
052    protected GrammarTransition<C>[][] transitions;
053
054    /** The grammar name */
055    private String name;
056
057
058    /** Default constructor */
059    public AbstractGrammar()
060    {
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public String getName()
069    {
070        return name;
071    }
072
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public void setName( String name )
079    {
080        this.name = name;
081    }
082
083
084    /**
085     * Get the transition associated with the state and tag
086     *
087     * @param state The current state
088     * @param tag The current tag
089     * @return A valid transition if any, or null.
090     */
091    public GrammarTransition<C> getTransition( Enum<?> state, int tag )
092    {
093        return transitions[state.ordinal()][tag & 0x00FF];
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public void executeAction( C container ) throws DecoderException
102    {
103
104        Enum<?> currentState = container.getTransition();
105        // We have to deal with the special case of a GRAMMAR_END state
106        if ( ( ( States ) currentState ).isEndState() )
107        {
108            return;
109        }
110
111        byte tagByte = container.getCurrentTLV().getTag();
112
113        // We will loop until no more actions are to be executed
114        @SuppressWarnings("unchecked")
115        GrammarTransition<C> transition = ( ( AbstractGrammar<C> ) container.getGrammar() ).getTransition(
116            currentState,
117            tagByte );
118
119        if ( transition == null )
120        {
121            String errorMessage = I18n.err( I18n.ERR_00001_BAD_TRANSITION_FROM_STATE, currentState,
122                Asn1StringUtils.dumpByte( tagByte ) );
123
124            LOG.error( errorMessage );
125
126            // If we have no more grammar on the stack, then this is an
127            // error
128            throw new DecoderException( errorMessage );
129        }
130
131        if ( IS_DEBUG )
132        {
133            LOG.debug( transition.toString() );
134        }
135
136        if ( transition.hasAction() )
137        {
138            Action<C> action = transition.getAction();
139            action.action( container );
140        }
141
142        container.setTransition( transition.getCurrentState() );
143    }
144}