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.shared.asn1.actions; 021 022 023import org.apache.directory.shared.asn1.DecoderException; 024import org.apache.directory.shared.asn1.ber.Asn1Container; 025import org.apache.directory.shared.asn1.ber.grammar.GrammarAction; 026import org.apache.directory.shared.asn1.ber.tlv.IntegerDecoder; 027import org.apache.directory.shared.asn1.ber.tlv.IntegerDecoderException; 028import org.apache.directory.shared.asn1.ber.tlv.TLV; 029import org.apache.directory.shared.asn1.ber.tlv.Value; 030import org.apache.directory.shared.i18n.I18n; 031import org.apache.directory.shared.util.Strings; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035 036/** 037 * The action used to read an integer value 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 */ 041public abstract class AbstractReadInteger<E extends Asn1Container> extends GrammarAction<E> 042{ 043 /** The logger */ 044 private static final Logger LOG = LoggerFactory.getLogger( AbstractReadInteger.class ); 045 046 /** Speedup for logs */ 047 private static final boolean IS_DEBUG = LOG.isDebugEnabled(); 048 049 /** the acceptable minimum value for the expected value to be parsed */ 050 private int minValue = 0; 051 052 /** the acceptable maximum value for the expected value to be parsed */ 053 private int maxValue = Integer.MAX_VALUE; 054 055 056 /** 057 * Instantiates a new AbstractReadInteger action. 058 */ 059 public AbstractReadInteger( String name ) 060 { 061 super( name ); 062 } 063 064 065 /** 066 * 067 * Creates a new instance of AbstractReadInteger. 068 * 069 * @param name the action's name 070 * @param minValue the acceptable minimum value for the expected value to be read 071 * @param maxValue the acceptable maximum value for the value to be read 072 */ 073 public AbstractReadInteger( String name, int minValue, int maxValue ) 074 { 075 super( name ); 076 077 this.minValue = minValue; 078 this.maxValue = maxValue; 079 } 080 081 082 /** 083 * 084 * set the integer value to the appropriate field of ASN.1 object present in the container 085 * 086 * @param value the integer value 087 * @param container the ASN.1 object's container 088 */ 089 protected abstract void setIntegerValue( int value, E container ); 090 091 092 /** 093 * {@inheritDoc} 094 */ 095 public final void action( E container ) throws DecoderException 096 { 097 TLV tlv = container.getCurrentTLV(); 098 099 // The Length should not be null 100 if ( tlv.getLength() == 0 ) 101 { 102 LOG.error( I18n.err( I18n.ERR_04066 ) ); 103 104 // This will generate a PROTOCOL_ERROR 105 throw new DecoderException( I18n.err( I18n.ERR_04067 ) ); 106 } 107 108 Value value = tlv.getValue(); 109 110 try 111 { 112 int number = IntegerDecoder.parse( value, minValue, maxValue ); 113 114 if ( IS_DEBUG ) 115 { 116 LOG.debug( "read integer value : {}", number ); 117 } 118 119 setIntegerValue( number, container ); 120 } 121 catch ( IntegerDecoderException ide ) 122 { 123 LOG.error( I18n.err( I18n.ERR_04070, Strings.dumpBytes( value.getData() ), ide 124 .getLocalizedMessage() ) ); 125 126 // This will generate a PROTOCOL_ERROR 127 throw new DecoderException( ide.getMessage(), ide ); 128 } 129 } 130}