diff --git a/src/main/java/org/harctoolbox/irp/IrpDatabase.java b/src/main/java/org/harctoolbox/irp/IrpDatabase.java index 7ef74e5a..4b0b01c5 100644 --- a/src/main/java/org/harctoolbox/irp/IrpDatabase.java +++ b/src/main/java/org/harctoolbox/irp/IrpDatabase.java @@ -123,6 +123,13 @@ public static boolean isValidating() { } public static synchronized void setValidating(boolean newValidating) throws SAXException { + if (newValidating && !XmlUtils.canValidate()) { + logger.log(Level.WARNING, "Validating not possible, ignoring."); + validating = false; + schema = null; + return; + } + validating = newValidating; if (validating) { if (schema == null) diff --git a/src/main/java/org/harctoolbox/xml/XmlUtils.java b/src/main/java/org/harctoolbox/xml/XmlUtils.java index a7e3cbba..67803a12 100644 --- a/src/main/java/org/harctoolbox/xml/XmlUtils.java +++ b/src/main/java/org/harctoolbox/xml/XmlUtils.java @@ -119,11 +119,26 @@ public final class XmlUtils { private static boolean debug = false; - private static final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + private static final SchemaFactory schemaFactory; static { - LSResourceResolver resourceResolver = new MyLSResourceResolver(); - schemaFactory.setResourceResolver(resourceResolver); + SchemaFactory sf; + try { + sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + } catch (IllegalArgumentException ex) { + logger.log(Level.WARNING, "SchemaFactory.newInstance failed, validation will not be available!"); + sf = null; + } + + schemaFactory = sf; + if (schemaFactory != null) { + LSResourceResolver resourceResolver = new MyLSResourceResolver(); + schemaFactory.setResourceResolver(resourceResolver); + } + } + + public static boolean canValidate() { + return schemaFactory != null; } public static void setDebug(boolean dbg) { @@ -405,7 +420,7 @@ public static void printDOM(Document doc) { } public static Schema readSchema(Source source) throws SAXException { - return schemaFactory.newSchema(source); + return canValidate() ? schemaFactory.newSchema(source) : null; } public static Schema readSchema(File schemaFile) throws SAXException { @@ -417,7 +432,7 @@ public static Schema readSchema(InputStream inputStream) throws SAXException { } public static Schema readSchema(URL schemaUrl) throws SAXException { - return schemaFactory.newSchema(schemaUrl); + return canValidate() ? schemaFactory.newSchema(schemaUrl) : null; } public static Schema readSchema(String schemaString) throws SAXException, FileNotFoundException, IOException { diff --git a/src/test/java/org/harctoolbox/irp/IrpTransmogrifierNGTest.java b/src/test/java/org/harctoolbox/irp/IrpTransmogrifierNGTest.java index fbc7d04a..a2edc28e 100644 --- a/src/test/java/org/harctoolbox/irp/IrpTransmogrifierNGTest.java +++ b/src/test/java/org/harctoolbox/irp/IrpTransmogrifierNGTest.java @@ -9,6 +9,7 @@ import org.harctoolbox.cmdline.CmdUtils; import org.harctoolbox.cmdline.FrequencyParser; import org.harctoolbox.cmdline.ProgramExitStatus; +import org.harctoolbox.xml.XmlUtils; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; @@ -692,6 +693,10 @@ public void testListHelp() { @Test(enabled = true) public void testValidate() { System.out.println("testValidate"); + if (!XmlUtils.canValidate()) { + System.out.println("test skipped, since validation not available on this platform"); + return; + } String result = execute("-c src/main/resources/IrpProtocols.xml,src/test/resources/IrpProtocols-silly.xml list"); assertTrue(result.length() > 1000); result = execute("--validate -c src/main/resources/IrpProtocols.xml,src/test/resources/IrpProtocols-silly.xml list");