Repository: dita-ot/dita-ot
Issue: Guard against possible NPE in XMLUtils.toErrorReporter(DITAOTLogger)

Using an invalid plugin I got this NPE in the XMLUtils.toErrorReporter method:

      .... java.lang.NullPointerException: Cannot invoke "net.sf.saxon.s9api.Location.getSystemId()" because the return value of "net.sf.saxon.s9api.XmlProcessingError.getLocation()" is null
	at org.dita.dost.util.XMLUtils.lambda$0(XMLUtils.java:202)
	at net.sf.saxon.style.Compilation.compileSingletonPackage(Compilation.java:117)
	at net.sf.saxon.s9api.XsltCompiler.compile(XsltCompiler.java:838)
	at org.dita.dost.module.XsltModule.execute(XsltModule.java:112)
	at org.dita.dost.ant.ExtensibleAntInvoker.execute(ExtensibleAntInvoker.java:189)
	
the NPE is caused by a FileNotFoundException thrown somewhere because an XSLT stylesheet is missing in my plugin:

       .../plugins/com.oxygenxml.webhelp.responsive/xsl/indexterms/extractIndexterms.xsl (No such file or directory)
         [pipeline] 	at java.base/java.io.FileInputStream.open0(Native Method)
         [pipeline] 	at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
         [pipeline] 	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
         [pipeline] 	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
         [pipeline] 	at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
         [pipeline] 	at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:189)
         [pipeline] 	at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:1114)
         [pipeline] 	at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
         [pipeline] 	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
         [pipeline] 	at org.ditang.relaxng.defaults.RelaxDefaultsParserConfiguration.parse(RelaxDefaultsParserConfiguration.java:119)
         [pipeline] 	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
         [pipeline] 	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
         [pipeline] 	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
         [pipeline] 	at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
         [pipeline] 	at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:439)
         [pipeline] 	at net.sf.saxon.event.Sender.send(Sender.java:168)
         [pipeline] 	at net.sf.saxon.style.StylesheetModule.sendStylesheetSource(StylesheetModule.java:157)
         [pipeline] 	at net.sf.saxon.style.StylesheetModule.loadStylesheet(StylesheetModule.java:229)
         [pipeline] 	at net.sf.saxon.style.Compilation.compileSingletonPackage(Compilation.java:113)
         [pipeline] 	at net.sf.saxon.s9api.XsltCompiler.compile(XsltCompiler.java:838)
         [pipeline] 	at org.dita.dost.module.XsltModule.execute(XsltModule.java:112)
         [pipeline] 	at org.dita.dost.ant.ExtensibleAntInvoker.execute(ExtensibleAntInvoker.java:1

Changed files:
src/main/java/org/dita/dost/util/XMLUtils.java

diff --git a/src/main/java/org/dita/dost/util/XMLUtils.java b/src/main/java/org/dita/dost/util/XMLUtils.java
index e57172dfa..a4c81e88b 100644
--- a/src/main/java/org/dita/dost/util/XMLUtils.java
+++ b/src/main/java/org/dita/dost/util/XMLUtils.java
@@ -9,11 +9,11 @@ package org.dita.dost.util;
 
 import com.google.common.annotations.VisibleForTesting;
 import net.sf.saxon.expr.instruct.TerminationException;
-import net.sf.saxon.lib.*;
+import net.sf.saxon.lib.CollationURIResolver;
+import net.sf.saxon.lib.ErrorReporter;
+import net.sf.saxon.lib.ExtensionFunctionDefinition;
 import net.sf.saxon.s9api.*;
-import net.sf.saxon.s9api.streams.Predicates;
 import net.sf.saxon.s9api.streams.Step;
-import net.sf.saxon.s9api.streams.XdmStream;
 import org.apache.xml.resolver.tools.CatalogResolver;
 import org.dita.dost.exception.DITAOTException;
 import org.dita.dost.log.DITAOTLogger;
@@ -197,15 +197,18 @@ public final class XMLUtils {
                 logger.warn(error.getMessage());
             } else {
                 if (error.getCause() instanceof FileNotFoundException) {
-                    error.asWarning();
-                    final StringBuilder buf = new StringBuilder()
-                            .append(error.getLocation().getSystemId())
-                            .append(":")
-                            .append(error.getLocation().getLineNumber())
-                            .append(":")
-                            .append(error.getLocation().getColumnNumber())
-                            .append(": ")
-                            .append(error.getMessage());
+                    error = error.asWarning();
+                    final StringBuilder buf = new StringBuilder();
+                    final Location location = error.getLocation();
+                    if (location != null) {
+                        buf.append(location.getSystemId())
+                                .append(":")
+                                .append(location.getLineNumber())
+                                .append(":")
+                                .append(location.getColumnNumber())
+                                .append(": ");
+                    }
+                    buf.append(error.getMessage());
                     logger.warn(buf.toString());
                 } else {
                     logger.error(error.getMessage());