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 DateDeserializer extends SimpleDeserializer 76 { 77 78 private static SimpleDateFormat zulu = 79 new SimpleDateFormat ("yyyy-MM-dd"); 80 81 private static SimpleDateFormat zuluUTC = 82 new SimpleDateFormat ("yyyy-MM-dd"); 83 85 static 86 { 87 zuluUTC.setTimeZone(TimeZone.getTimeZone("GMT")); 88 } 89 90 94 public DateDeserializer(Class javaType, QName xmlType) 95 { 96 super(javaType, xmlType); 97 } 98 99 private int adjustByZone(Date date, String zone) 101 { 102 int sign, hours, minutes; 103 104 if (zone.charAt(0) == 'Z') 105 { 106 return 0; 107 } 108 else if (zone.charAt(0) == '-') 109 { 110 sign = 1; 112 } 113 else if (zone.charAt(0) == '+') 114 { 115 sign = -1; 116 } 117 else 118 { 119 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 120 } 121 122 if (zone.charAt(3) != ':') 123 { 124 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 125 } 126 127 try 128 { 129 hours = Integer.parseInt(zone.substring(1, 3)); 130 minutes = Integer.parseInt(zone.substring(4, 6)); 131 } 132 catch (Exception e) 133 { 134 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 135 } 136 137 if (hours > 14 || (hours == 14 && minutes > 0) || minutes > 59) 139 { 140 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 141 } 142 143 int offset = sign * (hours * 60 + minutes) * 60 * 1000; 144 145 date.setTime(date.getTime() + offset); 146 147 return -offset; 149 } 150 151 155 public Object makeValue(String source) 156 { 157 Calendar calendar; 158 Date result; 159 boolean bc = false; 160 String zonePortion = null; 161 162 if (source == null) 163 { 164 throw new NumberFormatException (Messages.getMessage("badDate00")); 165 } 166 167 if (source.charAt(0) == '+') 168 source = source.substring(1); 169 170 if (source.charAt(0) == '-') 171 { 172 source = source.substring(1); 173 bc = true; 174 } 175 176 if (source.length() < 10) 177 throw new NumberFormatException (Messages.getMessage("badDate00")); 178 179 if (source.charAt(4) != '-' || source.charAt(7) != '-') 180 throw new NumberFormatException (Messages.getMessage("badDate00")); 181 182 try 183 { 184 if (source.length() > 10) 185 { 186 zonePortion = source.substring(10); 187 188 synchronized (zuluUTC) 189 { 190 result = zuluUTC.parse(source.substring(0, 10)); 191 } 192 int offset = adjustByZone(result, zonePortion); 193 TimeZone timeZone = (offset == 0) ? TimeZone.getTimeZone("GMT") 194 : new SimpleTimeZone (offset, "GMT offset"); 195 calendar = Calendar.getInstance(timeZone); 196 } 197 else 198 { 199 synchronized (zulu) 200 { 201 result = zulu.parse(source); 202 } 203 calendar = Calendar.getInstance(); 204 } 205 } 206 catch (NumberFormatException e) 207 { 208 throw e; 209 } 210 catch (Exception e) 211 { 212 throw new NumberFormatException (e.toString()); 213 } 214 215 calendar.setTime(result); 216 217 if (bc) 219 { 220 calendar.set(Calendar.ERA, GregorianCalendar.BC); 221 result = calendar.getTime(); 222 } 223 224 if (super.javaType == Calendar .class) 225 { 226 return calendar; 227 } 228 229 return result; 230 } 231 } 232 | Popular Tags |