Search in sources :

Example 51 with XPathEvalException

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

the class XLFHandler method getNatTableColumnName.

/**
	 * 动态取得列名 规则:多个file中如果有相同的source-language和target-language则返回它们的属性值
	 * 多个file中如果有不同的source-language和target-language则返回“源”和“目标”
	 * @return source和target的列名集合;
	 */
public Hashtable<String, String> getNatTableColumnName() {
    Hashtable<String, String> re = new Hashtable<String, String>(2);
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    for (VTDNav vn : vnMap.values()) {
        ap.bind(vn);
        try {
            ap.selectXPath("//file");
            boolean flag1 = true;
            boolean flag2 = true;
            String source = null;
            String target = null;
            while (ap.evalXPath() != -1) {
                if (flag1) {
                    int index1 = vn.getAttrVal("source-language");
                    if (index1 != -1) {
                        String tempSource = vn.toNormalizedString(index1);
                        if (tempSource != null) {
                            if (source == null) {
                                source = tempSource;
                            } else {
                                if (source.equals(tempSource)) {
                                    flag1 = true;
                                } else {
                                    source = "源";
                                    flag1 = false;
                                }
                            }
                        }
                    } else {
                        source = "源";
                        flag1 = false;
                    }
                }
                if (flag2) {
                    int index2 = vn.getAttrVal("target-language");
                    if (index2 != -1) {
                        String tempTarget = vn.toNormalizedString(index2);
                        if (tempTarget != null) {
                            if (target == null) {
                                target = tempTarget;
                            } else {
                                if (target.equals(tempTarget)) {
                                    flag1 = true;
                                } else {
                                    target = "目标";
                                    flag2 = false;
                                }
                            }
                        }
                    } else {
                        target = "目标";
                        flag2 = false;
                    }
                }
            }
            re.put("source", source);
            re.put("target", target);
            return re;
        } catch (XPathEvalException e) {
            String errorMsg = Messages.getString("file.XLFHandler.logger5");
            LOGGER.error(errorMsg, e);
        } catch (NavException e) {
            String errorMsg = Messages.getString("file.XLFHandler.logger6");
            LOGGER.error(errorMsg, e);
        } catch (XPathParseException e) {
            String errorMsg = Messages.getString("file.XLFHandler.logger7");
            LOGGER.error(errorMsg, e);
        }
    }
    return null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) Hashtable(java.util.Hashtable) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 52 with XPathEvalException

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

the class XLFHandler method getAllRowIds.

/**
	 * 得到所有当前打开的文件包含的 RowId
	 * @return ;
	 */
public List<String> getAllRowIds() {
    HashSet<String> allRowIds = new HashSet<String>();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    VTDUtils vu = new VTDUtils();
    for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
        String fileName = entry.getKey();
        VTDNav vn = entry.getValue();
        ap.bind(vn);
        try {
            vu.bind(vn);
            ap.selectXPath(XPATH_ALL_TU);
            while (ap.evalXPath() != -1) {
                String rowId = RowIdUtil.getRowId(vn, fileName);
                if (rowId != null) {
                    allRowIds.add(rowId);
                }
            }
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
    }
    return new ArrayList<String>(allRowIds);
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ArrayList(java.util.ArrayList) VTDNav(com.ximpleware.VTDNav) HashSet(java.util.HashSet)

Example 53 with XPathEvalException

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

the class XLFHandler method validateSplitXlf.

/**
	 * 验证该文件是否符合xliff标准,主要是查看其根元素是否是xliff
	 * @param xlfPath
	 *            要验证的Xliff文件路径
	 * @return robert 2011-10-19
	 */
public boolean validateSplitXlf(String xlfPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        ap.selectXPath("/*");
        while (ap.evalXPath() != -1) {
            if ("xliff".equals(vn.toString(vn.getCurrentIndex()))) {
                return true;
            }
        }
        return false;
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return false;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 54 with XPathEvalException

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

the class XLFHandler method handleSomeSegment.

/**
	 * 处理多个文本段
	 * @param rowIds
	 *            行的唯一标识
	 * @param handler
	 *            单个文本段的处理实现 ;
	 */
private Map<String, Object> handleSomeSegment(List<String> rowIds, PerSegmentHandler handler) {
    if (rowIds == null || rowIds.isEmpty()) {
        return getSuccessResult();
    }
    VTDNav vn = null;
    VTDUtils vu = new VTDUtils();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIds);
    String errorMsg = null;
    Throwable error = null;
    for (Entry<String, List<String>> entry : map.entrySet()) {
        String fileName = entry.getKey();
        List<String> rowIdList = entry.getValue();
        vn = vnMap.get(fileName);
        vn.push();
        try {
            vu.bind(vn);
            ap.bind(vn);
            xm.bind(vn);
            for (String rowId : rowIdList) {
                handler.handle(rowId, vu, ap, xm);
            }
            // 保存并更新VTDNav对象
            saveAndReparse(xm, fileName);
        // return getSuccessResult(); //robert注释
        } catch (XPathParseException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger11");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (XPathEvalException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger12");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (NavException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger12");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (ModifyException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger13");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (UnsupportedEncodingException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger14");
            error = e;
            LOGGER.error(errorMsg, e);
        } finally {
            vn.pop();
        }
    }
    vu = null;
    ap = null;
    xm = null;
    if (errorMsg != null) {
        return getErrorResult(Messages.getString("file.XLFHandler.msg3"), error);
    } else {
        return getSuccessResult();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ModifyException(com.ximpleware.ModifyException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) VTDNav(com.ximpleware.VTDNav)

Example 55 with XPathEvalException

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

the class XLPHandler method saveXliffInfo.

/**
	 * 保存 XLIFF 信息
	 * @param xliffInfo
	 *            XLIFF 信息 ;
	 */
public void saveXliffInfo(List<? extends Map<String, String>> xliffInfo) {
    try {
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        StringBuffer items = new StringBuffer();
        String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" />";
        ap.selectXPath("/xlfedit-project");
        if (ap.evalXPath() != -1) {
            for (Map<String, String> map : xliffInfo) {
                vn.push();
                String xliff = map.get(ATTR_XLIFF);
                String tgtEnc = map.get(ATTR_TGT_ENC);
                String xpath = "./item[@" + ATTR_XLIFF + "='" + xliff + "']";
                ap.selectXPath(xpath);
                if (ap.evalXPath() != -1) {
                    updateAttribute(xm, ATTR_TGT_ENC, tgtEnc);
                } else {
                    String item = MessageFormat.format(pattern, ATTR_XLIFF, xliff, ATTR_TGT_ENC, tgtEnc);
                    items.append(item).append(LINE_SEPARATOR);
                }
                vn.pop();
            }
            if (items.length() > 1) {
                xm.insertBeforeTail(items.toString());
            }
            save(xm);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException)

Aggregations

NavException (com.ximpleware.NavException)65 XPathEvalException (com.ximpleware.XPathEvalException)65 XPathParseException (com.ximpleware.XPathParseException)65 AutoPilot (com.ximpleware.AutoPilot)59 VTDNav (com.ximpleware.VTDNav)44 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 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 VTDGen (com.ximpleware.VTDGen)11 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