Search in sources :

Example 6 with TransUnitBean

use of net.heartsome.cat.ts.core.bean.TransUnitBean in project translationstudio8 by heartsome.

the class FindReplaceDialog method doReplaceAll.

/**
	 * 替换全部 ;
	 */
private void doReplaceAll() {
    XLIFFEditorImplWithNatTable editor = XLIFFEditorImplWithNatTable.getCurrent();
    if (editor == null) {
        return;
    }
    CellRegion cellRegion = null;
    if (editor.isHorizontalLayout()) {
        cellRegion = find(0, 0);
    } else {
        cellRegion = find(1, 0);
    }
    if (cellRegion == null) {
        // 无查找结果
        return;
    }
    boolean forward = forwardButton.getSelection();
    if (!forward) {
        forwardButton.setSelection(true);
    }
    int firstRowPosition = cellRegion.getPositionCoordinate().getRowPosition();
    HashMap<String, String> segments = new HashMap<String, String>();
    int count = 0;
    String findStr = cmbFind.getText();
    String replaceStr = cmbReplace.getText();
    do {
        PositionCoordinate coordinate = cellRegion.getPositionCoordinate();
        int rowPosition = coordinate.rowPosition;
        int columnPosition = coordinate.columnPosition;
        int rowIndex = coordinate.getLayer().getRowIndexByPosition(rowPosition);
        if (!editor.isHorizontalLayout()) {
            rowIndex = rowIndex / VerticalNatTableConfig.ROW_SPAN;
        }
        // 判断锁定
        TransUnitBean transUnit = editor.getRowTransUnitBean(rowIndex);
        String translate = transUnit.getTuProps().get("translate");
        if (translate != null && "no".equalsIgnoreCase(translate)) {
            rowPosition++;
            cellRegion = find(rowPosition, 0);
            continue;
        }
        String cellValue = (String) coordinate.getLayer().getDataValueByPosition(columnPosition, rowPosition);
        StringBuffer cellValueBf = new StringBuffer(cellValue);
        int start = cellValue.toUpperCase().indexOf(findStr.toUpperCase());
        while (start != -1) {
            cellValueBf.replace(start, start + findStr.length(), replaceStr);
            start = cellValueBf.indexOf(findStr, start);
            count++;
        }
        segments.put(editor.getXLFHandler().getRowId(rowIndex), cellValueBf.toString());
        rowPosition++;
        if (!editor.isHorizontalLayout()) {
            rowPosition++;
        }
        cellRegion = find(rowPosition, 0);
    } while (cellRegion.getPositionCoordinate().getRowPosition() != firstRowPosition);
    if (!forward) {
        forwardButton.setSelection(false);
        backwardButton.setSelection(true);
    }
    int columnIndex = 0;
    if (sourceButton.getSelection()) {
        columnIndex = editor.getSrcColumnIndex();
    } else {
        columnIndex = editor.getTgtColumnIndex();
    }
    try {
        editor.updateSegments(segments, columnIndex, null, null);
    } catch (ExecutionException e) {
        LOGGER.error(Messages.getString("dialog.FindReplaceDialog.logger1"), e);
    }
    String msg = Messages.getString("dialog.FindReplaceDialog.status3");
    statusLabel.setText(MessageFormat.format(msg, count));
    ActiveCellRegion.setActiveCellRegion(null);
}
Also used : XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) CellRegion(net.heartsome.cat.ts.ui.xliffeditor.nattable.search.coordinate.CellRegion) ActiveCellRegion(net.heartsome.cat.ts.ui.xliffeditor.nattable.search.coordinate.ActiveCellRegion) HashMap(java.util.HashMap) PositionCoordinate(net.sourceforge.nattable.coordinate.PositionCoordinate) ExecutionException(org.eclipse.core.commands.ExecutionException) Point(org.eclipse.swt.graphics.Point)

Example 7 with TransUnitBean

use of net.heartsome.cat.ts.core.bean.TransUnitBean in project translationstudio8 by heartsome.

the class TSFileHandler method openFile.

@Override
public Map<String, Object> openFile(File file, int tuCount) {
    long start = System.currentTimeMillis();
    // 验证文件是否存在
    if (file == null || !file.exists()) {
        String errorMsg = Messages.getString("file.TSFileHandler.logger5");
        logger.error(errorMsg);
        return getErrorResult(errorMsg, null);
    }
    // 判断是否还有缓存空间。
    boolean canCache = tuCount < TU_CACHE_SIZE;
    // 当前文件中解析并缓存翻译单元计数器。
    int parsedTuCount = 0;
    // 当前文件未解析缓存的翻译单元计数器。
    int noParseTuCount = 0;
    String filename = file.getAbsolutePath();
    int fileIndex = 1;
    // 解析文件并获取索引
    VTDGen vgRead = new VTDGen();
    if (vgRead.parseFile(filename, true)) {
        VTDNav vnRead = vgRead.getNav();
        VTDUtils vu = null;
        try {
            vu = new VTDUtils(vnRead);
            // 创建临时文件
            File tmpFile = createTmpFile();
            XMLModifier xm = new XMLModifier(vnRead);
            FileOutputStream fos = new FileOutputStream(tmpFile);
            xm.output(fos);
            fos.close();
            tmpFileMap.put(filename, tmpFile.getAbsolutePath());
            filesChangeStatus.put(filename, false);
        } catch (ModifyException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger6"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (TranscodeException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger7"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (IOException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger8"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (NavException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger9");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        }
        // 创建翻译单元集合缓存。
        Vector<TransUnitBean> tusCache = new Vector<TransUnitBean>();
        // 创建翻译单元索引集合缓存。
        Vector<String> tuIndexCache = new Vector<String>();
        // 初始化文件节点属性集合。
        fileAttrs = new Hashtable<Integer, Hashtable<String, String>>();
        AutoPilot apFile = new AutoPilot(vnRead);
        String fileNode = "/xliff/file";
        try {
            apFile.selectXPath(fileNode);
            while (apFile.evalXPath() != -1) {
                fileAttrs.put(fileIndex, vu.getCurrentElementAttributs());
                AutoPilot apTU = new AutoPilot(vnRead);
                apTU.selectXPath("body//trans-unit");
                vnRead.push();
                while (apTU.evalXPath() != -1) {
                    // 如果缓冲区未满,则解析文件内容并缓存,否则只计数,不解析内容。
                    if (canCache) {
                        String tuid = "";
                        String srcText = "";
                        String srcContent = "";
                        String tgtText = "";
                        String tgtContent = "";
                        Hashtable<String, String> srcProps = null;
                        Hashtable<String, String> tgtProps = null;
                        // 取翻译单元所有属性
                        String tmpNode = "";
                        vnRead.push();
                        Hashtable<String, String> tuProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        tuid = tuProps.get("id");
                        // 取翻译单元源节点完整文本,含内部标记。
                        vnRead.push();
                        tmpNode = "./source";
                        srcContent = vu.getElementContent(tmpNode);
                        // vnRead.pop();
                        // 取翻译单元源文本。
                        // vnRead.push();
                        srcText = vu.getElementPureText();
                        // 取翻译单元源节点属性。
                        srcProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        // 取翻译单元目标节点完整文本,含内部标记。
                        vnRead.push();
                        tmpNode = "./target";
                        tgtContent = vu.getElementContent(tmpNode);
                        // vnRead.pop();
                        // 取翻译单元目标文本。
                        // vnRead.push();
                        tgtText = vu.getElementPureText();
                        // 取翻译单元目标节点属性。
                        tgtProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        // 获取所有的 alttrans 匹配节点。
                        vnRead.push();
                        Vector<AltTransBean> matches = getAltTrans(vu);
                        vnRead.pop();
                        // 构建翻译单元对象,存储节点信息
                        TransUnitBean tub = new TransUnitBean(tuid, srcContent, srcText);
                        tub.setTuProps(tuProps);
                        tub.setSrcProps(srcProps);
                        tub.setTgtContent(tgtContent);
                        tub.setTgtText(tgtText);
                        tub.setTgtProps(tgtProps);
                        tub.setMatches(matches);
                        vnRead.push();
                        tub.setNotes(getNotes(vu));
                        vnRead.pop();
                        vnRead.push();
                        tub.setPropgroups(getPrpoGroups(vu));
                        vnRead.pop();
                        tusCache.add(tub);
                        tuIndexCache.add(filename + ";" + fileIndex + ";" + tuid);
                        // 解析的翻译单元节点计数
                        parsedTuCount++;
                        if (tuCount + parsedTuCount == TU_CACHE_SIZE) {
                            canCache = false;
                        }
                    } else {
                        // 未解析的翻译单元节点计数
                        noParseTuCount++;
                    }
                }
                vnRead.pop();
                // 文件节点索引计数
                fileIndex++;
            }
            transunits.put(filename, tusCache);
            tuIndexs.put(filename, tuIndexCache);
            actualTuCount.put(filename, parsedTuCount + noParseTuCount);
            accessHistory.put(filename, "");
        } catch (XPathEvalException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger10");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (NavException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger11");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (XPathParseException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger12");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        }
    } else {
        String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger13"), filename);
        logger.error(errorMsg);
        return getErrorResult(errorMsg, null);
    }
    long end = System.currentTimeMillis();
    // 输出结果
    long resultMS = end - start;
    long resultS = resultMS / 1000;
    long resultM = resultMS / (1000 * 60);
    System.gc();
    logger.info(Messages.getString("file.TSFileHandler.logger14"), new Object[] { resultM, resultS, resultMS });
    Map<String, Object> result = getSuccessResult();
    result.put("CurCachedTuCount", Integer.valueOf(parsedTuCount));
    result.put("TotalCachedTuCount", Integer.valueOf(parsedTuCount + tuCount));
    return result;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathParseException(com.ximpleware.XPathParseException) TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) ModifyException(com.ximpleware.ModifyException) Vector(java.util.Vector) Hashtable(java.util.Hashtable) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) VTDNav(com.ximpleware.VTDNav) File(java.io.File)

Example 8 with TransUnitBean

use of net.heartsome.cat.ts.core.bean.TransUnitBean in project translationstudio8 by heartsome.

the class XLFHandler method getTransUnit.

/**
	 * 获取指定索引的翻译单元对象。
	 * @param rowId
	 *            行的唯一标识,由三部分拼成
	 */
public TransUnitBean getTransUnit(String rowId) {
    if (tuSizeMap == null || rowId == null) {
        return null;
    }
    String tuNode = RowIdUtil.parseRowIdToXPath(rowId);
    if (tuNode == null) {
        return null;
    }
    VTDUtils vu = null;
    VTDNav vn = getVTDNavByRowId(rowId);
    try {
        vu = new VTDUtils(vn);
    } catch (NavException e1) {
        String errorMsg = Messages.getString("file.XLFHandler.logger4");
        LOGGER.error(errorMsg, e1);
        return null;
    }
    try {
        if (vu.pilot(tuNode) != -1) {
            // 导航到 trans-unit 节点
            String tuid = "";
            String srcText = "";
            String srcContent = "";
            String tgtText = "";
            String tgtContent = "";
            Hashtable<String, String> srcProps = new Hashtable<String, String>();
            Hashtable<String, String> tgtProps = new Hashtable<String, String>();
            // 取翻译单元所有属性
            vn.push();
            Hashtable<String, String> tuProps = vu.getCurrentElementAttributs();
            vn.pop();
            if (tuProps != null) {
                tuid = tuProps.get("id");
            }
            AutoPilot ap = new AutoPilot(vn);
            ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
            ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
            // 取翻译单元源节点完整文本,含内部标记。
            vn.push();
            if (vu.pilot(ap, "./source") != -1) {
                // 导航到 Source 子节点
                // 源节点完整内容。
                srcContent = vu.getElementContent();
                // 源节点纯文本内容。
                srcText = getTUPureText(vu.getVTDNav());
                // 源节点属性集合。
                srcProps = vu.getCurrentElementAttributs();
            }
            ap.resetXPath();
            vn.pop();
            // 取翻译单元目标节点完整文本,含内部标记。
            vn.push();
            if (vu.pilot(ap, "./target") != -1) {
                // 导航到 Target 子节点
                // 目标节点完整内容。
                tgtContent = vu.getElementContent();
                // 目标节点纯文本内容。
                tgtText = getTUPureText(vu.getVTDNav());
                // 目标节点属性集合。
                tgtProps = vu.getCurrentElementAttributs();
            }
            vn.pop();
            // 获取所有的 alttrans 匹配节点。
            vn.push();
            Vector<AltTransBean> matches = getAltTrans(vu);
            vn.pop();
            // 构建翻译单元对象,存储节点信息
            TransUnitBean tub = new TransUnitBean(tuid, srcContent, srcText);
            tub.setTuProps(tuProps);
            tub.setSrcProps(srcProps);
            tub.setTgtContent(tgtContent);
            tub.setTgtText(tgtText);
            tub.setTgtProps(tgtProps);
            tub.setMatches(matches);
            vn.push();
            tub.setNotes(getNotes(vu));
            vn.pop();
            vn.push();
            tub.setPropgroups(getPrpoGroups(vu));
            vn.pop();
            return tub;
        }
    } catch (XPathEvalException e) {
        String errorMsg = Messages.getString("file.XLFHandler.logger5");
        LOGGER.error(errorMsg, e);
        return null;
    } catch (NavException e) {
        String errorMsg = Messages.getString("file.XLFHandler.logger6");
        LOGGER.error(errorMsg, e);
        return null;
    } catch (XPathParseException e) {
        String errorMsg = Messages.getString("file.XLFHandler.logger7");
        LOGGER.error(errorMsg, e);
        return null;
    }
    return null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) Hashtable(java.util.Hashtable) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDNav(com.ximpleware.VTDNav)

Example 9 with TransUnitBean

use of net.heartsome.cat.ts.core.bean.TransUnitBean in project translationstudio8 by heartsome.

the class TermBaseSearchDialog method InsertGridTgtToEditor.

private void InsertGridTgtToEditor() {
    GridItem[] selection = grid.getSelection();
    if (null == selection || selection.length == 0) {
        return;
    }
    IXliffEditor tempEditor = null;
    IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (activeEditor instanceof IXliffEditor) {
        IXliffEditor editor = (IXliffEditor) activeEditor;
        tempEditor = editor;
    }
    if (tempEditor == null) {
        return;
    }
    int[] selectedRows = tempEditor.getSelectedRows();
    if (null == selectedRows || selectedRows.length == 0) {
        return;
    }
    int rowIndex = selectedRows[0];
    if (tempEditor == null || rowIndex < 0) {
        return;
    }
    TransUnitBean transUnit = tempEditor.getRowTransUnitBean(rowIndex);
    Hashtable<String, String> tuProp = transUnit.getTuProps();
    if (tuProp != null) {
        String translate = tuProp.get("translate");
        if (translate != null && translate.equalsIgnoreCase("no")) {
            MessageDialog.openInformation(tempEditor.getSite().getShell(), Messages.getString("view.TerminologyViewPart.msgTitle"), Messages.getString("view.TerminologyViewPart.msg1"));
            return;
        }
    }
    String tarTerm = selection[0].getText(1);
    if (null == tarTerm || tarTerm.isEmpty()) {
        return;
    }
    try {
        tempEditor.insertCell(rowIndex, tempEditor.getTgtColumnIndex(), tarTerm);
    // tempEditor.setFocus(); // 焦点给回编辑器
    } catch (ExecutionException e) {
        if (Constant.RUNNING_MODE == Constant.MODE_DEBUG) {
            e.printStackTrace();
        }
        MessageDialog.openInformation(tempEditor.getSite().getShell(), Messages.getString("view.TerminologyViewPart.msgTitle"), Messages.getString("view.TerminologyViewPart.msg2") + e.getMessage());
    }
}
Also used : TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) GridItem(org.eclipse.nebula.widgets.grid.GridItem) IEditorPart(org.eclipse.ui.IEditorPart) ExecutionException(org.eclipse.core.commands.ExecutionException) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) Point(org.eclipse.swt.graphics.Point)

Example 10 with TransUnitBean

use of net.heartsome.cat.ts.core.bean.TransUnitBean in project translationstudio8 by heartsome.

the class TerminologyViewPart method refresh.

public void refresh() {
    // FIXME when the program started ,select nothing.tempEditor is null.
    if (null == tempEditor) {
        return;
    }
    // handler.getTransUnit(rowId);
    TransUnitBean bean = tempEditor.getRowTransUnitBean(rowIndex);
    String pureText = bean.getSrcText();
    String srcLang = bean.getSrcLang();
    String tgtLang = bean.getTgtLang();
    tgtLang = tgtLang == null || tgtLang.equals("") ? tempEditor.getTgtColumnName() : tgtLang;
    srcLang = srcLang == null || "".equals(srcLang) ? tempEditor.getSrcColumnName() : srcLang;
    loadData(pureText, srcLang, tgtLang, true);
}
Also used : TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean)

Aggregations

TransUnitBean (net.heartsome.cat.ts.core.bean.TransUnitBean)13 IProject (org.eclipse.core.resources.IProject)5 IEditorPart (org.eclipse.ui.IEditorPart)5 FileEditorInput (org.eclipse.ui.part.FileEditorInput)5 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)4 TransUnitInfo2TranslationBean (net.heartsome.cat.ts.tm.bean.TransUnitInfo2TranslationBean)4 Hashtable (java.util.Hashtable)3 IXliffEditor (net.heartsome.cat.ts.ui.editors.IXliffEditor)3 ExecutionException (org.eclipse.core.commands.ExecutionException)3 Point (org.eclipse.swt.graphics.Point)3 AutoPilot (com.ximpleware.AutoPilot)2 NavException (com.ximpleware.NavException)2 VTDNav (com.ximpleware.VTDNav)2 XPathEvalException (com.ximpleware.XPathEvalException)2 XPathParseException (com.ximpleware.XPathParseException)2 HashMap (java.util.HashMap)2 AltTransBean (net.heartsome.cat.ts.core.bean.AltTransBean)2 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 ModifyException (com.ximpleware.ModifyException)1