Search in sources :

Example 11 with VTDUtils

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

the class QAXmlHandler method getAllRowIdsByLanguages.

/**
	 * 通过源与目标语言得到所有当前打开的文件包含的 RowId
	 * ///xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]
	 * @return ;
	 */
public List<String> getAllRowIdsByLanguages(String srcLanguage, String tgtLanguage) {
    List<String> allRowIds = new LinkedList<String>();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    VTDUtils vu = new VTDUtils();
    for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
        String fileName = entry.getKey();
        VTDNav vn = entry.getValue();
        ap.bind(vn);
        try {
            vu.bind(vn);
            // 查询相同目标与相同源文本的tu
            String XPATH_ALL_TU_BYLANGUAGE = "/xliff/file[upper-case(@source-language)=''{0}'' and upper-case(@target-language)=''{1}'']/body/descendant::trans-unit[(source/text()!='''' or source/*)]";
            String xpath = MessageFormat.format(XPATH_ALL_TU_BYLANGUAGE, new Object[] { srcLanguage, tgtLanguage });
            ap.selectXPath(xpath);
            while (ap.evalXPath() != -1) {
                String rowId = RowIdUtil.getRowId(vn, fileName);
                if (rowId != null) {
                    allRowIds.add(rowId);
                }
            }
        } catch (NavException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
        } catch (XPathParseException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
        } catch (XPathEvalException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
        }
    }
    return allRowIds;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDNav(com.ximpleware.VTDNav) LinkedList(java.util.LinkedList)

Example 12 with VTDUtils

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

the class QAXmlHandler method getFilteredTUContent.

/**
	 * 获取trans-unit节点过滤后的source和target节点的文本内容(不去掉标记),过滤条件为不包括上下文匹配,不包括完全匹配,不包括已锁文本,过滤条件在首选项中设置
	 * @return	Map<String, String >	两个键值对,srcContent --> 源文本的文本,tarContent --> 目标文本的文本
	 * 如果返回的是null,则标志source节点无内容,这对与行号就不自增
	 */
public Map<String, String> getFilteredTUContent(String xlfPath, String nodeXpath, Map<String, Boolean> filterMap) {
    Map<String, String> tuTextMap = new HashMap<String, String>();
    VTDNav vn = vnMap.get(xlfPath);
    AutoPilot ap = new AutoPilot(vn);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
    AutoPilot childAp = new AutoPilot(vn);
    try {
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            vn.push();
            //取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回,没有source节点,也返回null
            childAp.selectXPath("./source");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个source,因此此处用if不用while
                String srcContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回null
                if (srcContent == null || "".equals(srcContent)) {
                    return null;
                }
            } else {
                return null;
            }
            childAp.resetXPath();
            vn.pop();
            //首先过滤,如果有不应包括的文本段,则返回空
            if (!filterTheTU(vn, filterMap)) {
                return tuTextMap;
            }
            //下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
            childAp.selectXPath("./target");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个target,因此此处用if不用while
                String tarContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回null
                if (tarContent == null || "".equals(tarContent)) {
                    return tuTextMap;
                } else {
                    //两个检查项中的忽略标记,若有一项为true,那么就必须获取纯文本
                    tuTextMap.clear();
                }
            } else {
                return tuTextMap;
            }
            childAp.resetXPath();
        }
    } catch (Exception e) {
        logger.error(Messages.getString("qa.QAXmlHandler.logger13"), e);
    }
    return tuTextMap;
}
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 13 with VTDUtils

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

the class QAXmlHandler method getSubNodePureText.

/**
	 * 获取ph,etp,btp,it节点下sub子节点的纯文本
	 * //ph,etp,btp,it节点,只有其子节点sub内的文本才是翻译文本,故,去掉ph,etp,btp,it节点的纯文本
	 * @param vn
	 * @return
	 */
public String getSubNodePureText(VTDNav vn) {
    String subPureText = "";
    AutoPilot ap = new AutoPilot(vn);
    try {
        ap.selectXPath("./*");
        VTDUtils vUtils = new VTDUtils(vn);
        while (ap.evalXPath() != -1) {
            if (vUtils.getChildElementsCount() <= 0) {
                subPureText += " " + vUtils.getElementContent();
            } else {
                subPureText += " " + getTUPureText(vn);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger14"), e);
    }
    return subPureText;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) 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 14 with VTDUtils

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

the class QAXmlHandler method getFilteredTUTags.

/**
	 * 备注:--nonUse
	 * 获取经过过滤后的trans-unit节点的标记,过滤条件为,过滤掉不包括的文本段,如不包括上下文匹配,不包括完全匹配,不包括已锁文本,这些条件在首选项中设置。
	 * 若返回的为null, 则标志没有source节点或者source节点值为空
	 * @return	filteredTuTagMap	两个键值对,srcTag --> 源文本的标签,tarTag --> 目标文本的标签
	 */
public Map<String, List<Map<String, String>>> getFilteredTUTags(String xlfPath, String nodeXpath, Map<String, Boolean> filterMap) {
    Map<String, List<Map<String, String>>> filteredTuTagMap = new HashMap<String, List<Map<String, String>>>();
    VTDNav vn = vnMap.get(xlfPath);
    AutoPilot ap = new AutoPilot(vn);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
    AutoPilot childAp = new AutoPilot(vn);
    try {
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            vn.push();
            //取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回,没有source节点,也返回null
            childAp.selectXPath("./source");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个source,因此此处用if不用while
                String srcContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回null
                if (srcContent == null || "".equals(srcContent)) {
                    return null;
                } else {
                    filteredTuTagMap.put("srcTag", getTUTag(vn));
                }
            } else {
                return null;
            }
            childAp.resetXPath();
            vn.pop();
            //首先过滤,如果有不应包括的文本段,则返回空
            if (!filterTheTU(vn, filterMap)) {
                return filteredTuTagMap;
            }
            //下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
            childAp.selectXPath("./target");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个target,因此此处用if不用while
                String tarContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回null
                if (tarContent == null || "".equals(tarContent)) {
                    return filteredTuTagMap;
                } else {
                    filteredTuTagMap.put("tarTag", getTUTag(vn));
                }
            } else {
                return filteredTuTagMap;
            }
            childAp.resetXPath();
        }
    } catch (Exception e) {
        logger.error(Messages.getString("qa.QAXmlHandler.logger18"), e);
    }
    return filteredTuTagMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) 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 15 with VTDUtils

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

the class QAXmlHandler method getTransProgressData.

/**
	 * 针对文件分析的翻译进度分析,获取每个节点的未翻译文本段数、字数,已翻译文本段数、字数。 
	 * 如果返回为null,则标志用户退出分析
	 * 备注:此处的算法与逻辑,要与XLIFFEditorImplWithNatTable中的 updateStatusLine方法保持一致
	 * @param filePath
	 *            要处理的文件的路径
	 * @param monitor
	 *            进度条
	 * @param ignoreTag
	 *            是否忽略标记
	 * @param workInterval
	 *            进度条前进一格的间隔
	 * @param traversalTuIndex
	 *            循环trans-unit节点的序列号
	 * @return transProgDataMap,下面是其健值对详解 key: notTransPara --> 未翻译文本段数 key: translatedPara --> 已翻译文本段数 key:
	 *         notTransWords --> 未翻译字数 key: translatedWords --> 已翻译字数
	 */
public Map<String, Integer> getTransProgressData(String filePath, IProgressMonitor monitor, int workInterval, int traversalTuIndex) {
    Map<String, Integer> transProgDataMap = new HashMap<String, Integer>();
    int notTransPara = 0;
    int translatedPara = 0;
    int lockedPara = 0;
    int notTransWords = 0;
    int translatedWords = 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 ./*]");
        AutoPilot targetAp = new AutoPilot(vn);
        targetAp.selectXPath("./target");
        String srcLang = vUtils.getElementAttribute("/xliff/file[1]", "source-language");
        ap.selectXPath(XPATH_ALL_TU);
        while (ap.evalXPath() != -1) {
            traversalTuIndex++;
            int curWordsNum = 0;
            int inx = vn.getAttrVal("approved");
            String attriApproved = "";
            if (inx != -1) {
                attriApproved = vn.toString(inx);
            }
            //判断是否锁定
            boolean isLocked = false;
            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();
            // 查询该trans-unit节点下是否有target节点或者target节点的内容是否为空
            vn.push();
            if (targetAp.evalXPath() != -1) {
                String attriState = "";
                int stateInx = vn.getAttrVal("state");
                if (stateInx != -1) {
                    attriState = vn.toString(stateInx);
                }
                if ("yes".equals(attriApproved) && (!"translated".equals(attriState) || !"signed-off".equals(attriState))) {
                    translatedPara++;
                    translatedWords += curWordsNum;
                } else {
                    if ("translated".equals(attriState) || "signed-off".equals(attriState)) {
                        translatedPara++;
                        translatedWords += curWordsNum;
                    } else {
                        notTransWords += curWordsNum;
                        notTransPara++;
                    }
                }
            } else {
                notTransWords += curWordsNum;
                notTransPara++;
            }
            targetAp.resetXPath();
            vn.pop();
            if (isLocked) {
                lockedPara++;
                lockedWords += curWordsNum;
            }
            if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
                return null;
            }
        }
    } catch (Exception e) {
        if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
            return null;
        }
        e.printStackTrace();
        logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger23"), filePath), e);
    }
    transProgDataMap.put("notTransPara", notTransPara);
    transProgDataMap.put("translatedPara", translatedPara);
    transProgDataMap.put("lockedPara", lockedPara);
    transProgDataMap.put("notTransWords", notTransWords);
    transProgDataMap.put("translatedWords", translatedWords);
    transProgDataMap.put("lockedWords", lockedWords);
    return transProgDataMap;
}
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)

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