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.ldap.codec.controls.sort;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
025import org.apache.directory.api.asn1.ber.grammar.Grammar;
026import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
027import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
028import org.apache.directory.api.asn1.ber.tlv.BerValue;
029import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
030import org.apache.directory.api.util.Strings;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * Grammar for decoding SortResponseControl.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class SortResponseGrammar extends AbstractGrammar<SortResponseContainer>
041{
042    /** The logger */
043    static final Logger LOG = LoggerFactory.getLogger( SortRequestGrammar.class );
044
045    /** Speedup for logs */
046    static final boolean IS_DEBUG = LOG.isDebugEnabled();
047
048    /** The instance of grammar. SortResponseGrammar is a singleton */
049    private static Grammar<SortResponseContainer> instance = new SortResponseGrammar();
050
051
052    @SuppressWarnings("unchecked")
053    private SortResponseGrammar()
054    {
055        setName( SortResponseGrammar.class.getName() );
056
057        // Create the transitions table
058        super.transitions = new GrammarTransition[SortResponseStates.END_STATE.ordinal()][256];
059
060        super.transitions[SortResponseStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
061            new GrammarTransition<SortResponseContainer>( SortResponseStates.START_STATE,
062                SortResponseStates.SEQUENCE_STATE,
063                UniversalTag.SEQUENCE.getValue(), null );
064        
065        super.transitions[SortResponseStates.SEQUENCE_STATE.ordinal()][UniversalTag.ENUMERATED.getValue()] =
066            new GrammarTransition<SortResponseContainer>( SortResponseStates.SEQUENCE_STATE,
067                SortResponseStates.RESULT_CODE_STATE,
068                UniversalTag.ENUMERATED.getValue(), new StoreSortResponseResultCode<SortResponseContainer>() );
069
070        super.transitions[SortResponseStates.RESULT_CODE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
071            new GrammarTransition<SortResponseContainer>( SortResponseStates.RESULT_CODE_STATE,
072                SortResponseStates.AT_DESC_STATE,
073                UniversalTag.OCTET_STRING.getValue(), new GrammarAction<SortResponseContainer>()
074                {
075
076                    @Override
077                    public void action( SortResponseContainer container ) throws DecoderException
078                    {
079                        BerValue value = container.getCurrentTLV().getValue();
080
081                        String atType = Strings.utf8ToString( value.getData() );
082                        if ( IS_DEBUG )
083                        {
084                            LOG.debug( "AttributeType = " + atType );
085                        }
086                        
087                        container.getControl().setAttributeName( atType );
088                        container.setGrammarEndAllowed( true );
089                    }
090                } );
091
092    }
093
094
095    /**
096     * This class is a singleton.
097     * 
098     * @return An instance on this grammar
099     */
100    public static Grammar<?> getInstance()
101    {
102        return instance;
103    }
104}