View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1.ber.digester.rules ;
18  
19  
20  import java.nio.ByteBuffer;
21  
22  import org.apache.asn1.ber.TagEnum;
23  import org.apache.asn1.ber.TypeClass;
24  import org.apache.asn1.ber.digester.AbstractRule;
25  import org.apache.asn1.ber.primitives.PrimitiveUtils;
26  import org.apache.asn1.ber.primitives.UniversalTag;
27  
28  
29  /***
30   * A rule to Decode a BER encoded ASN.1 INTEGER into a Java primitive int.
31   * <p>
32   * The bytes to form the integer are extracted from the BER value which may
33   * arrive in multiple chunks.  The individual bytes are temporarily stored
34   * within a 4 byte array while incrementing a counter to track the capture.
35   * Once gathered the bytes are decoded into a int in the finish
36   * </p>
37   * <p>
38   * As a side effect once the decode is complete, the primitive value is pushed
39   * onto the primitive int stack to be utilized by other rules later.  If there
40   * is a loss of precision where the ASN.1 INTEGER is larger or smaller than
41   * the maximum or minimum value of a Java primitive integer an exception is
42   * thrown.
43   * </p>
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory
46   * Project</a>
47   * @version $Rev: 157644 $
48   */
49  public class PrimitiveBooleanRule extends AbstractRule
50  {
51      /*** the octet for the Java primitive boolean */
52      private byte value = 0 ;
53      /*** boolean flag to determine if we have read the single octet */
54      private boolean octetSet = false ;
55      /*** the tag this rule accepts */
56      private final TagEnum tag ;
57  
58  
59      // -----------------------------------------------------------------------
60      // C O N S T R U C T O R S
61      // -----------------------------------------------------------------------
62  
63  
64      /***
65       * Creates a default primitive boolean decoding rule that only accepts
66       * tags of UniversalTag.BOOLEAN.
67       */
68      public PrimitiveBooleanRule()
69      {
70          tag = UniversalTag.BOOLEAN ;
71      }
72  
73  
74      /***
75       * Creates a default primitive integer decoding rule that only accepts
76       * tags of UniversalTag.INTEGER.
77       */
78      public PrimitiveBooleanRule( TagEnum tag )
79      {
80          this.tag = tag ;
81      }
82  
83  
84      // -----------------------------------------------------------------------
85      // Rule Implementation
86      // -----------------------------------------------------------------------
87  
88  
89      /* (non-Javadoc)
90       * @see org.apache.asn1.ber.digester.Rule#tag(int, boolean,
91       * org.apache.asn1.ber.TypeClass)
92       */
93      public void tag( int id, boolean isPrimitive, TypeClass typeClass )
94      {
95          if ( id != tag.getTagId() )
96          {
97              throw new IllegalArgumentException(
98                      "Expecting " + tag.getName()
99                      + " with an id of " + tag.getTagId()
100                     + " but instead got a tag id of " + id ) ;
101         }
102     }
103 
104 
105     /* (non-Javadoc)
106      * @see org.apache.asn1.ber.digester.Rule#length(int)
107      */
108     public void length( int length )
109     {
110         if ( length != 1 )
111         {
112             throw new IllegalArgumentException( "The target primitive for this "
113                 + "rule only requires a single octet with a length of 1.  "
114                 + "The length of the field however is " + length ) ;
115         }
116     }
117 
118 
119     /* (non-Javadoc)
120      * @see org.apache.asn1.ber.digester.Rule#value(java.nio.ByteBuffer)
121      */
122     public void value( ByteBuffer buf )
123     {
124         if ( octetSet )
125         {
126             throw new IllegalArgumentException( "The target primitive for this "
127                 + "rule only requires a single octet with a length of 1.  "
128                 + "That octet has already been set." ) ;
129         }
130 
131         while ( buf.hasRemaining() )
132         {
133             value = buf.get() ;
134             octetSet = true ;
135         }
136     }
137 
138 
139     /* (non-Javadoc)
140      * @see org.apache.asn1.ber.digester.Rule#finish()
141      */
142     public void finish()
143     {
144         if ( getDigester() != null )
145         {
146             getDigester().pushBoolean(
147                     PrimitiveUtils.berDecodeBoolean( value ) ) ;
148         }
149 
150         // cleanup
151         this.value = 0 ;
152         this.octetSet = false ;
153     }
154 }