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