1 17 package org.apache.ws.jaxme.xs.util; 18 19 import java.text.FieldPosition ; 20 import java.text.Format ; 21 import java.text.ParsePosition ; 22 import java.util.Calendar ; 23 import java.util.TimeZone ; 24 25 26 31 public class XsDateTimeFormat extends Format { 32 final boolean parseDate; 33 final boolean parseTime; 34 35 XsDateTimeFormat(boolean pParseDate, boolean pParseTime) { 36 parseDate = pParseDate; 37 parseTime = pParseTime; 38 } 39 40 42 public XsDateTimeFormat() { 43 this(true, true); 44 } 45 46 private int parseInt(String pString, int pOffset, StringBuffer pDigits) { 47 int length = pString.length(); 48 pDigits.setLength(0); 49 while (pOffset < length) { 50 char c = pString.charAt(pOffset); 51 if (Character.isDigit(c)) { 52 pDigits.append(c); 53 ++pOffset; 54 } else { 55 break; 56 } 57 } 58 return pOffset; 59 } 60 61 public Object parseObject(String pString, ParsePosition pParsePosition) { 62 if (pString == null) { 63 throw new NullPointerException ("The String argument must not be null."); 64 } 65 if (pParsePosition == null) { 66 throw new NullPointerException ("The ParsePosition argument must not be null."); 67 } 68 int offset = pParsePosition.getIndex(); 69 int length = pString.length(); 70 71 boolean isMinus = false; 72 StringBuffer digits = new StringBuffer (); 73 int year, month, mday; 74 if (parseDate) { 75 if (offset < length) { 77 char c = pString.charAt(offset); 78 if (c == '+') { 79 ++offset; 80 } else if (c == '-') { 81 ++offset; 82 isMinus = true; 83 } 84 } 85 86 offset = parseInt(pString, offset, digits); 87 if (digits.length() < 4) { 88 pParsePosition.setErrorIndex(offset); 89 return null; 90 } 91 year = Integer.parseInt(digits.toString()); 92 93 if (offset < length && pString.charAt(offset) == '-') { 94 ++offset; 95 } else { 96 pParsePosition.setErrorIndex(offset); 97 return null; 98 } 99 100 offset = parseInt(pString, offset, digits); 101 if (digits.length() != 2) { 102 pParsePosition.setErrorIndex(offset); 103 return null; 104 } 105 month = Integer.parseInt(digits.toString()); 106 107 if (offset < length && pString.charAt(offset) == '-') { 108 ++offset; 109 } else { 110 pParsePosition.setErrorIndex(offset); 111 return null; 112 } 113 114 offset = parseInt(pString, offset, digits); 115 if (digits.length() != 2) { 116 pParsePosition.setErrorIndex(offset); 117 return null; 118 } 119 mday = Integer.parseInt(digits.toString()); 120 121 if (parseTime) { 122 if (offset < length && pString.charAt(offset) == 'T') { 123 ++offset; 124 } else { 125 pParsePosition.setErrorIndex(offset); 126 return null; 127 } 128 } 129 } else { 130 year = month = mday = 0; 131 } 132 133 int hour, minute, second, millis; 134 if (parseTime) { 135 offset = parseInt(pString, offset, digits); 136 if (digits.length() != 2) { 137 pParsePosition.setErrorIndex(offset); 138 return null; 139 } 140 hour = Integer.parseInt(digits.toString()); 141 142 if (offset < length && pString.charAt(offset) == ':') { 143 ++offset; 144 } else { 145 pParsePosition.setErrorIndex(offset); 146 return null; 147 } 148 149 offset = parseInt(pString, offset, digits); 150 if (digits.length() != 2) { 151 pParsePosition.setErrorIndex(offset); 152 return null; 153 } 154 minute = Integer.parseInt(digits.toString()); 155 156 if (offset < length && pString.charAt(offset) == ':') { 157 ++offset; 158 } else { 159 pParsePosition.setErrorIndex(offset); 160 return null; 161 } 162 163 offset = parseInt(pString, offset, digits); 164 if (digits.length() != 2) { 165 pParsePosition.setErrorIndex(offset); 166 return null; 167 } 168 second = Integer.parseInt(digits.toString()); 169 170 if (offset < length && pString.charAt(offset) == '.') { 171 ++offset; 172 offset = parseInt(pString, offset, digits); 173 if (digits.length() > 0) { 174 millis = Integer.parseInt(digits.toString()); 175 } else { 176 millis = 0; 177 } 178 } else { 179 millis = 0; 180 } 181 } else { 182 hour = minute = second = millis = 0; 183 } 184 185 digits.setLength(0); 186 digits.append("GMT"); 187 if (offset < length) { 188 char c = pString.charAt(offset); 189 if (c == 'Z') { 190 ++offset; 192 } else if (c == '+' || c == '-') { 193 digits.append(c); 194 ++offset; 195 for (int i = 0; i < 5; i++) { 196 if (offset >= length) { 197 pParsePosition.setErrorIndex(offset); 198 return null; 199 } 200 c = pString.charAt(offset); 201 if ((i != 2 && Character.isDigit(c)) || 202 (i == 2 && c == ':')) { 203 digits.append(c); 204 } else { 205 pParsePosition.setErrorIndex(offset); 206 return null; 207 } 208 ++offset; 209 } 210 } 211 } 212 213 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(digits.toString())); 214 cal.set(isMinus ? -year : year, parseDate ? month-1 : month, mday, hour, minute, second); 215 cal.set(Calendar.MILLISECOND, millis); 216 pParsePosition.setIndex(offset); 217 return cal; 218 } 219 220 private void append(StringBuffer pBuffer, int pNum, int pMinLen) { 221 String s = Integer.toString(pNum); 222 for (int i = s.length(); i < pMinLen; i++) { 223 pBuffer.append('0'); 224 } 225 pBuffer.append(s); 226 } 227 228 public StringBuffer format(Object pCalendar, StringBuffer pBuffer, FieldPosition pPos) { 229 if (pCalendar == null) { 230 throw new NullPointerException ("The Calendar argument must not be null."); 231 } 232 if (pBuffer == null) { 233 throw new NullPointerException ("The StringBuffer argument must not be null."); 234 } 235 if (pPos == null) { 236 throw new NullPointerException ("The FieldPosition argument must not be null."); 237 } 238 239 Calendar cal = (Calendar ) pCalendar; 240 if (parseDate) { 241 int year = cal.get(Calendar.YEAR); 242 if (year < 0) { 243 pBuffer.append('-'); 244 year = -year; 245 } 246 append(pBuffer, year, 4); 247 pBuffer.append('-'); 248 append(pBuffer, cal.get(Calendar.MONTH)+1, 2); 249 pBuffer.append('-'); 250 append(pBuffer, cal.get(Calendar.DAY_OF_MONTH), 2); 251 if (parseTime) { 252 pBuffer.append('T'); 253 } 254 } 255 if (parseTime) { 256 append(pBuffer, cal.get(Calendar.HOUR_OF_DAY), 2); 257 pBuffer.append(':'); 258 append(pBuffer, cal.get(Calendar.MINUTE), 2); 259 pBuffer.append(':'); 260 append(pBuffer, cal.get(Calendar.SECOND), 2); 261 int millis = cal.get(Calendar.MILLISECOND); 262 if (millis > 0) { 263 pBuffer.append('.'); 264 append(pBuffer, millis, 3); 265 } 266 } 267 TimeZone tz = cal.getTimeZone(); 268 int offset = cal.get(Calendar.ZONE_OFFSET); 270 if (tz.inDaylightTime(cal.getTime())) { 271 offset += cal.get(Calendar.DST_OFFSET); 272 } 273 if (offset == 0) { 274 pBuffer.append('Z'); 275 } else { 276 if (offset < 0) { 277 pBuffer.append('-'); 278 offset = -offset; 279 } else { 280 pBuffer.append('+'); 281 } 282 int minutes = offset / (60*1000); 283 int hours = minutes / 60; 284 minutes -= hours * 60; 285 append(pBuffer, hours, 2); 286 pBuffer.append(':'); 287 append(pBuffer, minutes, 2); 288 } 289 return pBuffer; 290 } 291 } 292 | Popular Tags |