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

Unable to set locale for xml validation to english

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8, 11, 17, 20, 21
    • xml

      ADDITIONAL SYSTEM INFORMATION :
      Java 17.0.7

      A DESCRIPTION OF THE PROBLEM :
      If I create a xsd validator I'm unable to get the results in english.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - set `user.language` to `de`
      - create a validator
      - set language of this validator to "en"


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      error message is in english
      ACTUAL -
      error message is in german (user.language)

      ---------- BEGIN SOURCE ----------
      package de.quick.common.xml;

      import java.io.ByteArrayInputStream;
      import java.nio.charset.StandardCharsets;
      import java.util.Locale;
      import javax.xml.XMLConstants;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.Schema;
      import javax.xml.validation.SchemaFactory;
      import javax.xml.validation.Validator;

      import org.junit.Assert;
      import org.junit.Test;
      import org.xml.sax.SAXException;

      public class XsdValidatorBugTest {

        private String xsd = """
            <?xml version="1.0" encoding="ISO-8859-1"?>
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                       targetNamespace="http://abc"
                       elementFormDefault="qualified" attributeFormDefault="unqualified" version="V1.0.0">
                  
                <xs:element name="Kug">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="PG">
                              <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1"/>
                                    <xs:pattern value="[A-Z]+"/>
                                </xs:restriction>
                              </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:schema>""";

        private String xml = """
            <?xml version="1.0" encoding="ISO-8859-1"?>
            <Kug xmlns="http://abc">
                <PG>AB1</PG>
            </Kug>""";


        @Test
        public void validator_has_wrong_language() throws Exception {
          // make sure user.language is "de"
          // if it fails use "-Duser.language=es" as parameter
          Assert.assertEquals("de", System.getProperty("user.language"));

          // create validator
          Validator xsdValidator = createXsdValidator();


          // ******************************************************************************************************************
          // test #1 ... set language to french
          xsdValidator.setProperty("http://apache.org/xml/properties/locale", new Locale("fr"));


          StreamSource source1 = new StreamSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.ISO_8859_1)));
          try {
            xsdValidator.validate(source1);
            Assert.fail("not expected to reach here");
          } catch (SAXException e) {
            Assert.assertEquals("cvc-pattern-valid : La valeur 'AB1' n'est pas un facet valide par rapport au modxE8le '[A-Z]+' pour le type '#AnonType_PGKug'.", e.getMessage());
          }


          // ******************************************************************************************************************
          // test #2 ... now setting language to english
          xsdValidator.setProperty("http://apache.org/xml/properties/locale", new Locale("en"));


          StreamSource source2 = new StreamSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.ISO_8859_1)));
          try {
            xsdValidator.validate(source2);
            Assert.fail("not expected to reach here");
          } catch (SAXException e) {
            Assert.assertEquals("cvc-pattern-valid: Value 'AB1' is not facet-valid with respect to pattern '[A-Z]+' for type '#AnonType_PGKug'.", e.getMessage());
          }
        }


        private Validator createXsdValidator() throws Exception {
          SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
          StreamSource xsdSource = new StreamSource(new ByteArrayInputStream(xsd.getBytes(StandardCharsets.ISO_8859_1)));
          Schema schema = schemaFactory.newSchema(xsdSource);
          return schema.newValidator();
        }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated: