KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4 import java.util.Enumeration;
5 import java.util.Vector;
6
7 public abstract class ASN1Sequence
8     extends DERObject
9 {
10     private Vector seq = new Vector();
11
12     /**
13      * return an ASN1Sequence from the given object.
14      *
15      * @param obj the object we want converted.
16      * @exception IllegalArgumentException if the object cannot be converted.
17      */

18     public static ASN1Sequence getInstance(
19         Object obj)
20     {
21         if (obj == null || obj instanceof ASN1Sequence)
22         {
23             return (ASN1Sequence)obj;
24         }
25
26         throw new IllegalArgumentException("unknown object in getInstance");
27     }
28
29     /**
30      * Return an ASN1 sequence from a tagged object. There is a special
31      * case here, if an object appears to have been explicitly tagged on
32      * reading but we were expecting it to be implictly tagged in the
33      * normal course of events it indicates that we lost the surrounding
34      * sequence - so we need to add it back (this will happen if the tagged
35      * object is a sequence that contains other sequences). If you are
36      * dealing with implicitly tagged sequences you really <b>should</b>
37      * be using this method.
38      *
39      * @param obj the tagged object.
40      * @param explicit true if the object is meant to be explicitly tagged,
41      * false otherwise.
42      * @exception IllegalArgumentException if the tagged object cannot
43      * be converted.
44      */

45     public static ASN1Sequence getInstance(
46         ASN1TaggedObject obj,
47         boolean explicit)
48     {
49         if (explicit)
50         {
51             if (!obj.isExplicit())
52             {
53                 throw new IllegalArgumentException("object implicit - explicit expected.");
54             }
55
56             return (ASN1Sequence)obj.getObject();
57         }
58         else
59         {
60             //
61
// constructed object which appears to be explicitly tagged
62
// when it should be implicit means we have to add the
63
// surrounding sequence.
64
//
65
if (obj.isExplicit())
66             {
67                 if (obj instanceof BERTaggedObject)
68                 {
69                     return new BERSequence(obj.getObject());
70                 }
71                 else
72                 {
73                     return new DERSequence(obj.getObject());
74                 }
75             }
76             else
77             {
78                 if (obj.getObject() instanceof ASN1Sequence)
79                 {
80                     return (ASN1Sequence)obj.getObject();
81                 }
82             }
83         }
84
85         throw new IllegalArgumentException(
86                 "unknown object in getInstanceFromTagged");
87     }
88
89     public Enumeration getObjects()
90     {
91         return seq.elements();
92     }
93
94     /**
95      * return the object at the sequence postion indicated by index.
96      *
97      * @param index the sequence number (starting at zero) of the object
98      * @return the object at the sequence postion indicated by index.
99      */

100     public DEREncodable getObjectAt(
101         int index)
102     {
103         return (DEREncodable)seq.elementAt(index);
104     }
105
106     /**
107      * return the number of objects in this sequence.
108      *
109      * @return the number of objects in this sequence.
110      */

111     public int size()
112     {
113         return seq.size();
114     }
115
116     public int hashCode()
117     {
118         Enumeration e = this.getObjects();
119         int hashCode = 0;
120
121         while (e.hasMoreElements())
122         {
123             Object o = e.nextElement();
124             
125             if (o != null)
126             {
127                 hashCode ^= o.hashCode();
128             }
129         }
130
131         return hashCode;
132     }
133
134     public boolean equals(
135         Object o)
136     {
137         if (o == null || !(o instanceof ASN1Sequence))
138         {
139             return false;
140         }
141
142         ASN1Sequence other = (ASN1Sequence)o;
143
144         if (this.size() != other.size())
145         {
146             return false;
147         }
148
149         Enumeration s1 = this.getObjects();
150         Enumeration s2 = other.getObjects();
151
152         while (s1.hasMoreElements())
153         {
154             Object o1 = s1.nextElement();
155             Object o2 = s2.nextElement();
156
157             if (o1 != null && o2 != null)
158             {
159                 if (!o1.equals(o2))
160                 {
161                     return false;
162                 }
163             }
164             else if (o1 == null && o2 == null)
165             {
166                 continue;
167             }
168             else
169             {
170                 return false;
171             }
172         }
173
174         return true;
175     }
176
177     protected void addObject(
178         DEREncodable obj)
179     {
180         seq.addElement(obj);
181     }
182
183     abstract void encode(DEROutputStream out)
184         throws IOException;
185 }
186
Popular Tags