Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8298087

XML Schema Validation reports an required attribute twice via ErrorHandler

    XMLWordPrintable

Details

    • b01
    • b21
    • generic
    • generic
    • Verified

    Backports

      Description

        ADDITIONAL SYSTEM INFORMATION :
        Java 17.0.5, Java 11.0.17

        A DESCRIPTION OF THE PROBLEM :
        XML Schema Validation reports an required attribute twice via `ErrorHandler`. The bug is in `com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java:3220` (Java 17.05). With prior a version the validation error is reported only once.

        REGRESSION : Last worked in version 17

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        run provided source code

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        error is reported once
        ACTUAL -
        error is reported twice

        ---------- BEGIN SOURCE ----------
        import org.xml.sax.ErrorHandler;
        import org.xml.sax.SAXException;
        import org.xml.sax.SAXParseException;

        import javax.xml.XMLConstants;
        import javax.xml.transform.Source;
        import javax.xml.transform.stream.StreamSource;
        import java.io.IOException;
        import java.io.StringReader;
        import java.util.ArrayList;
        import java.util.List;
        import java.util.Locale;
        import java.util.function.Function;

        import static java.util.Collections.unmodifiableList;
        import static java.util.Locale.ENGLISH;
        import static javax.xml.validation.SchemaFactory.newInstance;

        public class Main {

            private final static String xsd = """
                    <?xml version="1.0" encoding="UTF-8" ?>
                    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="https://agebhar1.github.io/schema/Example"
                            elementFormDefault="qualified">
                                
                        <element name="root">
                            <complexType>
                                <sequence>
                                    <element name="a">
                                        <complexType>
                                            <simpleContent>
                                                <extension base="string">
                                                    <attribute name="enabled" type="boolean" use="required"/>
                                                </extension>
                                            </simpleContent>
                                        </complexType>
                                    </element>
                                </sequence>
                            </complexType>
                        </element>
                                
                    </schema>
                    """;

            private final static String xml = """
                    <e:root xmlns:e="https://agebhar1.github.io/schema/Example">
                        <e:a>string</e:a>
                    </e:root>
                    """;

            private static final Function<String, Source> toSource = (String it) -> new StreamSource(new StringReader(it));

            public static void main(String[] args) {
                Locale.setDefault(ENGLISH);
                var ex = validateXMLWithSchema(xsd.transform(toSource), xml.transform(toSource));
                assert ex.size() == 1;
            }

            public static List<SAXParseException> validateXMLWithSchema(final Source xsd, final Source xml) {
                final List<SAXParseException> exceptions = new ArrayList<>();
                try {
                    final var factory = newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                    final var schema = factory.newSchema(xsd);
                    final var 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);
            }

        }
        ---------- END SOURCE ----------

        FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

                joehw Joe Wang
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: