1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.utils.Messages; 59 60 import javax.xml.namespace.QName ; 61 import java.text.SimpleDateFormat ; 62 import java.util.Calendar ; 63 import java.util.Date ; 64 import java.util.GregorianCalendar ; 65 import java.util.SimpleTimeZone ; 66 import java.util.TimeZone ; 67 68 75 public class CalendarDeserializer extends SimpleDeserializer 76 { 77 78 private static SimpleDateFormat zulu = 79 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 80 82 static 83 { 84 zulu.setTimeZone(TimeZone.getTimeZone("GMT")); 85 } 86 87 91 public CalendarDeserializer(Class javaType, QName xmlType) 92 { 93 super(javaType, xmlType); 94 } 95 96 100 public Object makeValue(String source) 101 { 102 Calendar calendar = Calendar.getInstance(); 103 Date date; 104 boolean bc = false; 105 106 if (source != null) 108 { 109 if (source.charAt(0) == '+') 110 source = source.substring(1); 111 112 if (source.charAt(0) == '-') 113 { 114 source = source.substring(1); 115 bc = true; 116 } 117 118 if (source.length() < 19) 119 throw new NumberFormatException (Messages.getMessage("badDateTime00")); 120 121 if (source.charAt(4) != '-' || source.charAt(7) != '-' || 122 source.charAt(10) != 'T') 123 throw new NumberFormatException (Messages.getMessage("badDate00")); 124 125 if (source.charAt(13) != ':' || source.charAt(16) != ':') 126 throw new NumberFormatException (Messages.getMessage("badTime00")); 127 } 128 129 try 131 { 132 synchronized (zulu) 133 { 134 date = zulu.parse(source == null ? null : 135 (source.substring(0, 19) + ".000Z")); 136 } 137 } 138 catch (Exception e) 139 { 140 throw new NumberFormatException (e.toString()); 141 } 142 143 int pos = 19; 144 145 if (source != null) 147 { 148 if (pos < source.length() && source.charAt(pos) == '.') 149 { 150 int milliseconds = 0; 151 int start = ++pos; 152 while (pos < source.length() && 153 Character.isDigit(source.charAt(pos))) 154 pos++; 155 156 String decimal = source.substring(start, pos); 157 if (decimal.length() == 3) 158 { 159 milliseconds = Integer.parseInt(decimal); 160 } 161 else if (decimal.length() < 3) 162 { 163 milliseconds = Integer.parseInt((decimal + "000") 164 .substring(0, 3)); 165 } 166 else 167 { 168 milliseconds = Integer.parseInt(decimal.substring(0, 3)); 169 if (decimal.charAt(3) >= '5') ++milliseconds; 170 } 171 172 date.setTime(date.getTime() + milliseconds); 174 } 175 176 if (pos + 5 < source.length() && 178 (source.charAt(pos) == '+' || (source.charAt(pos) == '-'))) 179 { 180 if (!Character.isDigit(source.charAt(pos + 1)) || 181 !Character.isDigit(source.charAt(pos + 2)) || 182 source.charAt(pos + 3) != ':' || 183 !Character.isDigit(source.charAt(pos + 4)) || 184 !Character.isDigit(source.charAt(pos + 5))) 185 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 186 187 int hours = (source.charAt(pos + 1) - '0') * 10 188 + source.charAt(pos + 2) - '0'; 189 int mins = (source.charAt(pos + 4) - '0') * 10 190 + source.charAt(pos + 5) - '0'; 191 int milliseconds = (hours * 60 + mins) * 60 * 1000; 192 193 if (source.charAt(pos) == '+') milliseconds = -milliseconds; 195 date.setTime(date.getTime() + milliseconds); 196 calendar.setTimeZone(new SimpleTimeZone (-milliseconds, "GMT Offset")); 197 pos += 6; 198 } 199 200 if (pos < source.length() && source.charAt(pos) == 'Z') 201 { 202 pos++; 203 calendar.setTimeZone(TimeZone.getTimeZone("GMT")); 204 } 205 206 if (pos < source.length()) 207 throw new NumberFormatException (Messages.getMessage("badChars00")); 208 } 209 210 calendar.setTime(date); 211 212 if (bc) 214 { 215 calendar.set(Calendar.ERA, GregorianCalendar.BC); 216 } 217 218 if (super.javaType == Date .class) 219 { 220 return date; 221 } 222 else 223 { 224 return calendar; 225 } 226 } 227 } 228 | Popular Tags |