1 2 package com.ca.commons.security.asn1; 3 4 import java.util.Hashtable ; 5 import java.math.BigInteger ; 6 7 15 public class Primitive extends ASN1Object implements java.io.Serializable  16 { 17 18 private Class basicType; 19 private Object value; 20 21 private static Hashtable checkup = new Hashtable (20); 22 23 static{ 24 register(ASN1Type.BOOLEAN, (new Boolean (true)).getClass()); 25 register(ASN1Type.INTEGER, (new BigInteger ("0")).getClass()); 26 register(ASN1Type.OCTET_STRING, (new Object ()).getClass()); 27 register(ASN1Type.NULL, (new Object ()).getClass()); 28 register(ASN1Type.OBJECT_ID, (new String ()).getClass()); 29 register(ASN1Type.BIT_STRING, (new Object ()).getClass()); 30 register(ASN1Type.IA5String, (new String ()).getClass()); 31 register(ASN1Type.T61String, (new String ()).getClass()); 32 register(ASN1Type.PrintableString, (new String ()).getClass()); 33 register(ASN1Type.UTCTime, (new String ()).getClass()); 34 register(ASN1Type.GENERALIZEDTIME, (new String ()).getClass()); 35 36 register(ASN1Type.UniversalString, (new Object ()).getClass()); 37 register(ASN1Type.BMPString, (new Object ()).getClass()); 38 register(ASN1Type.ENUMERATED, ( new BigInteger ("0")).getClass()); 39 } 40 41 44 private static void register(ASN1Type type, Class r) 45 { 46 checkup.put(type, r); 47 } 48 49 52 public Primitive() 53 {} 54 55 58 public void init(ASN1Type type) 59 { 60 asn1Type = type; 61 byteArray = null; 62 basicType = (Class ) checkup.get(type); 63 value = null; 64 } 65 66 69 public Class getType() 70 { 71 return basicType; 72 } 73 74 77 public Object getValue() 78 { 79 return value; 80 } 81 82 87 public void setValue(Object v) 88 { 89 if (ofType(v, basicType)) 90 { 91 value = v; 92 } 93 else 94 { 95 throw new IllegalArgumentException ("Incompatible type"); 96 } 97 } 98 99 132 133 136 public String toString() 137 { 138 String result = super.toString(); 139 result += "\n\t" + basicType.toString(); 140 result += "\tvalue " + value; 141 return result; 142 } 143 144 147 private boolean ofType(Object o, Class t) 148 { 149 if (t.getName().equals("java.lang.Boolean") 150 && (o instanceof Boolean )) 151 { 152 return true; 153 } 154 if (t.getName().equals("java.lang.String") 155 && (o instanceof String )) 156 { 157 return true; 158 } 159 if (t.getName().equals("java.math.BigInteger") 160 && (o instanceof BigInteger )) 161 { 162 return true; 163 } 164 if (t.getName().equals("java.lang.Object") 165 && (o instanceof Object )) 166 { 167 return true; 168 } 169 return false; 170 } 171 } 172 | Popular Tags |