diff --git a/src/main/java/com/ctc/wstx/msv/W3CMultiSchemaFactory.java b/src/main/java/com/ctc/wstx/msv/W3CMultiSchemaFactory.java index a0710f37..e6a0bb1c 100644 --- a/src/main/java/com/ctc/wstx/msv/W3CMultiSchemaFactory.java +++ b/src/main/java/com/ctc/wstx/msv/W3CMultiSchemaFactory.java @@ -25,10 +25,11 @@ import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; +import org.codehaus.stax2.validation.XMLValidationSchema; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; - +import org.xml.sax.EntityResolver; import org.xml.sax.Locator; import com.sun.msv.grammar.ExpressionPool; @@ -42,8 +43,6 @@ import com.sun.msv.reader.xmlschema.WSDLGrammarReaderController; import com.sun.msv.reader.xmlschema.XMLSchemaReader; -import org.codehaus.stax2.validation.XMLValidationSchema; - /** * This is a StAX2 schema factory that can parse and create schema instances * for creating validators that validate documents to check their validity @@ -66,7 +65,8 @@ public class W3CMultiSchemaFactory { private final SAXParserFactory parserFactory; - + private EntityResolver entityResolver; + public W3CMultiSchemaFactory() { parserFactory = SAXParserFactory.newInstance(); parserFactory.setNamespaceAware(true); @@ -125,7 +125,7 @@ public void switchSource(Source source, State newState) { } } - + /** * Creates an XMLValidateSchema that can be used to validate XML instances against * any of the schemas defined in the Map of schemaSources. @@ -136,6 +136,20 @@ public void switchSource(Source source, State newState) { public XMLValidationSchema createSchema(String baseURI, Map schemaSources) throws XMLStreamException { + return createSchema(baseURI, schemaSources, this.entityResolver); + } + + /** + * Creates an XMLValidateSchema that can be used to validate XML instances against + * any of the schemas defined in the Map of schemaSources. + * + * @param baseURI Base URI for resolving dependant schemas + * @param schemaSources Map of schemas, namespace to Source + * @param entityResolver Entity resolver used to resolve path to unknown schemas + */ + public XMLValidationSchema createSchema(String baseURI, + Map schemaSources, EntityResolver entityResolver) throws XMLStreamException + { Map embeddedSources = new HashMap(); for (Map.Entry source : schemaSources.entrySet()) { if (source.getValue() instanceof DOMSource) { @@ -150,7 +164,9 @@ public XMLValidationSchema createSchema(String baseURI, } } - WSDLGrammarReaderController ctrl = new WSDLGrammarReaderController(null, baseURI, embeddedSources); + final WSDLGrammarReaderController ctrl = new WSDLGrammarReaderController(null, baseURI, embeddedSources); + ctrl.setEntityResolver(entityResolver); + final RecursiveAllowedXMLSchemaReader xmlSchemaReader = new RecursiveAllowedXMLSchemaReader(ctrl, parserFactory); final MultiSchemaReader multiSchemaReader = new MultiSchemaReader(xmlSchemaReader); for (Source source : schemaSources.values()) { @@ -164,4 +180,10 @@ public XMLValidationSchema createSchema(String baseURI, return new W3CSchema(grammar); } + public W3CMultiSchemaFactory setEntityResolver(EntityResolver entityResolver) + { + this.entityResolver = entityResolver; + + return this; + } } diff --git a/src/test/java/wstxtest/msv/Issue_175_ver1.xml b/src/test/java/wstxtest/msv/Issue_175_ver1.xml new file mode 100644 index 00000000..c9a8f38b --- /dev/null +++ b/src/test/java/wstxtest/msv/Issue_175_ver1.xml @@ -0,0 +1,13 @@ + + + + Element #1 from Version 1 + 12345 + Element #2 from Version 1 + ENUM4 + + \ No newline at end of file diff --git a/src/test/java/wstxtest/msv/Issue_175_ver2.xml b/src/test/java/wstxtest/msv/Issue_175_ver2.xml new file mode 100644 index 00000000..74f8eecd --- /dev/null +++ b/src/test/java/wstxtest/msv/Issue_175_ver2.xml @@ -0,0 +1,14 @@ + + + + Element #1 from Version 2 + 12345 + Element #2 from Version 2 + ENUM1 + Element #2 from Version 2 + + \ No newline at end of file diff --git a/src/test/java/wstxtest/msv/Issue_175_ver3.xml b/src/test/java/wstxtest/msv/Issue_175_ver3.xml new file mode 100644 index 00000000..b4c7e3f3 --- /dev/null +++ b/src/test/java/wstxtest/msv/Issue_175_ver3.xml @@ -0,0 +1,15 @@ + + + + Element #1 from Main Version 1 + 12345 + Element #2 from Main Version 3 + ENUM2 + Element #5 from Main Version 3 + + \ No newline at end of file diff --git a/src/test/java/wstxtest/msv/TestIssue_175.java b/src/test/java/wstxtest/msv/TestIssue_175.java new file mode 100644 index 00000000..e8fd81a5 --- /dev/null +++ b/src/test/java/wstxtest/msv/TestIssue_175.java @@ -0,0 +1,620 @@ +package wstxtest.msv; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; + +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.junit.Assert; +import org.junit.Test; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import com.ctc.wstx.msv.W3CMultiSchemaFactory; +import com.ctc.wstx.stax.WstxInputFactory; + +/** + * Test Issue_175 - Added to test resolving XSD source files using the Entity Resolver + * + * When using various versions of XML that is build using imported XSD, in order to validate the XML correctly, the schema + * validation object must be built using the correct XSD. Typically, the main XSD uses an generic import to the XSD built + * with. In this scenario, the correct XSD must be resolved in order for the XML Validation to work correctly. + * + * In this test, two separate folders containing the versioned XSDs is used to demonstrate. The developer could have placed + * all the versioned XSDs in the same 'schema' folder. + * + * @author Tim Martin + */ +public class TestIssue_175 +{ + @Test + /** + * This tests in the case where the XSDs import statements files names/path do not match the file/path and naming + * used in the implementation. + */ + public void testReadWithNoEntityResolver() + { + try + { + // Build Schema Validation object for Version 1 + File fMainXsd1 = new File(getClass().getResource("schema/main/issue_175_main_v1.xsd").getFile()); + final File fImportXsd1 = new File(getClass().getResource("schema/import/issue_175_import_v1.xsd").getFile()); + + // Test to make sure we picked up the XSD + Assert.assertTrue(fMainXsd1.exists()); + Assert.assertTrue(fImportXsd1.exists()); + + final Map schemas_Ver1 = new HashMap<>(); + schemas_Ver1.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd1)); + schemas_Ver1.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd1)); + + final W3CMultiSchemaFactory sf = new W3CMultiSchemaFactory(); + sf.createSchema(null, schemas_Ver1); + + Assert.fail("Exception should have been thrown"); + } + catch (XMLStreamException xse) + { + Assert.assertEquals("Failed to load schemas", xse.getMessage()); + } + + try + { + // Build Schema Validation object for Version 2 + final File fMainXsd2 = new File(getClass().getResource("schema/main/issue_175_main_v2.xsd").getFile()); + final File fImportXsd2 = new File(getClass().getResource("schema/import/issue_175_import_v1.xsd").getFile()); + + final Map schemas_Ver2 = new HashMap<>(); + schemas_Ver2.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd2)); + schemas_Ver2.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd2)); + + final W3CMultiSchemaFactory sf = new W3CMultiSchemaFactory(); + sf.createSchema(null, schemas_Ver2); + } + catch (XMLStreamException xse) + { + Assert.assertEquals("Failed to load schemas", xse.getMessage()); + } + + try + { + // Build Schema Validation object for Version 3 + final File fMainXsd3 = new File(getClass().getResource("schema/main/issue_175_main_v3.xsd").getFile()); + final File fImportXsd3 = new File(getClass().getResource("schema/import/issue_175_import_v2.xsd").getFile()); + + final Map schemas_Ver3 = new HashMap<>(); + schemas_Ver3.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd3)); + schemas_Ver3.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd3)); + + final W3CMultiSchemaFactory sf = new W3CMultiSchemaFactory(); + sf.createSchema(null, schemas_Ver3); + } + catch (XMLStreamException xse) + { + Assert.assertEquals("Failed to load schemas", xse.getMessage()); + } + } + + @Test + /** + * This tests in the case where the XML version matches the schema version, this validates the correct XSDs + * are being picked up. + */ + public void testValidationWithEntityResolver() + { + try + { + // Build Schema Validation object for Version 1 + File fMainXsd1 = new File(getClass().getResource("schema/main/issue_175_main_v1.xsd").getFile()); + final File fImportXsd1 = new File(getClass().getResource("schema/import/issue_175_import_v1.xsd").getFile()); + + // Test to make sure we picked up the XSD + Assert.assertTrue(fMainXsd1.exists()); + Assert.assertTrue(fImportXsd1.exists()); + + final Map schemas_Ver1 = new HashMap<>(); + schemas_Ver1.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd1)); + schemas_Ver1.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd1)); + + final W3CMultiSchemaFactory sf = new W3CMultiSchemaFactory(); + sf.setEntityResolver(new EntityResolver() { + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException + { + System.out.println("ResolveEntity() - Public id: [" + publicId + "], System id: [" + systemId + "]"); + + InputSource is = null; + + File tmp = new File(URI.create(systemId).getPath()); + + String fileName = tmp.getName(); + if ("issue_175_import.xsd".equalsIgnoreCase(fileName)) + { + tmp = fImportXsd1; + } + + try + { + is = new InputSource(new FileReader(tmp)); + is.setSystemId(tmp.toURI().toString()); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + + System.out.println("ResolveEntity() - System Id: [" + is.getSystemId() + "]"); + + return is; + } + }); + + final XMLValidationSchema valSchemaVal_Ver1 = sf.createSchema(null, schemas_Ver1); + + // Build Schema Validation object for Version 2 + final File fMainXsd2 = new File(getClass().getResource("schema/main/issue_175_main_v2.xsd").getFile()); + final File fImportXsd2 = new File(getClass().getResource("schema/import/issue_175_import_v1.xsd").getFile()); + + final Map schemas_Ver2 = new HashMap<>(); + schemas_Ver2.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd2)); + schemas_Ver2.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd2)); + + final XMLValidationSchema valSchemaVal_Ver2 = sf.createSchema(null, schemas_Ver2, new EntityResolver() { + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException + { + System.out.println("ResolveEntity() - Public id: [" + publicId + "], System id: [" + systemId + "]"); + + InputSource is = null; + + File tmp = new File(URI.create(systemId).getPath()); + + String fileName = tmp.getName(); + if ("issue_175_import.xsd".equalsIgnoreCase(fileName)) + { + tmp = fImportXsd2; + } + + try + { + is = new InputSource(new FileReader(tmp)); + is.setSystemId(tmp.toURI().toString()); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + + System.out.println("ResolveEntity() - System Id: [" + is.getSystemId() + "]"); + + return is; + } + }); + + // Build Schema Validation object for Version 3 + final File fMainXsd3 = new File(getClass().getResource("schema/main/issue_175_main_v3.xsd").getFile()); + final File fImportXsd3 = new File(getClass().getResource("schema/import/issue_175_import_v2.xsd").getFile()); + + final Map schemas_Ver3 = new HashMap<>(); + schemas_Ver3.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd3)); + schemas_Ver3.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd3)); + + final XMLValidationSchema valSchemaVal_Ver3 = sf.createSchema(null, schemas_Ver3, new EntityResolver() { + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException + { + System.out.println("ResolveEntity() - Public id: [" + publicId + "], System id: [" + systemId + "]"); + + InputSource is = null; + + File tmp = new File(URI.create(systemId).getPath()); + + String fileName = tmp.getName(); + if ("issue_175_import.xsd".equalsIgnoreCase(fileName)) + { + tmp = fImportXsd3; + } + + try + { + is = new InputSource(new FileReader(tmp)); + is.setSystemId(tmp.toURI().toString()); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + + System.out.println("ResolveEntity() - System Id: [" + is.getSystemId() + "]"); + + return is; + } + }); + + try + { + // Xsd Validation - Version 1 XML data + final WstxInputFactory xf = new WstxInputFactory(); + xf.configureForXmlConformance(); + + final XMLStreamReader2 xmlRdr = (XMLStreamReader2) xf.createXMLStreamReader(getClass().getResourceAsStream("Issue_175_ver1.xml"), "utf-8"); + xmlRdr.setValidationProblemHandler(new ValidationProblemHandler() + { + @Override + public void reportProblem(XMLValidationProblem arg0) throws XMLValidationException + { + if (arg0.getSeverity() == XMLValidationProblem.SEVERITY_WARNING ) + { + System.out.println("Validation Warning: " + arg0.getMessage()); + } + else + { + System.out.println("Validation Error: " + arg0.getMessage()); + throw arg0.toException(); + } + } + }); + + // Validate + xmlRdr.validateAgainst(valSchemaVal_Ver1); + + // Run thru each element, etc. + while(xmlRdr.hasNext()) { xmlRdr.next(); } + } + catch (Exception ex) + { + ex.printStackTrace(); + Assert.fail("Unexpected error: " + ex.toString()); + } + + try + { + // Xsd Validation - Version 2 XML data + final WstxInputFactory xf = new WstxInputFactory(); + xf.configureForXmlConformance(); + + final XMLStreamReader2 xmlRdr = (XMLStreamReader2) xf.createXMLStreamReader(getClass().getResourceAsStream("Issue_175_ver2.xml"), "utf-8"); + xmlRdr.setValidationProblemHandler(new ValidationProblemHandler() + { + @Override + public void reportProblem(XMLValidationProblem arg0) throws XMLValidationException + { + if (arg0.getSeverity() == XMLValidationProblem.SEVERITY_WARNING ) + { + System.out.println("Validation Warning: " + arg0.getMessage()); + } + else + { + System.out.println("Validation Error: " + arg0.getMessage()); + throw arg0.toException(); + } + } + }); + + // Validate + xmlRdr.validateAgainst(valSchemaVal_Ver2); + + // Run thru each element, etc. + while(xmlRdr.hasNext()) { xmlRdr.next(); } + } + catch (Exception ex) + { + ex.printStackTrace(); + Assert.fail("Unexpected error: " + ex.toString()); + } + + try + { + // Xsd Validation - Version 3 XML data + final WstxInputFactory xf = new WstxInputFactory(); + xf.configureForXmlConformance(); + + XMLStreamReader2 xmlRdr = (XMLStreamReader2) xf.createXMLStreamReader(getClass().getResourceAsStream("Issue_175_ver3.xml"), "utf-8"); + xmlRdr.setValidationProblemHandler(new ValidationProblemHandler() + { + @Override + public void reportProblem(XMLValidationProblem arg0) throws XMLValidationException + { + if (arg0.getSeverity() == XMLValidationProblem.SEVERITY_WARNING ) + { + System.out.println("Validation Warning: " + arg0.getMessage()); + } + else + { + System.out.println("Validation Error: " + arg0.getMessage()); + throw arg0.toException(); + } + } + }); + + // Validate + xmlRdr.validateAgainst(valSchemaVal_Ver3); + + // Run thru each element, etc. + while(xmlRdr.hasNext()) { xmlRdr.next(); } + } + catch (Exception ex) + { + ex.printStackTrace(); + Assert.fail("Unexpected error: " + ex.toString()); + } + } + catch (Exception ex) + { + ex.printStackTrace(); + Assert.fail("Unexpected error: " + ex.toString()); + } + } + + @Test + /** + * This tests in the case where the XML version does not match the schema version, this validates the correct XSDs + * are being picked up. + */ + public void testValidationOfWrongXmlVersionWithEntityResolver() + { + try + { + // Build Schema Validation object for Version 1 + File fMainXsd1 = new File(getClass().getResource("schema/main/issue_175_main_v1.xsd").getFile()); + final File fImportXsd1 = new File(getClass().getResource("schema/import/issue_175_import_v1.xsd").getFile()); + + // Test to make sure we picked up the XSD + Assert.assertTrue(fMainXsd1.exists()); + Assert.assertTrue(fImportXsd1.exists()); + + final Map schemas_Ver1 = new HashMap<>(); + schemas_Ver1.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd1)); + schemas_Ver1.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd1)); + + final W3CMultiSchemaFactory sf = new W3CMultiSchemaFactory(); + sf.setEntityResolver(new EntityResolver() { + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException + { + System.out.println("ResolveEntity() - Public id: [" + publicId + "], System id: [" + systemId + "]"); + + InputSource is = null; + + File tmp = new File(URI.create(systemId).getPath()); + + String fileName = tmp.getName(); + if ("issue_175_import.xsd".equalsIgnoreCase(fileName)) + { + tmp = fImportXsd1; + } + + try + { + is = new InputSource(new FileReader(tmp)); + is.setSystemId(tmp.toURI().toString()); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + + System.out.println("ResolveEntity() - System Id: [" + is.getSystemId() + "]"); + + return is; + } + }); + + final XMLValidationSchema valSchemaVal_Ver1 = sf.createSchema(null, schemas_Ver1); + + // Build Schema Validation object for Version 2 + final File fMainXsd2 = new File(getClass().getResource("schema/main/issue_175_main_v2.xsd").getFile()); + final File fImportXsd2 = new File(getClass().getResource("schema/import/issue_175_import_v1.xsd").getFile()); + + final Map schemas_Ver2 = new HashMap<>(); + schemas_Ver2.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd2)); + schemas_Ver2.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd2)); + + final XMLValidationSchema valSchemaVal_Ver2 = sf.createSchema(null, schemas_Ver2, new EntityResolver() { + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException + { + System.out.println("ResolveEntity() - Public id: [" + publicId + "], System id: [" + systemId + "]"); + + InputSource is = null; + + File tmp = new File(URI.create(systemId).getPath()); + + String fileName = tmp.getName(); + if ("issue_175_import.xsd".equalsIgnoreCase(fileName)) + { + tmp = fImportXsd2; + } + + try + { + is = new InputSource(new FileReader(tmp)); + is.setSystemId(tmp.toURI().toString()); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + + System.out.println("ResolveEntity() - System Id: [" + is.getSystemId() + "]"); + + return is; + } + }); + + // Build Schema Validation object for Version 3 + final File fMainXsd3 = new File(getClass().getResource("schema/main/issue_175_main_v3.xsd").getFile()); + final File fImportXsd3 = new File(getClass().getResource("schema/import/issue_175_import_v2.xsd").getFile()); + + final Map schemas_Ver3 = new HashMap<>(); + schemas_Ver3.put("http://www.example.org/issue_175_main", new StreamSource(fMainXsd3)); + schemas_Ver3.put("http://www.example.org/issue_175_import", new StreamSource(fImportXsd3)); + + final XMLValidationSchema valSchemaVal_Ver3 = sf.createSchema(null, schemas_Ver3, new EntityResolver() { + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException + { + System.out.println("ResolveEntity() - Public id: [" + publicId + "], System id: [" + systemId + "]"); + + InputSource is = null; + + File tmp = new File(URI.create(systemId).getPath()); + + String fileName = tmp.getName(); + if ("issue_175_import.xsd".equalsIgnoreCase(fileName)) + { + tmp = fImportXsd3; + } + + try + { + is = new InputSource(new FileReader(tmp)); + is.setSystemId(tmp.toURI().toString()); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + + System.out.println("ResolveEntity() - System Id: [" + is.getSystemId() + "]"); + + return is; + } + }); + + try + { + // Xsd Validation - Version 1 Schema, Version 2 XML data + final WstxInputFactory xf = new WstxInputFactory(); + xf.configureForXmlConformance(); + + final XMLStreamReader2 xmlRdr = (XMLStreamReader2) xf.createXMLStreamReader(getClass().getResourceAsStream("Issue_175_ver2.xml"), "utf-8"); + xmlRdr.setValidationProblemHandler(new ValidationProblemHandler() + { + @Override + public void reportProblem(XMLValidationProblem arg0) throws XMLValidationException + { + if (arg0.getSeverity() == XMLValidationProblem.SEVERITY_WARNING ) + { + System.out.println("Validation Warning: " + arg0.getMessage()); + } + else + { + System.out.println("Validation Error: " + arg0.getMessage()); + throw arg0.toException(); + } + } + }); + + // Validate + xmlRdr.validateAgainst(valSchemaVal_Ver1); + + // Run thru each element, etc. + while(xmlRdr.hasNext()) { xmlRdr.next(); } + + Assert.fail("Error expected"); + } + catch (Exception ex) + { + //ex.printStackTrace(); + Assert.assertEquals("ParseError at [row,col]:[12,3]\n" + + "Message: element \"Element5\" was found where no element may occur", ex.getMessage()); + } + + try + { + // Xsd Validation - Version 2 Schema, Version 3 XML data + final WstxInputFactory xf = new WstxInputFactory(); + xf.configureForXmlConformance(); + + final XMLStreamReader2 xmlRdr = (XMLStreamReader2) xf.createXMLStreamReader(getClass().getResourceAsStream("Issue_175_ver3.xml"), "utf-8"); + xmlRdr.setValidationProblemHandler(new ValidationProblemHandler() + { + @Override + public void reportProblem(XMLValidationProblem arg0) throws XMLValidationException + { + if (arg0.getSeverity() == XMLValidationProblem.SEVERITY_WARNING ) + { + System.out.println("Validation Warning: " + arg0.getMessage()); + } + else + { + System.out.println("Validation Error: " + arg0.getMessage()); + throw arg0.toException(); + } + } + }); + + // Validate + xmlRdr.validateAgainst(valSchemaVal_Ver2); + + // Run thru each element, etc. + while(xmlRdr.hasNext()) { xmlRdr.next(); } + + Assert.fail("Error expected"); + } + catch (Exception ex) + { + //ex.printStackTrace(); + Assert.assertEquals("ParseError at [row,col]:[6,2]\n" + + "Message: unexpected attribute \"Attrib4\"", ex.getMessage()); + } + + try + { + // Xsd Validation - Version 3 Schema, Version 1 XML data + final WstxInputFactory xf = new WstxInputFactory(); + xf.configureForXmlConformance(); + + XMLStreamReader2 xmlRdr = (XMLStreamReader2) xf.createXMLStreamReader(getClass().getResourceAsStream("Issue_175_ver1.xml"), "utf-8"); + xmlRdr.setValidationProblemHandler(new ValidationProblemHandler() + { + @Override + public void reportProblem(XMLValidationProblem arg0) throws XMLValidationException + { + if (arg0.getSeverity() == XMLValidationProblem.SEVERITY_WARNING ) + { + System.out.println("Validation Warning: " + arg0.getMessage()); + } + else + { + System.out.println("Validation Error: " + arg0.getMessage()); + throw arg0.toException(); + } + } + }); + + // Validate + xmlRdr.validateAgainst(valSchemaVal_Ver3); + + // Run thru each element, etc. + while(xmlRdr.hasNext()) { xmlRdr.next(); } + + Assert.fail("Error expected"); + } + catch (Exception ex) + { + //ex.printStackTrace(); + Assert.assertEquals("ParseError at [row,col]:[6,2]\n" + + "Message: element \"Data\" is missing \"Attrib4\" attribute", ex.getMessage()); + } + } + catch (Exception ex) + { + ex.printStackTrace(); + Assert.fail("Unexpected error: " + ex.toString()); + } + } +} diff --git a/src/test/resources/wstxtest/msv/schema/import/issue_175_import_v1.xsd b/src/test/resources/wstxtest/msv/schema/import/issue_175_import_v1.xsd new file mode 100644 index 00000000..1f1d3ec1 --- /dev/null +++ b/src/test/resources/wstxtest/msv/schema/import/issue_175_import_v1.xsd @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/wstxtest/msv/schema/import/issue_175_import_v2.xsd b/src/test/resources/wstxtest/msv/schema/import/issue_175_import_v2.xsd new file mode 100644 index 00000000..93497ffe --- /dev/null +++ b/src/test/resources/wstxtest/msv/schema/import/issue_175_import_v2.xsd @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v1.xsd b/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v1.xsd new file mode 100644 index 00000000..6edc2506 --- /dev/null +++ b/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v1.xsd @@ -0,0 +1,109 @@ + + + + + + + + + Schema definition for Issue #175 (issue_175_main.xsd) + + + + + + + + + + + + + + + + + + + + + + + + + + + Main element in the XML + + + + + + + + + + + + + + + + + + First data element + + + + + + + + Second data element + + + + + + + + Third data element + + + + + + + + + + + + + + + + + Fourth data element + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v2.xsd b/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v2.xsd new file mode 100644 index 00000000..a9d5fcef --- /dev/null +++ b/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v2.xsd @@ -0,0 +1,118 @@ + + + + + + + + + Schema definition for Issue #175 (issue_175_main.xsd) + + + + + + + + + + + + + + + + + + + + + + + + + + + Main element in the XML + + + + + + + + + + + + + + + + + + + First data element + + + + + + + + Second data element + + + + + + + + Third data element + + + + + + + + + + + + + + + + + Fourth data element + + + + + + + + + + + + + + + + + Fifth data element (v2.0) + + + + \ No newline at end of file diff --git a/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v3.xsd b/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v3.xsd new file mode 100644 index 00000000..1b5c04ed --- /dev/null +++ b/src/test/resources/wstxtest/msv/schema/main/issue_175_main_v3.xsd @@ -0,0 +1,126 @@ + + + + + + + + + Schema definition for Issue #175 (issue_175_main.xsd) + + + + + + + + + + + + + + + + + + + + + + + + + + + Main element in the XML + + + + + + + + + + + + + + + + + + + First data element + + + + + + + + Second data element + + + + + + + + Third data element + + + + + + + + + + + + + + + + + Fourth data element + + + + + + + + + + + + + + + + + + + + + + + + + Fifth data element (v2.0) + + + + \ No newline at end of file