001package org.apache.commons.ssl.org.bouncycastle.asn1.cms; 002 003import java.util.Enumeration; 004import java.util.Hashtable; 005import java.util.Vector; 006 007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable; 008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector; 009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier; 010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Set; 011import org.apache.commons.ssl.org.bouncycastle.asn1.DERSet; 012 013/** 014 * This is helper tool to construct {@link Attributes} sets. 015 */ 016public class AttributeTable 017{ 018 private Hashtable attributes = new Hashtable(); 019 020 public AttributeTable( 021 Hashtable attrs) 022 { 023 attributes = copyTable(attrs); 024 } 025 026 public AttributeTable( 027 ASN1EncodableVector v) 028 { 029 for (int i = 0; i != v.size(); i++) 030 { 031 Attribute a = Attribute.getInstance(v.get(i)); 032 033 addAttribute(a.getAttrType(), a); 034 } 035 } 036 037 public AttributeTable( 038 ASN1Set s) 039 { 040 for (int i = 0; i != s.size(); i++) 041 { 042 Attribute a = Attribute.getInstance(s.getObjectAt(i)); 043 044 addAttribute(a.getAttrType(), a); 045 } 046 } 047 048 public AttributeTable( 049 Attribute attr) 050 { 051 addAttribute(attr.getAttrType(), attr); 052 } 053 054 public AttributeTable( 055 Attributes attrs) 056 { 057 this(ASN1Set.getInstance(attrs.toASN1Primitive())); 058 } 059 060 private void addAttribute( 061 ASN1ObjectIdentifier oid, 062 Attribute a) 063 { 064 Object value = attributes.get(oid); 065 066 if (value == null) 067 { 068 attributes.put(oid, a); 069 } 070 else 071 { 072 Vector v; 073 074 if (value instanceof Attribute) 075 { 076 v = new Vector(); 077 078 v.addElement(value); 079 v.addElement(a); 080 } 081 else 082 { 083 v = (Vector)value; 084 085 v.addElement(a); 086 } 087 088 attributes.put(oid, v); 089 } 090 } 091 092 /** 093 * Return the first attribute matching the OBJECT IDENTIFIER oid. 094 * 095 * @param oid type of attribute required. 096 * @return first attribute found of type oid. 097 */ 098 public Attribute get( 099 ASN1ObjectIdentifier oid) 100 { 101 Object value = attributes.get(oid); 102 103 if (value instanceof Vector) 104 { 105 return (Attribute)((Vector)value).elementAt(0); 106 } 107 108 return (Attribute)value; 109 } 110 111 /** 112 * Return all the attributes matching the OBJECT IDENTIFIER oid. The vector will be 113 * empty if there are no attributes of the required type present. 114 * 115 * @param oid type of attribute required. 116 * @return a vector of all the attributes found of type oid. 117 */ 118 public ASN1EncodableVector getAll( 119 ASN1ObjectIdentifier oid) 120 { 121 ASN1EncodableVector v = new ASN1EncodableVector(); 122 123 Object value = attributes.get(oid); 124 125 if (value instanceof Vector) 126 { 127 Enumeration e = ((Vector)value).elements(); 128 129 while (e.hasMoreElements()) 130 { 131 v.add((Attribute)e.nextElement()); 132 } 133 } 134 else if (value != null) 135 { 136 v.add((Attribute)value); 137 } 138 139 return v; 140 } 141 142 public int size() 143 { 144 int size = 0; 145 146 for (Enumeration en = attributes.elements(); en.hasMoreElements();) 147 { 148 Object o = en.nextElement(); 149 150 if (o instanceof Vector) 151 { 152 size += ((Vector)o).size(); 153 } 154 else 155 { 156 size++; 157 } 158 } 159 160 return size; 161 } 162 163 public Hashtable toHashtable() 164 { 165 return copyTable(attributes); 166 } 167 168 public ASN1EncodableVector toASN1EncodableVector() 169 { 170 ASN1EncodableVector v = new ASN1EncodableVector(); 171 Enumeration e = attributes.elements(); 172 173 while (e.hasMoreElements()) 174 { 175 Object value = e.nextElement(); 176 177 if (value instanceof Vector) 178 { 179 Enumeration en = ((Vector)value).elements(); 180 181 while (en.hasMoreElements()) 182 { 183 v.add(Attribute.getInstance(en.nextElement())); 184 } 185 } 186 else 187 { 188 v.add(Attribute.getInstance(value)); 189 } 190 } 191 192 return v; 193 } 194 195 public Attributes toASN1Structure() 196 { 197 return new Attributes(this.toASN1EncodableVector()); 198 } 199 200 private Hashtable copyTable( 201 Hashtable in) 202 { 203 Hashtable out = new Hashtable(); 204 Enumeration e = in.keys(); 205 206 while (e.hasMoreElements()) 207 { 208 Object key = e.nextElement(); 209 210 out.put(key, in.get(key)); 211 } 212 213 return out; 214 } 215 216 /** 217 * Return a new table with the passed in attribute added. 218 * 219 * @param attrType 220 * @param attrValue 221 * @return 222 */ 223 public AttributeTable add(ASN1ObjectIdentifier attrType, ASN1Encodable attrValue) 224 { 225 AttributeTable newTable = new AttributeTable(attributes); 226 227 newTable.addAttribute(attrType, new Attribute(attrType, new DERSet(attrValue))); 228 229 return newTable; 230 } 231 232 public AttributeTable remove(ASN1ObjectIdentifier attrType) 233 { 234 AttributeTable newTable = new AttributeTable(attributes); 235 236 newTable.attributes.remove(attrType); 237 238 return newTable; 239 } 240}