SchemaFactoryImpl extends SchemaFactory2 { public Schema2 newSchema(SAXSource source) throws SAXException { Resolver resolver = null; LSResourceResolver resourceResolver = getResourceResolver(); if (resourceResolver != null) resolver = LS.createResolver(resourceResolver); SAXResolver saxResolver = new SAXResolver(resolver); ErrorHandler eh = getErrorHandler(); if (eh == null) eh = new DraconianErrorHandler(); Parseable<Pattern, NameClass, Locator, VoidValue, CommentListImpl, AnnotationsImpl> parseable = createParseable(source, saxResolver, eh); SchemaPatternBuilder spb = new SchemaPatternBuilder(); try { return new SchemaImpl(this, spb, SchemaBuilderImpl.parse(parseable, eh, getDatatypeLibraryFactory(), spb, false)); } catch (IOException io) { SAXParseException e = new SAXParseException(io.getMessage(), null, io); eh.fatalError(e); throw e; } catch (IllegalSchemaException e) { throw new SAXException("invalid schema"); } } protected  SchemaFactoryImpl(); Schema2 newSchema(SAXSource source); void setProperty(String name, Object object); Object getProperty(String name); static final public String PROPERTY_DATATYPE_LIBRARY_FACTORY; }
@Test(dataProvider = "valid") public void testValidCharStream(String schemaString, String docString) throws SAXException, IOException { factory().newSchema(charStreamSource(schemaString)).newValidator().validate(charStreamSource(docString)); }
@Test(dataProvider = "valid") public void testValidFile(String schemaString, String docString) throws SAXException, IOException { factory().newSchema(fileSource(schemaString)).newValidator().validate(fileSource(docString)); }
@Test public void testErrorHandlerNoThrow() throws SAXException, IOException { SchemaFactory f = factory(); Validator v = f.newSchema(charStreamSource(createSchema("doc"))).newValidator(); CountErrorHandler eh = new CountErrorHandler() { public void error(SAXParseException e) throws SAXException { if (errorCount == 0) Assert.assertEquals(e.getLineNumber(), 2); super.error(e); } }; v.setErrorHandler(eh); Assert.assertSame(v.getErrorHandler(), eh); v.validate(charStreamSource("<doc>\n<bad/></doc>")); Assert.assertTrue(eh.errorCount > 0); }
@Test(expectedExceptions = { RuntimeException.class }) public void testErrorHandlerThrowRuntime() throws SAXException, IOException { SchemaFactory f = factory(); Validator v = f.newSchema(charStreamSource(createSchema("doc"))).newValidator(); v.setErrorHandler(new DraconianErrorHandler() { public void error(SAXParseException e) throws SAXException { Assert.assertEquals(e.getLineNumber(), 2); throw new RuntimeException(); } }); v.validate(charStreamSource("<doc>\n<bad/></doc>")); throw new AssertionError(); }
@Test(expectedExceptions = { MySAXException.class }) public void testErrorHandlerThrowSAX() throws SAXException, IOException { SchemaFactory f = factory(); Validator v = f.newSchema(charStreamSource(createSchema("doc"))).newValidator(); v.setErrorHandler(new DraconianErrorHandler() { public void error(SAXParseException e) throws SAXException { Assert.assertEquals(e.getLineNumber(), 2); throw new MySAXException(); } }); v.validate(charStreamSource("<doc>\n<bad/></doc>")); throw new AssertionError(); }
@Test public void testInstanceResourceResolver() throws SAXException, IOException { SchemaFactory f = factory(); Validator v = f.newSchema(charStreamSource(element("doc", element("inner")))).newValidator(); Assert.assertNull(v.getResourceResolver()); LSResourceResolver rr = new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { int slashIndex = systemId.lastIndexOf('/'); if (slashIndex >= 0) systemId = systemId.substring(slashIndex + 1); Assert.assertEquals(systemId, "e.xml"); Assert.assertEquals(type, "http: LSInput in = new LSInputImpl(); in.setStringData("<inner/>"); return in; } }; v.setResourceResolver(rr); Assert.assertSame(v.getResourceResolver(), rr); v.validate(charStreamSource("<!DOCTYPE doc [ <!ENTITY e SYSTEM 'e.xml'> ]><doc>&e;</doc>")); }
@Test public void testSchemaResourceResolver() throws SAXException, IOException { SchemaFactory f = factory(); Assert.assertNull(f.getResourceResolver()); LSResourceResolver rr = new LSResourceResolver() { public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { Assert.assertEquals(systemId, "myschema"); Assert.assertEquals(type, getLSType()); Assert.assertNull(baseURI); Assert.assertNull(namespaceURI); Assert.assertNull(publicId); LSInput in = new LSInputImpl(); in.setStringData(createSchema("doc")); return in; } }; f.setResourceResolver(rr); Assert.assertSame(f.getResourceResolver(), rr); Validator v = f.newSchema(charStreamSource(externalRef("myschema"))).newValidator(); v.validate(charStreamSource("<doc/>")); }
@Test(expectedExceptions = { UnsupportedOperationException.class }) public void testNewSchemaNoArgs() throws SAXException { factory().newSchema(); }
@Test(dataProvider = "supportedFeatures") public void testFeatureInheritance(String feature, Boolean defaultValueObj) throws SAXException, SAXNotRecognizedException, SAXNotSupportedException { SchemaFactory f = factory(); boolean defaultValue = defaultValueObj; Assert.assertEquals(f.getFeature(feature), defaultValue); f.setFeature(feature, !defaultValue); ValidatorHandler vh = f.newSchema(charStreamSource(createSchema("doc"))).newValidatorHandler(); Assert.assertEquals(vh.getFeature(feature), defaultValue); }