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