KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Enumeration JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 public abstract class ASN1Sequence
27     extends DERObject
28 {
29     private Vector JavaDoc seq = new Vector JavaDoc();
30
31     /**
32      * return an ASN1Sequence from the given object.
33      *
34      * @param obj the object we want converted.
35      * @exception IllegalArgumentException if the object cannot be converted.
36      */

37     public static ASN1Sequence getInstance(
38         Object JavaDoc obj)
39     {
40         if (obj == null || obj instanceof ASN1Sequence)
41         {
42             return (ASN1Sequence)obj;
43         }
44
45         throw new IllegalArgumentException JavaDoc("unknown object in getInstance");
46     }
47
48     /**
49      * Return an ASN1 sequence from a tagged object. There is a special
50      * case here, if an object appears to have been explicitly tagged on
51      * reading but we were expecting it to be implictly tagged in the
52      * normal course of events it indicates that we lost the surrounding
53      * sequence - so we need to add it back (this will happen if the tagged
54      * object is a sequence that contains other sequences). If you are
55      * dealing with implicitly tagged sequences you really <b>should</b>
56      * be using this method.
57      *
58      * @param obj the tagged object.
59      * @param explicit true if the object is meant to be explicitly tagged,
60      * false otherwise.
61      * @exception IllegalArgumentException if the tagged object cannot
62      * be converted.
63      */

64     public static ASN1Sequence getInstance(
65         ASN1TaggedObject obj,
66         boolean explicit)
67     {
68         if (explicit)
69         {
70             if (!obj.isExplicit())
71             {
72                 throw new IllegalArgumentException JavaDoc("object implicit - explicit expected.");
73             }
74
75             return (ASN1Sequence)obj.getObject();
76         }
77         else
78         {
79             //
80
// constructed object which appears to be explicitly tagged
81
// when it should be implicit means we have to add the
82
// surrounding sequence.
83
//
84
if (obj.isExplicit())
85             {
86                 if (obj instanceof BERTaggedObject)
87                 {
88                     return new BERSequence(obj.getObject());
89                 }
90                 else
91                 {
92                     return new DERSequence(obj.getObject());
93                 }
94             }
95             else
96             {
97                 if (obj.getObject() instanceof ASN1Sequence)
98                 {
99                     return (ASN1Sequence)obj.getObject();
100                 }
101             }
102         }
103
104         throw new IllegalArgumentException JavaDoc(
105                 "unknown object in getInstanceFromTagged");
106     }
107
108     public Enumeration JavaDoc getObjects()
109     {
110         return seq.elements();
111     }
112
113     /**
114      * return the object at the sequence postion indicated by index.
115      *
116      * @param the sequence number (starting at zero) of the object
117      * @return the object at the sequence postion indicated by index.
118      */

119     public DEREncodable getObjectAt(
120         int index)
121     {
122         return (DEREncodable)seq.elementAt(index);
123     }
124
125     /**
126      * return the number of objects in this sequence.
127      *
128      * @return the number of objects in this sequence.
129      */

130     public int size()
131     {
132         return seq.size();
133     }
134
135     public int hashCode()
136     {
137         Enumeration JavaDoc e = this.getObjects();
138         int hashCode = 0;
139
140         while (e.hasMoreElements())
141         {
142             Object JavaDoc o = e.nextElement();
143
144             if (o != null)
145             {
146                 hashCode ^= o.hashCode();
147             }
148         }
149
150         return hashCode;
151     }
152
153     public boolean equals(
154         Object JavaDoc o)
155     {
156         if (o == null || !(o instanceof ASN1Sequence))
157         {
158             return false;
159         }
160
161         ASN1Sequence other = (ASN1Sequence)o;
162
163         if (this.size() != other.size())
164         {
165             return false;
166         }
167
168         Enumeration JavaDoc s1 = this.getObjects();
169         Enumeration JavaDoc s2 = other.getObjects();
170
171         while (s1.hasMoreElements())
172         {
173             Object JavaDoc o1 = s1.nextElement();
174             Object JavaDoc o2 = s2.nextElement();
175
176             if (o1 != null && o2 != null)
177             {
178                 if (!o1.equals(o2))
179                 {
180                     return false;
181                 }
182             }
183             else if (o1 == null && o2 == null)
184             {
185                 continue;
186             }
187             else
188             {
189                 return false;
190             }
191         }
192
193         return true;
194     }
195
196     protected void addObject(
197         DEREncodable obj)
198     {
199         seq.addElement(obj);
200     }
201
202     abstract void encode(DEROutputStream out)
203         throws IOException JavaDoc;
204 }
205
Popular Tags