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.search.subentries;
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.BooleanDecoder;
30  import org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException;
31  import org.apache.directory.api.asn1.ber.tlv.TLV;
32  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
33  import org.apache.directory.api.i18n.I18n;
34  import org.apache.directory.api.util.Strings;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * This class implements the SubEntryControl. All the actions are declared in
41   * this class. As it is a singleton, these declaration are only done once.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public final class SubentriesGrammar extends AbstractGrammar<SubentriesContainer>
46  {
47      /** The logger */
48      static final Logger LOG = LoggerFactory.getLogger( SubentriesGrammar.class );
49  
50      /** The instance of grammar. SubEntryControlGrammar is a singleton */
51      private static Grammar<SubentriesContainer> instance = new SubentriesGrammar();
52  
53  
54      /**
55       * Creates a new SubEntryGrammar object.
56       */
57      @SuppressWarnings("unchecked")
58      private SubentriesGrammar()
59      {
60          setName( SubentriesGrammar.class.getName() );
61  
62          // Create the transitions table
63          super.transitions = new GrammarTransition[SubentriesStates.LAST_SUB_ENTRY_STATE.ordinal()][256];
64  
65          super.transitions[SubentriesStates.START_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] =
66              new GrammarTransition<SubentriesContainer>( SubentriesStates.START_STATE,
67                  SubentriesStates.SUB_ENTRY_VISIBILITY_STATE, UniversalTag.BOOLEAN.getValue(),
68                  new GrammarAction<SubentriesContainer>( "SubEntryControl visibility" )
69                  {
70                      public void action( SubentriesContainer container ) throws DecoderException
71                      {
72                          TLV tlv = container.getCurrentTLV();
73  
74                          // We get the value. If it's a 0, it's a FALSE. If it's
75                          // a FF, it's a TRUE. Any other value should be an error,
76                          // but we could relax this constraint. So if we have
77                          // something
78                          // which is not 0, it will be interpreted as TRUE, but we
79                          // will generate a warning.
80                          BerValue value = tlv.getValue();
81  
82                          try
83                          {
84                              container.getSubentriesControl().setVisibility( BooleanDecoder.parse( value ) );
85  
86                              // We can have an END transition
87                              container.setGrammarEndAllowed( true );
88                          }
89                          catch ( BooleanDecoderException bde )
90                          {
91                              LOG.error( I18n.err( I18n.ERR_04054, Strings.dumpBytes( value.getData() ), bde.getMessage() ) );
92  
93                              // This will generate a PROTOCOL_ERROR
94                              throw new DecoderException( bde.getMessage() );
95                          }
96                      }
97                  } );
98      }
99  
100 
101     /**
102      * This class is a singleton.
103      * 
104      * @return An instance on this grammar
105      */
106     public static Grammar<SubentriesContainer> getInstance()
107     {
108         return instance;
109     }
110 }