From 2179a8f2d622f832aa21eb7f48e8ab055bc55731 Mon Sep 17 00:00:00 2001 From: Joe Wang Date: Tue, 2 May 2023 03:12:06 +0000 Subject: [PATCH] 8298087: XML Schema Validation reports an required attribute twice via ErrorHandler Reviewed-by: naoto --- .../internal/impl/xs/XMLSchemaValidator.java | 8 +- .../validation/ErrorHandlingTest.java | 123 ++++++++++++++++++ 2 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 test/jaxp/javax/xml/jaxp/unittest/validation/ErrorHandlingTest.java diff --git a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java index 5bc7563a928..4520ca3eeb7 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved. */ /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -112,7 +112,7 @@ import jdk.xml.internal.JdkXmlUtils; * @author Elena Litani IBM * @author Andy Clark IBM * @author Neeraj Bajaj, Sun Microsystems, inc. - * @LastModified: May 2021 + * @LastModified: Apr 2023 */ public class XMLSchemaValidator implements XMLComponent, XMLDocumentFilter, FieldActivator, RevalidationHandler, XSElementDeclHelper { @@ -3217,10 +3217,6 @@ public class XMLSchemaValidator // {required} is true matches one of the attribute information items in the element // information item's [attributes] as per clause 3.1 above. if (currUse.fUse == SchemaSymbols.USE_REQUIRED) { - if (!isSpecified) - reportSchemaError( - "cvc-complex-type.4", - new Object[] { element.rawname, currDecl.fName }); if (!isSpecified) { if (currDecl.fTargetNamespace != null) { reportSchemaError( diff --git a/test/jaxp/javax/xml/jaxp/unittest/validation/ErrorHandlingTest.java b/test/jaxp/javax/xml/jaxp/unittest/validation/ErrorHandlingTest.java new file mode 100644 index 00000000000..b007c73296f --- /dev/null +++ b/test/jaxp/javax/xml/jaxp/unittest/validation/ErrorHandlingTest.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package validation; + +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import static java.util.Collections.unmodifiableList; +import java.util.List; +import javax.xml.XMLConstants; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; +import org.testng.Assert; +import org.testng.annotations.Test; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/* + * @test + * @bug 8298087 + * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest + * @run testng validation.ErrorHandlingTest + * @summary Verifies error handling. + */ +public class ErrorHandlingTest { + private final static String xsd = "" + +"" + +"\n" + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +" " + +"\n" + +""; + + private final static String xml = "\n" + +" string\n" + +""; + + /** + * Verifies that validation error is reported properly (once rather than twice). + * + * @throws Exception if the test fails + */ + @Test + public void test() throws Exception { + List ex = validateXMLWithSchema(new StreamSource(new StringReader(xsd)), + new StreamSource(new StringReader(xml))); + Assert.assertEquals(ex.size(), 1); + } + + public static List validateXMLWithSchema(final Source xsd, final Source xml) { + final List exceptions = new ArrayList<>(); + try { + final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + final Schema schema = factory.newSchema(xsd); + final Validator validator = schema.newValidator(); + validator.setErrorHandler(new ErrorHandler() { + @Override + public void warning(final SAXParseException exception) { + System.err.printf("Warning: %s%n", exception); + exceptions.add(exception); + } + + @Override + public void error(final SAXParseException exception) { + System.err.printf("Error: %s%n", exception); + exceptions.add(exception); + } + + @Override + public void fatalError(final SAXParseException exception) { + System.err.printf("Fatal: %s%n", exception); + exceptions.add(exception); + } + }); + + validator.validate(xml); + + } catch (final SAXException | IOException e) { + System.err.printf("Exception: %s%n", e.getMessage()); + } + return unmodifiableList(exceptions); + } + +}