diff --git a/src/main/java/org/apache/xmlbeans/impl/richParser/XMLStreamReaderExtImpl.java b/src/main/java/org/apache/xmlbeans/impl/richParser/XMLStreamReaderExtImpl.java
index 6b80189bf..c015a3f0b 100644
--- a/src/main/java/org/apache/xmlbeans/impl/richParser/XMLStreamReaderExtImpl.java
+++ b/src/main/java/org/apache/xmlbeans/impl/richParser/XMLStreamReaderExtImpl.java
@@ -182,11 +182,11 @@ public InputStream getBase64Value()
throws XMLStreamException, InvalidLexicalValueException {
_charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
String text = _charSeq.toString();
- byte[] buf = Base64.getMimeDecoder().decode(text.getBytes(StandardCharsets.ISO_8859_1));
- if (buf != null) {
+ try {
+ byte[] buf = Base64.getMimeDecoder().decode(text.getBytes(StandardCharsets.ISO_8859_1));
return new ByteArrayInputStream(buf);
- } else {
- throw new InvalidLexicalValueException("invalid base64Binary value", _charSeq.getLocation());
+ } catch (IllegalArgumentException e) {
+ throw new InvalidLexicalValueException("invalid base64Binary value", e, _charSeq.getLocation());
}
}
@@ -332,11 +332,11 @@ public InputStream getAttributeHexBinaryValue(int index) throws XMLStreamExcepti
public InputStream getAttributeBase64Value(int index) throws XMLStreamException {
String text = _charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM).toString();
- byte[] buf = Base64.getMimeDecoder().decode(text.getBytes(StandardCharsets.ISO_8859_1));
- if (buf != null) {
+ try {
+ byte[] buf = Base64.getMimeDecoder().decode(text.getBytes(StandardCharsets.ISO_8859_1));
return new ByteArrayInputStream(buf);
- } else {
- throw new InvalidLexicalValueException("invalid base64Binary value", _charSeq.getLocation());
+ } catch (IllegalArgumentException e) {
+ throw new InvalidLexicalValueException("invalid base64Binary value", e, _charSeq.getLocation());
}
}
@@ -486,11 +486,11 @@ public InputStream getAttributeHexBinaryValue(String uri, String local) throws X
public InputStream getAttributeBase64Value(String uri, String local) throws XMLStreamException {
CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
String text = cs.toString();
- byte[] buf = Base64.getMimeDecoder().decode(text.getBytes(StandardCharsets.ISO_8859_1));
- if (buf != null) {
+ try {
+ byte[] buf = Base64.getMimeDecoder().decode(text.getBytes(StandardCharsets.ISO_8859_1));
return new ByteArrayInputStream(buf);
- } else {
- throw new InvalidLexicalValueException("invalid base64Binary value", _charSeq.getLocation());
+ } catch (IllegalArgumentException e) {
+ throw new InvalidLexicalValueException("invalid base64Binary value", e, _charSeq.getLocation());
}
}
diff --git a/src/test/java/misc/checkin/RichParserTests.java b/src/test/java/misc/checkin/RichParserTests.java
index 0d9be40c9..f0c1049e5 100755
--- a/src/test/java/misc/checkin/RichParserTests.java
+++ b/src/test/java/misc/checkin/RichParserTests.java
@@ -16,6 +16,7 @@
package misc.checkin;
import org.apache.xmlbeans.*;
+import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
import org.apache.xmlbeans.impl.richParser.XMLStreamReaderExt;
import org.apache.xmlbeans.impl.richParser.XMLStreamReaderExtImpl;
import org.junit.jupiter.api.Test;
@@ -36,6 +37,7 @@
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
/**
@@ -66,6 +68,34 @@ void testPrimitiveTypes() throws Exception {
}
}
+ @Test
+ void testInvalidBase64ThrowsInvalidLexicalValue() throws Exception {
+ // "A" is a single base64 char, the MIME decoder rejects it with
+ // IllegalArgumentException. The rich parser must surface that as the
+ // documented InvalidLexicalValueException, like the other getters.
+ XMLStreamReaderExt elem = atFirstStartElement("A");
+ assertThrows(InvalidLexicalValueException.class, elem::getBase64Value);
+
+ XMLStreamReaderExt attByIndex = atFirstStartElement("");
+ assertThrows(InvalidLexicalValueException.class, () -> attByIndex.getAttributeBase64Value(0));
+
+ XMLStreamReaderExt attByName = atFirstStartElement("");
+ assertThrows(InvalidLexicalValueException.class, () -> attByName.getAttributeBase64Value("", "b"));
+ }
+
+ private static XMLStreamReaderExt atFirstStartElement(String xml) throws Exception {
+ XMLStreamReader xsr = XmlObject.Factory.parse(xml).newXMLStreamReader();
+ XMLStreamReaderExt ext = new XMLStreamReaderExtImpl(xsr);
+ int evt = ext.getEventType();
+ while (evt != XMLEvent.START_ELEMENT && ext.hasNext()) {
+ evt = ext.next();
+ }
+ if (evt != XMLEvent.START_ELEMENT) {
+ throw new IllegalStateException("no start element in: " + xml);
+ }
+ return ext;
+ }
+
private static final String[] strings = {
" this is a long string ... in attribute ",
" this is a long string\n... in text "};