Search in sources :

Example 1 with ImportDocx

use of net.heartsome.cat.ts.ui.docx.ImportDocx 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;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) FlagErrorException(net.heartsome.cat.ts.ui.docx.common.FlagErrorException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FlagErrorException(net.heartsome.cat.ts.ui.docx.common.FlagErrorException) ImportDocx(net.heartsome.cat.ts.ui.docx.ImportDocx) TagBean(net.heartsome.cat.ts.ui.docx.common.TagBean) ErrorBean(net.heartsome.cat.ts.ui.docx.common.ErrorBean) List(java.util.List) ArrayList(java.util.ArrayList) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler) RowBean(net.heartsome.cat.ts.ui.docx.common.RowBean)

Aggregations

XMLModifier (com.ximpleware.XMLModifier)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)1 ImportDocx (net.heartsome.cat.ts.ui.docx.ImportDocx)1 ErrorBean (net.heartsome.cat.ts.ui.docx.common.ErrorBean)1 FlagErrorException (net.heartsome.cat.ts.ui.docx.common.FlagErrorException)1 RowBean (net.heartsome.cat.ts.ui.docx.common.RowBean)1 TagBean (net.heartsome.cat.ts.ui.docx.common.TagBean)1 CoreException (org.eclipse.core.runtime.CoreException)1