View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.codec.controls.sort;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
25  import org.apache.directory.api.asn1.ber.grammar.Grammar;
26  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
27  import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
28  import org.apache.directory.api.asn1.ber.tlv.BerValue;
29  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30  import org.apache.directory.api.util.Strings;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * Grammar for decoding SortResponseControl.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class SortResponseGrammar extends AbstractGrammar<SortResponseContainer>
41  {
42      /** The logger */
43      static final Logger LOG = LoggerFactory.getLogger( SortRequestGrammar.class );
44  
45      /** Speedup for logs */
46      static final boolean IS_DEBUG = LOG.isDebugEnabled();
47  
48      /** The instance of grammar. SortResponseGrammar is a singleton */
49      private static Grammar<SortResponseContainer> instance = new SortResponseGrammar();
50  
51  
52      @SuppressWarnings("unchecked")
53      private SortResponseGrammar()
54      {
55          setName( SortResponseGrammar.class.getName() );
56  
57          // Create the transitions table
58          super.transitions = new GrammarTransition[SortResponseStates.END_STATE.ordinal()][256];
59  
60          super.transitions[SortResponseStates.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
61              new GrammarTransition<SortResponseContainer>( SortResponseStates.START_STATE,
62                  SortResponseStates.SEQUENCE_STATE,
63                  UniversalTag.SEQUENCE.getValue(), null );
64          
65          super.transitions[SortResponseStates.SEQUENCE_STATE.ordinal()][UniversalTag.ENUMERATED.getValue()] =
66              new GrammarTransition<SortResponseContainer>( SortResponseStates.SEQUENCE_STATE,
67                  SortResponseStates.RESULT_CODE_STATE,
68                  UniversalTag.ENUMERATED.getValue(), new StoreSortResponseResultCode<SortResponseContainer>() );
69  
70          super.transitions[SortResponseStates.RESULT_CODE_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
71              new GrammarTransition<SortResponseContainer>( SortResponseStates.RESULT_CODE_STATE,
72                  SortResponseStates.AT_DESC_STATE,
73                  UniversalTag.OCTET_STRING.getValue(), new GrammarAction<SortResponseContainer>()
74                  {
75  
76                      @Override
77                      public void action( SortResponseContainer container ) throws DecoderException
78                      {
79                          BerValue value = container.getCurrentTLV().getValue();
80  
81                          String atType = Strings.utf8ToString( value.getData() );
82                          if ( IS_DEBUG )
83                          {
84                              LOG.debug( "AttributeType = " + atType );
85                          }
86                          
87                          container.getControl().setAttributeName( atType );
88                          container.setGrammarEndAllowed( true );
89                      }
90                  } );
91  
92      }
93  
94  
95      /**
96       * This class is a singleton.
97       * 
98       * @return An instance on this grammar
99       */
100     public static Grammar<?> getInstance()
101     {
102         return instance;
103     }
104 }