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    package org.apache.directory.shared.ldap.codec.search.controls.subentries;
021    
022    
023    import org.apache.directory.shared.asn1.ber.IAsn1Container;
024    import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
025    import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
026    import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
027    import org.apache.directory.shared.asn1.ber.grammar.IGrammar;
028    import org.apache.directory.shared.asn1.ber.tlv.TLV;
029    import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
030    import org.apache.directory.shared.asn1.ber.tlv.Value;
031    import org.apache.directory.shared.asn1.codec.DecoderException;
032    import org.apache.directory.shared.asn1.util.BooleanDecoder;
033    import org.apache.directory.shared.asn1.util.BooleanDecoderException;
034    import org.apache.directory.shared.i18n.I18n;
035    import org.apache.directory.shared.ldap.util.StringTools;
036    import org.slf4j.Logger;
037    import org.slf4j.LoggerFactory;
038    
039    
040    /**
041     * This class implements the SubEntryControl. All the actions are declared in
042     * this class. As it is a singleton, these declaration are only done once.
043     * 
044     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045     * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $, 
046     */
047    public class SubentriesControlGrammar extends AbstractGrammar
048    {
049        /** The logger */
050        static final Logger log = LoggerFactory.getLogger( SubentriesControlGrammar.class );
051    
052        /** The instance of grammar. SubEntryControlGrammar is a singleton */
053        private static IGrammar instance = new SubentriesControlGrammar();
054    
055    
056        /**
057         * Creates a new SubEntryGrammar object.
058         */
059        private SubentriesControlGrammar()
060        {
061            name = SubentriesControlGrammar.class.getName();
062            statesEnum = SubentriesControlStatesEnum.getInstance();
063    
064            // Create the transitions table
065            super.transitions = new GrammarTransition[SubentriesControlStatesEnum.LAST_SUB_ENTRY_STATE][256];
066    
067            super.transitions[SubentriesControlStatesEnum.START_STATE][UniversalTag.BOOLEAN_TAG] = 
068                new GrammarTransition( SubentriesControlStatesEnum.START_STATE, 
069                                        SubentriesControlStatesEnum.SUB_ENTRY_VISIBILITY_STATE, UniversalTag.BOOLEAN_TAG, 
070                    new GrammarAction( "SubEntryControl visibility" )
071                {
072                    public void action( IAsn1Container container ) throws DecoderException
073                    {
074                        SubentriesControlContainer subEntryContainer = ( SubentriesControlContainer ) container;
075                        SubentriesControl control = subEntryContainer.getSubEntryControl();
076    
077                        TLV tlv = subEntryContainer.getCurrentTLV();
078    
079                        // We get the value. If it's a 0, it's a FALSE. If it's
080                        // a FF, it's a TRUE. Any other value should be an error,
081                        // but we could relax this constraint. So if we have
082                        // something
083                        // which is not 0, it will be interpreted as TRUE, but we
084                        // will generate a warning.
085                        Value value = tlv.getValue();
086    
087                        try
088                        {
089                            control.setVisibility( BooleanDecoder.parse( value ) );
090    
091                            // We can have an END transition
092                            container.grammarEndAllowed( true );
093                        }
094                        catch ( BooleanDecoderException bde )
095                        {
096                            log.error( I18n.err( I18n.ERR_04054, StringTools.dumpBytes( value.getData() ), bde.getMessage() ) );
097    
098                            // This will generate a PROTOCOL_ERROR
099                            throw new DecoderException( bde.getMessage() );
100                        }
101                    }
102                } );
103        }
104    
105    
106        /**
107         * This class is a singleton.
108         * 
109         * @return An instance on this grammar
110         */
111        public static IGrammar getInstance()
112        {
113            return instance;
114        }
115    }