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.search.subentries; 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.BooleanDecoder; 030import org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException; 031import org.apache.directory.api.asn1.ber.tlv.TLV; 032import org.apache.directory.api.asn1.ber.tlv.UniversalTag; 033import org.apache.directory.api.i18n.I18n; 034import org.apache.directory.api.util.Strings; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038 039/** 040 * This class implements the SubEntryControl. All the actions are declared in 041 * this class. As it is a singleton, these declaration are only done once. 042 * 043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 044 */ 045public final class SubentriesGrammar extends AbstractGrammar<SubentriesContainer> 046{ 047 /** The logger */ 048 static final Logger LOG = LoggerFactory.getLogger( SubentriesGrammar.class ); 049 050 /** The instance of grammar. SubEntryControlGrammar is a singleton */ 051 private static Grammar<SubentriesContainer> instance = new SubentriesGrammar(); 052 053 054 /** 055 * Creates a new SubEntryGrammar object. 056 */ 057 @SuppressWarnings("unchecked") 058 private SubentriesGrammar() 059 { 060 setName( SubentriesGrammar.class.getName() ); 061 062 // Create the transitions table 063 super.transitions = new GrammarTransition[SubentriesStates.LAST_SUB_ENTRY_STATE.ordinal()][256]; 064 065 super.transitions[SubentriesStates.START_STATE.ordinal()][UniversalTag.BOOLEAN.getValue()] = 066 new GrammarTransition<SubentriesContainer>( SubentriesStates.START_STATE, 067 SubentriesStates.SUB_ENTRY_VISIBILITY_STATE, UniversalTag.BOOLEAN.getValue(), 068 new GrammarAction<SubentriesContainer>( "SubEntryControl visibility" ) 069 { 070 public void action( SubentriesContainer container ) throws DecoderException 071 { 072 TLV tlv = container.getCurrentTLV(); 073 074 // We get the value. If it's a 0, it's a FALSE. If it's 075 // a FF, it's a TRUE. Any other value should be an error, 076 // but we could relax this constraint. So if we have 077 // something 078 // which is not 0, it will be interpreted as TRUE, but we 079 // will generate a warning. 080 BerValue value = tlv.getValue(); 081 082 try 083 { 084 container.getSubentriesControl().setVisibility( BooleanDecoder.parse( value ) ); 085 086 // We can have an END transition 087 container.setGrammarEndAllowed( true ); 088 } 089 catch ( BooleanDecoderException bde ) 090 { 091 LOG.error( I18n.err( I18n.ERR_04054, Strings.dumpBytes( value.getData() ), bde.getMessage() ) ); 092 093 // This will generate a PROTOCOL_ERROR 094 throw new DecoderException( bde.getMessage() ); 095 } 096 } 097 } ); 098 } 099 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}