Search in sources :

Example 96 with VTDNav

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

the class QAXmlHandler method getHunspellAvailableLang.

/**
	 * 获取 hunspell 所支持的语言	2013-01-15
	 * @param xmlPath
	 * @return
	 */
public Map<String, String> getHunspellAvailableLang(String xmlPath) {
    Map<String, String> langMap = new HashMap<String, String>();
    VTDGen vg = new VTDGen();
    if (!vg.parseFile(xmlPath, true)) {
        logger.error("Hunspell 语言管理文件破损,无法解析。");
        return null;
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    AutoPilot childAP = new AutoPilot(vn);
    String xpath = "/config/language";
    try {
        ap.selectXPath(xpath);
        while (ap.evalXPath() != -1) {
            String code = null;
            String dictionary = null;
            vn.push();
            childAP.selectXPath("./isoCode");
            if (childAP.evalXPath() != -1) {
                if (vn.getText() != -1) {
                    code = vn.toRawString(vn.getText());
                }
            }
            vn.pop();
            vn.push();
            childAP.selectXPath("./dict");
            if (childAP.evalXPath() != -1) {
                if (vn.getText() != -1) {
                    dictionary = vn.toRawString(vn.getText());
                }
            }
            vn.pop();
            if (code != null && dictionary != null && !"".equals(code) && !"".equals(dictionary)) {
                langMap.put(code, dictionary);
            }
        }
    } catch (Exception e) {
        logger.error("Hunspell 语言管理文件内容获取失败!", e);
    }
    return langMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 97 with VTDNav

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

the class QAXmlHandler method addAspellConfig.

/**
	 * 将语言与词典对添加到 Aspell 配置文件中
	 * @throws Exception
	 */
public void addAspellConfig(String xmlPath, String lang, String dic, boolean isUpdate) throws Exception {
    VTDNav vn = vnMap.get(xmlPath);
    XMLModifier xm = null;
    VTDUtils vu = new VTDUtils(vn);
    if (isUpdate) {
        String xpath = "/aspell/aspellDictionaries/" + lang + "/text()";
        xm = vu.update(xpath, dic);
    } else {
        String xpath = "/aspell/aspellDictionaries/text()";
        String insertValue = "\n<" + lang + ">" + dic + "</" + lang + ">\n";
        xm = vu.insert(xpath, insertValue);
    }
    vu.bind(xm.outputAndReparse());
    saveAndReparse(xm, xmlPath);
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) VTDNav(com.ximpleware.VTDNav)

Example 98 with VTDNav

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

the class QAXmlHandler method getEditProgressData.

/**
	 * 针对文件分析的翻译进度分析,获取每个节点的未翻译文本段数、字数,已翻译文本段数、字数。
	 * 如果返回为null,则标志用户退出分析
	 * @param filePath		要处理的文件的路径
	 * @param monitor		进度条
	 * @param ignoreTag		是否忽略标记
	 * @param workInterval	进度条前进一格的间隔
	 * @param traversalTuIndex	循环trans-unit节点的序列号
	 * @return	editProgDataMap,下面是其健值对详解
	 * key: notApprovedParas --> 未批准文本段数
	 * key: approvedParas --> 已批准文本段数
	 * key: notApprovedWords --> 未批准字数
	 * key: approvedWords --> 已批准字数
	 */
public Map<String, Integer> getEditProgressData(String filePath, IProgressMonitor monitor, int workInterval, int traversalTuIndex) {
    Map<String, Integer> editProgDataMap = new HashMap<String, Integer>();
    int notApprovedParas = 0;
    int approvedParas = 0;
    int lockedParas = 0;
    int notApprovedWords = 0;
    int approvedWords = 0;
    int lockedWords = 0;
    try {
        VTDNav vn = vnMap.get(filePath);
        Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + filePath);
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vUtils = new VTDUtils(vn);
        AutoPilot sourceAp = new AutoPilot(vn);
        sourceAp.selectXPath("./source[text()!='' or ./*]");
        String srcLang = vUtils.getElementAttribute("/xliff/file[1]", "source-language");
        ap.selectXPath(XPATH_ALL_TU);
        while (ap.evalXPath() != -1) {
            traversalTuIndex++;
            int curWordsNum = 0;
            //判断是否锁定
            boolean isLocked = false;
            int inx = vn.getAttrVal("translate");
            if (inx != -1 && "no".equals(vn.toString(inx))) {
                isLocked = true;
            }
            //根据是否忽略标记,获取源文本的字数
            vn.push();
            if (sourceAp.evalXPath() != -1) {
                String sourceText = getTUPureText(vn);
                curWordsNum = CountWord.wordCount(sourceText, srcLang);
            }
            sourceAp.resetXPath();
            vn.pop();
            String approved = "";
            int approveIndex = vn.getAttrVal("approved");
            if (approveIndex != -1) {
                approved = vn.toString(approveIndex);
            }
            if ("yes".equals(approved)) {
                approvedParas++;
                approvedWords += curWordsNum;
            } else {
                notApprovedParas++;
                notApprovedWords += curWordsNum;
            }
            if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
                return null;
            }
            if (isLocked) {
                lockedParas++;
                lockedWords += curWordsNum;
            }
        }
    } catch (Exception e) {
        if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
            return null;
        }
        e.printStackTrace();
        logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger24"), filePath), e);
    }
    editProgDataMap.put("notApprovedParas", notApprovedParas);
    editProgDataMap.put("approvedParas", approvedParas);
    editProgDataMap.put("lockedParas", lockedParas);
    editProgDataMap.put("notApprovedWords", notApprovedWords);
    editProgDataMap.put("approvedWords", approvedWords);
    editProgDataMap.put("lockedWords", lockedWords);
    return editProgDataMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 99 with VTDNav

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

the class XLFHandler method getFileCountInXliff.

/**
	 * 得到 File 节点个数(此方法仅用于解析单个文件时)
	 * @return ;
	 */
public int getFileCountInXliff(String fileName) {
    VTDNav vn = vnMap.get(fileName);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + fileName);
    AutoPilot ap = new AutoPilot(vn);
    try {
        ap.selectXPath("count(/xliff/file)");
        // 整个 xliff 文件中的
        int countAllFile = (int) ap.evalXPathToNumber();
        // file 节点的个数
        return countAllFile;
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return -1;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav)

Example 100 with VTDNav

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

the class XLFHandler method updateLanguages.

/**
	 * 设置指定 XLIFF 文件中指定 file 节点的语言代码信息(source-language、target-language 属性值)
	 * @param fileName
	 *            指定文件名
	 * @param XliffBeans
	 *            XliffBean 集合;
	 */
public void updateLanguages(String fileName, List<XliffBean> xliffBeans) {
    if (xliffBeans == null || xliffBeans.isEmpty()) {
        return;
    }
    VTDNav vn = vnMap.get(fileName);
    AutoPilot ap = new AutoPilot(vn);
    AutoPilot subAp = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = new XMLModifier(vn);
        for (XliffBean bean : xliffBeans) {
            Set<String> originals = bean.getOriginals();
            for (String original : originals) {
                int index = vu.pilot(subAp, "/xliff/file[@original='" + original + "']");
                if (index != -1) {
                    xm = vu.update(ap, xm, "./@source-language", bean.getSourceLanguage(), VTDUtils.CREATE_IF_NOT_EXIST);
                    xm = vu.update(ap, xm, "./@target-language", bean.getTargetLanguage(), VTDUtils.CREATE_IF_NOT_EXIST);
                }
            }
        }
        saveAndReparse(xm, fileName);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) 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