Search in sources :

Example 86 with VTDUtils

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

the class XLFHandler method updateAndSave.

/**
	 * 修改翻译单元内容并保存
	 * @param rowId
	 * @param subXPath
	 * @param newValue
	 *            ;
	 */
public void updateAndSave(String rowId, String subXPath, String newValue) {
    String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
    String fileName = RowIdUtil.getFileNameByRowId(rowId);
    VTDNav vn = vnMap.get(fileName);
    try {
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = vu.update(tuXPath + subXPath, newValue, VTDUtils.CREATE_IF_NOT_EXIST);
        saveAndReparse(xm, fileName);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 87 with VTDUtils

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

the class XLFHandler method generateTMXString.

public String generateTMXString(String systemUser, String filePath, String srcLang, String tgtLang, String original, VTDNav vn, boolean isAddApprove, boolean isAddSignedOff, boolean isAddTranslate, boolean isAddDraft, boolean isAddLocked, int contextSize, IProject project) {
    String strChangeBy = systemUser;
    StringBuffer re = new StringBuffer();
    String xpath = "/xliff/file[@original='" + original + "' and @source-language='" + srcLang + "' and @target-language='" + tgtLang + "']/body//trans-unit[source/text()!='' or source/*]";
    AutoPilot ap = new AutoPilot(vn);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    AutoPilot ap2 = new AutoPilot(vn);
    ap2.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    VTDUtils vu = null;
    boolean isNull = true;
    boolean blnLocked = false;
    boolean blnDraft = false;
    boolean blnTranslate = false;
    boolean blnApproved = false;
    boolean blnSignedOff = false;
    String srcText;
    String tgtText;
    String sendToTM;
    String locked;
    String state;
    String approved;
    String id;
    String creationDate;
    String changeDate;
    // 添加自定义属性
    String strProp = getProjectProp(project);
    try {
        ap.selectXPath(xpath);
        vu = new VTDUtils(vn);
        vn.push();
        while (ap.evalXPath() != -1) {
            srcText = vu.getValue("./source/text()");
            tgtText = vu.getValue("./target/text()");
            // 源文或目标文本段为空(包括空格),不添加到记忆库
            if (srcText == null || srcText.trim().equals("") || tgtText == null || tgtText.trim().equals("")) {
                continue;
            }
            // 标记为“不添加到记忆库”状态的文本段不添加到记忆库(默认为 yes,表示添加到记忆库)。
            sendToTM = vu.getCurrentElementAttribut("hs:send-to-tm", "yes");
            if (sendToTM.equalsIgnoreCase("no")) {
                continue;
            }
            // 锁定状态
            locked = vu.getCurrentElementAttribut("translate", "yes");
            blnLocked = false;
            if (locked.equalsIgnoreCase("no")) {
                blnLocked = isAddLocked;
            }
            blnDraft = false;
            blnTranslate = false;
            blnApproved = false;
            blnSignedOff = false;
            if (locked.equalsIgnoreCase("yes")) {
                state = vu.getValue("./target/@state");
                approved = vu.getCurrentElementAttribut("approved", "no");
                // 是否添加草稿到记忆库
                if (state != null && state.equalsIgnoreCase("new") && approved.equalsIgnoreCase("no")) {
                    blnDraft = isAddDraft;
                }
                // 是否添加完成翻译文本段到记忆库
                if (approved.equalsIgnoreCase("no") && state != null && state.equalsIgnoreCase("translated")) {
                    blnTranslate = isAddTranslate;
                }
                // 是否添加已批准文本段到记忆库
                if (approved.equalsIgnoreCase("yes") && (state == null || !state.equalsIgnoreCase("signed-off"))) {
                    blnApproved = isAddApprove;
                }
                // 是否添加已签发文本段到记忆库
                if (state != null && state.equalsIgnoreCase("signed-off")) {
                    blnSignedOff = isAddSignedOff;
                }
            }
            if (!(blnLocked || blnDraft || blnTranslate || blnApproved || blnSignedOff)) {
                continue;
            }
            id = vu.getCurrentElementAttribut("id", null);
            re.append("<tu ");
            // 添加tu的属性
            creationDate = CommonFunction.retTMXDate();
            changeDate = creationDate;
            re.append("tuid=\"" + (original.hashCode() + "-" + id.hashCode()) + "\" ");
            re.append("changedate=\"" + changeDate + "\" changeid=\"" + strChangeBy + "\" ");
            re.append("creationdate=\"" + creationDate + "\" creationid=\"" + strChangeBy + "\" >\n");
            // 添加上下文prop
            re.append("<prop type=\"x-preContext\">");
            ap2.selectXPath("/xliff/file[@original='" + original + "' and @source-language='" + srcLang + "' and @target-language='" + tgtLang + "']/body//trans-unit[@id='" + id + "']");
            re.append(getContext(vu, ap2, contextSize, true));
            re.append("</prop>\n");
            re.append("<prop type=\"x-nextContext\">");
            re.append(getContext(vu, ap2, contextSize, false));
            re.append("</prop>\n");
            re.append(strProp);
            re.append("<tuv xml:lang=\"" + srcLang + "\" >\n");
            re.append("<seg>").append(srcText.trim()).append("</seg>\n");
            re.append("</tuv>\n");
            // 添加tuv(target)
            re.append("<tuv xml:lang=\"" + tgtLang + "\">\n");
            re.append("<seg>").append(tgtText.trim()).append("</seg>\n");
            re.append("</tuv>\n");
            re.append("</tu>\n");
            isNull = false;
        }
    } 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);
    } catch (XPathParseException e) {
        LOGGER.error(MessageFormat.format(Messages.getString("file.XLFHandler.logger17"), filePath), e);
    } finally {
        vn.pop();
    }
    if (isNull) {
        return null;
    } else {
        return re.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)

Example 88 with VTDUtils

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

the class XLFHandler method getNodeHeader.

/**
	 * 获取指定节点的头
	 * @param xlfPath
	 * @param nodeName
	 *            节点名称
	 * @param nodeXPath
	 *            节点路径
	 * @return robert 2011-10-20
	 */
public String getNodeHeader(String xlfPath, String nodeName, String nodeXPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    String xliffNodeHeader = "";
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(nodeXPath);
        VTDUtils vu = new VTDUtils(vn);
        while (ap.evalXPath() != -1) {
            xliffNodeHeader = vu.getElementHead();
        }
        xliffNodeHeader += "\n</" + nodeName + ">";
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return xliffNodeHeader;
}
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)

Example 89 with VTDUtils

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

the class TSFileHandler method analysisEditorProgress.

// 分析编辑进度。直接返回报告内容的 XML 字符串。
protected String analysisEditorProgress(VTDNav vn, final String filepath, float eqvFactor) throws XPathParseException, XPathEvalException, NavException {
    VTDUtils vu = new VTDUtils(vn);
    StringBuilder sb = new StringBuilder();
    sb.append("\t<status-infos purpose=\"");
    sb.append(Messages.getString("file.TSFileHandler.analysisEditorProgress"));
    sb.append("\" filepath=\"" + filepath + "\">\n");
    int approvedSegs = 0;
    int unapprovedSegs = 0;
    int approvedWords = 0;
    int unapprovedWords = 0;
    boolean isApproved = false;
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("//trans-unit");
    while (ap.evalXPath() != -1) {
        int inx = vn.getAttrVal("approved");
        // 判断 approved 属性的值。存在取其结果,不存在取默认值 no。
        if (inx != -1) {
            isApproved = Boolean.parseBoolean(vn.toString(inx));
        } else {
            isApproved = false;
        }
        vn.push();
        // 开始统计分析。
        String srcText = vu.getChildPureText("source");
        String lang = vu.getElementAttribute("source", "xml:lang");
        int words = wordCount(srcText, lang);
        if (isApproved) {
            approvedSegs++;
            approvedWords += words;
        } else {
            unapprovedSegs++;
            unapprovedWords += words;
        }
        vn.pop();
    }
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.approvedSegs"));
    sb.append("\" statisticunits=\"segment\">");
    sb.append(approvedSegs);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.unapprovedSegs"));
    sb.append("\" statisticunits=\"segment\">");
    sb.append(unapprovedSegs);
    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(approvedSegs * 100 / (approvedSegs + unapprovedSegs));
    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(approvedSegs + unapprovedSegs);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.approvedWords"));
    sb.append("\" statisticunits=\"word\">");
    sb.append(approvedWords);
    sb.append("</status-info>\n");
    sb.append("\t\t<status-info type=\"");
    sb.append(Messages.getString("file.TSFileHandler.unapprovedWords"));
    sb.append("\" statisticunits=\"word\">");
    sb.append(unapprovedWords);
    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(approvedWords * 100 / (approvedWords + unapprovedWords));
    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(approvedWords + unapprovedWords);
    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 90 with VTDUtils

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

the class XLFHandler method updateNote.

public void updateNote(Map<String, List<String>> mapRowIdByFileName, String oldFrom, String oldText, String newFrom, String newText) {
    oldFrom = StringUtilsBasic.checkNullStr(oldFrom);
    oldText = StringUtilsBasic.checkNullStr(oldText);
    newFrom = StringUtilsBasic.checkNullStr(newFrom);
    newText = StringUtilsBasic.checkNullStr(newText);
    StringBuffer subXPath = new StringBuffer();
    if (!oldFrom.equals("")) {
        subXPath.append("@from='" + oldFrom + "' and ");
    }
    subXPath.insert(0, "/note[");
    subXPath.append("text()='");
    try {
        XMLModifier xm = new XMLModifier();
        VTDUtils vu = new VTDUtils();
        Iterator<Entry<String, List<String>>> it = mapRowIdByFileName.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> entry = it.next();
            String fileName = entry.getKey();
            VTDNav vn = vnMap.get(fileName);
            xm.bind(vn);
            vu.bind(vn);
            for (String rowId : entry.getValue()) {
                StringBuffer sbTuXPath = new StringBuffer(RowIdUtil.parseRowIdToXPath(rowId));
                xm = vu.update(null, xm, sbTuXPath.append(subXPath).append(oldText).append("']/text()").toString(), newText);
                xm = vu.update(null, xm, sbTuXPath.append(subXPath).append(oldText).append("']/@from").toString(), newFrom, 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) Entry(java.util.Map.Entry) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) 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