use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class OpenConversionDialogHandler method execute.
@Override
public Object execute(ExecutionEvent event, List<IFile> list) {
CommonFunction.removeRepeateSelect(list);
if (list == null || list.size() == 0) {
MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.OpenConversionDialogHandler.msgTitle1"), Messages.getString("handler.OpenConversionDialogHandler.msg1"));
return null;
}
ArrayList<ConverterViewModel> converterViewModels = new ArrayList<ConverterViewModel>();
for (int i = 0; i < list.size(); i++) {
Object adapter = Platform.getAdapterManager().getAdapter(list.get(i), IConversionItem.class);
IConversionItem sourceItem = null;
if (adapter instanceof IConversionItem) {
sourceItem = (IConversionItem) adapter;
}
ConverterViewModel converterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_POSITIVE);
// 记住所选择的文件
converterViewModel.setConversionItem(sourceItem);
converterViewModels.add(converterViewModel);
}
IProject project = list.get(0).getProject();
ConversionWizard wizard = new ConversionWizard(converterViewModels, project);
TSWizardDialog dialog = new TSWizardDialog(shell, wizard) {
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
getButton(IDialogConstants.FINISH_ID).setText(Messages.getString("handler.OpenConversionDialogHandler.finishLbl"));
}
};
int result = dialog.open();
if (result == IDialogConstants.OK_ID) {
final List<ConverterViewModel> models = wizard.getConverterViewModels();
final List<IFile> targetFiles = new ArrayList<IFile>();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.setTaskName(Messages.getString("handler.OpenConversionDialogHandler.task1"));
monitor.beginTask(Messages.getString("handler.OpenConversionDialogHandler.task2"), models.size());
for (ConverterViewModel converterViewModel : models) {
try {
IProgressMonitor subMonitor = Progress.getSubMonitor(monitor, 1);
subMonitor.setTaskName(Messages.getString("handler.OpenConversionDialogHandler.task3") + converterViewModel.getConversionItem().getLocation().toOSString());
converterViewModel.convertWithoutJob(subMonitor);
List<File> tgtFileList = converterViewModel.getGenerateTgtFileList();
for (File f : tgtFileList) {
IFile tgtIfile = ConverterUtil.localPath2IFile(f.getAbsolutePath());
if (tgtIfile != null) {
targetFiles.add(tgtIfile);
}
}
// 若新转换的 xliff 文件重复,那么关闭已经打开的重复文件,防止文件同步冲突 robert 2013-04-01
Display.getDefault().syncExec(new Runnable() {
public void run() {
CommonFunction.closePointEditor(targetFiles);
}
});
} catch (ConverterException e) {
final String message = e.getMessage();
LOGGER.error("", e);
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), message);
}
});
} catch (Exception e) {
final String message = e.getMessage();
LOGGER.error("", e);
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), message);
}
});
}
}
monitor.done();
}
};
try {
new ProgressMonitorDialog(shell).run(true, true, runnable);
} catch (InvocationTargetException e) {
MessageDialog.openError(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), e.getMessage());
LOGGER.error("", e);
} catch (InterruptedException e) {
MessageDialog.openError(shell, Messages.getString("handler.OpenConversionDialogHandler.msgTitle2"), e.getMessage());
LOGGER.error(e.getMessage());
}
if (wizard.isOpenPreTranslation()) {
// 加载转换器扩展
final IExecutePretranslation[] impls = new IExecutePretranslation[1];
IConfigurationElement[] config2 = Platform.getExtensionRegistry().getConfigurationElementsFor("net.heartsome.converter.extension.pretranslation");
try {
for (IConfigurationElement e : config2) {
final Object o = e.createExecutableExtension("class");
if (o instanceof IExecutePretranslation) {
ISafeRunnable runnable1 = new ISafeRunnable() {
public void handleException(Throwable exception) {
LOGGER.error(Messages.getString("handler.OpenConversionDialogHandler.logger1"), exception);
}
public void run() throws Exception {
impls[0] = (IExecutePretranslation) o;
}
};
SafeRunner.run(runnable1);
}
}
} catch (CoreException ex) {
LOGGER.error(Messages.getString("handler.OpenConversionDialogHandler.logger1"), ex);
}
if (impls[0] != null) {
if (targetFiles.size() < 1) {
return null;
}
impls[0].executePreTranslation(targetFiles);
}
}
}
return null;
}
use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class OpenReverseConversionDialogHandler method execute.
public Object execute(ExecutionEvent event, List<IFile> list) {
if (list == null || list.size() == 0) {
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), Messages.getString("handler.OpenReverseConversionDialogHandler.msgTitle1"), Messages.getString("handler.OpenReverseConversionDialogHandler.msg1"));
return null;
}
// 首先验证是否是合并打开的文件 --robert 2012-10-17
System.out.println(list.get(0).getFullPath().toOSString());
if (isEditor) {
// 针对合并打开
if (list.get(0).getFullPath().toOSString().endsWith(".xlp")) {
List<String> multiFiles = new XLFHandler().getMultiFiles(list.get(0));
if (multiFiles.size() > 0) {
list = new ArrayList<IFile>();
}
for (String filePath : multiFiles) {
list.add(ResourceUtils.fileToIFile(filePath));
}
}
}
List<IFile> lstFiles = new ArrayList<IFile>();
XLFValidator.resetFlag();
for (IFile iFile : list) {
if (!XLFValidator.validateXliffFile(iFile)) {
lstFiles.add(iFile);
}
}
CommonFunction.removeRepeateSelect(list);
XLFValidator.resetFlag();
if (!(list instanceof ArrayList)) {
list = new ArrayList<IFile>(list);
}
list.removeAll(lstFiles);
if (list.size() == 0) {
return null;
}
ArrayList<ConverterViewModel> converterViewModels = new ArrayList<ConverterViewModel>();
for (int i = 0; i < list.size(); i++) {
Object adapter = Platform.getAdapterManager().getAdapter(list.get(i), IConversionItem.class);
IConversionItem sourceItem = null;
if (adapter instanceof IConversionItem) {
sourceItem = (IConversionItem) adapter;
}
ConverterViewModel converterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_REVERSE);
// 记住所选择的文件
converterViewModel.setConversionItem(sourceItem);
converterViewModels.add(converterViewModel);
}
IProject project = list.get(0).getProject();
ReverseConversionWizard wizard = new ReverseConversionWizard(converterViewModels, project);
TSWizardDialog dialog = new TSWizardDialog(shell, wizard) {
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
getButton(IDialogConstants.FINISH_ID).setText(Messages.getString("handler.OpenConversionDialogHandler.finishLbl"));
}
};
int result = dialog.open();
if (result == IDialogConstants.OK_ID) {
final List<ConverterViewModel> models = wizard.getConverterViewModels();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.setTaskName(Messages.getString("handler.OpenReverseConversionDialogHandler.task1"));
monitor.beginTask(Messages.getString("handler.OpenReverseConversionDialogHandler.task2"), models.size());
for (ConverterViewModel converterViewModel : models) {
try {
IProgressMonitor subMonitor = Progress.getSubMonitor(monitor, 1);
subMonitor.setTaskName(Messages.getString("handler.OpenReverseConversionDialogHandler.task3") + converterViewModel.getConversionItem().getLocation().toOSString());
converterViewModel.convertWithoutJob(subMonitor);
} catch (ConverterException e) {
// Bug #2485:转换文件失败时,提示框显示有问题
throw new InvocationTargetException(e, e.getMessage());
}
}
monitor.done();
}
};
try {
new ProgressMonitorDialog(shell).run(true, true, runnable);
} catch (InvocationTargetException e) {
MessageDialog.openInformation(shell, Messages.getString("handler.OpenReverseConversionDialogHandler.msgTitle2"), e.getMessage());
LOGGER.error("", e);
} catch (InterruptedException e) {
MessageDialog.openInformation(shell, Messages.getString("handler.OpenReverseConversionDialogHandler.msgTitle2"), e.getMessage());
LOGGER.error("", e);
}
}
return null;
}
use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class ConverterViewModel method convert.
/**
* 正向转换
* @param sourceItem
* @param monitor
* @return ;
*/
private Map<String, String> convert(final IConversionItem sourceItem, IProgressMonitor monitor) throws ConverterException {
Map<String, String> result = null;
boolean convertFlg = false;
String xliffDir = ConverterUtil.toLocalPath(configBean.getXliffDir());
String targetFile = ConverterUtil.toLocalPath(configBean.getTarget());
String skeletonFile = ConverterUtil.toLocalPath(configBean.getSkeleton());
// 转换前的准备
ConverterContext converterContext = new ConverterContext(configBean);
final Map<String, String> configuration = converterContext.getConvertConfiguration();
// 转换前,生成临时的XLIFF文件,用此文件生成指定目标语言的XLIFF文件
File targetTempFile = null;
try {
targetTempFile = File.createTempFile("tempxlf", "xlf");
} catch (IOException e) {
LOGGER.error(Messages.getString("model.ConverterViewModel.msg10"), e);
}
configuration.put(Converter.ATTR_XLIFF_FILE, targetTempFile.getAbsolutePath());
if (configBean.getFileType().equals(FileFormatUtils.MS)) {
IPreferenceStore ps = net.heartsome.cat.ts.ui.Activator.getDefault().getPreferenceStore();
String path = ps.getString(ITranslationPreferenceConstants.PATH_OF_OPENOFFICE);
String port = ps.getString(ITranslationPreferenceConstants.PORT_OF_OPENOFFICE);
configuration.put("ooPath", path);
configuration.put("ooPort", port);
}
// 创建skeleton文件
File skeleton = new File(skeletonFile);
if (!skeleton.exists()) {
try {
File parent = skeleton.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
skeleton.createNewFile();
} catch (IOException e) {
String message = MessageFormat.format(Messages.getString("model.ConverterViewModel.msg11"), skeletonFile);
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterException(status);
}
}
try {
// 执行转换
Converter converter = getConverter();
if (converter == null) {
// Build a message
String message = Messages.getString("model.ConverterViewModel.msg2") + configBean.getFileType();
// Build a new IStatus
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
throw new ConverterException(status);
}
result = converter.convert(configuration, monitor);
final String alert = result.get("ttx2xlfAlert39238409230481092830");
if (result.containsKey("ttx2xlfAlert39238409230481092830")) {
//ttx 转 xlf 时,提示含有未预翻译,不推荐,但没办法。
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.getString("handler.PreviewTranslationHandler.msgTitle"), alert);
}
});
}
// 处理骨架文件,将骨架文件路径修改为项目相对路径,此路径写入external-file节点的href属性
String projectPath = sourceItem.getProject().getLocation().toOSString();
String sklPath = skeletonFile.replace(projectPath, "");
// 处理目标语言, 创建多个目标语言的文件
List<Language> tgtLang = configBean.getHasSelTgtLangList();
if (tgtLang != null && tgtLang.size() > 0) {
// 解析XLIFF文件
File f = new File(targetTempFile.getAbsolutePath());
FileInputStream is = null;
byte[] b = new byte[(int) f.length()];
try {
is = new FileInputStream(f);
is.read(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
VTDGen vg = new VTDGen();
vg.setDoc(b);
try {
vg.parse(true);
} catch (VTDException e) {
String message = Messages.getString("model.ConverterViewModel.msg12");
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterException(status);
}
VTDNav vn = vg.getNav();
VTDUtils vu = new VTDUtils();
// 生成多个XLIFF文件,只是修改目标语言和骨架文件路径
for (Language lang : tgtLang) {
// 修复 bug 2949 ,当文件名中出现 XLIFF 时,文件名获取失败,下面注释代码为之前的代码。 --robert 2013-04-01
// String[] pathArray = targetFile.split(Constant.FOLDER_XLIFF);
// StringBuffer xlffPath = new StringBuffer(pathArray[0]);
// xlffPath.append(Constant.FOLDER_XLIFF).append(File.separator).append(lang.getCode())
// .append(pathArray[1]);
String fileName = targetFile.substring(xliffDir.length());
StringBuffer xlfPahtSB = new StringBuffer();
xlfPahtSB.append(xliffDir);
xlfPahtSB.append(File.separator);
xlfPahtSB.append(lang.getCode());
xlfPahtSB.append(fileName);
File tmpFile = new File(xlfPahtSB.toString());
generateTgtFileList.add(tmpFile);
if (!tmpFile.exists()) {
File parent = tmpFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try {
tmpFile.createNewFile();
} catch (IOException e) {
String message = Messages.getString("model.ConverterViewModel.msg13");
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterException(status);
}
}
try {
vu.bind(vn.duplicateNav());
} catch (NavException e) {
LOGGER.error("", e);
}
XMLModifier xm = vu.update("/xliff/file/@target-language", lang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST);
xm = vu.update(null, xm, "/xliff/file/header/skl/external-file/@href", TextUtil.cleanString(sklPath));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(tmpFile);
// 写入文件
xm.output(fos);
} catch (Exception e) {
String message = Messages.getString("model.ConverterViewModel.msg13");
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterNotFoundException(status);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
}
}
vg.clear();
}
convertFlg = true;
} catch (OperationCanceledException e) {
LOGGER.info("ConverterViewerModel: 取消转换");
} finally {
if (!convertFlg) {
for (File f : generateTgtFileList) {
if (f != null && f.exists()) {
f.delete();
}
}
if (skeleton != null && skeleton.exists()) {
skeleton.delete();
}
}
targetTempFile.delete();
sourceItem.refresh();
}
return result;
}
use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class ConverterViewModel method reverseConvert.
/**
* 逆向转换
* @param sourceItem
* @param monitor
* @return ;
*/
private Map<String, String> reverseConvert(IConversionItem sourceItem, IProgressMonitor monitor) throws ConverterException {
Map<String, String> result = null;
String sourcePathStr = configBean.getSource();
String targetFile = ConverterUtil.toLocalPath(configBean.getTarget());
boolean convertSuccessful = false;
File target = null;
try {
target = new File(targetFile);
if (!target.exists()) {
try {
File parent = target.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
target.createNewFile();
} catch (IOException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("model.ConverterViewModel.logger4");
Object[] args = { targetFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
}
}
Converter converter = getConverter();
if (converter == null) {
// Build a message
String message = Messages.getString("model.ConverterViewModel.msg3") + configBean.getFileType();
// Build a new IStatus
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
throw new ConverterNotFoundException(status);
}
ConverterContext converterContext = new ConverterContext(configBean);
final Map<String, String> configuration = converterContext.getReverseConvertConfiguraion();
if (configBean.getFileType().equals("x-msoffice2003")) {
IPreferenceStore ps = net.heartsome.cat.ts.ui.Activator.getDefault().getPreferenceStore();
String path = ps.getString(ITranslationPreferenceConstants.PATH_OF_OPENOFFICE);
String port = ps.getString(ITranslationPreferenceConstants.PORT_OF_OPENOFFICE);
configuration.put("ooPath", path);
configuration.put("ooPort", port);
}
result = converter.convert(configuration, monitor);
convertSuccessful = true;
} catch (OperationCanceledException e) {
// 捕获用户取消操作的异常
LOGGER.info(Messages.getString("model.ConverterViewModel.logger2"), e);
throw e;
} catch (ConverterNotFoundException e) {
// Let the StatusManager handle the Status and provide a hint
StatusManager.getManager().handle(e.getStatus(), StatusManager.LOG | StatusManager.SHOW);
if (LOGGER.isErrorEnabled()) {
LOGGER.error(e.getMessage(), e);
}
throw e;
} catch (ConverterException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error(MessageFormat.format(Messages.getString("model.ConverterViewModel.logger3"), sourcePathStr), e);
}
throw e;
} finally {
if (!convertSuccessful) {
// 在转换失败或用户取消转换时,清除目标文件和骨架文件
if (target != null && target.exists()) {
target.delete();
}
}
sourceItem.refresh();
}
return result;
}
use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class ConverterViewModel method convert.
@Override
public Map<String, String> convert(Map<String, String> parameters) {
Converter converter = getConverter();
if (converter != null) {
System.out.println("" + converter.getName());
try {
converter.convert(null, null);
} catch (ConverterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String> result = new HashMap<String, String>(1);
result.put("name", converter.getName());
return result;
}
return null;
}
Aggregations