001package org.apache.commons.ssl.org.bouncycastle.asn1.pkcs;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Set;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
011
012public class Attribute
013    extends ASN1Object
014{
015    private ASN1ObjectIdentifier attrType;
016    private ASN1Set              attrValues;
017
018    /**
019     * return an Attribute object from the given object.
020     *
021     * @param o the object we want converted.
022     * @exception IllegalArgumentException if the object cannot be converted.
023     */
024    public static Attribute getInstance(
025        Object o)
026    {
027        if (o == null || o instanceof Attribute)
028        {
029            return (Attribute)o;
030        }
031        
032        if (o instanceof ASN1Sequence)
033        {
034            return new Attribute((ASN1Sequence)o);
035        }
036
037        throw new IllegalArgumentException("unknown object in factory: " + o.getClass().getName());
038    }
039    
040    public Attribute(
041        ASN1Sequence seq)
042    {
043        attrType = (ASN1ObjectIdentifier)seq.getObjectAt(0);
044        attrValues = (ASN1Set)seq.getObjectAt(1);
045    }
046
047    public Attribute(
048        ASN1ObjectIdentifier attrType,
049        ASN1Set             attrValues)
050    {
051        this.attrType = attrType;
052        this.attrValues = attrValues;
053    }
054
055    public ASN1ObjectIdentifier getAttrType()
056    {
057        return attrType;
058    }
059    
060    public ASN1Set getAttrValues()
061    {
062        return attrValues;
063    }
064
065    public ASN1Encodable[] getAttributeValues()
066    {
067        return attrValues.toArray();
068    }
069
070    /** 
071     * Produce an object suitable for an ASN1OutputStream.
072     * <pre>
073     * Attribute ::= SEQUENCE {
074     *     attrType OBJECT IDENTIFIER,
075     *     attrValues SET OF AttributeValue
076     * }
077     * </pre>
078     */
079    public ASN1Primitive toASN1Primitive()
080    {
081        ASN1EncodableVector v = new ASN1EncodableVector();
082
083        v.add(attrType);
084        v.add(attrValues);
085
086        return new DERSequence(v);
087    }
088}