Search in sources :

Example 1 with ImportException

use of net.heartsome.cat.common.core.exception.ImportException 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 ImportException

use of net.heartsome.cat.common.core.exception.ImportException in project translationstudio8 by heartsome.

the class NewTermDbImportPage method executeImport.

/**
	 * 执行导入
	 * @param tmxFile
	 * @param tbxFile
	 * @param dbMetaData
	 * @param monitor
	 *            ;
	 */
public void executeImport(String tbxFile, MetaData dbMetaData, IProgressMonitor monitor) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.setTaskName(Messages.getString("wizard.NewTermDbImportPage.task2"));
    monitor.beginTask("", 1);
    int tbxResult = -10;
    String message = "";
    if (tbxFile != null) {
        try {
            tbxResult = DatabaseService.importTbxWithFile(tbxFile, new SubProgressMonitor(monitor, 100), dbMetaData, getTbxImportStrategy());
        } catch (ImportException e) {
            message = e.getMessage();
        }
        if (!message.equals("")) {
            final String _message = message;
            Display.getDefault().asyncExec(new Runnable() {

                public void run() {
                    setErrorMessage(_message);
                }
            });
        }
    }
    StringBuffer resultMessage = new StringBuffer();
    if (tbxResult != DatabaseService.SUCCESS) {
        if (tbxResult == DatabaseService.FAILURE_1) {
            resultMessage.append(Messages.getString("wizard.NewTermDbImportPage.msg1"));
        } else if (tbxResult == DatabaseService.FAILURE_2) {
            resultMessage.append(Messages.getString("wizard.NewTermDbImportPage.msg2"));
        } else if (tbxResult == DatabaseService.FAILURE_3) {
            resultMessage.append(Messages.getString("wizard.NewTermDbImportPage.msg3"));
        } else if (tbxResult == DatabaseService.FAILURE_4) {
            resultMessage.append(Messages.getString("wizard.NewTermDbImportPage.msg4"));
        } else if (tbxResult == DatabaseService.FAILURE) {
            resultMessage.append(Messages.getString("wizard.NewTermDbImportPage.msg5"));
        } else if (tbxResult == DatabaseService.CANCEL) {
            resultMessage.append(Messages.getString("wizard.NewTermDbImportPage.msg6"));
        }
        if (!resultMessage.toString().equals("")) {
            final String _message = resultMessage.toString();
            Display.getDefault().asyncExec(new Runnable() {

                public void run() {
                    setErrorMessage(_message);
                }
            });
        }
    }
    monitor.done();
}
Also used : ImportException(net.heartsome.cat.common.core.exception.ImportException) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 3 with ImportException

use of net.heartsome.cat.common.core.exception.ImportException in project translationstudio8 by heartsome.

the class ImportTmx method importTmxFile.

public void importTmxFile(File tmxFile) throws ImportException {
    try {
        reader = new TmxReader(tmxFile);
        reader.tryToClearTags(true);
        String srcLang = reader.getTmxHeader().getSrclang();
        if (srcLang == null || srcLang.equals("*all*") || srcLang.equals("")) {
            // 从TMX中读取不到源语言,此时只能执行增加
            importStrategy = Constants.IMPORT_MODEL_ALWAYSADD;
        }
    } catch (TmxReadException e) {
        throw new ImportException(e.getMessage());
    }
    doImport();
}
Also used : ImportException(net.heartsome.cat.common.core.exception.ImportException)

Example 4 with ImportException

use of net.heartsome.cat.common.core.exception.ImportException in project translationstudio8 by heartsome.

the class Utils method clearTmxFile.

public static File clearTmxFile(File f) throws ImportException {
    Logger logger = LoggerFactory.getLogger(Utils.class);
    File tempFile = null;
    BufferedWriter writer = null;
    try {
        tempFile = File.createTempFile("tmxtemp", ".tmx");
        writer = new BufferedWriter(new FileWriter(tempFile));
    } catch (IOException e1) {
        logger.error("", e1);
    }
    String encoding = FileEncodingDetector.detectFileEncoding(f);
    FileInputStream in = null;
    try {
        in = new FileInputStream(f);
    } catch (FileNotFoundException e) {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e1) {
                logger.error("", e1);
            }
        }
        logger.error("", e);
    }
    InputStreamReader inr = null;
    try {
        inr = new InputStreamReader(in, encoding);
    } catch (UnsupportedEncodingException e1) {
        try {
            if (writer != null) {
                writer.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            logger.error("", e);
        }
        throw new ImportException(Messages.getString("database.Utils.msg1"));
    }
    BufferedReader bfr = new BufferedReader(inr);
    try {
        String line = bfr.readLine();
        // // Bug #3428
        line = clearXMLEnconding(line);
        boolean flg = true;
        while (line != null) {
            if (flg) {
                int tmpos = line.indexOf("<header");
                if (tmpos != -1) {
                    // porp started
                    StringBuffer headerbf = new StringBuffer();
                    headerbf.append(line);
                    int tempeos = line.indexOf(">", tmpos);
                    while (tempeos == -1) {
                        line = bfr.readLine();
                        if (line == null) {
                            throw new ImportException(Messages.getString("database.Utils.msg2"));
                        }
                        headerbf.append("\n").append(line);
                        tempeos = headerbf.indexOf(">");
                    }
                    String headerStart = headerbf.substring(0, tempeos + 1);
                    writer.write(headerStart);
                    int t = line.indexOf("/>");
                    if (t != -1) {
                        String end = line.substring(t + 2, line.length());
                        if (end != null && end.length() != 0) {
                            writer.write(end);
                        }
                        line = bfr.readLine();
                        flg = false;
                        continue;
                    }
                    t = line.indexOf("</header>");
                    if (t != -1) {
                        String end = line.substring(t, line.length());
                        writer.write(end);
                        line = bfr.readLine();
                        flg = false;
                        continue;
                    }
                    // read to </header>
                    line = bfr.readLine();
                    while (line != null) {
                        int endof = line.indexOf("</header>");
                        if (endof != -1) {
                            line = line.substring(endof, line.length());
                            flg = false;
                            break;
                        }
                        line = bfr.readLine();
                    }
                }
            }
            writer.write(line);
            line = bfr.readLine();
        }
    } catch (IOException e) {
        logger.error("", e);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
            if (bfr != null) {
                bfr.close();
            }
        } catch (IOException e) {
            logger.error("", e);
        }
    }
    return tempFile;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Logger(org.slf4j.Logger) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) ImportException(net.heartsome.cat.common.core.exception.ImportException) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 5 with ImportException

use of net.heartsome.cat.common.core.exception.ImportException in project translationstudio8 by heartsome.

the class DatabaseService method importTbxWithFile.

/**
	 * 将TBX文件导入到指定的数据库中
	 * @param fileName
	 *            TBX文件完整路径
	 * @param monitor
	 *            需要使用的进度条,如果不需要使用进度条,则为null
	 * @param metaData
	 *            数据库元数据,封装了一系列连接数据库的参数,用于连接数据,参考{@link MetaData}
	 * @param strategy
	 *            TBX导入策略,参考{@link Constants}中的定义
	 * @return 导入结果,为int型数据,参考{@link ImportAbstract}中的定义;;
	 * @throws ImportException
	 */
public static int importTbxWithFile(String fileName, IProgressMonitor monitor, MetaData metaData, int strategy) throws ImportException {
    File file = new File(fileName);
    if (file.exists() || !file.isDirectory()) {
        if (!fileName.toLowerCase().endsWith(".tbx")) {
            monitor.beginTask("", 100);
            File convet2tbx = null;
            try {
                convet2tbx = ConverterUtil.convert2Tbx(fileName, new SubProgressMonitor(monitor, 30));
            } catch (OperationCanceledException e) {
                return CANCEL;
            } catch (ImportException e) {
                LOGGER.error("", e);
                throw new ImportException(e.getMessage().replace("\n", " "));
            }
            if (convet2tbx != null) {
                file = convet2tbx;
            } else {
                file = new File(fileName);
            }
        } else {
            monitor.beginTask("", 70);
        }
        DBOperator dbOp = getDBOperator(metaData);
        try {
            dbOp.start();
            ImportAbstract impOp = new ImportTbx(dbOp, strategy);
            int result = impOp.doImport(file.getAbsolutePath(), new SubProgressMonitor(monitor, 70));
            return result;
        } catch (SQLException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger10") + dbOp.getMetaData().getDatabaseName(), e);
            return ImportAbstract.FAILURE_3;
        } catch (ClassNotFoundException e) {
            LOGGER.error(Messages.getString("service.DatabaseService.logger11"), e);
            return ImportAbstract.FAILURE_3;
        } finally {
            if (dbOp != null) {
                try {
                    dbOp.end();
                } catch (SQLException e) {
                    LOGGER.error(Messages.getString("service.DatabaseService.logger12"), e);
                }
            }
            monitor.done();
        }
    }
    return ImportAbstract.FAILURE;
}
Also used : ImportException(net.heartsome.cat.common.core.exception.ImportException) ImportTbx(net.heartsome.cat.document.ImportTbx) ImportAbstract(net.heartsome.cat.document.ImportAbstract) SQLException(java.sql.SQLException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SystemDBOperator(net.heartsome.cat.database.SystemDBOperator) DBOperator(net.heartsome.cat.database.DBOperator) File(java.io.File) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

ImportException (net.heartsome.cat.common.core.exception.ImportException)19 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 File (java.io.File)5 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 DBOperator (net.heartsome.cat.database.DBOperator)2 SystemDBOperator (net.heartsome.cat.database.SystemDBOperator)2 AutomaticQATrigger (net.heartsome.cat.ts.ui.xliffeditor.nattable.qa.AutomaticQATrigger)2 IProject (org.eclipse.core.resources.IProject)2 EOFException (com.ximpleware.EOFException)1 EncodingException (com.ximpleware.EncodingException)1 EntityException (com.ximpleware.EntityException)1 ModifyException (com.ximpleware.ModifyException)1