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 ;
18  
19  
20  /***
21   * Type safe enum for an ASN.1 type class.  This can be take one of the 
22   * following four values: 
23   * <ul>
24   * <li>UNIVERSAL</li>
25   * <li>APPLICATION</li>
26   * <li>CONTEXT_SPECIFIC</li>
27   * <li>PRIVATE</li>
28   * </ul>
29   * 
30   * @author <a href="mailto:dev@directory.apache.org">
31   * Apache Directory Project</a>
32   * @version $Rev: 289141 $
33   */
34  public class TypeClass
35  {
36      /*** value for the universal type class */
37      public static final int UNIVERSAL_VAL = 0 ;
38      /*** value for the application type class */
39      public static final int APPLICATION_VAL = 0x40 ;
40      /*** value for the context specific type class */
41      public static final int CONTEXT_SPECIFIC_VAL = 0x80 ;
42      /*** value for the private type class */
43      public static final int PRIVATE_VAL = 0xc0 ;
44  
45      /*** enum for the universal type class */
46      public static final TypeClass UNIVERSAL = 
47          new TypeClass( "UNIVERSAL", UNIVERSAL_VAL ) ;
48      /*** enum for the application type class */
49      public static final TypeClass APPLICATION = 
50          new TypeClass( "APPLICATION", APPLICATION_VAL ) ;
51      /*** enum for the context specific type class  */
52      public static final TypeClass CONTEXT_SPECIFIC = 
53          new TypeClass( "CONTEXT_SPECIFIC", CONTEXT_SPECIFIC_VAL ) ;
54      /*** enum for the private type class  */
55      public static final TypeClass PRIVATE = 
56          new TypeClass( "PRIVATE", PRIVATE_VAL ) ;
57  
58      /*** the name of this enumeration element */
59      private final String name;
60      /*** the value of this enumeration element */
61      private final int value;
62  
63  
64      /***
65       * Private constructor so no other instances can be created other than the
66       * public static constants in this class.
67       *
68       * @param name a string name for the enumeration value.
69       * @param value the integer value of the enumeration.
70       */
71      private TypeClass( final String name, final int value )
72      {
73          this.name = name;
74          this.value = value;
75      }
76  
77  
78      /***
79       * Get's the name of this enumeration element.
80       *
81       * @return the name of the enumeration element
82       */
83      public final String getName()
84      {
85          return this.name;
86      }
87  
88  
89      /***
90       * Get's the value of this enumeration element.
91       *
92       * @return the value of the enumeration element
93       */
94      public final int getValue()
95      {
96          return this.value;
97      }
98  
99  
100     /***
101      * Gets the enumeration type for the type class regardless of case.
102      * 
103      * @param className the type class name
104      * @return the TypeClass for the name
105      */
106     public static TypeClass getTypeClass( String className )
107     {
108         // check first using == since it will be the predominate use case
109         if ( className == APPLICATION.getName() )
110         {
111             return APPLICATION ;
112         }
113         else if ( className == CONTEXT_SPECIFIC.getName() )
114         {
115             return CONTEXT_SPECIFIC ;
116         }
117         else if ( className == PRIVATE.getName() )
118         {
119             return PRIVATE ;
120         }
121         else if ( className == UNIVERSAL.getName() )
122         {
123             return UNIVERSAL ;
124         }
125         
126         if ( className.equalsIgnoreCase( TypeClass.PRIVATE.getName() ) )
127         {
128             return TypeClass.PRIVATE ;
129         }
130         
131         if ( className.equalsIgnoreCase( TypeClass.UNIVERSAL.getName() ) )
132         {
133             return TypeClass.UNIVERSAL ;
134         }
135         
136         if ( className.equalsIgnoreCase( TypeClass.APPLICATION.getName() ) )
137         {
138             return TypeClass.APPLICATION ;
139         }
140         
141         if ( className.equalsIgnoreCase( 
142                         TypeClass.CONTEXT_SPECIFIC.getName() ) )
143         {
144             return TypeClass.CONTEXT_SPECIFIC ;
145         }
146         
147         throw new IllegalArgumentException( "Unknown type class name"
148             + className ) ;
149     }
150     
151     
152     /***
153      * Gets the ASN.1 type's class using a TLV tag.
154      * 
155      * @param octet the first octet of the TLV
156      * @return the TypeClass enumeration for the ASN.1 type's class
157      */
158     public static TypeClass getTypeClass( int octet )
159     {
160         TypeClass tc = null ;
161         int l_value = octet & PRIVATE_VAL ;
162         
163         switch ( l_value )
164         {
165             case( UNIVERSAL_VAL ):
166                 tc = UNIVERSAL ;
167                 break ;
168             case( APPLICATION_VAL ):
169                 tc = APPLICATION ;
170                 break ;
171             case( CONTEXT_SPECIFIC_VAL ):
172                 tc = CONTEXT_SPECIFIC ;
173                 break ;
174             case( PRIVATE_VAL ):
175                 tc = PRIVATE ;
176                 break ;
177         }
178         
179         return tc ;
180     }
181 }