Search in sources :

Example 1 with RowBean

use of net.heartsome.cat.ts.ui.docx.common.RowBean in project translationstudio8 by heartsome.

the class ImportDocxDialog method ananysisStatusAndComment.

/**
	 * 处理状态以及批注
	 * @param rowList
	 */
private void ananysisStatusAndComment(List<RowBean> rowList) throws Exception {
    String rowId = "";
    for (RowBean bean : rowList) {
        if (bean.isLocked()) {
            continue;
        }
        rowId = bean.getRowId();
        if (rowId == null || rowId.length() <= 0) {
            continue;
        }
        int status = -1;
        boolean targetNull = true;
        ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
        if (ap.evalXPath() != -1) {
            // 先处理状态
            // 检查 target 是否为空
            vn.push();
            ap.selectXPath("./target[text()!='' or *]");
            if (ap.evalXPath() != -1) {
                targetNull = false;
            }
            vn.pop();
            // 如果译文为空,那状态应为未翻译,如果译文不为空,那状态应不为未翻译
            if (targetNull) {
                status = DocxConstant.STATUS_unstrans;
            }
            setOtherStatus(status);
            if (hasComment) {
                // 处理批注的问题。
                // 首先删除所有批注
                vn.push();
                ap.selectXPath("./note");
                while (ap.evalXPath() != -1) {
                    xm.remove();
                }
                StringBuffer commentSB = new StringBuffer();
                if (bean.getComment() != null) {
                    for (CommentBean commentBean : bean.getComment()) {
                        // <note from='Mac'>2013-05-13:test</note>
                        commentSB.append("<note from='");
                        commentSB.append(commentBean.getUser() + "'>");
                        commentSB.append(commentBean.getTime() + ":" + commentBean.getText());
                        commentSB.append("</note>");
                    }
                }
                vn.pop();
                if (commentSB.length() > 0) {
                    xm.insertBeforeTail(commentSB.toString());
                }
            }
        }
    }
}
Also used : CommentBean(net.heartsome.cat.ts.ui.docx.common.CommentBean) RowBean(net.heartsome.cat.ts.ui.docx.common.RowBean)

Example 2 with RowBean

use of net.heartsome.cat.ts.ui.docx.common.RowBean 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)

Example 3 with RowBean

use of net.heartsome.cat.ts.ui.docx.common.RowBean in project translationstudio8 by heartsome.

the class ImportDocxDialog method importDocxToXliffList.

/**
	 * 将数据从 docx 中导入 xliff 文件。
	 */
private void importDocxToXliffList(List<RowBean> rowList) throws Exception {
    String rowId = "";
    StringBuffer contentSB = new StringBuffer();
    for (RowBean bean : rowList) {
        rowId = bean.getRowId();
        if (bean.isLocked()) {
            continue;
        }
        if (rowId == null || rowId.length() <= 0) {
            continue;
        }
        ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
        if (ap.evalXPath() != -1) {
            // 开始处理源文本
            contentSB = new StringBuffer();
            vn.push();
            ap.selectXPath("./source");
            if (ap.evalXPath() != -1) {
                String header = vu.getElementHead();
                contentSB.append(header);
                for (Object object : bean.getSrcElement()) {
                    if (object instanceof TagBean) {
                        contentSB.append(((TagBean) object).getText());
                    } else {
                        contentSB.append(object);
                    }
                }
                contentSB.append("</source>");
                xm.remove();
            }
            vn.pop();
            // 开始处理目标文本段
            ap.selectXPath("./target");
            if (ap.evalXPath() != -1) {
                String header = vu.getElementHead();
                contentSB.append(header);
                for (Object object : bean.getTgtElement()) {
                    if (object instanceof TagBean) {
                        contentSB.append(((TagBean) object).getText());
                    } else {
                        contentSB.append(object);
                    }
                }
                contentSB.append("</target>");
                xm.remove();
                xm.insertAfterElement(contentSB.toString());
            } else {
                contentSB.append("<target>");
                for (Object object : bean.getTgtElement()) {
                    if (object instanceof TagBean) {
                        contentSB.append(((TagBean) object).getText());
                    } else {
                        contentSB.append(object);
                    }
                }
                contentSB.append("</target>");
                xm.insertBeforeTail(contentSB.toString());
            }
        }
    }
    tempXlfHandler.saveAndReparse(xm, xliffFullPath);
    parseXliff(tempXlfHandler);
    // 再处理状态与批注的问题
    ananysisStatusAndComment(rowList);
    tempXlfHandler.saveAndReparse(xm, xliffFullPath);
}
Also used : TagBean(net.heartsome.cat.ts.ui.docx.common.TagBean) RowBean(net.heartsome.cat.ts.ui.docx.common.RowBean)

Example 4 with RowBean

use of net.heartsome.cat.ts.ui.docx.common.RowBean in project translationstudio8 by heartsome.

the class ImportDocx method getDataFromWord.

public List<RowBean> getDataFromWord() throws FlagErrorException {
    List<RowBean> rowList = new ArrayList<RowBean>();
    // 文件标识正确
    boolean flagRight = false;
    try {
        tempFolder = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + new File(docxPath).getName();
        docxFolderPath = ZipUtil.upZipFile(docxPath, tempFolder);
        documentXmlPath = docxFolderPath + File.separator + "word" + File.separator + "document.xml";
        VTDGen vg = new VTDGen();
        boolean parseResult = vg.parseFile(documentXmlPath, true);
        if (!parseResult) {
            throw new Exception(MessageFormat.format(Messages.getString("ImportDocxDialog.ok.parseError"), docxPath));
        }
        vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        AutoPilot childAP = new AutoPilot(vn);
        extendAP = new AutoPilot(vn);
        otherAP = new AutoPilot(vn);
        commentAP = new AutoPilot(vn);
        vu = new VTDUtils(vn);
        ap.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        childAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        extendAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        otherAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        commentAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        String xpath = "/w:document/w:body/w:tbl/w:tr";
        ap.selectXPath(xpath);
        boolean isHeader = true;
        while (ap.evalXPath() != -1) {
            RowBean bean = new RowBean();
            int tcIdx = 0;
            vn.push();
            childAP.selectXPath("./w:tc");
            // 备注:状态只写不读。故,不需要读状态一栏。
            while (childAP.evalXPath() != -1) {
                tcIdx++;
                // 表头是没有数据的,故不读取数据
                if (tcIdx == 1) {
                    // 先取第一列,获取其行号,以及 rowId,如果是头,里面保存的是固定值:heartsomeExportDocxFlag,若未找到,直接退出程序。
                    if (isHeader) {
                        boolean validResult = validFileLegalized();
                        if (!validResult) {
                            flagRight = true;
                            throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
                        } else {
                            flagRight = true;
                        }
                    } else {
                        getIndexAndRowId(bean);
                    }
                } else if (tcIdx == 2) {
                    // 第二列,为源文本
                    if (isHeader) {
                        String columnType = getColumnType();
                        if (columnType == null || columnType.trim().isEmpty() || !columnType.contains(DocxConstant.COLUMN_TYPE_src)) {
                            throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
                        }
                    } else {
                        bean.setSrcElement(getSrcOrTgtText());
                    }
                } else if (tcIdx == 3) {
                    // 第三列,为目标文本
                    if (isHeader) {
                        String columnType = getColumnType();
                        if (columnType == null || columnType.trim().isEmpty() || !columnType.contains(DocxConstant.COLUMN_TYPE_tgt)) {
                            throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
                        }
                    } else {
                        bean.setTgtElement(getSrcOrTgtText());
                    }
                } else if (tcIdx == 4) {
                    if (isHeader) {
                        String typeStr = getColumnType();
                        if (typeStr.contains(DocxConstant.COLUMN_TYPE_comment)) {
                            hasComment = true;
                        } else if (typeStr.contains(DocxConstant.COLUMN_TYPE_status)) {
                            hasStatus = true;
                        }
                    } else {
                        if (hasComment && hasStatus) {
                            getComments(bean);
                        } else if (hasComment) {
                            getComments(bean);
                        }
                    }
                }
            }
            isHeader = false;
            vn.pop();
            rowList.add(bean);
        }
    } catch (FlagErrorException e) {
        throw new FlagErrorException(e.getMessage());
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        if (!flagRight) {
            throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
        }
        if (new File(docxFolderPath).exists()) {
            deleteFileOrFolder(new File(docxFolderPath));
        }
    }
    return rowList;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) FlagErrorException(net.heartsome.cat.ts.ui.docx.common.FlagErrorException) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) VTDGen(com.ximpleware.VTDGen) File(java.io.File) FlagErrorException(net.heartsome.cat.ts.ui.docx.common.FlagErrorException) RowBean(net.heartsome.cat.ts.ui.docx.common.RowBean)

Aggregations

RowBean (net.heartsome.cat.ts.ui.docx.common.RowBean)4 ArrayList (java.util.ArrayList)2 FlagErrorException (net.heartsome.cat.ts.ui.docx.common.FlagErrorException)2 TagBean (net.heartsome.cat.ts.ui.docx.common.TagBean)2 AutoPilot (com.ximpleware.AutoPilot)1 VTDGen (com.ximpleware.VTDGen)1 XMLModifier (com.ximpleware.XMLModifier)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)1 ImportDocx (net.heartsome.cat.ts.ui.docx.ImportDocx)1 CommentBean (net.heartsome.cat.ts.ui.docx.common.CommentBean)1 ErrorBean (net.heartsome.cat.ts.ui.docx.common.ErrorBean)1 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)1 CoreException (org.eclipse.core.runtime.CoreException)1