| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Callable; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.om.Sequence; |
| 13 |
import net.sf.saxon.om.ZeroOrOne; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.value.*; |
| 16 |
|
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class ParseIetfDate extends SystemFunction implements Callable { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public ZeroOrOne<DateTimeValue> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 36 |
StringValue stringValue = (StringValue) arguments[0].head(); |
| 37 |
if (stringValue == null) { |
| 38 |
return ZeroOrOne.empty(); |
| 39 |
} |
| 40 |
return new ZeroOrOne<>(parse(stringValue.getStringValue(), context)); |
| 41 |
} |
| 42 |
|
| 43 |
private String[] dayNames = new String[]{ |
| 44 |
"Mon","Tue","Wed","Thu","Fri","Sat","Sun","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" |
| 45 |
}; |
| 46 |
|
| 47 |
private boolean isDayName(String string){ |
| 48 |
for (String s: dayNames){ |
| 49 |
if (s.equalsIgnoreCase(string)){ |
| 50 |
return true; |
| 51 |
} |
| 52 |
} |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
private String[] monthNames = new String[]{ |
| 57 |
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 58 |
}; |
| 59 |
|
| 60 |
private boolean isMonthName(String string){ |
| 61 |
for (String s: monthNames){ |
| 62 |
if (s.equalsIgnoreCase(string)){ |
| 63 |
return true; |
| 64 |
} |
| 65 |
} |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
private byte getMonthNumber(String string){ |
| 70 |
if ("Jan".equalsIgnoreCase(string)){ |
| 71 |
return (byte) 1; |
| 72 |
} else if ("Feb".equalsIgnoreCase(string)){ |
| 73 |
return (byte) 2; |
| 74 |
} else if ("Mar".equalsIgnoreCase(string)){ |
| 75 |
return (byte) 3; |
| 76 |
} else if ("Apr".equalsIgnoreCase(string)){ |
| 77 |
return (byte) 4; |
| 78 |
} else if ("May".equalsIgnoreCase(string)){ |
| 79 |
return (byte) 5; |
| 80 |
} else if ("Jun".equalsIgnoreCase(string)){ |
| 81 |
return (byte) 6; |
| 82 |
} else if ("Jul".equalsIgnoreCase(string)){ |
| 83 |
return (byte) 7; |
| 84 |
} else if ("Aug".equalsIgnoreCase(string)){ |
| 85 |
return (byte) 8; |
| 86 |
} else if ("Sep".equalsIgnoreCase(string)){ |
| 87 |
return (byte) 9; |
| 88 |
} else if ("Oct".equalsIgnoreCase(string)){ |
| 89 |
return (byte) 10; |
| 90 |
} else if ("Nov".equalsIgnoreCase(string)){ |
| 91 |
return (byte) 11; |
| 92 |
} else if ("Dec".equalsIgnoreCase(string)){ |
| 93 |
return (byte) 12; |
| 94 |
} |
| 95 |
return (byte) 0; |
| 96 |
} |
| 97 |
|
| 98 |
private int requireDSep(List<String> tokens, int i, String input) throws XPathException { |
| 99 |
boolean found = false; |
| 100 |
if (" ".equals(tokens.get(i))){ |
| 101 |
i++; |
| 102 |
found = true; |
| 103 |
} |
| 104 |
if ("-".equals(tokens.get(i))){ |
| 105 |
i++; |
| 106 |
found = true; |
| 107 |
} |
| 108 |
if (" ".equals(tokens.get(i))){ |
| 109 |
i++; |
| 110 |
found = true; |
| 111 |
} |
| 112 |
if (!found) { |
| 113 |
badDate("Date separator missing", input); |
| 114 |
} |
| 115 |
return i; |
| 116 |
} |
| 117 |
|
| 118 |
private static void badDate(String msg, String value) throws XPathException{ |
| 119 |
XPathException err = new XPathException( |
| 120 |
"Invalid IETF date value " + value + " (" + msg + ")"); |
| 121 |
err.setErrorCode("FORG0010"); |
| 122 |
throw err; |
| 123 |
} |
| 124 |
|
| 125 |
private String[] timezoneNames = new String[]{ |
| 126 |
"UT","UTC","GMT","EST","EDT","CST","CDT","MST","MDT","PST","PDT" |
| 127 |
}; |
| 128 |
|
| 129 |
private boolean isTimezoneName(String string) { |
| 130 |
for (String s: timezoneNames){ |
| 131 |
if (s.equalsIgnoreCase(string)){ |
| 132 |
return true; |
| 133 |
} |
| 134 |
} |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
private int getTimezoneOffsetFromName(String string){ |
| 139 |
if ("UT".equalsIgnoreCase(string)|"UTC".equalsIgnoreCase(string)|"GMT".equalsIgnoreCase(string)){ |
| 140 |
return 0; |
| 141 |
} else if ("EST".equalsIgnoreCase(string)){ |
| 142 |
return -5*60; |
| 143 |
} else if ("EDT".equalsIgnoreCase(string)){ |
| 144 |
return -4*60; |
| 145 |
} else if ("CST".equalsIgnoreCase(string)){ |
| 146 |
return -6*60; |
| 147 |
} else if ("CDT".equalsIgnoreCase(string)){ |
| 148 |
return -5*60; |
| 149 |
} else if ("MST".equalsIgnoreCase(string)){ |
| 150 |
return -7*60; |
| 151 |
} else if ("MDT".equalsIgnoreCase(string)){ |
| 152 |
return -6*60; |
| 153 |
} else if ("PST".equalsIgnoreCase(string)){ |
| 154 |
return -8*60; |
| 155 |
} else if ("PDT".equalsIgnoreCase(string)){ |
| 156 |
return -7*60; |
| 157 |
} |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public DateTimeValue parse(String input, XPathContext context) throws XPathException { |
| 171 |
List<String> tokens = tokenize(input); |
| 172 |
int year = 0; |
| 173 |
byte month = 0; |
| 174 |
byte day = 0; |
| 175 |
List<TimeValue> timeValue = new ArrayList<>(); |
| 176 |
int i = 0; |
| 177 |
String currentToken = tokens.get(i); |
| 178 |
if (currentToken.matches("[A-Za-z]+") && isDayName(currentToken)){ |
| 179 |
currentToken = tokens.get(++i); |
| 180 |
if (",".equals(currentToken)){ |
| 181 |
currentToken = tokens.get(++i); |
| 182 |
} |
| 183 |
if (!" ".equals(currentToken)){ |
| 184 |
badDate("Space missing after day name", input); |
| 185 |
} |
| 186 |
currentToken = tokens.get(++i); |
| 187 |
|
| 188 |
} |
| 189 |
if (isMonthName(currentToken)){ |
| 190 |
month = getMonthNumber(currentToken); |
| 191 |
i = requireDSep(tokens, i+1, input); |
| 192 |
currentToken = tokens.get(i); |
| 193 |
if (!currentToken.matches("[0-9]+")){ |
| 194 |
badDate("Day number expected after month name", input); |
| 195 |
} |
| 196 |
if (currentToken.length() > 2){ |
| 197 |
badDate("Day number exceeds two digits", input); |
| 198 |
} |
| 199 |
day = (byte) Integer.parseInt(currentToken); |
| 200 |
currentToken = tokens.get(++i); |
| 201 |
if (!" ".equals(currentToken)){ |
| 202 |
badDate("Space missing after day number", input); |
| 203 |
} |
| 204 |
|
| 205 |
i = parseTime(tokens, ++i, timeValue, input); |
| 206 |
currentToken = tokens.get(++i); |
| 207 |
if (!" ".equals(currentToken)){ |
| 208 |
badDate("Space missing after time string", input); |
| 209 |
} |
| 210 |
currentToken = tokens.get(++i); |
| 211 |
if (currentToken.matches("[0-9]+")) { |
| 212 |
year = checkTwoOrFourDigits(input, currentToken); |
| 213 |
} else { |
| 214 |
badDate("Year number expected after time", input); |
| 215 |
} |
| 216 |
} |
| 217 |
else if (currentToken.matches("[0-9]+")) { |
| 218 |
if (currentToken.length() > 2){ |
| 219 |
badDate("First number in string expected to be day in two digits", input); |
| 220 |
} |
| 221 |
day = (byte) Integer.parseInt(currentToken); |
| 222 |
i = requireDSep(tokens, ++i, input); |
| 223 |
currentToken = tokens.get(i); |
| 224 |
if (!isMonthName(currentToken)){ |
| 225 |
badDate("Abbreviated month name expected after day number", input); |
| 226 |
} |
| 227 |
month = getMonthNumber(currentToken); |
| 228 |
i = requireDSep(tokens, ++i, input); |
| 229 |
currentToken = tokens.get(i); |
| 230 |
if (currentToken.matches("[0-9]+")) { |
| 231 |
year = checkTwoOrFourDigits(input, currentToken); |
| 232 |
} else { |
| 233 |
badDate("Year number expected after month name", input); |
| 234 |
} |
| 235 |
currentToken = tokens.get(++i); |
| 236 |
if (!" ".equals(currentToken)){ |
| 237 |
badDate("Space missing after year number", input); |
| 238 |
} |
| 239 |
|
| 240 |
i = parseTime(tokens, ++i, timeValue, input); |
| 241 |
} |
| 242 |
else { |
| 243 |
badDate("String expected to begin with month name or day name (or day number)", input); |
| 244 |
} |
| 245 |
if (!GDateValue.isValidDate(year, month, day)) { |
| 246 |
badDate("Date is not valid", input); |
| 247 |
} |
| 248 |
currentToken = tokens.get(++i); |
| 249 |
if (!currentToken.equals(EOF)){ |
| 250 |
badDate("Extra content found in string after date", input); |
| 251 |
} |
| 252 |
DateValue date = new DateValue(year, month, day); |
| 253 |
TimeValue time = timeValue.get(0); |
| 254 |
if (time.getHour() == 24) { |
| 255 |
date = DateValue.tomorrow(date.getYear(), date.getMonth(), date.getDay()); |
| 256 |
time = new TimeValue((byte) 0, (byte) 0, (byte) 0, 0, time.getTimezoneInMinutes()); |
| 257 |
} |
| 258 |
return DateTimeValue.makeDateTimeValue(date, time); |
| 259 |
} |
| 260 |
|
| 261 |
private int checkTwoOrFourDigits(String input, String currentToken) throws XPathException { |
| 262 |
int year; |
| 263 |
if (currentToken.length() == 4){ |
| 264 |
year = Integer.parseInt(currentToken); |
| 265 |
} else if (currentToken.length() == 2){ |
| 266 |
year = Integer.parseInt(currentToken) +1900; |
| 267 |
} else { |
| 268 |
badDate("Year number must be two or four digits", input); |
| 269 |
year = 0; |
| 270 |
} |
| 271 |
return year; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public int parseTime(List<String> tokens, int currentPosition, List<TimeValue> result, String input) throws XPathException{ |
| 284 |
byte hour; |
| 285 |
byte minute; |
| 286 |
byte second = 0; |
| 287 |
int microsecond = 0; |
| 288 |
int tz = 0; |
| 289 |
int i = currentPosition; |
| 290 |
int n = currentPosition; |
| 291 |
StringBuilder currentToken = new StringBuilder(tokens.get(i)); |
| 292 |
if (!currentToken.toString().matches("[0-9]+")){ |
| 293 |
badDate("Hour number expected", input); |
| 294 |
} |
| 295 |
if (currentToken.length() > 2){ |
| 296 |
badDate("Hour number exceeds two digits", input); |
| 297 |
} |
| 298 |
hour = (byte) Integer.parseInt(currentToken.toString()); |
| 299 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 300 |
if (!":".equals(currentToken.toString())){ |
| 301 |
badDate("Separator ':' missing after hour", input); |
| 302 |
} |
| 303 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 304 |
if (!currentToken.toString().matches("[0-9]+")){ |
| 305 |
badDate("Minutes expected after hour", input); |
| 306 |
} |
| 307 |
if (currentToken.length() != 2){ |
| 308 |
badDate("Minutes must be exactly two digits", input); |
| 309 |
} |
| 310 |
minute = (byte) Integer.parseInt(currentToken.toString()); |
| 311 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 312 |
boolean finished = false; |
| 313 |
|
| 314 |
if (currentToken.toString().equals(EOF)){ |
| 315 |
|
| 316 |
n = i - 1; |
| 317 |
finished = true; |
| 318 |
} |
| 319 |
else if (":".equals(currentToken.toString())){ |
| 320 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 321 |
if (!currentToken.toString().matches("[0-9]+")){ |
| 322 |
badDate("Seconds expected after ':' separator after minutes", input); |
| 323 |
} |
| 324 |
if (currentToken.length() != 2){ |
| 325 |
badDate("Seconds number must have exactly two digits (before decimal point)", input); |
| 326 |
} |
| 327 |
second = (byte) Integer.parseInt(currentToken.toString()); |
| 328 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 329 |
if (currentToken.toString().equals(EOF)){ |
| 330 |
|
| 331 |
n = i - 1; |
| 332 |
finished = true; |
| 333 |
} |
| 334 |
else if (".".equals(currentToken.toString())){ |
| 335 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 336 |
if (!currentToken.toString().matches("[0-9]+")){ |
| 337 |
badDate("Fractional part of seconds expected after decimal point", input); |
| 338 |
} |
| 339 |
int len = Math.min(6, currentToken.length()); |
| 340 |
currentToken = new StringBuilder(currentToken.substring(0, len)); |
| 341 |
while (currentToken.length() < 6){ |
| 342 |
currentToken.append("0"); |
| 343 |
} |
| 344 |
microsecond = Integer.parseInt(currentToken.toString()); |
| 345 |
if (i < tokens.size() - 1) { |
| 346 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
if (!finished) { |
| 351 |
if (" ".equals(currentToken.toString())) { |
| 352 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 353 |
if (currentToken.toString().matches("[0-9]+")) { |
| 354 |
|
| 355 |
n = i - 2; |
| 356 |
finished = true; |
| 357 |
} |
| 358 |
} |
| 359 |
if (!finished) { |
| 360 |
if (currentToken.toString().matches("[A-Za-z]+")){ |
| 361 |
if (!isTimezoneName(currentToken.toString())){ |
| 362 |
badDate("Timezone name not recognised", input); |
| 363 |
} |
| 364 |
tz = getTimezoneOffsetFromName(currentToken.toString()); |
| 365 |
n = i; |
| 366 |
finished = true; |
| 367 |
|
| 368 |
} else if ("+".equals(currentToken.toString())|"-".equals(currentToken.toString())) { |
| 369 |
String sign = currentToken.toString(); |
| 370 |
int tzOffsetHours = 0; |
| 371 |
int tzOffsetMinutes = 0; |
| 372 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 373 |
if (!currentToken.toString().matches("[0-9]+")){ |
| 374 |
badDate("Parsing timezone offset, number expected after '" + sign + "'", input); |
| 375 |
} |
| 376 |
int tLength = currentToken.length(); |
| 377 |
if (tLength > 4){ |
| 378 |
badDate("Timezone offset does not have the correct number of digits", input); |
| 379 |
} |
| 380 |
else if (tLength >= 3){ |
| 381 |
tzOffsetHours = Integer.parseInt(currentToken.substring(0, tLength - 2)); |
| 382 |
tzOffsetMinutes = Integer.parseInt(currentToken.substring(tLength - 2, tLength)); |
| 383 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 384 |
} |
| 385 |
else { |
| 386 |
tzOffsetHours = Integer.parseInt(currentToken.toString()); |
| 387 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 388 |
if (":".equals(currentToken.toString())) { |
| 389 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 390 |
if (currentToken.toString().matches("[0-9]+")){ |
| 391 |
if (currentToken.length() != 2) { |
| 392 |
badDate("Parsing timezone offset, minutes must be two digits", input); |
| 393 |
} else { |
| 394 |
tzOffsetMinutes = Integer.parseInt(currentToken.toString()); |
| 395 |
} |
| 396 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
if (tzOffsetMinutes > 59) { |
| 401 |
badDate("Timezone offset minutes out of range", input); |
| 402 |
} |
| 403 |
tz = tzOffsetHours * 60 + tzOffsetMinutes; |
| 404 |
if (sign.equals("-")){ |
| 405 |
tz = -tz; |
| 406 |
} |
| 407 |
if (currentToken.toString().equals(EOF)){ |
| 408 |
n = i - 1; |
| 409 |
finished = true; |
| 410 |
} |
| 411 |
else if (" ".equals(currentToken.toString())) { |
| 412 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 413 |
if (currentToken.toString().matches("[0-9]+")) { |
| 414 |
|
| 415 |
n = i - 2; |
| 416 |
finished = true; |
| 417 |
} |
| 418 |
} |
| 419 |
if (!finished && "(".equals(currentToken.toString())) { |
| 420 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 421 |
if (" ".equals(currentToken.toString())) { |
| 422 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 423 |
} |
| 424 |
if (!currentToken.toString().matches("[A-Za-z]+")) { |
| 425 |
badDate("Timezone name expected after '('", input); |
| 426 |
} |
| 427 |
else if (currentToken.toString().matches("[A-Za-z]+")) { |
| 428 |
if (!isTimezoneName(currentToken.toString())){ |
| 429 |
badDate("Timezone name not recognised", input); |
| 430 |
} |
| 431 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 432 |
} |
| 433 |
if (" ".equals(currentToken.toString())) { |
| 434 |
currentToken = new StringBuilder(tokens.get(++i)); |
| 435 |
} |
| 436 |
if (!")".equals(currentToken.toString())){ |
| 437 |
badDate("Expected ')' after timezone name", input); |
| 438 |
} |
| 439 |
n = i; |
| 440 |
finished = true; |
| 441 |
} |
| 442 |
else if (!finished) { |
| 443 |
badDate("Unexpected content after timezone offset", input); |
| 444 |
} |
| 445 |
} else { |
| 446 |
badDate("Unexpected content in time (after minutes)", input); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
if (!finished) { |
| 451 |
throw new AssertionError("Should have finished"); |
| 452 |
} |
| 453 |
if (!isValidTime(hour, minute, second, microsecond, tz)) { |
| 454 |
badDate("Time/timezone is not valid", input); |
| 455 |
} |
| 456 |
TimeValue timeValue = new TimeValue(hour, minute, second, microsecond*1000, tz, ""); |
| 457 |
result.add(timeValue); |
| 458 |
return n; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public static boolean isValidTime(int hour, int minute, int second, int microsecond, int tz) { |
| 473 |
return (hour >= 0 && hour <= 23 && minute >= 0 && minute < 60 && second >= 0 && second < 60 |
| 474 |
&& microsecond >= 0 && microsecond < 1000000 |
| 475 |
|| hour == 24 && minute == 0 && second == 0 && microsecond == 0) |
| 476 |
&& tz >= -14 * 60 && tz <= 14 * 60; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
private List<String> tokenize(String input) throws XPathException{ |
| 482 |
List<String> tokens = new ArrayList<>(); |
| 483 |
input = input.trim(); |
| 484 |
if (input.isEmpty()){ |
| 485 |
badDate("Input is empty",input); |
| 486 |
return tokens; |
| 487 |
} |
| 488 |
int i = 0; |
| 489 |
input = input + (char) 0; |
| 490 |
while (true) { |
| 491 |
char c = input.charAt(i); |
| 492 |
if (c == 0) { |
| 493 |
tokens.add(EOF); |
| 494 |
return tokens; |
| 495 |
} |
| 496 |
if (Whitespace.isWhite(c)){ |
| 497 |
int j = i; |
| 498 |
while (Whitespace.isWhite(input.charAt(j++))){} |
| 499 |
tokens.add(" "); |
| 500 |
i = j-1; |
| 501 |
} else if (Character.isLetter(c)){ |
| 502 |
int j = i; |
| 503 |
while (Character.isLetter(input.charAt(j++))){} |
| 504 |
tokens.add(input.substring(i,j-1)); |
| 505 |
i = j-1; |
| 506 |
} else if (Character.isDigit(c)){ |
| 507 |
int j = i; |
| 508 |
while (Character.isDigit(input.charAt(j++))){} |
| 509 |
tokens.add(input.substring(i,j-1)); |
| 510 |
i = j-1; |
| 511 |
} else { |
| 512 |
tokens.add(input.substring(i,i+1)); |
| 513 |
i++; |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
private static final String EOF = ""; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|