AttributeTable.java revision 5db505e1f6a68c8d5dfdb0fed0b8607dea7bed96
1package org.bouncycastle.asn1.cms; 2 3import java.util.Enumeration; 4import java.util.Hashtable; 5import java.util.Vector; 6 7import org.bouncycastle.asn1.ASN1Encodable; 8import org.bouncycastle.asn1.ASN1EncodableVector; 9import org.bouncycastle.asn1.ASN1ObjectIdentifier; 10import org.bouncycastle.asn1.ASN1Set; 11import org.bouncycastle.asn1.DERObjectIdentifier; 12import org.bouncycastle.asn1.DERSet; 13 14/** 15 * This is helper tool to construct {@link Attributes} sets. 16 */ 17public class AttributeTable 18{ 19 private Hashtable attributes = new Hashtable(); 20 21 public AttributeTable( 22 Hashtable attrs) 23 { 24 attributes = copyTable(attrs); 25 } 26 27 public AttributeTable( 28 ASN1EncodableVector v) 29 { 30 for (int i = 0; i != v.size(); i++) 31 { 32 Attribute a = Attribute.getInstance(v.get(i)); 33 34 addAttribute(a.getAttrType(), a); 35 } 36 } 37 38 public AttributeTable( 39 ASN1Set s) 40 { 41 for (int i = 0; i != s.size(); i++) 42 { 43 Attribute a = Attribute.getInstance(s.getObjectAt(i)); 44 45 addAttribute(a.getAttrType(), a); 46 } 47 } 48 49 public AttributeTable( 50 Attribute attr) 51 { 52 addAttribute(attr.getAttrType(), attr); 53 } 54 55 public AttributeTable( 56 Attributes attrs) 57 { 58 this(ASN1Set.getInstance(attrs.toASN1Primitive())); 59 } 60 61 private void addAttribute( 62 ASN1ObjectIdentifier oid, 63 Attribute a) 64 { 65 Object value = attributes.get(oid); 66 67 if (value == null) 68 { 69 attributes.put(oid, a); 70 } 71 else 72 { 73 Vector v; 74 75 if (value instanceof Attribute) 76 { 77 v = new Vector(); 78 79 v.addElement(value); 80 v.addElement(a); 81 } 82 else 83 { 84 v = (Vector)value; 85 86 v.addElement(a); 87 } 88 89 attributes.put(oid, v); 90 } 91 } 92 93 /** 94 * @deprecated use ASN1ObjectIdentifier 95 */ 96 public Attribute get(DERObjectIdentifier oid) 97 { 98 return get(new ASN1ObjectIdentifier(oid.getId())); 99 } 100 101 /** 102 * Return the first attribute matching the OBJECT IDENTIFIER oid. 103 * 104 * @param oid type of attribute required. 105 * @return first attribute found of type oid. 106 */ 107 public Attribute get( 108 ASN1ObjectIdentifier oid) 109 { 110 Object value = attributes.get(oid); 111 112 if (value instanceof Vector) 113 { 114 return (Attribute)((Vector)value).elementAt(0); 115 } 116 117 return (Attribute)value; 118 } 119 120 /** 121 * @deprecated use ASN1ObjectIdentifier 122 */ 123 public ASN1EncodableVector getAll(DERObjectIdentifier oid) 124 { 125 return getAll(new ASN1ObjectIdentifier(oid.getId())); 126 } 127 128 /** 129 * Return all the attributes matching the OBJECT IDENTIFIER oid. The vector will be 130 * empty if there are no attributes of the required type present. 131 * 132 * @param oid type of attribute required. 133 * @return a vector of all the attributes found of type oid. 134 */ 135 public ASN1EncodableVector getAll( 136 ASN1ObjectIdentifier oid) 137 { 138 ASN1EncodableVector v = new ASN1EncodableVector(); 139 140 Object value = attributes.get(oid); 141 142 if (value instanceof Vector) 143 { 144 Enumeration e = ((Vector)value).elements(); 145 146 while (e.hasMoreElements()) 147 { 148 v.add((Attribute)e.nextElement()); 149 } 150 } 151 else if (value != null) 152 { 153 v.add((Attribute)value); 154 } 155 156 return v; 157 } 158 159 public int size() 160 { 161 int size = 0; 162 163 for (Enumeration en = attributes.elements(); en.hasMoreElements();) 164 { 165 Object o = en.nextElement(); 166 167 if (o instanceof Vector) 168 { 169 size += ((Vector)o).size(); 170 } 171 else 172 { 173 size++; 174 } 175 } 176 177 return size; 178 } 179 180 public Hashtable toHashtable() 181 { 182 return copyTable(attributes); 183 } 184 185 public ASN1EncodableVector toASN1EncodableVector() 186 { 187 ASN1EncodableVector v = new ASN1EncodableVector(); 188 Enumeration e = attributes.elements(); 189 190 while (e.hasMoreElements()) 191 { 192 Object value = e.nextElement(); 193 194 if (value instanceof Vector) 195 { 196 Enumeration en = ((Vector)value).elements(); 197 198 while (en.hasMoreElements()) 199 { 200 v.add(Attribute.getInstance(en.nextElement())); 201 } 202 } 203 else 204 { 205 v.add(Attribute.getInstance(value)); 206 } 207 } 208 209 return v; 210 } 211 212 public Attributes toASN1Structure() 213 { 214 return new Attributes(this.toASN1EncodableVector()); 215 } 216 217 private Hashtable copyTable( 218 Hashtable in) 219 { 220 Hashtable out = new Hashtable(); 221 Enumeration e = in.keys(); 222 223 while (e.hasMoreElements()) 224 { 225 Object key = e.nextElement(); 226 227 out.put(key, in.get(key)); 228 } 229 230 return out; 231 } 232 233 /** 234 * Return a new table with the passed in attribute added. 235 * 236 * @param attrType 237 * @param attrValue 238 * @return 239 */ 240 public AttributeTable add(ASN1ObjectIdentifier attrType, ASN1Encodable attrValue) 241 { 242 AttributeTable newTable = new AttributeTable(attributes); 243 244 newTable.addAttribute(attrType, new Attribute(attrType, new DERSet(attrValue))); 245 246 return newTable; 247 } 248 249 public AttributeTable remove(ASN1ObjectIdentifier attrType) 250 { 251 AttributeTable newTable = new AttributeTable(attributes); 252 253 newTable.attributes.remove(attrType); 254 255 return newTable; 256 } 257} 258