jaxp
  1. jaxp
  2. JAXP-55

javax.xml.validation.Schema is not thread safe

    Details

    • Issuezilla Id:
      55

      Description

      According documentation:

      A Schema object is thread safe and applications are encouraged to share it
      across many parsers in many threads.

      But I have problem when sharing schema instance, here is my code:

      package com.sargis;

      import org.w3c.dom.Document;
      import org.xml.sax.ErrorHandler;
      import org.xml.sax.SAXException;
      import org.xml.sax.SAXParseException;

      import javax.xml.XMLConstants;
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.ParserConfigurationException;
      import javax.xml.transform.Source;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.Schema;
      import javax.xml.validation.SchemaFactory;
      import javax.xml.validation.Validator;
      import java.io.File;
      import java.io.FileFilter;
      import java.io.IOException;
      import java.util.concurrent.BrokenBarrierException;
      import java.util.concurrent.CyclicBarrier;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.Executors;

      public class XMLValidationTest {

      private static final int NTHREADS = 25;
      private static final ExecutorService EXEC = Executors.newCachedThreadPool();

      private static final CyclicBarrier BARRIER = new CyclicBarrier(NTHREADS);

      public static final String IN_FOLDER =
      "/home/sargis/projects/twm/branches/ng/epayment/Vasp/dist/inout/in";
      public static final String XSD_PATH =
      "/home/sargis/projects/twm/branches/ng/epayment/Vasp/config/epayoth.xsd";

      private static Schema schema;

      public static void main(String[] args) {

      SchemaFactory factory =
      SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      Source schemaFile = new StreamSource(XSD_PATH);
      try

      { schema = factory.newSchema(schemaFile); }

      catch (SAXException e)

      { e.printStackTrace(); System.exit(-1); }

      File incoming = new File(IN_FOLDER);
      File[] files = incoming.listFiles(new FileFilter() {
      public boolean accept(File file)

      { return file.isFile() && file.getName().endsWith(".xml"); }

      });

      for (int i = 0; i < files.length; i++)

      { EXEC.execute(new XMLValiddator(files[i], i)); }

      EXEC.shutdown();
      }

      private static class XMLValiddator implements Runnable {

      private File file;
      private int index;

      public XMLValiddator(File file, int index)

      { this.file = file; this.index = index; }

      public void run() {

      try

      { System.out.printf("Waiting for barrier: %s%n", index); BARRIER.await(); System.out.println("Validating...."); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(file); Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandlerImpl()); validator.validate(new DOMSource(document)); }

      catch (IOException e)

      { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); }

      catch (ParserConfigurationException e)

      { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); }

      catch (InterruptedException e)

      { e.printStackTrace(); }

      }
      }

      private static class ErrorHandlerImpl implements ErrorHandler {

      public void warning(SAXParseException exception) throws SAXException

      { System.out.printf("**Parsing Warning. Line: %s URI: %s Message: %s%n", exception.getLineNumber(), exception.getSystemId(), exception.getMessage()); }

      public void error(SAXParseException exception) throws SAXException

      { String msg = String.format("**Parsing Error. Line: %s URI: %s Message: %s%n", exception.getLineNumber(), exception.getSystemId(), exception.getMessage()); System.out.println(msg); throw new SAXException(msg); }

      public void fatalError(SAXParseException exception) throws SAXException

      { String msg = String.format("**Parsing Fatal Error. Line: %s URI: %s Message: %s%n", exception.getLineNumber(), exception.getSystemId(), exception.getMessage()); System.out.println(msg); throw new SAXException(msg); }

      }

      }

      Here is my XSD:

      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified">

      <xs:element name="eOtherPayment">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="OtherPaymentSerialID"/>
      <xs:element ref="CustomsCode"/>
      <xs:element ref="DeclarantCode"/>
      <xs:element ref="CompanyCode"/>
      <xs:element ref="BankCode"/>
      <xs:element ref="Transactions"/>
      <xs:element ref="Payments"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name="OtherPaymentSerialID">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:maxLength value="50"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="CustomsCode">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z0-9]

      {4}"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="DeclarantCode">
      <xs:simpleType>
      <xs:restriction base="U">
      <xs:minLength value="0"/>
      <xs:maxLength value="17"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="CompanyCode">
      <xs:simpleType>
      <xs:restriction base="U">
      <xs:minLength value="0"/>
      <xs:maxLength value="17"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="BankCode">
      <xs:simpleType>
      <xs:restriction base="U">
      <xs:minLength value="1"/>
      <xs:maxLength value="17"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="Transactions">
      <xs:complexType>
      <xs:sequence>
      <xs:element maxOccurs="10" ref="TransactionRecord"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name="TransactionRecord">
      <xs:complexType>
      <xs:sequence>
      <xs:element name="Code">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z0-9]{1,3}"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>
      <xs:element ref="RefOffice"/>
      <xs:element ref="RefYear"/>
      <xs:element ref="RefSerial"/>
      <xs:element ref="RefNumber"/>
      <xs:element ref="TransactionReference"/>
      <xs:element name="Amount" type="NMU"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name="RefOffice">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z0-9]{0,4}"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="RefYear">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:pattern value="([0-9]){0}|([0-9]){4}

      "/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="RefSerial">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]

      {0,1}

      "/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="RefNumber">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:pattern value="([0-9])*"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="TransactionReference">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
      <xs:maxLength value="35"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="Payments">
      <xs:complexType>
      <xs:sequence>
      <xs:element maxOccurs="10" ref="MeansOfPayment"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name="MeansOfPayment">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="Code"/>
      <xs:element ref="Reference"/>
      <xs:element name="Amount" type="NMU"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name="Code">
      <xs:simpleType>
      <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z0-9]

      {2}

      "/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:element name="Reference">
      <xs:simpleType>
      <xs:restriction base="U">
      <xs:minLength value="1"/>
      <xs:maxLength value="17"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:element>

      <xs:simpleType name="NMU">
      <xs:restriction base="xs:decimal">
      <xs:minInclusive value="0"/>
      <xs:fractionDigits value="2"/>
      </xs:restriction>
      </xs:simpleType>

      <xs:simpleType name="U">
      <xs:restriction base="xs:string">
      <xs:pattern value="([!-`]|[{-~])*"/>
      </xs:restriction>
      </xs:simpleType>

      </xs:schema>

      and XML template

      <?xml version="1.0" encoding="UTF-8"?>

      <eOtherPayment>

      <OtherPaymentSerialID>b972115d-829e-43ff-aebb-c7157ef25c71</OtherPaymentSerialID>
      <CustomsCode>01AP</CustomsCode>
      <DeclarantCode>A9901017</DeclarantCode>
      <CompanyCode>A0500823</CompanyCode>
      <BankCode>221</BankCode>
      <Transactions>
      <TransactionRecord>
      <Code>89</Code>
      <RefOffice>01AP</RefOffice>
      <RefYear>2008</RefYear>
      <RefSerial>A</RefSerial>
      <RefNumber>68</RefNumber>
      <TransactionReference>KJ091</TransactionReference>
      <Amount>5000</Amount>
      </TransactionRecord>
      <TransactionRecord>
      <Code>97</Code>
      <RefOffice/>
      <RefYear/>
      <RefSerial/>
      <RefNumber/>
      <TransactionReference>LL091</TransactionReference>
      <Amount>68700</Amount>
      </TransactionRecord>
      <TransactionRecord>
      <Code>98</Code>
      <RefOffice/>
      <RefYear/>
      <RefSerial/>
      <RefNumber/>
      <TransactionReference>HH098</TransactionReference>
      <Amount>8000</Amount>
      </TransactionRecord>
      <TransactionRecord>
      <Code>96</Code>
      <RefOffice/>
      <RefYear/>
      <RefSerial/>
      <RefNumber/>
      <TransactionReference>PO091</TransactionReference>
      <Amount>7500</Amount>
      </TransactionRecord>
      <TransactionRecord>
      <Code>92</Code>
      <RefOffice/>
      <RefYear/>
      <RefSerial/>
      <RefNumber/>
      <TransactionReference>098</TransactionReference>
      <Amount>14000</Amount>
      </TransactionRecord>
      </Transactions>
      <Payments>
      <MeansOfPayment>
      <Code>21</Code>
      <Reference>EF01</Reference>
      <Amount>26800</Amount>
      </MeansOfPayment>
      <MeansOfPayment>
      <Code>22</Code>
      <Reference>G</Reference>
      <Amount>15000</Amount>
      </MeansOfPayment>
      <MeansOfPayment>
      <Code>20</Code>
      <Reference>HH</Reference>
      <Amount>9810</Amount>
      </MeansOfPayment>
      <MeansOfPayment>
      <Code>20</Code>
      <Reference>DEE</Reference>
      <Amount>51590</Amount>
      </MeansOfPayment>
      </Payments>
      </eOtherPayment>

      just create 25 XML files in IN_FOLDER folder and run my code. Here is exception:

      **Parsing Error. Line: -1 URI: null Message: cvc-complex-type.2.4.d: Invalid
      content was found starting with element '

      {TransactionRecord}

      '. No child element
      is expected at this point.

      at
      com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidComplexType(XMLSchemaValidator.java:3153)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3076)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:2978)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2121)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:791)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.finishNode(DOMValidatorHelper.java:338)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:243)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:100)
      at javax.xml.validation.Validator.validate(Validator.java:127)
      at com.sargis.XMLValidationTest$XMLValiddator.run(XMLValidationTest.java:87)
      at
      java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:619)
      org.xml.sax.SAXException: **Parsing Error. Line: -1 URI: null Message:
      cvc-complex-type.2.4.b: The content of element 'Payments' is not complete. One
      of '

      {MeansOfPayment}' is expected.

      at com.sargis.XMLValidationTest$ErrorHandlerImpl.error(XMLValidationTest.java:115)
      at
      com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
      at
      com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
      at
      com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidComplexType(XMLSchemaValidator.java:3147)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.elementLocallyValidType(XMLSchemaValidator.java:3076)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processElementContent(XMLSchemaValidator.java:2978)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleEndElement(XMLSchemaValidator.java:2121)
      at
      com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:791)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.finishNode(DOMValidatorHelper.java:338)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:243)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)
      at
      com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:100)
      at javax.xml.validation.Validator.validate(Validator.java:127)
      at com.sargis.XMLValidationTest$XMLValiddator.run(XMLValidationTest.java:87)
      at
      java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:619)
      **Parsing Error. Line: -1 URI: null Message: cvc-complex-type.2.4.b: The
      content of element 'Payments' is not complete. One of '{MeansOfPayment}

      ' is
      expected.

        Activity

        armsargis created issue -
        Hide
        armsargis added a comment -

        user was not assigned

        Show
        armsargis added a comment - user was not assigned
        kenaiadmin made changes -
        Field Original Value New Value
        issue.field.bugzillaimportkey 55 46739

          People

          • Assignee:
            jaxp-issues
            Reporter:
            armsargis
          • Votes:
            2 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: