001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.crypto.asn1.x509;
019    
020    import org.apache.geronimo.crypto.asn1.ASN1Encodable;
021    import org.apache.geronimo.crypto.asn1.ASN1EncodableVector;
022    import org.apache.geronimo.crypto.asn1.ASN1Sequence;
023    import org.apache.geronimo.crypto.asn1.ASN1Set;
024    import org.apache.geronimo.crypto.asn1.DERObject;
025    import org.apache.geronimo.crypto.asn1.DERObjectIdentifier;
026    import org.apache.geronimo.crypto.asn1.DERSequence;
027    
028    public class Attribute
029        extends ASN1Encodable
030    {
031        private DERObjectIdentifier attrType;
032        private ASN1Set             attrValues;
033    
034        /**
035         * return an Attribute object from the given object.
036         *
037         * @param o the object we want converted.
038         * @exception IllegalArgumentException if the object cannot be converted.
039         */
040        public static Attribute getInstance(
041            Object o)
042        {
043            if (o == null || o instanceof Attribute)
044            {
045                return (Attribute)o;
046            }
047    
048            if (o instanceof ASN1Sequence)
049            {
050                return new Attribute((ASN1Sequence)o);
051            }
052    
053            throw new IllegalArgumentException("unknown object in factory");
054        }
055    
056        public Attribute(
057            ASN1Sequence seq)
058        {
059            attrType = (DERObjectIdentifier)seq.getObjectAt(0);
060            attrValues = (ASN1Set)seq.getObjectAt(1);
061        }
062    
063        public Attribute(
064            DERObjectIdentifier attrType,
065            ASN1Set             attrValues)
066        {
067            this.attrType = attrType;
068            this.attrValues = attrValues;
069        }
070    
071        public DERObjectIdentifier getAttrType()
072        {
073            return attrType;
074        }
075    
076        public ASN1Set getAttrValues()
077        {
078            return attrValues;
079        }
080    
081        /**
082         * Produce an object suitable for an ASN1OutputStream.
083         * <pre>
084         * Attribute ::= SEQUENCE {
085         *     attrType OBJECT IDENTIFIER,
086         *     attrValues SET OF AttributeValue
087         * }
088         * </pre>
089         */
090        public DERObject toASN1Object()
091        {
092            ASN1EncodableVector v = new ASN1EncodableVector();
093    
094            v.add(attrType);
095            v.add(attrValues);
096    
097            return new DERSequence(v);
098        }
099    }