This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git
The following commit(s) were added to refs/heads/trunk by this push:
new c50b9d69 join text split by a pi in
ValidatingXMLStreamReader.bufferText (#140)
c50b9d69 is described below
commit c50b9d695a39d61f21b7ce7120affa1aeb99bdb7
Author: aizu-m <[email protected]>
AuthorDate: Mon Oct 19 14:47:08 2026 +0530
join text split by a pi in ValidatingXMLStreamReader.bufferText (#140)
* join text split by a pi in ValidatingXMLStreamReader.bufferText
* pin the caller-visible events around a pi in ValidatingXMLStreamReader
The packing reader is also what the caller reads, so a comment or pi
after text is now consumed into the merged CHARACTERS event while one
before any text is still passed on. Add a test that fixes that event
sequence, and mark the intended fall-through in bufferText.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: PJ Fanning <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../impl/validator/ValidatingXMLStreamReader.java | 6 +-
.../checkin/ValidatingXMLStreamReaderTests.java | 84 ++++++++++++++++++++++
2 files changed, 89 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/apache/xmlbeans/impl/validator/ValidatingXMLStreamReader.java
b/src/main/java/org/apache/xmlbeans/impl/validator/ValidatingXMLStreamReader.java
index b314d312..08b5265b 100644
---
a/src/main/java/org/apache/xmlbeans/impl/validator/ValidatingXMLStreamReader.java
+++
b/src/main/java/org/apache/xmlbeans/impl/validator/ValidatingXMLStreamReader.java
@@ -189,9 +189,13 @@ public class ValidatingXMLStreamReader
case XMLEvent.SPACE:
if (super.hasText())
_buffer.append(super.getText());
+ // fall through
case XMLEvent.COMMENT:
- //ignore
+ case XMLEvent.PROCESSING_INSTRUCTION:
+ // neither contributes character data, so text on both
+ // sides of it is one value - same as the Cur.COMMENT /
+ // Cur.PROCINST handling in the store's Validate
continue;
default:
return;
diff --git
a/src/test/java/ValidatingXSRTests/checkin/ValidatingXMLStreamReaderTests.java
b/src/test/java/ValidatingXSRTests/checkin/ValidatingXMLStreamReaderTests.java
index aba74641..7da0ce75 100755
---
a/src/test/java/ValidatingXSRTests/checkin/ValidatingXMLStreamReaderTests.java
+++
b/src/test/java/ValidatingXSRTests/checkin/ValidatingXMLStreamReaderTests.java
@@ -16,6 +16,7 @@ package ValidatingXSRTests.checkin;
import org.apache.xmlbeans.*;
import org.apache.xmlbeans.impl.validator.ValidatingXMLStreamReader;
+import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -25,15 +26,21 @@ import tools.util.JarUtil;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
+import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.StringReader;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.List;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ValidatingXMLStreamReaderTests {
@@ -383,4 +390,81 @@ public class ValidatingXMLStreamReaderTests {
assertTrue(valXsr.isValid(), "Content2 validation is broken.");
}
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "<count xmlns='urn:t'>5<?pi x?>5</count>",
+ "<count xmlns='urn:t'>5<?pi x?><!--c-->5</count>",
+ "<count xmlns='urn:t'>5<!--c-->5</count>"
+ })
+ void testTextSplitByProcessingInstruction(String doc) throws Exception {
+ // a comment or pi between two text chunks contributes no character
data,
+ // so the element value is 55 and must fail maxInclusive=9; the
validator
+ // only checks the first TEXT event, so the chunks have to arrive as
one
+ String xsd =
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='urn:t'>" +
+ " <xs:element name='count'>" +
+ " <xs:simpleType><xs:restriction base='xs:int'><xs:maxInclusive
value='9'/></xs:restriction></xs:simpleType>" +
+ " </xs:element>" +
+ "</xs:schema>";
+ SchemaTypeLoader stl = XmlBeans.loadXsd(new
XmlObject[]{SchemaDocument.Factory.parse(xsd)});
+
+ XMLStreamReader xsr =
XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(doc));
+ Collection<XmlError> errors = new ArrayList<>();
+
+ ValidatingXMLStreamReader valXsr = new ValidatingXMLStreamReader();
+ valXsr.init(xsr, false, null, stl, null, errors);
+
+ while (valXsr.hasNext()) {
+ valXsr.next();
+ }
+
+ assertFalse(valXsr.isValid(), "Text split by a pi was validated in
pieces: " + doc);
+ assertEquals(1, errors.size(), errors.toString());
+ assertEquals("cvc-maxInclusive-valid",
errors.iterator().next().getErrorCode());
+ }
+
+ @Test
+ void testEventsSeenAroundProcessingInstruction() throws Exception {
+ // the reader passes events through to its caller, so packing text also
+ // decides what the caller sees: a comment or pi after text is consumed
+ // into the merged CHARACTERS event, one before any text is passed on
+ String xsd =
+ "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='urn:t'>" +
+ " <xs:element name='v' type='xs:string'/>" +
+ "</xs:schema>";
+ SchemaTypeLoader stl = XmlBeans.loadXsd(new
XmlObject[]{SchemaDocument.Factory.parse(xsd)});
+ String doc = "<v xmlns='urn:t'><?lead x?>a<?pi x?><!--c-->b<?tail
x?></v>";
+
+ XMLStreamReader xsr =
XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(doc));
+ Collection<XmlError> errors = new ArrayList<>();
+
+ ValidatingXMLStreamReader valXsr = new ValidatingXMLStreamReader();
+ valXsr.init(xsr, false, null, stl, null, errors);
+
+ List<String> events = new ArrayList<>();
+ while (valXsr.hasNext()) {
+ int evType = valXsr.next();
+ switch (evType) {
+ case XMLEvent.START_ELEMENT:
+ events.add("start " + valXsr.getLocalName());
+ break;
+ case XMLEvent.END_ELEMENT:
+ events.add("end " + valXsr.getLocalName());
+ break;
+ case XMLEvent.PROCESSING_INSTRUCTION:
+ events.add("pi " + valXsr.getPITarget());
+ break;
+ case XMLEvent.CHARACTERS:
+ events.add("text " + valXsr.getText());
+ break;
+ default:
+ events.add("event " + evType);
+ }
+ }
+
+ assertEquals(Arrays.asList("start v", "pi lead", "text ab", "end v",
"event " + XMLEvent.END_DOCUMENT),
+ events);
+ assertTrue(valXsr.isValid(), errors.toString());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]