if (pattern == null) {
try {
pattern = Pattern.compile(patternString);
} catch (PatternSyntaxException e) {
// This should never happen
throw new ParsingException("unexpected pattern match error");
}
}
// See if the value matches the pattern.
Matcher matcher = pattern.matcher(value);
boolean matches = matcher.matches();
// If not, syntax error!
if (!matches) {
throw new ParsingException("Syntax error in dayTimeDuration");
}
// If the negative group matched, the value is negative.
if (matcher.start(GROUP_SIGN) != -1)
negative = true;
try {
// If the days group matched, parse that value.
days = parseGroup(matcher, GROUP_DAYS);
// If the hours group matched, parse that value.
hours = parseGroup(matcher, GROUP_HOURS);
// If the minutes group matched, parse that value.
minutes = parseGroup(matcher, GROUP_MINUTES);
// If the seconds group matched, parse that value.
seconds = parseGroup(matcher, GROUP_SECONDS);
// Special handling for fractional seconds, since
// they can have any resolution.
if (matcher.start(GROUP_NANOSECONDS) != -1) {
String nanosecondString = matcher.group(GROUP_NANOSECONDS);
// If there are less than 9 digits in the fractional seconds,
// pad with zeros on the right so it's nanoseconds.
while (nanosecondString.length() < 9)
nanosecondString += "0";
// If there are more than 9 digits in the fractional seconds,
// drop the least significant digits.
if (nanosecondString.length() > 9) {
nanosecondString = nanosecondString.substring(0, 9);
}
nanoseconds = Integer.parseInt(nanosecondString);
}
} catch (NumberFormatException e) {
// If we run into a number that's too big to be a long
// that's an error. Really, it's a processing error,
// since one can argue that we should handle that.
throw e;
}
// Here's a requirement that's not checked for in the pattern.
// The designator 'T' must be absent if all the time
// items are absent. So the string can't end in 'T'.
// Note that we don't have to worry about a zero length
// string, since the pattern won't allow that.
if (value.charAt(value.length()-1) == 'T')
throw new ParsingException("'T' must be absent if all" +
"time items are absent");
// If parsing went OK, create a new DayTimeDurationAttribute object and
// return it.
return new DayTimeDurationAttribute(negative, days, hours, minutes,