Search in sources :

Example 1 with DependencyException

use of cn.hutool.core.exceptions.DependencyException in project hutool by looly.

the class ExcelSaxUtil method readFrom.

/**
 * 从Excel的XML文档中读取内容,并使用{@link ContentHandler}处理
 *
 * @param xmlDocStream Excel的XML文档流
 * @param handler      文档内容处理接口,实现此接口用于回调处理数据
 * @throws DependencyException 依赖异常
 * @throws POIException        POI异常,包装了SAXException
 * @throws IORuntimeException  IO异常,如流关闭或异常等
 * @since 5.1.4
 */
public static void readFrom(InputStream xmlDocStream, ContentHandler handler) throws DependencyException, POIException, IORuntimeException {
    XMLReader xmlReader;
    try {
        // xmlReader = XMLReaderFactory.createXMLReader();
        // noinspection deprecation
        xmlReader = SAXHelper.newXMLReader();
    } catch (SAXException | ParserConfigurationException e) {
        if (e.getMessage().contains("org.apache.xerces.parsers.SAXParser")) {
            throw new DependencyException(e, "You need to add 'xerces:xercesImpl' to your project and version >= 2.11.0");
        } else {
            throw new POIException(e);
        }
    }
    xmlReader.setContentHandler(handler);
    try {
        xmlReader.parse(new InputSource(xmlDocStream));
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } catch (SAXException e) {
        throw new POIException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) IORuntimeException(cn.hutool.core.io.IORuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) DependencyException(cn.hutool.core.exceptions.DependencyException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) POIException(cn.hutool.poi.exceptions.POIException)

Aggregations

DependencyException (cn.hutool.core.exceptions.DependencyException)1 IORuntimeException (cn.hutool.core.io.IORuntimeException)1 POIException (cn.hutool.poi.exceptions.POIException)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1