KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > ASN1TaggedObject


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.IOException JavaDoc;
21
22 /**
23  * ASN.1 TaggedObject - in ASN.1 nottation this is any object proceeded by
24  * a [n] where n is some number - these are assume to follow the construction
25  * rules (as with sequences).
26  */

27 public abstract class ASN1TaggedObject
28     extends DERObject
29 {
30     int tagNo;
31     boolean empty = false;
32     boolean explicit = true;
33     DEREncodable obj = null;
34
35     static public ASN1TaggedObject getInstance(
36         ASN1TaggedObject obj,
37         boolean explicit)
38     {
39         if (explicit)
40         {
41             return (ASN1TaggedObject)obj.getObject();
42         }
43
44         throw new IllegalArgumentException JavaDoc("implicitly tagged tagged object");
45     }
46
47     /**
48      * Create a tagged object in the explicit style.
49      *
50      * @param tagNo the tag number for this object.
51      * @param obj the tagged object.
52      */

53     public ASN1TaggedObject(
54         int tagNo,
55         DEREncodable obj)
56     {
57         this.explicit = true;
58         this.tagNo = tagNo;
59         this.obj = obj;
60     }
61
62     /**
63      * Create a tagged object with the style given by the value of explicit.
64      * <p>
65      * If the object implements ASN1Choice the tag style will always be changed
66      * to explicit in accordance with the ASN.1 encoding rules.
67      * </p>
68      * @param explicit true if the object is explicitly tagged.
69      * @param tagNo the tag number for this object.
70      * @param obj the tagged object.
71      */

72     public ASN1TaggedObject(
73         boolean explicit,
74         int tagNo,
75         DEREncodable obj)
76     {
77         if (obj instanceof ASN1Choice)
78         {
79             this.explicit = true;
80         }
81         else
82         {
83             this.explicit = explicit;
84         }
85
86         this.tagNo = tagNo;
87         this.obj = obj;
88     }
89
90     public boolean equals(
91         Object JavaDoc o)
92     {
93         if (o == null || !(o instanceof ASN1TaggedObject))
94         {
95             return false;
96         }
97
98         ASN1TaggedObject other = (ASN1TaggedObject)o;
99
100         if (tagNo != other.tagNo || empty != other.empty || explicit != other.explicit)
101         {
102             return false;
103         }
104
105         if(obj == null)
106         {
107             if(other.obj != null)
108             {
109                 return false;
110             }
111         }
112         else
113         {
114             if(!(obj.equals(other.obj)))
115             {
116                 return false;
117             }
118         }
119
120         return true;
121     }
122
123     public int hashCode()
124     {
125         int code = tagNo;
126
127         if (obj != null)
128         {
129             code ^= obj.hashCode();
130         }
131
132         return code;
133     }
134
135     public int getTagNo()
136     {
137         return tagNo;
138     }
139
140     /**
141      * return whether or not the object may be explicitly tagged.
142      * <p>
143      * Note: if the object has been read from an input stream, the only
144      * time you can be sure if isExplicit is returning the true state of
145      * affairs is if it returns false. An implicitly tagged object may appear
146      * to be explicitly tagged, so you need to understand the context under
147      * which the reading was done as well, see getObject below.
148      */

149     public boolean isExplicit()
150     {
151         return explicit;
152     }
153
154     public boolean isEmpty()
155     {
156         return empty;
157     }
158
159     /**
160      * return whatever was following the tag.
161      * <p>
162      * Note: tagged objects are generally context dependent if you're
163      * trying to extract a tagged object you should be going via the
164      * appropriate getInstance method.
165      */

166     public DERObject getObject()
167     {
168         if (obj != null)
169         {
170             return obj.getDERObject();
171         }
172
173         return null;
174     }
175
176     abstract void encode(DEROutputStream out)
177         throws IOException JavaDoc;
178 }
179
Popular Tags