Skip to content

Commit

Permalink
Try to handle null SchemaFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
bengtmartensson committed Sep 30, 2024
1 parent afb0e97 commit 8770df0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/harctoolbox/irp/IrpDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/org/harctoolbox/xml/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 8770df0

Please sign in to comment.