KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > ASN1TaggedObject


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4
5 /**
6  * ASN.1 TaggedObject - in ASN.1 nottation this is any object proceeded by
7  * a [n] where n is some number - these are assume to follow the construction
8  * rules (as with sequences).
9  */

10 public abstract class ASN1TaggedObject
11     extends DERObject
12 {
13     int tagNo;
14     boolean empty = false;
15     boolean explicit = true;
16     DEREncodable obj = null;
17
18     static public ASN1TaggedObject getInstance(
19         ASN1TaggedObject obj,
20         boolean explicit)
21     {
22         if (explicit)
23         {
24             return (ASN1TaggedObject)obj.getObject();
25         }
26
27         throw new IllegalArgumentException("implicitly tagged tagged object");
28     }
29
30     /**
31      * @param tagNo the tag number for this object.
32      * @param obj the tagged object.
33      */

34     public ASN1TaggedObject(
35         int tagNo,
36         DEREncodable obj)
37     {
38         this.explicit = true;
39         this.tagNo = tagNo;
40         this.obj = obj;
41     }
42
43     /**
44      * @param explicit true if the object is explicitly tagged.
45      * @param tagNo the tag number for this object.
46      * @param obj the tagged object.
47      */

48     public ASN1TaggedObject(
49         boolean explicit,
50         int tagNo,
51         DEREncodable obj)
52     {
53         this.explicit = explicit;
54         this.tagNo = tagNo;
55         this.obj = obj;
56     }
57     
58     public boolean equals(
59         Object o)
60     {
61         if (o == null || !(o instanceof ASN1TaggedObject))
62         {
63             return false;
64         }
65         
66         ASN1TaggedObject other = (ASN1TaggedObject)o;
67         
68         if (tagNo != other.tagNo || empty != other.empty || explicit != other.explicit)
69         {
70             return false;
71         }
72         
73         if(obj == null)
74         {
75             if(other.obj != null)
76             {
77                 return false;
78             }
79         }
80         else
81         {
82             if(!(obj.equals(other.obj)))
83             {
84                 return false;
85             }
86         }
87         
88         return true;
89     }
90     
91     public int hashCode()
92     {
93         int code = tagNo;
94
95         if (obj != null)
96         {
97             code ^= obj.hashCode();
98         }
99
100         return code;
101     }
102
103     public int getTagNo()
104     {
105         return tagNo;
106     }
107
108     /**
109      * return whether or not the object may be explicitly tagged.
110      * <p>
111      * Note: if the object has been read from an input stream, the only
112      * time you can be sure if isExplicit is returning the true state of
113      * affairs is if it returns false. An implicitly tagged object may appear
114      * to be explicitly tagged, so you need to understand the context under
115      * which the reading was done as well, see getObject below.
116      */

117     public boolean isExplicit()
118     {
119         return explicit;
120     }
121
122     public boolean isEmpty()
123     {
124         return empty;
125     }
126
127     /**
128      * return whatever was following the tag.
129      * <p>
130      * Note: tagged objects are generally context dependent if you're
131      * trying to extract a tagged object you should be going via the
132      * appropriate getInstance method.
133      */

134     public DERObject getObject()
135     {
136         if (obj != null)
137         {
138             return obj.getDERObject();
139         }
140
141         return null;
142     }
143
144     abstract void encode(DEROutputStream out)
145         throws IOException;
146 }
147
Popular Tags