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.util.asn1.x509;
019    
020    import java.math.BigInteger;
021    
022    import org.apache.geronimo.util.asn1.ASN1Encodable;
023    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
024    import org.apache.geronimo.util.asn1.ASN1Sequence;
025    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
026    import org.apache.geronimo.util.asn1.DERInteger;
027    import org.apache.geronimo.util.asn1.DERObject;
028    import org.apache.geronimo.util.asn1.DERSequence;
029    import org.apache.geronimo.util.asn1.DERTaggedObject;
030    
031    public class GeneralSubtree
032        extends ASN1Encodable
033    {
034        private GeneralName  base;
035        private DERInteger minimum;
036        private DERInteger maximum;
037    
038        public GeneralSubtree(
039            ASN1Sequence seq)
040        {
041            base = GeneralName.getInstance(seq.getObjectAt(0));
042    
043            switch (seq.size())
044            {
045            case 1:
046                break;
047            case 2:
048                ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(1);
049                switch (o.getTagNo())
050                {
051                case 0 :
052                    minimum = DERInteger.getInstance(o, false);
053                    break;
054                case 1 :
055                    maximum = DERInteger.getInstance(o, false);
056                    break;
057                default:
058                    throw new IllegalArgumentException("Bad tag number: " + o.getTagNo());
059                }
060                break;
061            case 3 :
062                minimum = DERInteger.getInstance((ASN1TaggedObject)seq.getObjectAt(1), false);
063                maximum = DERInteger.getInstance((ASN1TaggedObject)seq.getObjectAt(2), false);
064                break;
065            default:
066                throw new IllegalArgumentException("Bad sequence size: " + seq.size());
067            }
068        }
069    
070        public static GeneralSubtree getInstance(
071            ASN1TaggedObject    o,
072            boolean             explicit)
073        {
074            return new GeneralSubtree(ASN1Sequence.getInstance(o, explicit));
075        }
076    
077        public static GeneralSubtree getInstance(
078            Object obj)
079        {
080            if (obj == null)
081            {
082                return null;
083            }
084    
085            if (obj instanceof GeneralSubtree)
086            {
087                return (GeneralSubtree)obj;
088            }
089    
090            return new GeneralSubtree(ASN1Sequence.getInstance(obj));
091        }
092    
093        public GeneralName getBase()
094        {
095            return base;
096        }
097    
098        public BigInteger getMinimum()
099        {
100            if (minimum == null)
101            {
102                return BigInteger.valueOf(0);
103            }
104    
105            return minimum.getValue();
106        }
107    
108        public BigInteger getMaximum()
109        {
110            if (maximum == null)
111            {
112                return null;
113            }
114    
115            return maximum.getValue();
116        }
117    
118        /*
119         * GeneralSubtree ::= SEQUENCE {
120         *      base                    GeneralName,
121         *      minimum         [0]     BaseDistance DEFAULT 0,
122         *      maximum         [1]     BaseDistance OPTIONAL }
123         */
124        public DERObject toASN1Object()
125        {
126            ASN1EncodableVector v = new ASN1EncodableVector();
127    
128            v.add(base);
129    
130            if (minimum != null)
131            {
132                v.add(new DERTaggedObject(false, 0, minimum));
133            }
134    
135            if (maximum != null)
136            {
137                v.add(new DERTaggedObject(false, 1, maximum));
138            }
139    
140            return new DERSequence(v);
141        }
142    }