KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > security > asn1 > Primitive


1
2 package com.ca.commons.security.asn1;
3
4 import java.util.Hashtable JavaDoc;
5 import java.math.BigInteger JavaDoc;
6
7 /**
8  * This class represents all ASN.1 primitive types. It is only
9  * used in this package. For each ASN.1 primitive type, there is a
10  * corresponding java class representation. For example, the BOOLEAN
11  * ASN1Type is represented by java Boolean class. When creating a Primitive
12  * object, a real object from its implementation class is actually assigned
13  * to the Primitive object.
14  */

15 public class Primitive extends ASN1Object implements java.io.Serializable JavaDoc
16 {
17
18     private Class JavaDoc basicType;
19     private Object JavaDoc value;
20     
21     private static Hashtable JavaDoc checkup = new Hashtable JavaDoc(20);
22     
23     static{
24         register(ASN1Type.BOOLEAN, (new Boolean JavaDoc(true)).getClass());
25         register(ASN1Type.INTEGER, (new BigInteger JavaDoc("0")).getClass());
26         register(ASN1Type.OCTET_STRING, (new Object JavaDoc()).getClass());
27         register(ASN1Type.NULL, (new Object JavaDoc()).getClass());
28         register(ASN1Type.OBJECT_ID, (new String JavaDoc()).getClass());
29         register(ASN1Type.BIT_STRING, (new Object JavaDoc()).getClass());
30         register(ASN1Type.IA5String, (new String JavaDoc()).getClass());
31         register(ASN1Type.T61String, (new String JavaDoc()).getClass());
32         register(ASN1Type.PrintableString, (new String JavaDoc()).getClass());
33         register(ASN1Type.UTCTime, (new String JavaDoc()).getClass());
34         register(ASN1Type.GENERALIZEDTIME, (new String JavaDoc()).getClass());
35         
36         register(ASN1Type.UniversalString, (new Object JavaDoc()).getClass());
37         register(ASN1Type.BMPString, (new Object JavaDoc()).getClass());
38         register(ASN1Type.ENUMERATED, ( new BigInteger JavaDoc("0")).getClass());
39     }
40     
41     /**
42      * Registers the implementation class of the ASN1Type.
43      */

44     private static void register(ASN1Type type, Class JavaDoc r)
45     {
46         checkup.put(type, r);
47     }
48     
49     /**
50      * Constructor.
51      */

52     public Primitive()
53     {}
54     
55     /**
56      * Initializes the Primitive object.
57      */

58     public void init(ASN1Type type)
59     {
60         asn1Type = type;
61         byteArray = null;
62         basicType = (Class JavaDoc) checkup.get(type);
63         value = null;
64     }
65     
66     /**
67      * Gets the implementation class of the Primitive object.
68      */

69     public Class JavaDoc getType()
70     {
71         return basicType;
72     }
73     
74     /**
75      * Gets the value of the Primitive object.
76      */

77     public Object JavaDoc getValue()
78     {
79         return value;
80     }
81     
82     /**
83      * Sets the value of the Primitive object. If the input object is not
84      * from the implementation class of the Primitive object, an
85      * IllegalArgumentException is thrown.
86      */

87     public void setValue(Object JavaDoc v)
88     {
89         if (ofType(v, basicType))
90         {
91             value = v;
92         }
93         else
94         {
95             throw new IllegalArgumentException JavaDoc("Incompatible type");
96         }
97     }
98     
99     /**
100      * Returns whether two objects are the same.
101     public boolean equals(Object o) {
102         if (!(o instanceof iss.security.asn1.Primitive)) {
103             return false;
104         }
105         if (!this.asn1Type.equals(o.asn1Type)) {
106             return false;
107         }
108         if (!this.basicType.getName().equals(o.basicType.getName())) {
109             return false;
110         }
111     
112         if (this.value instanceof java.lang.Boolean
113         || this.value instanceof java.lang.String
114         || this.value instanceof java.math.BigInteger) {
115             if (this.value.equals(o.value) {
116                 return true;
117             }
118         } else if (this.value instanceof Object) {
119             byte [] b1 = null;
120             byte [] b2 = null;
121             try {
122                 b1 = (byte []) this.value;
123                 b2 = (byte []) o.value;
124             } catch(ClassCastException cce) {
125                 return false;
126             }
127             return Util.compByteArray(b1, b2);
128         }
129         return false;
130 }
131      */

132     
133     /**
134      * A string representation of the Primitive object.
135      */

136     public String JavaDoc toString()
137     {
138         String JavaDoc result = super.toString();
139         result += "\n\t" + basicType.toString();
140         result += "\tvalue " + value;
141         return result;
142     }
143     
144     /**
145      * Checks whether the object o is in the class t.
146      */

147     private boolean ofType(Object JavaDoc o, Class JavaDoc t)
148     {
149         if (t.getName().equals("java.lang.Boolean")
150                 && (o instanceof Boolean JavaDoc))
151         {
152             return true;
153         }
154         if (t.getName().equals("java.lang.String")
155                 && (o instanceof String JavaDoc))
156         {
157             return true;
158         }
159         if (t.getName().equals("java.math.BigInteger")
160                 && (o instanceof BigInteger JavaDoc))
161         {
162             return true;
163         }
164         if (t.getName().equals("java.lang.Object")
165                 && (o instanceof Object JavaDoc))
166         {
167             return true;
168         }
169         return false;
170     }
171 }
172
Popular Tags