use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class ShowNextUntranslatedHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (!(editor instanceof XLIFFEditorImplWithNatTable)) {
return null;
}
XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
int[] selectedRows = xliffEditor.getSelectedRows();
if (selectedRows.length < 1) {
return null;
}
Arrays.sort(selectedRows);
int lastSelectedRow = selectedRows[selectedRows.length - 1];
XLFHandler handler = xliffEditor.getXLFHandler();
int row = handler.getNextUntranslatedSegmentIndex(lastSelectedRow);
if (row != -1) {
xliffEditor.jumpToRow(row);
} else {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openWarning(window.getShell(), "", "不存在下一未翻译文本段。");
}
return null;
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class ShowPreviousFuzzyHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (!(editor instanceof XLIFFEditorImplWithNatTable)) {
return null;
}
XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) editor;
int[] selectedRows = xliffEditor.getSelectedRows();
if (selectedRows.length < 1) {
return null;
}
Arrays.sort(selectedRows);
int firstSelectedRow = selectedRows[0];
XLFHandler handler = xliffEditor.getXLFHandler();
int row = handler.getPreviousFuzzySegmentIndex(firstSelectedRow);
if (row != -1) {
xliffEditor.jumpToRow(row);
} else {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openWarning(window.getShell(), "", "不存在上一模糊匹配文本段。");
}
return null;
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class ImportDocxDialog method beginImport.
/**
* 开始导入功能
*/
public boolean beginImport(final String docxPath) throws Exception {
if (xliffEditor == null || xliffEditor.getXLFHandler() == null) {
xlfHandler = new XLFHandler();
} else {
xlfHandler = xliffEditor.getXLFHandler();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
HsMultiActiveCellEditor.commit(true);
}
});
xm = new XMLModifier(xlfHandler.getVnMap().get(xliffFullPath));
xlfHandler.saveAndReparse(xm, xliffFullPath);
}
// 开始解析 xliff 文件
tempXlfHandler = new XLFHandler();
parseXliff(tempXlfHandler);
// UNDO 这里还应判断导入时两个文件是否对应,
try {
ImportDocx importWord = new ImportDocx(docxPath, xliffFullPath);
List<RowBean> rowList = importWord.getDataFromWord();
hasComment = importWord.isHasComment();
// 现在开始判断每个tu 节点的标记是否异常。若有异常进行提示
rowListFor: for (RowBean rowBean : rowList) {
String rowId = rowBean.getRowId();
if (rowId == null || rowId.length() <= 0) {
continue rowListFor;
}
if (curRowIsLocked(rowId)) {
rowBean.setLocked(true);
continue rowListFor;
}
String srcText = getSrcTextFromXliffByRowId(rowId);
List<String> xliffSrcTagList = getTagFromSrcText(srcText);
List<String> xliffTgtTagList = new ArrayList<String>();
xliffTgtTagList.addAll(xliffSrcTagList);
// 获取 rowBean 中源文的标记
List<TagBean> rowSrcTagList = new ArrayList<TagBean>();
for (Object object : rowBean.getSrcElement()) {
if (object instanceof TagBean) {
rowSrcTagList.add((TagBean) object);
}
}
// 0、首先验证 word 文档中的当前行能否找到与之对应的 xliff 文件
if (srcText == null) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_notFindTU));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
// 1、首先验证源文标记是否缺失
if (rowSrcTagList.size() < xliffSrcTagList.size()) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagLose));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
// 2、验证word源文标记是否多出
if (rowSrcTagList.size() > xliffSrcTagList.size()) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagMore));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
// UNDO 这里还应确定导出的标记是否发生改变
for (TagBean bean : rowSrcTagList) {
String rowTagText = bean.getText();
for (int i = 0; i < xliffSrcTagList.size(); i++) {
String xlfTagText = xliffSrcTagList.get(i);
if (xlfTagText.indexOf(rowTagText) == 0) {
if (!xlfTagText.equals(rowTagText)) {
bean.setText(xlfTagText);
}
xliffSrcTagList.remove(i);
bean.setTagType(DocxCommonFuction.getTagType(xlfTagText));
break;
}
}
}
if (xliffSrcTagList.size() > 0) {
// docx 文档中的标记被更换了
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagNotSame));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
int startTag = 0;
for (TagBean bean : rowSrcTagList) {
if (bean.getTagType() == DocxConstant.PAIRSTAR) {
startTag++;
} else if (bean.getTagType() == DocxConstant.PAIREND) {
startTag--;
}
if (startTag < 0) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagPostionError));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
}
// 4、验证 目标文本段中标记错误,或者位置不对应的情况
// 先获得word 中目标文本中的标记
// 获取 rowBean 中源文的标记
List<TagBean> rowTgtTagList = new ArrayList<TagBean>();
for (Object object : rowBean.getTgtElement()) {
if (object instanceof TagBean) {
rowTgtTagList.add((TagBean) object);
}
}
int modifiedTagSum = 0;
for (TagBean bean : rowTgtTagList) {
String rowTagText = bean.getText();
// 因为标记不允许修改,因此在 xliff 中,目标文本中的标记就是源文中的标记
for (int i = 0; i < xliffTgtTagList.size(); i++) {
String xlfTagText = xliffTgtTagList.get(i);
if (xlfTagText.indexOf(rowTagText) == 0) {
if (!xlfTagText.equals(rowTagText)) {
bean.setText(xlfTagText);
}
xliffTgtTagList.remove(i);
modifiedTagSum++;
bean.setTagType(DocxCommonFuction.getTagType(xlfTagText));
break;
}
}
}
if (modifiedTagSum != rowTgtTagList.size()) {
// docx 文档中的标记被更换了
errorList.add(new ErrorBean(rowBean.getIndex(), false, DocxConstant.ERROR_tagNotSame));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
startTag = 0;
for (TagBean bean : rowTgtTagList) {
if (bean.getTagType() == DocxConstant.PAIRSTAR) {
startTag++;
} else if (bean.getTagType() == DocxConstant.PAIREND) {
startTag--;
}
if (startTag < 0) {
errorList.add(new ErrorBean(rowBean.getIndex(), false, DocxConstant.ERROR_tagPostionError));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
}
if (startTag != 0) {
errorList.add(new ErrorBean(rowBean.getIndex(), false, DocxConstant.ERROR_pairTagError));
errorRowSet.add(rowBean.getRowId());
}
}
// 验证完后,开始导入功能,如果有错误提示信息,开始提示
if (errorList.size() > 0) {
StringBuffer errorSB = new StringBuffer();
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip1"));
for (ErrorBean bean : errorList) {
errorSB.append(MessageFormat.format(Messages.getString("ImportDocxDialog.import.errorTip2"), bean.getIndex()));
if (bean.getErrorType() == DocxConstant.ERROR_notFindTU) {
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip3"));
continue;
}
if (bean.isSrc()) {
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip4"));
} else {
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip5"));
}
switch(bean.getErrorType()) {
case DocxConstant.ERROR_tagLose:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip6"));
break;
case DocxConstant.ERROR_tagMore:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip7"));
break;
case DocxConstant.ERROR_tagPostionError:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip8"));
break;
case DocxConstant.ERROR_tagNotSame:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip9"));
break;
case DocxConstant.ERROR_pairTagError:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip11"));
break;
default:
break;
}
}
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip10"));
final String errorTip = errorSB.toString();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
ErrorTipDialog errorDialog = new ErrorTipDialog(getShell(), errorTip);
int result = errorDialog.open();
if (result == IDialogConstants.CANCEL_ID) {
continuImport = false;
}
}
});
if (!continuImport) {
return false;
}
String rowId = "";
// 先将 有错误的文本段进行清除
for (int i = 0; i < rowList.size(); i++) {
rowId = rowList.get(i).getRowId();
if (errorRowSet.contains(rowId)) {
rowList.remove(i);
i--;
}
}
}
importDocxToXliffList(rowList);
if (xliffEditor != null) {
parseXliff(xlfHandler);
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
xliffEditor.reloadData();
HsMultiCellEditorControl.activeSourceAndTargetCell(xliffEditor);
}
});
}
} catch (final FlagErrorException e) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openWarning(Display.getDefault().getActiveShell(), Messages.getString("all.dialog.warning"), e.getMessage());
}
});
LOGGER.error(e.getMessage(), e);
return false;
} catch (Exception e) {
LOGGER.error(Messages.getString("ImportDocxDialog.LOGGER.logger2"), e);
}
return true;
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class PluginConfigManage method executePlugin.
/**
* 运行自定义的插件
* @param bean
* ;
*/
@SuppressWarnings("unchecked")
public void executePlugin(PluginConfigBean bean) {
String commandLine = bean.getCommandLine();
if (commandLine == null || "".equals(commandLine)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg1"));
return;
}
try {
// 当输出(进程)为当前文档时
if (bean.getOutput().equals(PluginConstants.SEGMENT)) {
// 先检查是否有已经打开的文件,若没有,退出插件执行
XLIFFEditorImplWithNatTable nattable = XLIFFEditorImplWithNatTable.getCurrent();
if (nattable == null) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg2"));
return;
}
XLFHandler handler = nattable.getXLFHandler();
List<String> selectRowIds = nattable.getSelectedRowIds();
if (selectRowIds.size() <= 0) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg3"));
return;
}
if (selectRowIds.size() > 1) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg4"));
}
String rowId = selectRowIds.get(0);
sendSegment(bean, handler, rowId);
// 执行后返回的文件
String returnContent = runPlugin(bean);
// 如果返回为交换文件,则更新当前文本段
if (bean.getInput().equals(PluginConstants.EXCHANGEFILE)) {
handler.updateAndSave(rowId, "", returnContent);
// 更新完后,要刷新界面。此处未做,滞留。
}
}
// 当输出(进程)为当前文档时
if (bean.getOutput().equals(PluginConstants.DOCUMENT)) {
XLFHandler handler;
IFile selectIFile = null;
// 先检查是否有已经打开的文件,若没有,退出插件执行
XLIFFEditorImplWithNatTable nattable = XLIFFEditorImplWithNatTable.getCurrent();
if (nattable == null) {
// 如果当前没有打开的文件,那么获取左边导航框中选中的文件
ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
if (selection == null || selection.isEmpty() || !(selection instanceof StructuredSelection)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg5"));
return;
}
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Iterator<Object> selectIt = structuredSelection.iterator();
if (selectIt.hasNext()) {
Object object = selectIt.next();
if (object instanceof IFile) {
selectIFile = (IFile) object;
String fileExtension = selectIFile.getFileExtension();
if (!CommonFunction.validXlfExtension(fileExtension)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg5"));
return;
}
}
}
handler = new XLFHandler();
// 打开该xliff文件
Map<String, Object> resultMap = handler.openFile(selectIFile.getLocation().toOSString());
if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg5"));
return;
}
} else {
handler = nattable.getXLFHandler();
selectIFile = ResourceUtil.getFile(nattable.getEditorInput());
// IEditorInput fileInput = nattable.getEditorInput();
}
sendDocument(bean, selectIFile);
if (bean.getInput().equals(PluginConstants.DOCUMENT)) {
// 重新解析该文件
Map<String, Object> resultMap = handler.openFile(selectIFile.getLocation().toOSString());
if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
MessageDialog.openError(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg6"));
return;
}
}
}
// 当输出(进程)为空时
if (bean.getOutput().equals(PluginConstants.NONE)) {
runPlugin(bean);
}
} catch (Exception e) {
LOGGER.error("", e);
MessageDialog.openError(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg7"));
}
}
use of net.heartsome.cat.ts.core.file.XLFHandler in project translationstudio8 by heartsome.
the class BatchQAHandler method execute.
@SuppressWarnings("unchecked")
public Object execute(ExecutionEvent event) throws ExecutionException {
isMultiFile = false;
preferenceStore = Activator.getDefault().getPreferenceStore();
// UNDO 如果焦点在其他视图上时,获取的文件错误。
IFile multiTempIFile = null;
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
final Shell shell = window.getShell();
// ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
IViewPart viewPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("net.heartsome.cat.common.ui.navigator.view");
ArrayList<IFile> selectIFiles = new ArrayList<IFile>();
if (HandlerUtil.getActivePart(event) instanceof IViewPart) {
ISelection currentSelection = (StructuredSelection) viewPart.getSite().getSelectionProvider().getSelection();
if (currentSelection != null && !currentSelection.isEmpty() && currentSelection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
Iterator<Object> selectIt = structuredSelection.iterator();
while (selectIt.hasNext()) {
Object object = selectIt.next();
if (object instanceof IFile) {
IFile selectFile = (IFile) object;
String fileExtension = selectFile.getFileExtension();
// 如果后缀名不是xlf,那么就进行提示
if (fileExtension == null || !CommonFunction.validXlfExtension(fileExtension)) {
boolean isSure = MessageDialog.openConfirm(shell, Messages.getString("qa.all.dialog.warning"), MessageFormat.format(Messages.getString("qa.all.tip.notXliff"), new Object[] { selectFile.getFullPath() }));
if (!isSure) {
return null;
}
continue;
}
selectIFiles.add(selectFile);
} else if (object instanceof IProject) {
IProject selectProject = (IProject) object;
try {
ResourceUtils.getXliffs(selectProject, selectIFiles);
} catch (Exception e) {
e.printStackTrace();
logger.error(Messages.getString("qa.handlers.BatchQAHandler.log1"), e);
}
} else if (object instanceof IContainer) {
IContainer selectContainer = (IContainer) object;
try {
ResourceUtils.getXliffs(selectContainer, selectIFiles);
} catch (Exception e) {
logger.error(Messages.getString("qa.handlers.BatchQAHandler.log1"), e);
e.printStackTrace();
}
}
}
}
} else {
//如果左边未选择品质检查的类型,那么,获取nattable中打开的文件
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
String XLIFF_EDITOR_ID = "net.heartsome.cat.ts.ui.xliffeditor.nattable.editor";
if (activeEditor != null && !activeEditor.getSite().getId().equals(XLIFF_EDITOR_ID)) {
MessageDialog.openWarning(shell, Messages.getString("qa.all.dialog.warning"), Messages.getString("qa.handlers.BatchQAHandler.tip2"));
return null;
}
XLIFFEditorImplWithNatTable nattable = (XLIFFEditorImplWithNatTable) activeEditor;
isMultiFile = nattable.isMultiFile();
multiTempIFile = ((FileEditorInput) nattable.getEditorInput()).getFile();
if (isMultiFile) {
List<String> multiFilesList = new XLFHandler().getMultiFiles(multiTempIFile);
for (String multiFileStr : multiFilesList) {
selectIFiles.add(ResourceUtils.fileToIFile(multiFileStr));
}
} else {
selectIFiles.add(multiTempIFile);
}
}
CommonFunction.removeRepeateSelect(selectIFiles);
if (selectIFiles.size() == 0) {
MessageDialog.openWarning(shell, Messages.getString("qa.all.dialog.warning"), Messages.getString("qa.handlers.BatchQAHandler.tip1"));
return null;
}
List<IFile> lstFiles = new ArrayList<IFile>();
XLFValidator.resetFlag();
for (IFile iFile : selectIFiles) {
if (!XLFValidator.validateXliffFile(iFile)) {
lstFiles.add(iFile);
}
}
XLFValidator.resetFlag();
selectIFiles.removeAll(lstFiles);
if (selectIFiles.size() == 0) {
return null;
}
model = new QAModel();
model.setQaXlfList(selectIFiles);
quality = new QualityAssurance(model);
BatchQADialog dialog = new BatchQADialog(shell, model, isMultiFile);
int result = dialog.open();
if (result == IDialogConstants.OK_ID) {
// 先调用方法,查看品质检查结果视图是否处于显示状态,如果显示了的。删除数据
IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart view = workbenchPage.findView(QAResultViewPart.ID);
if (view != null) {
// 运行时,将结果视图中列表的数据清除
((QAResultViewPart) view).clearTableData();
}
QAResult qaResult = new QAResult();
// 存储品质检查的检查项
model.setBatchQAItemIdList(getBatchQAItems());
// 存储品质检查的检查时不包括的文本段
model.setNotInclude(getNotIncludePara());
//给品质检查结果视图发出通告,本次检查对象为合并打开文件
// qaResult.firePropertyChange(isMultiFile, new MultiFilesOper(selectIFiles.get(0).getProject(), selectIFiles, multiTempIFile));
// 将当前所处理的文件传至 qaResult
List<String> fileList = new ArrayList<String>();
for (IFile iFIle : model.getQaXlfList()) {
fileList.add(iFIle.getLocation().toOSString());
}
qaResult.setFilePathList(fileList);
HsMultiActiveCellEditor.commit(true);
if (isMultiFile) {
model.setMuliFiles(true);
model.setMultiOper(new MultiFilesOper(selectIFiles.get(0).getProject(), selectIFiles, multiTempIFile));
qaResult.setMultiOper(model.getMultiOper());
quality.beginMultiFileQA(qaResult);
} else {
model.setMuliFiles(false);
qaResult.setMultiOper(model.getMultiOper());
quality.beginQA(qaResult);
}
}
return null;
}
Aggregations