Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
42   232   8   3.82
10   84   0.43   11
11     1.64  
1    
 
 
  Generalized_Time       Line # 89 42 8 68.3% 0.6825397
 
  (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.*;
81   
82    /**
83    * This is the class that represents the GeneralizedTime ASN.1 construct. IAIK
84    * classes do have
85    * a class for GeneralizedTime, but its value is string. This class has a field
86    * that is a Calendar,
87    * which is deemed more useful when validating ACs.
88    */
 
89    public class Generalized_Time implements ASN1Type, Cloneable{
90    final public int GENERALIZEDTIME_TAG = 0x18;
91    final protected java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMddHHmmss'Z'");
92   
93    protected Calendar time = new GregorianCalendar(); // by default, set to Now
94   
95    /**
96    * Returns the Calendar set to the Time.
97    */
 
98  3658 toggle public Calendar getTime(){
99  3658 return time;
100    }
101   
102    /**
103    * Sets the time for this Generalized Time object using Calendar.
104    */
 
105  0 toggle public void setTime(Calendar time){
106  0 this.time=time;
107    }
108   
109    /**
110    * This constructor builds a Generalized_Time with the default TimeZone and
111    * set to current time (the system time at the time of creating this object).
112    */
 
113  19572 toggle protected Generalized_Time() {
114  19572 TimeZone tz = TimeZone.getDefault();
115  19572 tz.setRawOffset(0);
116  19572 formatter.setTimeZone(tz);
117    }
118   
119    /**
120    * This constructor copies the given Generalized_Time.
121    */
 
122  0 toggle public Generalized_Time(Generalized_Time gt){
123  0 this();
124   
125  0 time = new GregorianCalendar();
126  0 time.setTime(gt.time.getTime());
127  0 time.setTimeZone(gt.time.getTimeZone());
128    }
129   
130    /**
131    * This constructor builds a Generalized_Time with the time given as a
132    * Calendar.
133    */
 
134  100 toggle public Generalized_Time(Calendar time){
135  100 this();
136   
137  100 this.time = time;
138    }
139   
140    /**
141    * This constructor builds a Generalized_Time from ASN1Object of
142    * GeneralizedTime.
143    */
 
144  3718 toggle public Generalized_Time(ASN1Object ao) throws CodingException{
145  3718 this();
146   
147  3718 decode(ao);
148    }
149   
150    /**
151    * This method returns the ASN1Object of GeneralizedTime.
152    */
 
153  84 toggle public ASN1Object toASN1Object(){
154  84 String str = formatter.format(time.getTime());
155  84 return new iaik.asn1.GeneralizedTime(str);
156    }
157   
158    /**
159    * This method decodes ASN1Object. It must be GeneralizedTime.
160    *
161    * @param ao - the ASN1Object of GeneralizedTime
162    */
 
163  3718 toggle public void decode(ASN1Object ao) throws CodingException{
164  3718 String str = (String) ((iaik.asn1.GeneralizedTime)ao).getValue(); // of course, casting to
165    // Generalized time is not crucial for getting a Value (since the method is virtual)
166    // but it is essential to get ClassCastException, if ao is not a GeneralizedTime
167  3718 time = new GregorianCalendar();
168  3718 try{
169  3718 time.setTime(formatter.parse(str));
170    }catch (java.text.ParseException pe){ // this is supposed to be the older PERMIS AC, which did not correspond to the standard.
171    //time.set(Calendar.AM_PM, Calendar.AM);
172    //time.set(Calendar.YEAR, Integer.parseInt(str.substring(0, 4)));
173    //time.set(Calendar.MONTH, Integer.parseInt(str.substring(4, 6))-1);
174    //time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(str.substring(6, 8)));
175    //time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(str.substring(8, 10)));
176    //time.set(Calendar.MINUTE, Integer.parseInt(str.substring(10, 12)));
177    //time.set(Calendar.SECOND, Integer.parseInt(str.substring(12, 14)));
178  1478 time.set(Integer.parseInt(str.substring(0, 4)),
179    Integer.parseInt(str.substring(4, 6))-1,
180    Integer.parseInt(str.substring(6, 8)),
181    Integer.parseInt(str.substring(8, 10)),
182    Integer.parseInt(str.substring(10, 12)),
183    Integer.parseInt(str.substring(12, 14)));
184   
185  1478 char[] msecs = str.substring(14).toCharArray();
186   
187  1478 int milisecs = 0;
188  1478 int div = 1000;
189  1478 int i=0;
190   
191  1478 if (msecs[0]=='.'){
192  5912 for (i=1; msecs[i]>='0' && msecs[i]<='9'; i++){
193  4434 div /=10;
194  4434 milisecs = (milisecs * 10) + ((byte)msecs[i])-0x30;
195    }
196    }
197   
198  1478 milisecs *=div;
199  1478 time.set(Calendar.MILLISECOND, milisecs);
200   
201  1478 milisecs = 0;
202   
203  1478 if (msecs[i]!='Z'){
204  0 for (int j=1; j<=4; j++){
205  0 milisecs = (milisecs*10) + ((byte)msecs[i+j])-0x30;
206    }
207   
208  0 milisecs = (60*(milisecs/100)+milisecs%100)*60*1000; // miliseconds from GMT
209  0 if (msecs[i]=='-'){
210  0 milisecs=-milisecs;
211    }
212    }
213   
214  1478 time.set(Calendar.ZONE_OFFSET, milisecs);
215  1478 time.set(Calendar.DST_OFFSET, 0); // no daylight saving is here - the time is raw
216    }
217    }
218   
219   
 
220  8 toggle public String toString(){
221  8 return time.getTime().toString()+" -- GeneralizedTime --";
222    }
223   
 
224  8 toggle public String toString(String ident){
225  8 return toString();
226    }
227   
 
228  0 toggle public Object clone(){
229  0 return new Generalized_Time(this);
230    }
231   
232    }