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;
}
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();
}
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();
}
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;
}
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;
}
Aggregations