KickJava   Java API By Example, From Geeks To Geeks.

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


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 abstract public class ASN1Set
27     extends DERObject
28 {
29     protected Vector JavaDoc set = new Vector JavaDoc();
30
31     /**
32      * return an ASN1Set 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 ASN1Set getInstance(
38         Object JavaDoc obj)
39     {
40         if (obj == null || obj instanceof ASN1Set)
41         {
42             return (ASN1Set)obj;
43         }
44
45         throw new IllegalArgumentException JavaDoc("unknown object in getInstance");
46     }
47
48     /**
49      * Return an ASN1 set 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      * set - 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 sets 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 ASN1Set 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 (ASN1Set)obj.getObject();
76         }
77         else
78         {
79             //
80
// constructed object which appears to be explicitly tagged
81
// and it's really implicit means we have to add the
82
// surrounding sequence.
83
//
84
if (obj.isExplicit())
85             {
86                 ASN1Set set = new DERSet(obj.getObject());
87
88                 return set;
89             }
90             else
91             {
92                 if (obj.getObject() instanceof ASN1Set)
93                 {
94                     return (ASN1Set)obj.getObject();
95                 }
96
97                 //
98
// in this case the parser returns a sequence, convert it
99
// into a set.
100
//
101
ASN1EncodableVector v = new ASN1EncodableVector();
102
103                 if (obj.getObject() instanceof ASN1Sequence)
104                 {
105                     ASN1Sequence s = (ASN1Sequence)obj.getObject();
106                     Enumeration JavaDoc e = s.getObjects();
107
108                     while (e.hasMoreElements())
109                     {
110                         v.add((DEREncodable)e.nextElement());
111                     }
112
113                     return new DERSet(v);
114                 }
115             }
116         }
117
118         throw new IllegalArgumentException JavaDoc(
119                     "unknown object in getInstanceFromTagged");
120     }
121
122     public ASN1Set()
123     {
124     }
125
126     public Enumeration JavaDoc getObjects()
127     {
128         return set.elements();
129     }
130
131     /**
132      * return the object at the set postion indicated by index.
133      *
134      * @param the set number (starting at zero) of the object
135      * @return the object at the set postion indicated by index.
136      */

137     public DEREncodable getObjectAt(
138         int index)
139     {
140         return (DEREncodable)set.elementAt(index);
141     }
142
143     /**
144      * return the number of objects in this set.
145      *
146      * @return the number of objects in this set.
147      */

148     public int size()
149     {
150         return set.size();
151     }
152
153     public int hashCode()
154     {
155         Enumeration JavaDoc e = this.getObjects();
156         int hashCode = 0;
157
158         while (e.hasMoreElements())
159         {
160             hashCode ^= e.nextElement().hashCode();
161         }
162
163         return hashCode;
164     }
165
166     public boolean equals(
167         Object JavaDoc o)
168     {
169         if (o == null || !(o instanceof ASN1Set))
170         {
171             return false;
172         }
173
174         ASN1Set other = (ASN1Set)o;
175
176         if (this.size() != other.size())
177         {
178             return false;
179         }
180
181         Enumeration JavaDoc s1 = this.getObjects();
182         Enumeration JavaDoc s2 = other.getObjects();
183
184         while (s1.hasMoreElements())
185         {
186             if (!s1.nextElement().equals(s2.nextElement()))
187             {
188                 return false;
189             }
190         }
191
192         return true;
193     }
194
195     protected void addObject(
196         DEREncodable obj)
197     {
198         set.addElement(obj);
199     }
200
201     abstract void encode(DEROutputStream out)
202             throws IOException JavaDoc;
203 }
204
Popular Tags