我有以下XSLT
基于Xalan
:
TransformerFactory factory = TransformerFactory.newInstance();
XalanErrorListener listener = new XalanErrorListener();
factory.setErrorListener(listener);
// Create transformer
StreamSource config = new StreamSource(xslPath);
Transformer transformer = factory.newTransformer(config);
// Create input / ouput
StreamSource source = new StreamSource(inputPath);
StreamResult result = new StreamResult(outputPath);
// Transform
transformer.transform(source, result);
我XalanErrorListener
只是覆盖error
,fatalError
和类中的warning
方法javax.xml.transform.ErrorListener
并记录异常:
public final class XalanErrorListener implements ErrorListener {
static final Logger LOGGER = LoggerFactory.getLogger(XalanErrorListener.class);
@Override
public void error(TransformerException exception) throws TransformerException {
LOGGER.error(exception);
}
@Override
public void fatalError(TransformerException exception) throws TransformerException {
LOGGER.error(exception);
}
@Override
public void warning(TransformerException exception) throws TransformerException {
LOGGER.warn(exception);
}
}
然而,在编码错误的文件上执行时,我在控制台中收到以下消息:
(Location of error unknown)
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:
Invalid byte 2 of 2-byte UTF-8 sequence.
程序正常执行:没有抛出或记录异常,生成的文件为空!
如何捕获异常以按我想要的方式处理它?