Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
36   261   10   2.77
18   81   0.61   13
13     1.69  
1    
 
 
  Extensions       Line # 98 36 10 59.7% 0.5970149
 
  (1)
 
1    /*
2    * Copyright (c) 2006, University of Kent
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10    *
11    * Redistributions in binary form must reproduce the above copyright notice,
12    * this list of conditions and the following disclaimer in the documentation
13    * and/or other materials provided with the distribution.
14    *
15    * 1. Neither the name of the University of Kent nor the names of its
16    * contributors may be used to endorse or promote products derived from this
17    * software without specific prior written permission.
18    *
19    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22    * PURPOSE ARE DISCLAIMED.
23    *
24    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    * POSSIBILITY OF SUCH DAMAGE.
32    *
33    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
34    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
35    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
36    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
37    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
38    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
39    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
40    * SERIOUS FAULTS, IN THIS SOFTWARE.
41    *
42    * 5. This license is governed, except to the extent that local laws
43    * necessarily apply, by the laws of England and Wales.
44    */
45    /*
46    * Copyright (c) 2000-2005, University of Salford
47    * All rights reserved.
48    *
49    * Redistribution and use in source and binary forms, with or without
50    * modification, are permitted provided that the following conditions are met:
51    *
52    * Redistributions of source code must retain the above copyright notice, this
53    * list of conditions and the following disclaimer.
54    *
55    * Redistributions in binary form must reproduce the above copyright notice,
56    * this list of conditions and the following disclaimer in the documentation
57    * and/or other materials provided with the distribution.
58    *
59    * Neither the name of the University of Salford nor the names of its
60    * contributors may be used to endorse or promote products derived from this
61    * software without specific prior written permission.
62    *
63    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
64    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
67    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
68    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
69    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
70    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
71    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
72    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
73    * POSSIBILITY OF SUCH DAMAGE.
74    */
75   
76    package issrg.ac;
77   
78    import iaik.asn1.*;
79    import iaik.asn1.structures.*;
80   
81    import java.util.Vector;
82    import java.util.Enumeration;
83   
84    /**
85    * This is the class that represents the Extensions ASN.1 construct. It has a
86    * vector of extensions.
87    *
88    * <p>This class also maintains a register of custom Extension subclasses that
89    * will be used to decode specific extensions, distinguished by their OID. When
90    * an Extension with a known OID is encountered, the corresponding class is
91    * instantiated by calling its constructor with a single ASN1Object argument.
92    * If no class is registered for such OID, or construction fails, an instance
93    * of Extension will be constructed.
94    *
95    * @see #registerExtension
96    */
97   
 
98    public class Extensions implements ASN1Type, Cloneable{
99   
100    protected Vector values = new Vector();
101   
102    /**
103    * Returns the set of Extensions. Never null, but can be empty.
104    */
 
105  1779 toggle public Vector getValues(){
106  1779 return values;
107    }
108   
109    /**
110    * Sets the set of Extensions. Cannot be null, but can be empty.
111    */
 
112  0 toggle public void setValues(Vector values){
113  0 this.values=values;
114    }
115   
 
116  0 toggle protected Extensions() {
117    }
118   
119    /**
120    * This constructor copies a given Extensions.
121    *
122    * @param e - the Extensions to copy
123    */
 
124  0 toggle public Extensions(Extensions e){
125  0 values = new Vector(e.values);
126    }
127   
128    /**
129    * This constructor creates Extensions from a Vector. Each element must be
130    * a Extension subclass.
131    *
132    * @param values - a Vector of extensions; each element must be an Extension;
133    * cannot be null
134    */
 
135  20 toggle public Extensions(Vector values){
136  20 this.values = values;
137    }
138   
139    /**
140    * This constructor builds Extensions with a single Extension.
141    *
142    * @param extension - the Extension
143    */
 
144  0 toggle public Extensions(Extension extension){
145  0 values = new Vector();
146  0 values.addElement(extension);
147    }
148   
149    /**
150    * This constructor builds Extensions from a ASN1Object of it.
151    *
152    * @param ao - the ASN1Object representing the Extensions
153    */
 
154  1859 toggle public Extensions(ASN1Object ao) throws CodingException{
155  1859 decode(ao);
156    }
157   
158    /**
159    * This method returns the ASN1Object of Extensions.
160    *
161    * @return SEQUENCE of Extension
162    */
 
163  42 toggle public ASN1Object toASN1Object() throws CodingException{
164  42 ASN1Object result = new SEQUENCE();
165  42 if (values==null){
166  0 values=new Vector();
167    }
168   
169  162 for (Enumeration e = values.elements(); e.hasMoreElements();){
170  120 result.addComponent(((Extension)e.nextElement()).toASN1Object());
171    }
172   
173  42 return result;
174    }
175   
176    /**
177    * This method decodes a given ASN1Object. It must be a SEQUENCE of elements,
178    * each being a valid Extension.
179    *
180    * <p>It checks if the registry contains a class for the ObjectID of each
181    * extension in the list. If there is such class, it attempts to construct
182    * the Extension using a constructor of that class with a single ASN1Object
183    * argument to it. If this fails or there is no class registered for this
184    * ObjectID, an Extension is constructed.
185    *
186    * @param ao - the ASN1Object of Extensions
187    */
 
188  1859 toggle public void decode(ASN1Object ao) throws CodingException{
189  1859 if (!ao.isA(ASN.SEQUENCE)){
190  0 throw new CodingException("Extensions SEQUENCE tag was expected");
191    }
192   
193  1859 values = new Vector();
194  4975 for (int i=ao.countComponents(); i-->0;){
195  3116 String oid = (String)((ObjectID) ao.getComponentAt(i).getComponentAt(0)).getValue();
196    // String oid = new Extension(ao.getComponentAt(i)).getExtensionOID();
197  3116 Class extension = (Class) registry.get(oid);
198  3116 Extension e=null;
199   
200  3116 try {
201  3116 if (extension!=null) e=(Extension)extension.getConstructor(new Class[] {ASN1Object.class})
202    .newInstance(new Object[] {ao.getComponentAt(i)});
203    } catch (Exception nsme) {
204    }
205   
206  3116 if (e==null){ // failed to decode extension or no such extension was registered
207  0 e=new Extension(ao.getComponentAt(i));
208    }
209   
210  3116 values.insertElementAt(e,0);
211   
212   
213    //values.insertElementAt(new Extension(ao.getComponentAt(i)), 0);
214    }
215    }
216   
217   
 
218  0 toggle public String toString(){
219  0 return toString("");
220    }
221   
 
222  4 toggle public String toString(String ident){
223  4 Enumeration e = values.elements();
224  4 String result = "SEQUENCE { -- Extensions -- ";
225   
226  4 if (e.hasMoreElements()){
227  0 result = result+"\n"+ident;
228  0 for (;e.hasMoreElements();){
229  0 result = result +" "+((Extension)e.nextElement()).toString(ident+" ")+
230  0 (e.hasMoreElements()?",":"")+"\n"+ident;
231    }
232    }
233   
234  4 return result+"}";
235    }
236   
237   
 
238  0 toggle public Object clone(){
239  0 return new Extensions(this);
240    }
241   
242    protected static java.util.Map registry = new java.util.Hashtable();
243   
244    /**
245    * This method lets you register your extensions. Now if an attribute with a particular OID will be
246    * encountered, it will be parsed by your code directly.
247    *
248    * <p><code>...<br>
249    * Extension.registerAttribute("1.2.3.4.5", MyCuteAttribute.class);
250    * </code> <br>
251    * Now the constructor of your class will be invoked automatically whenever the OID is encountered in
252    * the attributes of an encoded AC.
253    *
254    * @param oid is the dotted form of the OID
255    * @param extension is the class of your attribute
256    */
 
257  171 toggle public static void registerExtension(String oid, Class extension){
258  171 registry.put(oid, extension);
259    }
260    }
261