Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public Date getDateValue()
_charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
try {
return new GDateBuilder(_charSeq).getDate();
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException | IllegalStateException e) {
throw new InvalidLexicalValueException(e, _charSeq.getLocation());
}
}
Expand Down Expand Up @@ -353,7 +353,7 @@ public Date getAttributeDateValue(int index) throws XMLStreamException {
try {
return new GDateBuilder(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM))
.getDate();
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException | IllegalStateException e) {
throw new InvalidLexicalValueException(e, _charSeq.getLocation());
}
}
Expand Down Expand Up @@ -507,7 +507,7 @@ public Date getAttributeDateValue(String uri, String local) throws XMLStreamExce
try {
CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
return new GDateBuilder(cs).getDate();
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException | IllegalStateException e) {
throw new InvalidLexicalValueException(e, _charSeq.getLocation());
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/misc/checkin/RichParserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ void testInvalidQNameThrowsInvalidLexicalValue() throws Exception {
assertEquals(new QName("urn:x", "good"), good.getQNameValue());
}

@Test
void testInvalidDateThrowsInvalidLexicalValue() throws Exception {
// getDateValue converts the parsed GDate to a java.util.Date via
// GDateBuilder.getDate(), which throws IllegalStateException - not
// IllegalArgumentException - when the value is not a complete date
// (a time/gYear/gYearMonth reaching a dateTime field) or its year is
// before the Julian epoch. The getter only caught IllegalArgumentException
// so the IllegalStateException escaped instead of the documented
// InvalidLexicalValueException the other getters surface.
XMLStreamReaderExt timeOnly = atFirstStartElement("<a>12:00:00</a>");
assertThrows(InvalidLexicalValueException.class, timeOnly::getDateValue);

XMLStreamReaderExt yearOnly = atFirstStartElement("<a>2001</a>");
assertThrows(InvalidLexicalValueException.class, yearOnly::getDateValue);

XMLStreamReaderExt ancient = atFirstStartElement("<a>-5000-01-01T00:00:00Z</a>");
assertThrows(InvalidLexicalValueException.class, ancient::getDateValue);

XMLStreamReaderExt attByIndex = atFirstStartElement("<a b='12:00:00'/>");
assertThrows(InvalidLexicalValueException.class, () -> attByIndex.getAttributeDateValue(0));

XMLStreamReaderExt attByName = atFirstStartElement("<a b='12:00:00'/>");
assertThrows(InvalidLexicalValueException.class, () -> attByName.getAttributeDateValue("", "b"));

// a well-formed dateTime still converts
XMLStreamReaderExt good = atFirstStartElement("<a>2001-11-26T21:32:52Z</a>");
assertEquals(new XmlCalendar("2001-11-26T21:32:52Z").getTime(), good.getDateValue());
}

private static XMLStreamReaderExt atFirstStartElement(String xml) throws Exception {
XMLStreamReader xsr = XmlObject.Factory.parse(xml).newXMLStreamReader();
XMLStreamReaderExt ext = new XMLStreamReaderExtImpl(xsr);
Expand Down