Search in sources :

Example 6 with EOFException

use of com.ximpleware.EOFException in project translationstudio8 by heartsome.

the class DocUtils method isTMX.

/**
	 * 判断是否是正确的 TMX 文件
	 * @param fileName
	 * @return ;
	 * @throws FileNotFoundException
	 * @throws ParseException
	 * @throws EntityException
	 * @throws EOFException
	 * @throws EncodingException
	 */
public static VTDUtils isTMX(String fileName) throws FileNotFoundException, EncodingException, ParseException {
    VTDGen vg = new VTDGen();
    FileInputStream fis = null;
    File f = null;
    try {
        f = new File(fileName);
        fis = new FileInputStream(f);
        byte[] b = new byte[(int) f.length()];
        int offset = 0;
        int numRead = 0;
        // I choose this value randomally,
        int numOfBytes = 1048576;
        // any other (not too big) value also can be here.
        if (b.length - offset < numOfBytes) {
            numOfBytes = b.length - offset;
        }
        while (offset < b.length && (numRead = fis.read(b, offset, numOfBytes)) >= 0) {
            offset += numRead;
            if (b.length - offset < numOfBytes) {
                numOfBytes = b.length - offset;
            }
        }
        vg.setDoc(b);
        vg.parse(true);
    } catch (IOException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger1"), e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    String rootPath = "/tmx";
    VTDUtils vu = new VTDUtils();
    try {
        vu.bind(vn);
        ap.selectXPath(rootPath);
        if (ap.evalXPath() == -1) {
            return null;
        }
    } catch (NavException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathEvalException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathParseException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } finally {
        vg.clear();
    }
    return vu;
}
Also used : NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) NavException(com.ximpleware.NavException) IOException(java.io.IOException) EncodingException(com.ximpleware.EncodingException) FileNotFoundException(java.io.FileNotFoundException) ParseException(com.ximpleware.ParseException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Aggregations

EOFException (com.ximpleware.EOFException)6 EncodingException (com.ximpleware.EncodingException)6 EntityException (com.ximpleware.EntityException)6 NavException (com.ximpleware.NavException)6 ParseException (com.ximpleware.ParseException)6 VTDGen (com.ximpleware.VTDGen)6 XPathEvalException (com.ximpleware.XPathEvalException)6 XPathParseException (com.ximpleware.XPathParseException)6 IOException (java.io.IOException)6 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)6 AutoPilot (com.ximpleware.AutoPilot)5 VTDNav (com.ximpleware.VTDNav)5 FileNotFoundException (java.io.FileNotFoundException)3 MessageFormat (java.text.MessageFormat)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ModifyException (com.ximpleware.ModifyException)1 TranscodeException (com.ximpleware.TranscodeException)1 VTDException (com.ximpleware.VTDException)1 SQLException (java.sql.SQLException)1