1package org.bouncycastle.asn1.cms;
2
3import org.bouncycastle.asn1.ASN1Encodable;
4import org.bouncycastle.asn1.ASN1EncodableVector;
5import org.bouncycastle.asn1.ASN1Object;
6import org.bouncycastle.asn1.ASN1ObjectIdentifier;
7import org.bouncycastle.asn1.ASN1Primitive;
8import org.bouncycastle.asn1.ASN1Sequence;
9import org.bouncycastle.asn1.ASN1Set;
10import org.bouncycastle.asn1.DERObjectIdentifier;
11import org.bouncycastle.asn1.DERSequence;
12
13public class Attribute
14    extends ASN1Object
15{
16    private ASN1ObjectIdentifier attrType;
17    private ASN1Set             attrValues;
18
19    /**
20     * return an Attribute object from the given object.
21     *
22     * @param o the object we want converted.
23     * @exception IllegalArgumentException if the object cannot be converted.
24     */
25    public static Attribute getInstance(
26        Object o)
27    {
28        if (o instanceof Attribute)
29        {
30            return (Attribute)o;
31        }
32
33        if (o != null)
34        {
35            return new Attribute(ASN1Sequence.getInstance(o));
36        }
37
38        return null;
39    }
40
41    private Attribute(
42        ASN1Sequence seq)
43    {
44        attrType = (ASN1ObjectIdentifier)seq.getObjectAt(0);
45        attrValues = (ASN1Set)seq.getObjectAt(1);
46    }
47
48    /**
49     * @deprecated use ASN1ObjectIdentifier
50     */
51    public Attribute(
52        DERObjectIdentifier attrType,
53        ASN1Set             attrValues)
54    {
55        this.attrType = new ASN1ObjectIdentifier(attrType.getId());
56        this.attrValues = attrValues;
57    }
58
59    public Attribute(
60        ASN1ObjectIdentifier attrType,
61        ASN1Set             attrValues)
62    {
63        this.attrType = attrType;
64        this.attrValues = attrValues;
65    }
66
67    public ASN1ObjectIdentifier getAttrType()
68    {
69        return attrType;
70    }
71
72    public ASN1Set getAttrValues()
73    {
74        return attrValues;
75    }
76
77    public ASN1Encodable[] getAttributeValues()
78    {
79        return attrValues.toArray();
80    }
81
82    /**
83     * Produce an object suitable for an ASN1OutputStream.
84     * <pre>
85     * Attribute ::= SEQUENCE {
86     *     attrType OBJECT IDENTIFIER,
87     *     attrValues SET OF AttributeValue
88     * }
89     * </pre>
90     */
91    public ASN1Primitive toASN1Primitive()
92    {
93        ASN1EncodableVector v = new ASN1EncodableVector();
94
95        v.add(attrType);
96        v.add(attrValues);
97
98        return new DERSequence(v);
99    }
100}
101