From 5ba99cd1efde98280700469f6392f7341fce3eb3 Mon Sep 17 00:00:00 2001 From: ChrisTrenkamp Date: Mon, 19 Apr 2021 17:11:45 -0400 Subject: [PATCH] Fix for invalid attribute values when it does not conform to the DTD (#128) Fixing invalid attribute values in DTDValidator. --- .../java/com/ctc/wstx/dtd/DTDValidator.java | 2 +- .../vstream/TestInvalidAttributeValue.java | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/test/java/wstxtest/vstream/TestInvalidAttributeValue.java diff --git a/src/main/java/com/ctc/wstx/dtd/DTDValidator.java b/src/main/java/com/ctc/wstx/dtd/DTDValidator.java index 18862913..09f240a6 100644 --- a/src/main/java/com/ctc/wstx/dtd/DTDValidator.java +++ b/src/main/java/com/ctc/wstx/dtd/DTDValidator.java @@ -254,7 +254,7 @@ public String validateAttribute(String localName, String uri, /* [WSTX-190] NPE if we continued (after reported didn't * throw an exception); nothing more to do, let's leave */ - return new String(valueChars, valueStart, valueEnd); + return new String(valueChars, valueStart, valueEnd-valueStart); } int index = mAttrCount++; if (index >= mAttrSpecs.length) { diff --git a/src/test/java/wstxtest/vstream/TestInvalidAttributeValue.java b/src/test/java/wstxtest/vstream/TestInvalidAttributeValue.java new file mode 100644 index 00000000..0bf9554a --- /dev/null +++ b/src/test/java/wstxtest/vstream/TestInvalidAttributeValue.java @@ -0,0 +1,57 @@ +package wstxtest.vstream; + +import stax2.BaseStax2Test; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.stream.*; + +import org.codehaus.stax2.XMLStreamReader2; +import org.codehaus.stax2.validation.ValidationProblemHandler; +import org.codehaus.stax2.validation.XMLValidationException; +import org.codehaus.stax2.validation.XMLValidationProblem; +import org.codehaus.stax2.validation.XMLValidationSchema; +import org.codehaus.stax2.validation.XMLValidationSchemaFactory; + +public class TestInvalidAttributeValue + extends BaseStax2Test +{ + public void testInvalidAttributeValue() throws Exception + { + final String DOC = "\n"; + + final String INPUT_DTD = +"\n" ++"\n" +; + + XMLInputFactory f = getInputFactory(); + setCoalescing(f, true); + + XMLValidationSchemaFactory schemaFactory = + XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_DTD); + XMLValidationSchema schema = schemaFactory.createSchema(new StringReader(INPUT_DTD)); + XMLStreamReader2 sr = (XMLStreamReader2)f.createXMLStreamReader( + new StringReader(DOC)); + + final List probs = new ArrayList(); + + sr.validateAgainst(schema); + sr.setValidationProblemHandler(new ValidationProblemHandler() { + @Override + public void reportProblem(XMLValidationProblem problem) + throws XMLValidationException { + probs.add(problem); + } + }); + + assertTokenType(START_ELEMENT, sr.next()); + assertEquals("root", sr.getLocalName()); + + final String verboseValue = sr.getAttributeValue(null, "verbose"); + + assertEquals("yes", verboseValue); + } +}