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