Search in sources :

Example 1 with EntityException

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

the class ImportAbstract method doImport.

/**
	 * 导入文件内容
	 * @param fileContent
	 * @param srcLang
	 * @param monitor
	 * @return ;
	 * @throws ImportException
	 */
public int doImport(String fileContent, String srcLang, IProgressMonitor monitor) throws ImportException {
    if (monitor == null) {
        this.monitor = new NullProgressMonitor();
    } else {
        this.monitor = monitor;
    }
    // 解析文件
    String message = "";
    VTDGen vg = new VTDGen();
    vg.setDoc(fileContent.getBytes());
    try {
        vg.parse(true);
    } catch (EncodingException e) {
        logger.error(Messages.getString("document.ImportAbstract.logger1"), e);
        message = Messages.getString("document.ImportAbstract.msg1");
        throw new ImportException(message + e.getMessage());
    } catch (EOFException e) {
        logger.error("", e);
        message = Messages.getString("document.ImportAbstract.tbx.msg1");
        throw new ImportException(message + e.getMessage());
    } catch (EntityException e) {
        logger.error("", e);
        message = Messages.getString("document.ImportAbstract.tbx.msg1");
        throw new ImportException(message + e.getMessage());
    } catch (ParseException e) {
        logger.error(Messages.getString("document.ImportAbstract.logger3"), e);
        String errMsg = e.getMessage();
        if (errMsg.indexOf("invalid encoding") != -1) {
            // 编码异常
            message = Messages.getString("document.ImportAbstract.msg1");
        } else {
            message = Messages.getString("document.ImportAbstract.tbx.msg1");
        }
        throw new ImportException(message + e.getMessage());
    }
    try {
        // 构建VTD解析工具
        vu = new VTDUtils(vg.getNav());
        dbOperator.beginTransaction();
        // 执行导入
        executeImport(srcLang);
        dbOperator.commit();
    } catch (VTDException e) {
        logger.error("", e);
        try {
            dbOperator.rollBack();
        } catch (SQLException e1) {
            logger.error("", e);
        }
        return FAILURE_4;
    } catch (SQLException e) {
        logger.error("", e);
        try {
            dbOperator.rollBack();
        } catch (SQLException e1) {
            logger.error("", e1);
        }
        throw new ImportException(Messages.getString("document.ImportAbstract.tbx.importDbError") + e.getMessage());
    } catch (OperationCanceledException e) {
        logger.error("", e);
        try {
            dbOperator.rollBack();
        } catch (SQLException e1) {
            logger.error("", e1);
            return CANCEL;
        }
    } catch (Exception e) {
        logger.error("", e);
        try {
            dbOperator.rollBack();
        } catch (SQLException e1) {
            logger.error("", e1);
        }
        throw new ImportException(Messages.getString("document.ImportAbstract.tbx.importDbError") + e.getMessage());
    }
    return SUCCESS;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) EncodingException(com.ximpleware.EncodingException) SQLException(java.sql.SQLException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) EntityException(com.ximpleware.EntityException) VTDGen(com.ximpleware.VTDGen) NavException(com.ximpleware.NavException) EncodingException(com.ximpleware.EncodingException) SQLException(java.sql.SQLException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ImportException(net.heartsome.cat.common.core.exception.ImportException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ModifyException(com.ximpleware.ModifyException) ParseException(com.ximpleware.ParseException) VTDException(com.ximpleware.VTDException) EntityException(com.ximpleware.EntityException) ImportException(net.heartsome.cat.common.core.exception.ImportException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) VTDException(com.ximpleware.VTDException) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException)

Example 2 with EntityException

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

the class DocUtils method isTBX.

/**
	 * 判断是否是正确的 TBX 文件
	 * @param fileName
	 *            TBX 文件的全路径
	 * @return 反回null,验证失败
	 * @throws ParseException
	 * @throws EntityException
	 * @throws EOFException
	 * @throws EncodingException
	 * @throws FileNotFoundException
	 */
public static VTDUtils isTBX(String fileName) throws EncodingException, ParseException, FileNotFoundException {
    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 = "/martif";
    VTDUtils vtdUtils = new VTDUtils();
    try {
        vtdUtils.bind(vn);
        ap.selectXPath(rootPath);
        if (ap.evalXPath() == -1) {
            // } else {
            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 vtdUtils;
}
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)

Example 3 with EntityException

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

the class TextUtil method loadISOLang.

private static void loadISOLang(String strLangFile) {
    if (strLangFile == null) {
        return;
    }
    ISOLang = new Hashtable<String, String>();
    VTDGen vg = new VTDGen();
    // vg.setDoc(strLangFile.getBytes());
    try {
        vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(strLangFile)));
        vg.parse(true);
        VTDNav vn = vg.getNav();
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/languages/lang");
        int codeIndex;
        String code = null;
        String langName;
        while ((ap.evalXPath()) != -1) {
            codeIndex = vn.getAttrVal("code");
            if (codeIndex != -1) {
                code = vn.toString(codeIndex);
            }
            langName = vu.getElementPureText();
            if (code != null && langName != null) {
                ISOLang.put(code, langName);
            }
        }
        ap.resetXPath();
    } catch (NavException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathEvalException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EncodingException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EOFException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EntityException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (ParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (IOException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } finally {
        vg.clear();
    }
}
Also used : MessageFormat(java.text.MessageFormat) EncodingException(com.ximpleware.EncodingException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) VTDNav(com.ximpleware.VTDNav)

Example 4 with EntityException

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

the class TextUtil method loadLanguages.

private static void loadLanguages() {
    descriptions = null;
    isBidi = null;
    descriptions = new Hashtable<String, String>();
    isBidi = new Hashtable<String, String>();
    String strFile = CoreActivator.LANGUAGE_CODE_PATH;
    VTDGen vg = new VTDGen();
    try {
        vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(strFile)));
        vg.parse(true);
        VTDNav vn = vg.getNav();
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/languages/lang");
        int codeIndex;
        String code = null;
        int bidiIndex;
        String bidi = null;
        String langName;
        while ((ap.evalXPath()) != -1) {
            codeIndex = vn.getAttrVal("code");
            if (codeIndex != -1) {
                code = vn.toString(codeIndex);
            }
            bidiIndex = vn.getAttrVal("bidi");
            if (bidiIndex != -1) {
                bidi = vn.toString(bidiIndex);
            }
            langName = vu.getElementPureText();
            if (code != null && langName != null) {
                descriptions.put(code, langName);
            }
            if (code != null && bidi != null) {
                isBidi.put(code, bidi);
            }
        }
        ap.resetXPath();
    } catch (NavException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathEvalException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EncodingException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EOFException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EntityException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (ParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (IOException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } finally {
        vg.clear();
    }
}
Also used : MessageFormat(java.text.MessageFormat) EncodingException(com.ximpleware.EncodingException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) VTDNav(com.ximpleware.VTDNav)

Example 5 with EntityException

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

the class TextUtil method loadCountries.

private static void loadCountries() {
    String strFile = CoreActivator.ISO3166_1_PAHT;
    countries = new Hashtable<String, String>();
    VTDGen vg = new VTDGen();
    try {
        vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(strFile)));
        vg.parse(true);
        VTDNav vn = vg.getNav();
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/ISO_3166-1_List_en/ISO_3166-1_Entry");
        while ((ap.evalXPath()) != -1) {
            countries.put(vu.getChildContent("ISO_3166-1_Alpha-2_Code_element"), vu.getChildContent("ISO_3166-1_Country_name"));
        }
        ap.resetXPath();
    } catch (NavException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathEvalException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EncodingException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EOFException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EntityException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (ParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (IOException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } finally {
        vg.clear();
    }
}
Also used : MessageFormat(java.text.MessageFormat) EncodingException(com.ximpleware.EncodingException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) 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