我已經基於Xalan
的folowwing XSLT
:與Xalan的XSLT捕獲異常
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
簡單地覆蓋從javax.xml.transform.ErrorListener
類error
,fatalError
和warning
方法和記錄異常:
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.
程序正常執行:沒有拋出或記錄異常,並且生成的文件爲空!
我該如何捕捉異常來處理它我想要的方式?
所以''XalanErrorListener是一類你寫的嗎?如果是這樣,您必須顯示該課程... – home
編輯完成! –