Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
49   323   13   3.27
20   108   0.55   15
15     1.8  
1    
 
 
  Attribute       Line # 101 49 13 64.3% 0.64285713
 
  (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    import java.util.Vector;
81    import java.util.Enumeration;
82   
83    /**
84    * This class represents an Attribute ASN.1 construct. It contains a vector of
85    * values, each of them
86    * being a subclass of AttributeValue.
87    *
88    * <p>This class maintains a register of AttributeValue subclasses, one per
89    * attribute object identifier (OID). This way it can construct the specific
90    * AttributeValue subclasses when an Attribute with a specific OID is
91    * encountered
92    * By default, it creates just
93    * AttributeValue elements when parsing
94    * an ASN1Object, but you can register your own AttributeValue subclasses using
95    * registerAttribute method. The subclasses must have a constructor
96    * with ASN1Object as a parameter.
97    *
98    * @see #registerAttribute
99    */
100   
 
101    public class Attribute implements ASN1Type, Cloneable{
102    protected String type = "";
103   
104    /**
105    * Returns the OID of the Attribute.
106    */
 
107  1817 toggle public String getType(){
108  1817 return type;
109    }
110   
111    /**
112    * Sets the OID of the Attribute type. It should be in the dotted notation.
113    */
 
114  0 toggle public void setType(String type){
115  0 this.type=type;
116    }
117   
118    protected Vector values = new Vector();
119   
120    /**
121    * Returns the vector of values of the attribute. Each element is a
122    * AttributeValue or its subclass.
123    */
 
124  1813 toggle public Vector getValues(){
125  1813 return values;
126    }
127   
128    /**
129    * Sets the vector of values of the attribute. The sequence of them does not
130    * matter, since they
131    * are encoded as a SET.
132    *
133    * @param values - the Vector of attribute values; each element must be of
134    * AttributeValue class or a subclass of it.
135    */
 
136  0 toggle public void setValues(Vector values){
137  0 this.values=values;
138    }
139   
 
140  0 toggle protected Attribute() {
141    }
142   
143    /**
144    * This constructor builds an Attribute from ASN1Object.
145    *
146    * @param ao - the ASN1Object containing the Attribute
147    */
 
148  1864 toggle public Attribute(ASN1Object ao) throws CodingException{
149  1864 decode(ao);
150    }
151   
152    /**
153    * This constructor creates a copy of Attribute. All values are copied into
154    * a new collection, so changing the set of values in the original Attribute
155    * does not affect the copy.
156    *
157    * @param a - the Attribute to copy
158    */
 
159  0 toggle public Attribute(Attribute a){
160  0 type = new String(a.type);
161  0 values = new Vector(a.values);
162    //for (Enumeration e=a.values.elements(); e.hasMoreElements();){
163    // values.add(((Cloneable)e.nextElement()).clone());
164    //}
165    }
166   
167    /**
168    * This constructor creates an Attribute of the given type and a set of
169    * values.
170    *
171    * @param type - the OID of the Attribute in dotted form
172    * @param values - the Vector of attribute values; each element must be of
173    * AttributeValue class or its subclass; should not be null; in fact, the
174    * standard requires that at least one element were present
175    */
 
176  20 toggle public Attribute(String type, Vector values){
177  20 this.type = type;
178  20 this.values = values;
179    }
180   
181    /**
182    * This constructor builds an Attribute given its type and a single value.
183    *
184    * @param type - the OID of the Attribute in the dotted form
185    * @param value - the AttributeValue of the Attribute
186    */
 
187  0 toggle public Attribute(String type, AttributeValue value){
188  0 this.type = type;
189  0 values = new Vector();
190  0 values.addElement(value);
191    }
192   
193   
194    /**
195    * This method returns the ASN1Object of this Attribute.
196    *
197    * @return SEQUENCE of ObjectID and a SET of attribute values
198    */
 
199  42 toggle public ASN1Object toASN1Object() throws CodingException{
200  42 ASN1Object result = new SEQUENCE();
201   
202  42 result.addComponent(new ObjectID(type));
203   
204  42 ASN1Object set = new SET();
205  92 for (Enumeration e=values.elements(); e.hasMoreElements();){
206  50 set.addComponent(((AttributeValue)e.nextElement()).toASN1Object());
207    }
208  42 result.addComponent(set);
209   
210  42 return result;
211    }
212   
213    /**
214    * This method decodes the given ASN1Object and sets this attribute type and
215    * value, as in ASN1Object. This method looks for AttributeValue subclasses
216    * in the registry to find the appropriate subclass to decode individual
217    * values. If no appropriate subclass is found, AttributeValue is used.
218    *
219    * <p>You can register custom AttributeValue subclasses for decoding values of
220    * specific attribute types using registerAttribute method.
221    *
222    * @param ao - the ASN1Object to decode; it must be a SEQUENCE of ObjectID and
223    * a SET of AttributeValues
224    *
225    * @see #registerAttribute
226    */
 
227  1864 toggle public void decode(ASN1Object ao) throws CodingException{
228  1864 if (!ao.isA(ASN.SEQUENCE)){
229  0 throw new CodingException("Attribute SEQUENCE tag was expected");
230    }
231   
232  1864 if (ao.countComponents()!=2){
233  0 throw new CodingException("Illegal number of entries in Attribute tag: "+Integer.toString(ao.countComponents()));
234    }
235   
236  1864 this.type = (String)((ObjectID)ao.getComponentAt(0)).getValue();
237   
238  1864 ASN1Object tag = ao.getComponentAt(1);
239   
240  1864 if (!tag.isA(ASN.SET)){
241  0 throw new CodingException("SET of AttributeValue was expected");
242    }
243   
244  1864 values = new Vector();
245  3845 for (int i=tag.countComponents(); i-->0; ){
246  1981 Class attribute = (Class)registry.get(this.type);
247  1981 if (attribute==null){
248  840 attribute=AttributeValue.class;
249    }
250   
251  1981 try{
252  1981 values.insertElementAt(attribute.getConstructor(new Class[]{ASN1Object.class})
253    .newInstance(new Object[]{tag.getComponentAt(i)}),
254    0);
255    }catch (NoSuchMethodException nsme){
256  0 attribute=null;
257    }catch (java.lang.reflect.InvocationTargetException ite){
258  0 attribute=null;
259    }catch (IllegalAccessException iae){
260  0 attribute=null;
261    }catch (InstantiationException ie){
262  0 attribute=null;
263    }
264   
265  1981 if (attribute==null){ // could not use a registered class
266  0 values.insertElementAt(new AttributeValue(tag.getComponentAt(i)), 0);
267    }
268  1864 };
269    }
270   
271   
 
272  0 toggle public String toString(){
273  0 return toString("");
274    }
275   
 
276  4 toggle public String toString(String ident){
277  4 Enumeration e = values.elements();
278   
279  4 String result = "SEQUENCE { -- Attribute --\n "+ident+
280    "type = "+type+" -- ObjectIdentifier --"+
281  4 ((e.hasMoreElements())?
282    ",\n "+ident+
283    "values = SET { -- Set of AttributeValues --\n"
284    :"\n");
285   
286   
287  8 for (; e.hasMoreElements();){
288  4 result = result + " "+ident+((AttributeValue)(e.nextElement())).toString(ident+" ")+
289  4 (e.hasMoreElements()? ",\n" : ("\n "+ident+"}\n"));
290    }
291   
292  4 return result+ident+"}";
293    }
294   
295   
296   
 
297  0 toggle public Object clone(){
298  0 return new Attribute(this);
299    }
300   
301    protected static java.util.Map registry = new java.util.Hashtable();
302   
303    /**
304    * This method lets you register custom AttributeValue subclasses that will be
305    * used to decode values of specific attributes.
306    *
307    * <p><code>...<br>
308    * Attribute.registerAttribute("1.2.3.4.5", MyCuteAttribute.class);
309    * </code> <br>
310    * Now the constructor of your class will be invoked automatically whenever
311    * the OID is encountered in
312    * the attributes of an encoded AC.
313    *
314    * @param oid is the dotted form of the OID
315    * @param attribute is the class that will decode single attribute values;
316    * must be a subclass of AttributeValue and must have a constructor with
317    * ASN1Object as an argument
318    */
 
319  1 toggle public static void registerAttribute(String oid, Class attribute){
320  1 registry.put(oid, attribute);
321    }
322   
323    }