Search in sources :

Example 36 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 37 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)

Example 38 with VTDUtils

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

the class XLFHandler method getAllSrcTextForFuzzy.

/**
	 * 针对繁殖翻译。获取所有的源文。robert 2012-09-20
	 * @return
	 */
public Map<String, FuzzyTransDataBean> getAllSrcTextForFuzzy(ArrayList<String> rowIdList, boolean ignoreTag) {
    Map<String, FuzzyTransDataBean> textMap = new LinkedHashMap<String, FuzzyTransDataBean>();
    try {
        VTDNav vn = null;
        AutoPilot ap = new AutoPilot();
        VTDUtils vUtils = new VTDUtils();
        AutoPilot childAp = new AutoPilot();
        for (String rowId : rowIdList) {
            // 标识译文是否为空,如果为空,则为true,不为空则为 false
            boolean isTargetNull = false;
            // 是否锁定
            boolean isLock = false;
            vn = getVTDNavByRowId(rowId).duplicateNav();
            ap.bind(vn);
            vUtils.bind(vn);
            childAp.bind(vn);
            vn.push();
            ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
            if (ap.evalXPath() != -1) {
                // 如果,当前文本段是处于锁定状态的,就不用获取
                int index = vn.getAttrVal("translate");
                if (index != -1 && "no".equalsIgnoreCase(vn.toString(index))) {
                    isLock = true;
                }
                String srcText = null;
                vn.push();
                childAp.selectXPath("./source");
                if (childAp.evalXPath() != -1) {
                    // 如果忽略标记的话,就没有必要获取source节点的完整内容了
                    if (!ignoreTag) {
                        srcText = vUtils.getElementContent();
                    } else {
                        srcText = getTUPureText(vn);
                    }
                }
                vn.pop();
                vn.push();
                childAp.selectXPath("./target[text()!='' or *]");
                if (childAp.evalXPath() != -1) {
                    isTargetNull = false;
                } else {
                    isTargetNull = true;
                }
                vn.pop();
                // 存放值
                textMap.put(rowId, new FuzzyTransDataBean(srcText.trim(), isTargetNull, isLock));
            }
            vn.pop();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return textMap;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FuzzyTransDataBean(net.heartsome.cat.ts.core.bean.FuzzyTransDataBean) 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) LinkedHashMap(java.util.LinkedHashMap)

Example 39 with VTDUtils

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

the class XLFHandler method deleteNodeByXpath.

/**
	 * 从当前xlfPath的文件中删除xpath指定的内容 Add By Jason
	 * @param xlfPath
	 *            文件
	 * @param xpath
	 *            如当前VN定位在trans-unit节点,则./target|./alt-trans 为删除所有的target节点和alt-trans节点 ;
	 */
public void deleteNodeByXpath(String xlfPath, String xpath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    try {
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = vu.delete(xpath, VTDUtils.PILOT_TO_END);
        saveAndReparse(xm, xlfPath);
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) 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 40 with VTDUtils

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

the class XLFHandler method getTMXFileContent.

/**
	 * 根据同一个 XLIFF 文件中的 rowId 生成 TMX 文件
	 * @param list
	 *            rowId
	 * @param creationTool
	 *            创建工具
	 * @param creationToolVersion
	 *            创建工具版本
	 * @param srcName
	 *            源语言
	 * @param monitor
	 *            进度条
	 * @return TMX 文件
	 */
public StringBuffer getTMXFileContent(List<String> list, String creationTool, String creationToolVersion, String srcName, IProgressMonitor monitor) {
    if (list == null || list.size() == 0) {
        return new StringBuffer();
    }
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.beginTask(Messages.getString("file.XLFHandler.task3"), list.size());
    if (monitor.isCanceled())
        return new StringBuffer();
    StringBuffer re = new StringBuffer();
    re.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    re.append("<tmx version=\"1.4b\">");
    re.append("<header creationtool=\"" + creationTool + "\" creationtoolversion=\"" + creationToolVersion + "\" srclang=\"" + srcName + "\" adminlang=\"en\" datatype=\"xml\" o-tmf=\"XLIFF\" segtype=\"paragraph\">");
    re.append("</header>");
    re.append("<body>");
    VTDNav vn = getVTDNavByRowId(list.get(0));
    AutoPilot ap = new AutoPilot(vn);
    VTDUtils vu = null;
    try {
        vu = new VTDUtils(vn);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        monitor.done();
    }
    String defaultSrcLang = "";
    String defaultTgtLang = "";
    String fileXPath = RowIdUtil.getFileXpathByRowId(list.get(0));
    if (vu.pilot(ap, fileXPath) != -1) {
        defaultSrcLang = vu.getValue(ap, "./@source-language");
        defaultTgtLang = vu.getValue(ap, "./@target-language");
    }
    if (defaultTgtLang == null) {
        defaultTgtLang = "";
    }
    for (String rowId : list) {
        if (monitor.isCanceled())
            return new StringBuffer();
        String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
        try {
            ap.selectXPath(tuXPath);
            ap.evalXPath();
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
            monitor.done();
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
            monitor.done();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
            monitor.done();
        }
        re.append("<tu ");
        // 添加tu的属性
        boolean hasTuId = false;
        String id = null;
        String attrNameXp = "./@*[name]";
        String attrValueXp = "./@";
        List<String> tuPropNames = vu.getValues(ap, attrNameXp);
        for (String name : tuPropNames) {
            String value = vu.getValue(ap, attrValueXp + name);
            re.append(name + "=\"" + value + "\" ");
            if (name.equals("id")) {
                id = value;
            }
            if (!hasTuId) {
                if (name.equals("tuid")) {
                    hasTuId = true;
                }
            }
        }
        if (!hasTuId) {
            re.append("tuid=\"" + genTuId(rowId, id) + "\"");
        }
        re.append(">");
        String typeXp = "./prop-group/prop/@prop-type";
        List<String> typeValues = vu.getValues(ap, typeXp);
        if (typeValues != null) {
            for (String typeValue : typeValues) {
                re.append("<prop type=\"" + typeValue + "\">");
                re.append(vu.getValue(ap, "./prop-group/prop[@prop-type='" + typeValue + "']/text()"));
                re.append("</prop>");
            }
        }
        // HS的自定义属性组
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        String hsPropXp = "./hs:prop-group/prop/@prop-type";
        typeValues = vu.getValues(ap, hsPropXp);
        if (typeValues != null) {
            for (String typeValue : typeValues) {
                re.append("<prop type=\"" + typeValue + "\">");
                re.append(vu.getValue(ap, "./hs:prop-group/prop[@prop-type='" + typeValue + "']/text()"));
                re.append("</prop>");
            }
        }
        // 添加tuv(source)
        re.append("<tuv ");
        // 添加tuv(source)属性
        attrNameXp = "./source/@*[name]";
        attrValueXp = "./source/@";
        List<String> sourcePropNames = vu.getValues(ap, attrNameXp);
        if (sourcePropNames != null) {
            for (String name : sourcePropNames) {
                String value = vu.getValue(ap, attrValueXp + name);
                re.append(name + "=\"" + value + "\" ");
            }
        }
        /*
			 * source节点可能没有xml:lang属性,如果没有,则要去file节点去取source-language(Required)属性的值
			 */
        if (sourcePropNames == null || !sourcePropNames.contains("xml:lang")) {
            re.append("xml:lang=\"" + defaultSrcLang + "\"");
        }
        re.append(">");
        // 添加上下文prop
        re.append("<prop type=\"x-preContext\">");
        re.append(getContext(vu, ap, contextNum, true));
        re.append("</prop>");
        re.append("<prop type=\"x-nextContext\">");
        re.append(getContext(vu, ap, contextNum, false));
        re.append("</prop>");
        String sourceTextXp = "./source/text()";
        re.append("<seg>").append(vu.getValue(ap, sourceTextXp)).append("</seg>");
        re.append("</tuv>");
        // 添加tuv(target)
        re.append("<tuv ");
        // 添加tuv(target)属性
        attrNameXp = "./target/@*[name]";
        attrValueXp = "./target/@";
        List<String> targetPropNames = vu.getValues(ap, attrNameXp);
        if (targetPropNames != null) {
            for (String name : targetPropNames) {
                String value = vu.getValue(ap, attrValueXp + name);
                re.append(name + "=\"" + value + "\" ");
            }
        }
        /*
			 * target节点可能没有xml:lang属性,如果没有,则要去file节点去取target-language(Optional)属性的值
			 */
        if (targetPropNames == null || !targetPropNames.contains("xml:lang")) {
            re.append("xml:lang=\"" + defaultTgtLang + "\"");
        }
        re.append(">");
        String targetTextXp = "./target/text()";
        re.append("<seg>").append(vu.getValue(ap, targetTextXp)).append("</seg>");
        re.append("</tuv>");
        re.append("</tu>");
        monitor.worked(1);
    }
    re.append("</body>");
    re.append("</tmx>");
    monitor.done();
    return re;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) 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