Search in sources :

Example 21 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.

the class QAXmlHandler method getTuProp.

/**
	 * 得到翻译单元的属性值	,来自XLFHandler
	 * @param rowId
	 *            行的唯一标识
	 * @param propName
	 *            属性名
	 * @return 属性值;
	 */
public String getTuProp(String rowId, String propName) {
    VTDNav vn = getVTDNavByRowId(rowId);
    String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
    try {
        VTDUtils vu = new VTDUtils(vn);
        return vu.getValue(tuXPath + "/@" + propName);
    } catch (NavException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger7"), e);
    }
    return null;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 22 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.

the class TSFileHandler method analysisTranslatorProgress.

// 分析翻译进度。直接返回报告内容的 XML 字符串。
protected String analysisTranslatorProgress(VTDNav vn, final String filepath, float eqvFactor) throws NavException, XPathParseException, XPathEvalException {
    VTDUtils vu = new VTDUtils(vn);
    StringBuilder sb = new StringBuilder();
    sb.append("\t<status-infos purpose=\"");
    sb.append(Messages.getString("file.TSFileHandler.analysisTranslatorProgress"));
    sb.append("\" filepath=\"" + filepath + "\">\n");
    int translatedSegs = 0;
    int untranslatedSegs = 0;
    int translatedWords = 0;
    int untranslatedWords = 0;
    boolean isTranslated = false;
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("//trans-unit");
    while (ap.evalXPath() != -1) {
        String tgtText = vu.getChildPureText("target");
        // 目标文本为空或 null 表示未翻译,反之为已翻译
        if (tgtText == null || tgtText.trim().equals("")) {
            isTranslated = false;
        } else {
            isTranslated = true;
        }
        vn.push();
        // 开始统计分析。
        String srcText = vu.getChildPureText("source");
        String lang = vu.getElementAttribute("source", "xml:lang");
        int words = wordCount(srcText, lang);
        if (isTranslated) {
            translatedSegs++;
            translatedWords += words;
        } else {
            untranslatedSegs++;
            untranslatedWords += words;
        }
        vn.pop();
    }
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.translatedSegs"));
    sb.append("\" statisticunits=\"segment\">");
    sb.append(translatedSegs);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.untranslatedSegs"));
    sb.append("\" statisticunits=\"segment\">");
    sb.append(untranslatedSegs);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.percent1"));
    sb.append("\" statisticunits=\"segment\">");
    sb.append(translatedSegs * 100 / (translatedSegs + untranslatedSegs));
    sb.append("%");
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.total1"));
    sb.append("\" statisticunits=\"segment\">");
    sb.append(translatedSegs + untranslatedSegs);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.translatedWords"));
    sb.append("\" statisticunits=\"word\">");
    sb.append(translatedWords);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.untranslatedWords"));
    sb.append("\" statisticunits=\"word\">");
    sb.append(untranslatedWords);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.percent2"));
    sb.append("\" statisticunits=\"word\">");
    sb.append(translatedWords * 100 / (translatedWords + untranslatedWords));
    sb.append("%");
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.total2"));
    sb.append("\" statisticunits=\"word\">");
    sb.append(translatedWords + untranslatedWords);
    sb.append("</status-info>\n");
    sb.append("\t</status-infos>\n");
    return sb.toString();
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot)

Example 23 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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 24 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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 25 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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

VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)137 AutoPilot (com.ximpleware.AutoPilot)112 NavException (com.ximpleware.NavException)103 VTDNav (com.ximpleware.VTDNav)99 XPathParseException (com.ximpleware.XPathParseException)83 XPathEvalException (com.ximpleware.XPathEvalException)81 IOException (java.io.IOException)64 ModifyException (com.ximpleware.ModifyException)62 TranscodeException (com.ximpleware.TranscodeException)49 CoreException (org.eclipse.core.runtime.CoreException)45 XMLModifier (com.ximpleware.XMLModifier)41 VTDGen (com.ximpleware.VTDGen)33 FileNotFoundException (java.io.FileNotFoundException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)29 HashMap (java.util.HashMap)27 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)27 LinkedHashMap (java.util.LinkedHashMap)25 ArrayList (java.util.ArrayList)23 XQException (javax.xml.xquery.XQException)20 LinkedList (java.util.LinkedList)14