001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.asn1.x509;
020    
021    import java.math.BigInteger;
022    
023    import org.apache.geronimo.util.asn1.ASN1Encodable;
024    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
025    import org.apache.geronimo.util.asn1.ASN1Sequence;
026    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
027    import org.apache.geronimo.util.asn1.DERInteger;
028    import org.apache.geronimo.util.asn1.DERObject;
029    import org.apache.geronimo.util.asn1.DERSequence;
030    import org.apache.geronimo.util.asn1.DERTaggedObject;
031    
032    public class GeneralSubtree
033        extends ASN1Encodable
034    {
035        private GeneralName  base;
036        private DERInteger minimum;
037        private DERInteger maximum;
038    
039        public GeneralSubtree(
040            ASN1Sequence seq)
041        {
042            base = GeneralName.getInstance(seq.getObjectAt(0));
043    
044            switch (seq.size())
045            {
046            case 1:
047                break;
048            case 2:
049                ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(1);
050                switch (o.getTagNo())
051                {
052                case 0 :
053                    minimum = DERInteger.getInstance(o, false);
054                    break;
055                case 1 :
056                    maximum = DERInteger.getInstance(o, false);
057                    break;
058                default:
059                    throw new IllegalArgumentException("Bad tag number: " + o.getTagNo());
060                }
061                break;
062            case 3 :
063                minimum = DERInteger.getInstance((ASN1TaggedObject)seq.getObjectAt(1), false);
064                maximum = DERInteger.getInstance((ASN1TaggedObject)seq.getObjectAt(2), false);
065                break;
066            default:
067                throw new IllegalArgumentException("Bad sequence size: " + seq.size());
068            }
069        }
070    
071        public static GeneralSubtree getInstance(
072            ASN1TaggedObject    o,
073            boolean             explicit)
074        {
075            return new GeneralSubtree(ASN1Sequence.getInstance(o, explicit));
076        }
077    
078        public static GeneralSubtree getInstance(
079            Object obj)
080        {
081            if (obj == null)
082            {
083                return null;
084            }
085    
086            if (obj instanceof GeneralSubtree)
087            {
088                return (GeneralSubtree)obj;
089            }
090    
091            return new GeneralSubtree(ASN1Sequence.getInstance(obj));
092        }
093    
094        public GeneralName getBase()
095        {
096            return base;
097        }
098    
099        public BigInteger getMinimum()
100        {
101            if (minimum == null)
102            {
103                return BigInteger.valueOf(0);
104            }
105    
106            return minimum.getValue();
107        }
108    
109        public BigInteger getMaximum()
110        {
111            if (maximum == null)
112            {
113                return null;
114            }
115    
116            return maximum.getValue();
117        }
118    
119        /*
120         * GeneralSubtree ::= SEQUENCE {
121         *      base                    GeneralName,
122         *      minimum         [0]     BaseDistance DEFAULT 0,
123         *      maximum         [1]     BaseDistance OPTIONAL }
124         */
125        public DERObject toASN1Object()
126        {
127            ASN1EncodableVector v = new ASN1EncodableVector();
128    
129            v.add(base);
130    
131            if (minimum != null)
132            {
133                v.add(new DERTaggedObject(false, 0, minimum));
134            }
135    
136            if (maximum != null)
137            {
138                v.add(new DERTaggedObject(false, 1, maximum));
139            }
140    
141            return new DERSequence(v);
142        }
143    }