001package org.apache.commons.ssl.org.bouncycastle.asn1.crmf;
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.DERSequence;
010
011public class AttributeTypeAndValue
012    extends ASN1Object
013{
014    private ASN1ObjectIdentifier type;
015    private ASN1Encodable       value;
016
017    private AttributeTypeAndValue(ASN1Sequence seq)
018    {
019        type = (ASN1ObjectIdentifier)seq.getObjectAt(0);
020        value = (ASN1Encodable)seq.getObjectAt(1);
021    }
022
023    public static AttributeTypeAndValue getInstance(Object o)
024    {
025        if (o instanceof AttributeTypeAndValue)
026        {
027            return (AttributeTypeAndValue)o;
028        }
029
030        if (o != null)
031        {
032            return new AttributeTypeAndValue(ASN1Sequence.getInstance(o));
033        }
034
035        return null;
036    }
037
038    public AttributeTypeAndValue(
039        String oid,
040        ASN1Encodable value)
041    {
042        this(new ASN1ObjectIdentifier(oid), value);
043    }
044
045    public AttributeTypeAndValue(
046        ASN1ObjectIdentifier type,
047        ASN1Encodable value)
048    {
049        this.type = type;
050        this.value = value;
051    }
052
053    public ASN1ObjectIdentifier getType()
054    {
055        return type;
056    }
057
058    public ASN1Encodable getValue()
059    {
060        return value;
061    }
062
063    /**
064     * <pre>
065     * AttributeTypeAndValue ::= SEQUENCE {
066     *           type         OBJECT IDENTIFIER,
067     *           value        ANY DEFINED BY type }
068     * </pre>
069     * @return a basic ASN.1 object representation.
070     */
071    public ASN1Primitive toASN1Primitive()
072    {
073        ASN1EncodableVector v = new ASN1EncodableVector();
074
075        v.add(type);
076        v.add(value);
077
078        return new DERSequence(v);
079    }
080}