Search in sources :

Example 91 with AutoPilot

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

the class QAXmlHandler method getNodeContent.

/**
	 * 获取指定文件的指定节点的内容(除节点头与尾之外)
	 * @param xlfPath
	 * @param nodeXpath
	 * @return
	 * robert	2011-10-26
	 */
public String getNodeContent(String xlfPath, String nodeXpath) {
    String nodeContent = "";
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
    try {
        AutoPilot ap = apMap.get(xlfPath);
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            nodeContent = vUtils.getElementContent();
        }
    } catch (Exception e) {
        logger.error(Messages.getString("qa.QAXmlHandler.logger16"), e);
        e.printStackTrace();
    }
    return nodeContent;
}
Also used : 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 92 with AutoPilot

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

the class QAXmlHandler method openFile.

/**
	 * 解析文件(同时操作进度条)
	 * @param file
	 * @param monitor
	 * @param totalWork
	 * @return ;
	 */
private Map<String, Object> openFile(File file, IProgressMonitor monitor) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        monitor.beginTask(MessageFormat.format(Messages.getString("qa.QAXmlHandler.task1"), file.getName()), 10);
        String filename = file.getAbsolutePath();
        // 解析文件并获取索引
        VTDGen vgRead = new VTDGen();
        if (vgRead.parseFile(filename, true)) {
            vnRead = vgRead.getNav();
            if (monitor.isCanceled()) {
                return getReturnResult();
            }
            monitor.worked(3);
            try {
                AutoPilot ap = new AutoPilot(vnRead);
                apMap.put(filename, ap);
                // 记录xliff文件命名空间
                //			ap.selectXPath("namespace-uri(/xliff)");
                //			String xmlns;
                //			if ((xmlns = ap.evalXPathToString()) != null) {
                //				xliffXmlnsMap.put(filename, xmlns);
                //			} else {
                //				String errorMsg = "文件“" + filename + "”,不是合法的 XLIFF 文件!";
                //				return getErrorResult(errorMsg, null);
                //			}
                monitor.worked(1);
                ap.resetXPath();
                if (monitor.isCanceled()) {
                    return getReturnResult();
                }
                ap.selectXPath("count(" + XPATH_ALL_TU + ")");
                // 整个xliff文件中的trans-unit节点的个数
                int countAllTU = (int) ap.evalXPathToNumber();
                monitor.worked(6);
                tuSizeMap.put(filename, countAllTU);
                vnMap.put(filename, vnRead);
            } catch (XPathParseException e) {
                String errorMsg = Messages.getString("qa.QAXmlHandler.logger1");
                logger.error(errorMsg, e);
                return getErrorResult(errorMsg, e);
            }
            accessHistory.put(filename, "");
        } else {
            String errorMsg = MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger2"), filename);
            logger.error(errorMsg);
            return getErrorResult(errorMsg, null);
        }
    } finally {
        monitor.done();
    }
    return getSuccessResult();
}
Also used : XPathParseException(com.ximpleware.XPathParseException) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen)

Example 93 with AutoPilot

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

the class QAXmlHandler method getTUTag.

/**
	 * {@link #getTuTagStr(String, String)} 
	 * <div style='color:red'>该方法与 getTuTagStr 方法类似,如修改应保持一致</div>
	 * 获取tu节点下的标记
	 * @param ap
	 * @param vn
	 * @return
	 */
public List<Map<String, String>> getTUTag(VTDNav vn) {
    List<Map<String, String>> tagList = new LinkedList<Map<String, String>>();
    Map<String, String> tagMap;
    try {
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vUtils = new VTDUtils(vn);
        //如果没有相关标记,退出程序
        if (vUtils.getChildElementsCount() <= 0) {
            return tagList;
        }
        ap.selectXPath("./*");
        //如果子节点大于0,那继续处理
        if (vUtils.getChildElementsCount() > 0) {
            while (ap.evalXPath() != -1) {
                String childNodeName = vUtils.getCurrentElementName();
                if (QAConstant.QA_mrk.equals(childNodeName)) {
                    String tagStr = vUtils.getElementHead() + "</" + childNodeName + ">";
                    tagMap = new HashMap<String, String>();
                    tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
                    tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(tagStr));
                    tagList.add(tagMap);
                    //如果该节点下还有子节点标记,再获取其子节点标记
                    if (vUtils.getChildElementsCount() > 0) {
                        List<Map<String, String>> childTagList = getTUTag(vn);
                        for (int index = 0; index < childTagList.size(); index++) {
                            tagList.add(childTagList.get(index));
                        }
                    }
                } else if (QAConstant.QA_g.equals(childNodeName) || QAConstant.QA_sub.equals(childNodeName)) {
                    String tagStr = vUtils.getElementHead() + "</" + childNodeName + ">";
                    tagMap = new HashMap<String, String>();
                    tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
                    tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(tagStr));
                    tagList.add(tagMap);
                    if (vUtils.getChildElementsCount() > 0) {
                        List<Map<String, String>> childTagList = getTUTag(vn);
                        for (int index = 0; index < childTagList.size(); index++) {
                            tagList.add(childTagList.get(index));
                        }
                    }
                } else {
                    //ph节点的值为code data或者一个sub节点,因此,要考虑到sub节点的情况
                    if (vUtils.getChildElementsCount() <= 0) {
                        //其他节点,比如ph,etp,btp,it内都没有可翻译文本,故获取其整体文本段即可
                        String childFrag = vUtils.getElementHead() + vUtils.getElementContent() + "</" + childNodeName + ">";
                        tagMap = new HashMap<String, String>();
                        tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
                        tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(childFrag));
                        tagList.add(tagMap);
                    } else {
                        //先将该标记的纯文本获取出来
                        String childFrag = vUtils.getElementHead() + vUtils.getElementContent() + "</" + childNodeName + ">";
                        String childContent = vUtils.getElementContent();
                        String pureCode = "";
                        //获取该节点的纯文本
                        vn.push();
                        String txtNode = "./text()";
                        AutoPilot childAp = new AutoPilot(vn);
                        childAp.selectXPath(txtNode);
                        int txtIndex = -1;
                        while ((txtIndex = childAp.evalXPath()) != -1) {
                            pureCode += (" " + vn.toString(txtIndex));
                        }
                        vn.pop();
                        String tagStr = childFrag.replace(childContent == null ? "" : childContent, pureCode == null ? "" : pureCode);
                        tagMap = new HashMap<String, String>();
                        tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
                        tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(tagStr));
                        tagList.add(tagMap);
                        //下面添加该节点内的子节点sub标记
                        List<Map<String, String>> childTagList = getSubNodeTag(vn);
                        for (int index = 0; index < childTagList.size(); index++) {
                            tagList.add(childTagList.get(index));
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger18"), e);
    }
    return tagList;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) LinkedList(java.util.LinkedList) 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 94 with AutoPilot

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

the class QAXmlHandler method getNonTransElementsRegex.

/**
	 * 只获取非译元素的正则表达式
	 * @param filePath
	 * @return
	 */
public List<String> getNonTransElementsRegex(String filePath) {
    List<String> regexList = new ArrayList<String>();
    VTDNav vn = vnMap.get(filePath);
    AutoPilot ap = new AutoPilot(vn);
    validNull(vn, ap, filePath);
    try {
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath("/nonTrans/element/regular");
        while (ap.evalXPath() != -1) {
            regexList.add(vUtils.getElementContent());
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger19"), e);
    }
    return regexList;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) 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 95 with AutoPilot

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

the class QAXmlHandler method getFilteredTUTextForMultiParaConsis.

/**
	 * 针对合并打开的文本段一致性检查的情况,获取trans-unit节点过滤后的值,过滤条件为不包括上下文匹配,不包括完全匹配,不包括已锁文本,过滤条件在首选项中设置
	 * @param xlfPath	
	 * @param nodeXpath	
	 * @param filterMap	过滤条件
	 * @return ;
	 */
public Map<String, ParaConsisDataBean> getFilteredTUTextForMultiParaConsis(List<String> rowIdList, Map<String, Boolean> filterMap, boolean checkSameSource, boolean checkSameTarget, boolean srcIgnoreTag, boolean tarIgnoreTag) {
    Map<String, ParaConsisDataBean> filteredTuTextMap = new HashMap<String, ParaConsisDataBean>();
    try {
        //检查项有两个,即相同源文不同译文,相同译文不同源文,如果某项不检查,那么它的忽略标记为false
        if (!checkSameSource) {
            srcIgnoreTag = false;
        }
        if (!checkSameTarget) {
            tarIgnoreTag = false;
        }
        for (String rowId : rowIdList) {
            String xlfPath = RowIdUtil.getFileNameByRowId(rowId);
            VTDNav vn = vnMap.get(xlfPath);
            AutoPilot ap = new AutoPilot(vn);
            Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
            AutoPilot childAp = new AutoPilot(vn);
            VTDUtils vUtils = new VTDUtils(vn);
            ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
            if (ap.evalXPath() != -1) {
                ParaConsisDataBean dataBean = new ParaConsisDataBean();
                vn.push();
                //取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回null,没有source节点,也返回null
                childAp.selectXPath("./source");
                if (childAp.evalXPath() != -1) {
                    //因为标准里面只有一个source,因此此处用if不用while
                    String srcContent = vUtils.getElementContent();
                    //如果源文本为空或无值,则返回null
                    if (srcContent == null || "".equals(srcContent)) {
                        continue;
                    } else {
                        //两个检查项中的忽略标记,若有一项为true,那么就必须获取纯文本
                        if (srcIgnoreTag || tarIgnoreTag) {
                            dataBean.setSrcContent(srcContent.trim());
                            dataBean.setSrcPureText(getTUPureText(vn).trim());
                        } else {
                            dataBean.setSrcContent(srcContent.trim());
                        }
                    }
                } else {
                    continue;
                }
                childAp.resetXPath();
                vn.pop();
                //首先过滤,如果有不应包括的文本段,则返回一个空对象
                if (!filterTheTU(vn, filterMap)) {
                    continue;
                }
                vn.push();
                //下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
                childAp.selectXPath("./target");
                if (childAp.evalXPath() != -1) {
                    //因为标准里面只有一个target,因此此处用if不用while
                    String tgtContent = vUtils.getElementContent();
                    //如果源文本为空或无值,则返回空对象
                    if (tgtContent == null || "".equals(tgtContent)) {
                        continue;
                    } else {
                        //两个检查项中的忽略标记,若有一项为true,那么就必须获取纯文本
                        if (srcIgnoreTag || tarIgnoreTag) {
                            dataBean.setTgtContent(tgtContent.trim());
                            dataBean.setTgtPureText(getTUPureText(vn).trim());
                        } else {
                            dataBean.setTgtContent(tgtContent.trim());
                        }
                    }
                } else {
                    continue;
                }
                dataBean.setLineNumber(rowIdList.indexOf(rowId) + 1);
                vn.pop();
                filteredTuTextMap.put(rowId, dataBean);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger13"), e);
    }
    return filteredTuTextMap;
}
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

AutoPilot (com.ximpleware.AutoPilot)308 VTDNav (com.ximpleware.VTDNav)173 NavException (com.ximpleware.NavException)150 XPathParseException (com.ximpleware.XPathParseException)145 XPathEvalException (com.ximpleware.XPathEvalException)137 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)112 IOException (java.io.IOException)103 ModifyException (com.ximpleware.ModifyException)95 TranscodeException (com.ximpleware.TranscodeException)82 CoreException (org.eclipse.core.runtime.CoreException)76 UnsupportedEncodingException (java.io.UnsupportedEncodingException)58 VTDGen (com.ximpleware.VTDGen)50 FileNotFoundException (java.io.FileNotFoundException)49 XMLModifier (com.ximpleware.XMLModifier)46 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)44 ArrayList (java.util.ArrayList)42 HashMap (java.util.HashMap)39 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)34 LinkedList (java.util.LinkedList)25