Search in sources :

Example 51 with VTDNav

use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.

the class TSFileHandler method xliffAnalysis.

protected Map<String, Object> xliffAnalysis(File file, int analysisMode, float eqvFactor) throws ModifyException, NavException, TranscodeException, IOException, XPathParseException, XPathEvalException {
    String filepath = file.getAbsolutePath();
    // TODO 确定报告生成的地址及文件名。
    String reportFile = "";
    if (file == null || !file.exists()) {
        String msg = Messages.getString("file.TSFileHandler.logger21");
        logger.error(msg);
        return getErrorResult(msg, null);
    }
    // 判断文件是否已打开并已修改,是则提示是否保存。
    if (filesChangeStatus.get(file.getAbsolutePath())) {
        String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger29"), filepath);
        logger.warn(msg);
        Map<String, Object> result = getErrorResult(msg, null);
        result.put("FileChanged", true);
        return result;
    }
    VTDGen vg = new VTDGen();
    if (vg.parseFile(filepath, true)) {
        VTDNav vn = vg.getNav();
        // 创建分析报告结构
        vg.setDoc("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<hsts-analysisreport>\n</hsts-analysisreport>".getBytes());
        VTDNav vnReport = vg.getNav();
        XMLModifier xm = new XMLModifier(vnReport);
        // 分析状态信息
        if (TSFileHandler.ANALYSIS_MODE_STATUS == (analysisMode & TSFileHandler.ANALYSIS_MODE_STATUS)) {
            String xml = analysisStatus(vn.duplicateNav(), filepath, eqvFactor);
            vnReport.toElement(VTDNav.ROOT);
            xm.insertAfterElement(xml);
        }
        // 分析翻译进度
        if (TSFileHandler.ANALYSIS_MODE_TRANSLATOR_PROGRESS == (analysisMode & TSFileHandler.ANALYSIS_MODE_TRANSLATOR_PROGRESS)) {
            String xml = analysisTranslatorProgress(vn.duplicateNav(), filepath, eqvFactor);
            vnReport.toElement(VTDNav.ROOT);
            xm.insertAfterElement(xml);
        }
        // 分析编辑进度
        if (TSFileHandler.ANALYSIS_MODE_EDITOR_PROGRESS == (analysisMode & TSFileHandler.ANALYSIS_MODE_EDITOR_PROGRESS)) {
            String xml = analysisEditorProgress(vn.duplicateNav(), filepath, eqvFactor);
            vnReport.toElement(VTDNav.ROOT);
            xm.insertAfterElement(xml);
        }
        xm.output(reportFile);
    } else {
        String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger28"), filepath);
        logger.error(msg);
        return getErrorResult(msg, null);
    }
    Map<String, Object> result = getSuccessResult();
    return result;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDGen(com.ximpleware.VTDGen) VTDNav(com.ximpleware.VTDNav)

Example 52 with VTDNav

use of com.ximpleware.VTDNav 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 53 with VTDNav

use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.

the class XLFHandler method getTUPositionByRowId.

/**
	 * 根据ROWID获取TU节点在文件中的位置,位置是从1开始的 --robert 2012-05-07
	 * @return ;
	 */
public Integer getTUPositionByRowId(String rowId) {
    int tuPosition = -1;
    VTDNav vn = vnMap.get(RowIdUtil.getFileNameByRowId(rowId));
    AutoPilot ap = new AutoPilot(vn);
    String xpath = RowIdUtil.parseRowIdToXPath(rowId);
    try {
        ap.selectXPath(xpath);
        if (ap.evalXPath() != -1) {
            ap.selectXPath("count(preceding::trans-unit[source/text()!='' or source/*])");
            tuPosition = (int) ap.evalXPathToNumber() + 1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tuPosition;
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) XQException(javax.xml.xquery.XQException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 54 with VTDNav

use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.

the class XLFHandler method resetTuNodes.

/**
	 * 重置 TU 子节点的 XML 内容。
	 * @param subNodesOfTU
	 *            TU 子节点的 XML 内容
	 */
public void resetTuNodes(Map<String, String> subNodesOfTU) {
    if (subNodesOfTU == null || subNodesOfTU.size() == 0) {
        return;
    }
    List<String> rowIdList = new ArrayList<String>(subNodesOfTU.keySet());
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIdList);
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    VTDUtils vu = new VTDUtils();
    for (Entry<String, List<String>> entry : map.entrySet()) {
        String fileName = entry.getKey();
        List<String> rowIds = entry.getValue();
        VTDNav vn = vnMap.get(fileName);
        try {
            ap.bind(vn);
            xm.bind(vn);
            vu.bind(vn);
            for (String rowId : rowIds) {
                String tgtNode = subNodesOfTU.get(rowId);
                String xpath = RowIdUtil.parseRowIdToXPath(rowId);
                xm = vu.update(ap, xm, xpath, tgtNode);
            }
            // 保存并更新 VTDNav
            saveAndReparse(xm, fileName);
        } catch (ModifyException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) VTDNav(com.ximpleware.VTDNav)

Example 55 with VTDNav

use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.

the class XLFHandler method generateTMXToUpdateTM.

/**
	 * 更新记忆库
	 * @param file
	 *            xliff 文件
	 * @param srcLang
	 *            源语言,当添加所有语言时,值为 null
	 * @param tgtLang
	 *            目标语言,当添加所有语言时,值为 null
	 * @param isAddApprove
	 *            是否添加已批准文本段到记忆库
	 * @param isAddSignedOff
	 *            是否添加已签发文本段到记忆库
	 * @param isAddTranslate
	 *            是否添加已翻译文本段到记忆库
	 * @param isAddDraft
	 *            是否添加草稿状态文本段到记忆库
	 * @param isTagAddTM
	 *            是否将标记为不添加到记忆库的文本段添加到记忆库
	 * @param isAddNoState
	 *            是否添加无状态文本段到记忆库(未实现)
	 * @return 每个数组中第一个元素表示源语言,第二个元素为生成的 TMX 字符串
	 */
public String[] generateTMXToUpdateTM(IFile file, boolean isAddApprove, boolean isAddSignedOff, boolean isAddTranslate, boolean isAddDraft, boolean isAddLocked, int contextSize, String systemUser) {
    if (file == null) {
        return null;
    }
    String filePath = ResourceUtils.iFileToOSPath(file);
    VTDNav vn = vnMap.get(filePath);
    // 当文件未打开时,要先解析 xliff 文件
    if (vn == null) {
        VTDGen vg = new VTDGen();
        String path = ResourceUtils.iFileToOSPath(file);
        if (vg.parseFile(path, true)) {
            vn = vg.getNav();
        }
    }
    StringBuffer sbTMX = new StringBuffer();
    String srcLang = null;
    // if (srcLang == null || tgtLang == null) {
    AutoPilot ap = new AutoPilot(vn);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    String srcLanguage;
    String tgtLanguage;
    String original;
    String strTMX;
    try {
        VTDUtils vu = new VTDUtils(vn);
        vn.push();
        ap.selectXPath("/xliff//file");
        AutoPilot ap2 = new AutoPilot(vn);
        ap2.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        // sbTMX 是否添加了头部信息
        boolean isContainHeader = false;
        while (ap.evalXPath() != -1) {
            vn.push();
            srcLanguage = vu.getCurrentElementAttribut("source-language", null);
            srcLang = srcLanguage;
            tgtLanguage = vu.getCurrentElementAttribut("target-language", null);
            original = vu.getCurrentElementAttribut("original", null);
            if (srcLanguage == null) {
                continue;
            }
            if (tgtLanguage == null) {
                continue;
            }
            vn.pop();
            strTMX = generateTMXString(systemUser, filePath, srcLanguage, tgtLanguage, original, vn, isAddApprove, isAddSignedOff, isAddTranslate, isAddDraft, isAddLocked, contextSize, file.getProject());
            if (strTMX != null) {
                if (!isContainHeader) {
                    isContainHeader = true;
                    sbTMX.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    sbTMX.append("<tmx version=\"1.4b\">");
                    sbTMX.append("<header creationtool=\"" + Constants.TMX_CREATIONTOOL + "\" creationtoolversion=\"" + Constants.TMX_CREATIONTOOLVERSION + "\" srclang=\"" + srcLanguage + "\" adminlang=\"en\" datatype=\"xml\" o-tmf=\"XLIFF\" segtype=\"paragraph\">");
                    sbTMX.append("</header>");
                    sbTMX.append("<body>");
                }
                sbTMX.append(strTMX);
            }
        }
        if (sbTMX.length() > 0) {
            sbTMX.append("</body>");
            sbTMX.append("</tmx>");
        } else {
            return null;
        }
    } catch (XPathParseException e) {
        LOGGER.error(MessageFormat.format(Messages.getString("file.XLFHandler.logger17"), filePath), e);
    } catch (XPathEvalException e) {
        LOGGER.error(MessageFormat.format(Messages.getString("file.XLFHandler.logger17"), filePath), e);
    } catch (NavException e) {
        LOGGER.error(MessageFormat.format(Messages.getString("file.XLFHandler.logger17"), filePath), e);
    } finally {
        vn.pop();
    }
    return new String[] { srcLang, sbTMX.toString() };
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDGen(com.ximpleware.VTDGen) VTDNav(com.ximpleware.VTDNav)

Aggregations

VTDNav (com.ximpleware.VTDNav)206 AutoPilot (com.ximpleware.AutoPilot)177 NavException (com.ximpleware.NavException)128 XPathParseException (com.ximpleware.XPathParseException)115 XPathEvalException (com.ximpleware.XPathEvalException)110 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)99 IOException (java.io.IOException)85 ModifyException (com.ximpleware.ModifyException)81 TranscodeException (com.ximpleware.TranscodeException)69 CoreException (org.eclipse.core.runtime.CoreException)69 XMLModifier (com.ximpleware.XMLModifier)53 UnsupportedEncodingException (java.io.UnsupportedEncodingException)49 FileNotFoundException (java.io.FileNotFoundException)46 VTDGen (com.ximpleware.VTDGen)45 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)43 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)27 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)25 LinkedList (java.util.LinkedList)23