001: package org.bouncycastle.asn1.cms;
002:
003: import java.util.Enumeration;
004: import java.util.Hashtable;
005: import java.util.Vector;
006:
007: import org.bouncycastle.asn1.ASN1EncodableVector;
008: import org.bouncycastle.asn1.ASN1Set;
009: import org.bouncycastle.asn1.DEREncodableVector;
010: import org.bouncycastle.asn1.DERObjectIdentifier;
011:
012: public class AttributeTable {
013: private Hashtable attributes = new Hashtable();
014:
015: public AttributeTable(Hashtable attrs) {
016: attributes = copyTable(attrs);
017: }
018:
019: public AttributeTable(DEREncodableVector v) {
020: for (int i = 0; i != v.size(); i++) {
021: Attribute a = Attribute.getInstance(v.get(i));
022:
023: addAttribute(a.getAttrType(), a);
024: }
025: }
026:
027: public AttributeTable(ASN1Set s) {
028: for (int i = 0; i != s.size(); i++) {
029: Attribute a = Attribute.getInstance(s.getObjectAt(i));
030:
031: addAttribute(a.getAttrType(), a);
032: }
033: }
034:
035: private void addAttribute(DERObjectIdentifier oid, Attribute a) {
036: Object value = attributes.get(oid);
037:
038: if (value == null) {
039: attributes.put(oid, a);
040: } else {
041: Vector v;
042:
043: if (value instanceof Attribute) {
044: v = new Vector();
045:
046: v.addElement(value);
047: v.addElement(a);
048: } else {
049: v = (Vector) value;
050:
051: v.addElement(a);
052: }
053:
054: attributes.put(oid, v);
055: }
056: }
057:
058: /**
059: * Return the first attribute matching the OBJECT IDENTIFIER oid.
060: *
061: * @param oid type of attribute required.
062: * @return first attribute found of type oid.
063: */
064: public Attribute get(DERObjectIdentifier oid) {
065: Object value = attributes.get(oid);
066:
067: if (value instanceof Vector) {
068: return (Attribute) ((Vector) value).elementAt(0);
069: }
070:
071: return (Attribute) value;
072: }
073:
074: /**
075: * Return all the attributes matching the OBJECT IDENTIFIER oid. The vector will be
076: * empty if there are no attributes of the required type present.
077: *
078: * @param oid type of attribute required.
079: * @return a vector of all the attributes found of type oid.
080: */
081: public ASN1EncodableVector getAll(DERObjectIdentifier oid) {
082: ASN1EncodableVector v = new ASN1EncodableVector();
083:
084: Object value = attributes.get(oid);
085:
086: if (value instanceof Vector) {
087: Enumeration e = ((Vector) value).elements();
088:
089: while (e.hasMoreElements()) {
090: v.add((Attribute) e.nextElement());
091: }
092: } else if (value != null) {
093: v.add((Attribute) value);
094: }
095:
096: return v;
097: }
098:
099: public Hashtable toHashtable() {
100: return copyTable(attributes);
101: }
102:
103: public ASN1EncodableVector toASN1EncodableVector() {
104: ASN1EncodableVector v = new ASN1EncodableVector();
105: Enumeration e = attributes.elements();
106:
107: while (e.hasMoreElements()) {
108: Object value = e.nextElement();
109:
110: if (value instanceof Vector) {
111: Enumeration en = ((Vector) value).elements();
112:
113: while (en.hasMoreElements()) {
114: v.add(Attribute.getInstance(en.nextElement()));
115: }
116: } else {
117: v.add(Attribute.getInstance(value));
118: }
119: }
120:
121: return v;
122: }
123:
124: private Hashtable copyTable(Hashtable in) {
125: Hashtable out = new Hashtable();
126: Enumeration e = in.keys();
127:
128: while (e.hasMoreElements()) {
129: Object key = e.nextElement();
130:
131: out.put(key, in.get(key));
132: }
133:
134: return out;
135: }
136: }
|