KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > ASN1TaggedObject


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

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

29 public abstract class ASN1TaggedObject
30     extends DERObject
31 {
32     int tagNo;
33     boolean empty = false;
34     boolean explicit = true;
35     DEREncodable obj = null;
36
37     static public ASN1TaggedObject getInstance(
38         ASN1TaggedObject obj,
39         boolean explicit)
40     {
41         if (explicit)
42         {
43             return (ASN1TaggedObject)obj.getObject();
44         }
45
46         throw new IllegalArgumentException JavaDoc("implicitly tagged tagged object");
47     }
48
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      * @param explicit true if the object is explicitly tagged.
64      * @param tagNo the tag number for this object.
65      * @param obj the tagged object.
66      */

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

136     public boolean isExplicit()
137     {
138         return explicit;
139     }
140
141     public boolean isEmpty()
142     {
143         return empty;
144     }
145
146     /**
147      * return whatever was following the tag.
148      * <p>
149      * Note: tagged objects are generally context dependent if you're
150      * trying to extract a tagged object you should be going via the
151      * appropriate getInstance method.
152      */

153     public DERObject getObject()
154     {
155         if (obj != null)
156         {
157             return obj.getDERObject();
158         }
159
160         return null;
161     }
162
163     abstract void encode(DEROutputStream out)
164         throws IOException JavaDoc;
165 }
166
Popular Tags