1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.asn1.actions;
21
22
23 import org.apache.directory.api.asn1.DecoderException;
24 import org.apache.directory.api.asn1.ber.Asn1Container;
25 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
26 import org.apache.directory.api.asn1.ber.tlv.BerValue;
27 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
28 import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
29 import org.apache.directory.api.asn1.ber.tlv.TLV;
30 import org.apache.directory.api.i18n.I18n;
31 import org.apache.directory.api.util.Strings;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40
41
42
43 public abstract class AbstractReadInteger<E extends Asn1Container> extends GrammarAction<E>
44 {
45
46 private static final Logger LOG = LoggerFactory.getLogger( AbstractReadInteger.class );
47
48
49 private int minValue = 0;
50
51
52 private int maxValue = Integer.MAX_VALUE;
53
54
55
56
57
58
59
60 public AbstractReadInteger( String name )
61 {
62 super( name );
63 }
64
65
66
67
68
69
70
71
72
73
74 public AbstractReadInteger( String name, int minValue, int maxValue )
75 {
76 super( name );
77
78 this.minValue = minValue;
79 this.maxValue = maxValue;
80 }
81
82
83
84
85
86
87
88
89
90 protected abstract void setIntegerValue( int value, E container );
91
92
93
94
95
96 @Override
97 public final void action( E container ) throws DecoderException
98 {
99 TLV tlv = container.getCurrentTLV();
100
101
102 if ( tlv.getLength() == 0 )
103 {
104 String msg = I18n.err( I18n.ERR_01101_NULL_LENGTH );
105
106 LOG.error( msg );
107
108
109 throw new DecoderException( msg );
110 }
111
112 BerValue value = tlv.getValue();
113
114 try
115 {
116 int number = IntegerDecoder.parse( value, minValue, maxValue );
117
118 if ( LOG.isDebugEnabled() )
119 {
120 LOG.debug( I18n.msg( I18n.MSG_01100_INTEGER_VALUE, number ) );
121 }
122
123 setIntegerValue( number, container );
124 }
125 catch ( IntegerDecoderException ide )
126 {
127 LOG.error( I18n.err( I18n.ERR_01102_INVALID_INTEGER, Strings.dumpBytes( value.getData() ), ide
128 .getLocalizedMessage() ) );
129
130
131 throw new DecoderException( ide.getMessage(), ide );
132 }
133 }
134 }