Search in sources :

Example 46 with XPathParseException

use of com.ximpleware.XPathParseException 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 47 with XPathParseException

use of com.ximpleware.XPathParseException 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 48 with XPathParseException

use of com.ximpleware.XPathParseException 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 49 with XPathParseException

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

Example 50 with XPathParseException

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

the class XLFHandler method saveAsText.

/**
	 * 存为纯文本
	 * @param fileName
	 * @param out
	 * @param elementName
	 * @return ;
	 */
public boolean saveAsText(String fileName, String out, String elementName) {
    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(out));
        VTDNav vn = vnMap.get(fileName);
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath("/xliff/file");
        while (ap.evalXPath() != -1) {
            AutoPilot subAp = new AutoPilot(vn);
            subAp.selectXPath("descendant::trans-unit[source/text()!='']");
            while (subAp.evalXPath() != -1) {
                if ("target".equals(elementName)) {
                    String approvedValue = vu.getValue("./@approved");
                    approvedValue = approvedValue == null ? "no" : approvedValue;
                    if (!"yes".equalsIgnoreCase(approvedValue)) {
                        // 未批准
                        continue;
                    }
                }
                String value = vu.getValue("./" + elementName + "/text()");
                if (value != null) {
                    bos.write((value + "\n").getBytes());
                }
            }
        }
        return true;
    } catch (FileNotFoundException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
        return false;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) VTDNav(com.ximpleware.VTDNav)

Aggregations

XPathParseException (com.ximpleware.XPathParseException)71 NavException (com.ximpleware.NavException)66 XPathEvalException (com.ximpleware.XPathEvalException)66 AutoPilot (com.ximpleware.AutoPilot)65 VTDNav (com.ximpleware.VTDNav)46 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)36 ModifyException (com.ximpleware.ModifyException)17 ArrayList (java.util.ArrayList)17 XMLModifier (com.ximpleware.XMLModifier)15 IOException (java.io.IOException)15 VTDGen (com.ximpleware.VTDGen)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)9 ParseException (com.ximpleware.ParseException)8 TranscodeException (com.ximpleware.TranscodeException)7 FileNotFoundException (java.io.FileNotFoundException)7 FileOutputStream (java.io.FileOutputStream)6 EOFException (com.ximpleware.EOFException)5 EncodingException (com.ximpleware.EncodingException)5 EntityException (com.ximpleware.EntityException)5