Skip to content
Closed
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 @@ -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());
}
}

Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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());
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/test/java/misc/checkin/RichParserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,7 @@
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;


/**
Expand Down Expand Up @@ -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>A</a>");
assertThrows(InvalidLexicalValueException.class, elem::getBase64Value);

XMLStreamReaderExt attByIndex = atFirstStartElement("<a b=\"A\"/>");
assertThrows(InvalidLexicalValueException.class, () -> attByIndex.getAttributeBase64Value(0));

XMLStreamReaderExt attByName = atFirstStartElement("<a b=\"A\"/>");
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 "};
Expand Down