1 16 17 package org.apache.axis.encoding.ser; 18 19 import org.apache.axis.utils.Messages; 20 21 import javax.xml.namespace.QName ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Calendar ; 24 import java.util.Date ; 25 import java.util.GregorianCalendar ; 26 import java.util.TimeZone ; 27 28 35 public class CalendarDeserializer extends SimpleDeserializer { 36 37 private static SimpleDateFormat zulu = 38 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 39 41 static { 42 zulu.setTimeZone(TimeZone.getTimeZone("GMT")); 43 } 44 45 49 public CalendarDeserializer(Class javaType, QName xmlType) { 50 super(javaType, xmlType); 51 } 52 53 57 public Object makeValue(String source) { 58 Calendar calendar = Calendar.getInstance(); 59 Date date; 60 boolean bc = false; 61 62 if (source == null || source.length() == 0) { 64 throw new NumberFormatException ( 65 Messages.getMessage("badDateTime00")); 66 } 67 if (source.charAt(0) == '+') { 68 source = source.substring(1); 69 } 70 if (source.charAt(0) == '-') { 71 source = source.substring(1); 72 bc = true; 73 } 74 if (source.length() < 19) { 75 throw new NumberFormatException ( 76 Messages.getMessage("badDateTime00")); 77 } 78 if (source.charAt(4) != '-' || source.charAt(7) != '-' || 79 source.charAt(10) != 'T') { 80 throw new NumberFormatException (Messages.getMessage("badDate00")); 81 } 82 if (source.charAt(13) != ':' || source.charAt(16) != ':') { 83 throw new NumberFormatException (Messages.getMessage("badTime00")); 84 } 85 try { 87 synchronized (zulu) { 88 date = zulu.parse(source.substring(0, 19) + ".000Z"); 89 } 90 } catch (Exception e) { 91 throw new NumberFormatException (e.toString()); 92 } 93 int pos = 19; 94 95 if (pos < source.length() && source.charAt(pos) == '.') { 97 int milliseconds = 0; 98 int start = ++pos; 99 while (pos < source.length() && 100 Character.isDigit(source.charAt(pos))) { 101 pos++; 102 } 103 String decimal = source.substring(start, pos); 104 if (decimal.length() == 3) { 105 milliseconds = Integer.parseInt(decimal); 106 } else if (decimal.length() < 3) { 107 milliseconds = Integer.parseInt((decimal + "000") 108 .substring(0, 3)); 109 } else { 110 milliseconds = Integer.parseInt(decimal.substring(0, 3)); 111 if (decimal.charAt(3) >= '5') { 112 ++milliseconds; 113 } 114 } 115 116 date.setTime(date.getTime() + milliseconds); 118 } 119 120 if (pos + 5 < source.length() && 122 (source.charAt(pos) == '+' || (source.charAt(pos) == '-'))) { 123 if (!Character.isDigit(source.charAt(pos + 1)) || 124 !Character.isDigit(source.charAt(pos + 2)) || 125 source.charAt(pos + 3) != ':' || 126 !Character.isDigit(source.charAt(pos + 4)) || 127 !Character.isDigit(source.charAt(pos + 5))) { 128 throw new NumberFormatException ( 129 Messages.getMessage("badTimezone00")); 130 } 131 int hours = (source.charAt(pos + 1) - '0') * 10 132 + source.charAt(pos + 2) - '0'; 133 int mins = (source.charAt(pos + 4) - '0') * 10 134 + source.charAt(pos + 5) - '0'; 135 int milliseconds = (hours * 60 + mins) * 60 * 1000; 136 137 if (source.charAt(pos) == '+') { 139 milliseconds = -milliseconds; 140 } 141 date.setTime(date.getTime() + milliseconds); 142 pos += 6; 143 } 144 if (pos < source.length() && source.charAt(pos) == 'Z') { 145 pos++; 146 calendar.setTimeZone(TimeZone.getTimeZone("GMT")); 147 } 148 if (pos < source.length()) { 149 throw new NumberFormatException (Messages.getMessage("badChars00")); 150 } 151 calendar.setTime(date); 152 153 if (bc) { 155 calendar.set(Calendar.ERA, GregorianCalendar.BC); 156 } 157 if (super.javaType == Date .class) { 158 return date; 159 } else { 160 return calendar; 161 } 162 } 163 } 164 | Popular Tags |