Search in sources :

Example 56 with NavException

use of com.ximpleware.NavException 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 57 with NavException

use of com.ximpleware.NavException 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 58 with NavException

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

the class XLFHandler method resetSplitSegment.

/**
	 * 恢复分割的文本段
	 * @param rowId
	 *            唯一标识
	 * @param tuFragment
	 *            翻译单元的原始XML文本;
	 */
public void resetSplitSegment(String rowId, String tuFragment) {
    String groupXPath = RowIdUtil.parseRowIdToGroupXPath(rowId);
    String fileName = RowIdUtil.getFileNameByRowId(rowId);
    VTDNav vn = vnMap.get(fileName);
    AutoPilot ap = new AutoPilot(vn);
    try {
        ap.selectXPath(groupXPath);
        if (ap.evalXPath() != -1) {
            XMLModifier xm = new XMLModifier(vn);
            // 添加 trans-unit 节点
            xm.insertBeforeElement(tuFragment);
            // 删除 group 节点
            xm.remove(vn.getElementFragment());
            saveAndReparse(xm, fileName);
            int index = rowIds.indexOf(rowId + "-1");
            // 添加分割前的RowId
            rowIds.add(index, rowId);
            rowIds.remove(rowId + "-1");
            // 移除分割后的RowId
            rowIds.remove(rowId + "-2");
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VTDNav(com.ximpleware.VTDNav)

Example 59 with NavException

use of com.ximpleware.NavException 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)

Example 60 with NavException

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

the class XLFHandler method updateDocumentInfo.

/**
	 * 修改文档属性
	 * @param fileInfo
	 *            file节点的信息
	 * @param projectInfo
	 *            project prop-group 的信息
	 * @param customerInfo
	 *            customer prop-group 的信息;
	 */
public void updateDocumentInfo(String fileName, Map<String, String> fileInfo, Map<String, String> projectInfo, Map<String, String> customerInfo) {
    if (fileInfo == null || fileInfo.size() == 0) {
        return;
    }
    Object value = fileInfo.get(DocumentPropertiesKeys.ORIGINAL);
    if (value != null) {
        VTDNav vn = vnMap.get(fileName);
        AutoPilot ap = new AutoPilot(vn);
        String original = escapeTag(value.toString());
        try {
            ap.selectXPath("//file[@original='" + original + "']");
            if (ap.evalXPath() != -1) {
                // 定位到相应的file节点
                XMLModifier xm = new XMLModifier(vn);
                value = fileInfo.get(DocumentPropertiesKeys.TARGET_LANGUAGE);
                if (value != null) {
                    // 修改 target-language 属性
                    String targetLanguage = escapeTag(value.toString());
                    int attrIdx = vn.getAttrVal("target-language");
                    if (attrIdx != -1) {
                        xm.updateToken(attrIdx, targetLanguage);
                    } else {
                        xm.insertAttribute(" target-language=\"" + targetLanguage + "\" ");
                    }
                }
                // 定位到 header 节点
                ap.selectXPath("./header");
                if (ap.evalXPath() == -1) {
                    saveAndReparse(xm, fileName);
                    return;
                }
                value = fileInfo.get(DocumentPropertiesKeys.SKL);
                if (value != null) {
                    // 修改 skl 骨架文件路径
                    String skl = escapeTag(value.toString());
                    vn.push();
                    ap.selectXPath("./skl/external-file");
                    if (ap.evalXPath() != -1) {
                        int attrIdx = vn.getAttrVal("href");
                        if (attrIdx != -1) {
                            xm.updateToken(attrIdx, skl);
                        } else {
                            xm.insertAttribute(" href=\"" + skl + "\" ");
                        }
                    }
                    vn.pop();
                }
                ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
                String propGroup = "";
                if (projectInfo != null && projectInfo.size() > 0) {
                    vn.push();
                    // 先删除之前的 project prop-group.
                    ap.selectXPath("./hs:prop-group[@name='project']");
                    if (ap.evalXPath() != -1) {
                        xm.remove(vn.getElementFragment());
                    }
                    vn.pop();
                    // 保存新的 project prop-group.
                    propGroup += "<hs:prop-group name=\"project\">";
                    for (Entry<String, String> entry : projectInfo.entrySet()) {
                        propGroup += "<hs:prop prop-type=\"" + escapeTag(entry.getKey()) + "\">" + escapeTag(entry.getValue()) + "</hs:prop>";
                    }
                    propGroup += "</hs:prop-group>";
                }
                if (customerInfo != null && customerInfo.size() > 0) {
                    // 先删除之前的 customer prop-group.
                    vn.push();
                    ap.selectXPath("./hs:prop-group[@name='customer']");
                    if (ap.evalXPath() != -1) {
                        xm.remove(vn.getElementFragment());
                    }
                    vn.pop();
                    // 保存新的 customer prop-group.
                    propGroup += "<hs:prop-group name=\"customer\">";
                    for (Entry<String, String> entry : customerInfo.entrySet()) {
                        propGroup += "<hs:prop prop-type=\"" + escapeTag(entry.getKey()) + "\">" + escapeTag(entry.getValue()) + "</hs:prop>";
                    }
                    propGroup += "</hs:prop-group>";
                }
                // 执行修改
                if (!"".equals(propGroup)) {
                    xm.insertAfterHead(propGroup);
                    ap.selectXPath("//hs:prop-group");
                    if (ap.evalXPath() == -1) {
                        // 不存在namespace“xmlns:hs”
                        ap.selectXPath("/xliff");
                        if (ap.evalXPath() != -1) {
                            xm.insertAttribute(" xmlns:hs=\"" + hsR7NSUrl + "\" ");
                        }
                    }
                }
                // 保存文件并更新VTDNav对象
                saveAndReparse(xm, fileName);
            }
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (ModifyException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VTDNav(com.ximpleware.VTDNav)

Aggregations

NavException (com.ximpleware.NavException)93 AutoPilot (com.ximpleware.AutoPilot)70 XPathParseException (com.ximpleware.XPathParseException)70 XPathEvalException (com.ximpleware.XPathEvalException)67 VTDNav (com.ximpleware.VTDNav)63 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)58 XMLModifier (com.ximpleware.XMLModifier)31 ModifyException (com.ximpleware.ModifyException)29 ArrayList (java.util.ArrayList)22 IOException (java.io.IOException)21 VTDGen (com.ximpleware.VTDGen)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)13 TranscodeException (com.ximpleware.TranscodeException)12 ParseException (com.ximpleware.ParseException)11 FileOutputStream (java.io.FileOutputStream)11 List (java.util.List)10 LinkedList (java.util.LinkedList)9 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8