Search in sources :

Example 56 with XPathParseException

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

the class QAXmlHandler method getLanguages.

/**
	 * 得到所有语言对 备注:重复,从XLFHandler.java中拷取
	 * @return 语言对的Map<br/>
	 *         key: 源语言;value: 对应的目标语言(可以是多个)
	 */
public Map<String, ArrayList<String>> getLanguages() {
    TreeMap<String, ArrayList<String>> languages = new TreeMap<String, ArrayList<String>>();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    VTDUtils vu = new VTDUtils();
    for (VTDNav vn : vnMap.values()) {
        ap.bind(vn);
        try {
            vu.bind(vn);
            ap.selectXPath("/xliff/file");
            while (ap.evalXPath() != -1) {
                String srcLanguage = vu.getCurrentElementAttribut("source-language", null);
                String tgtLanguage = vu.getCurrentElementAttribut("target-language", null);
                if (srcLanguage == null) {
                    // TODO 该file节点不存在“source-language”属性,提醒添加
                    continue;
                }
                if (tgtLanguage == null) {
                    // TODO 该file节点不存在“target-language”属性,提醒添加
                    continue;
                }
                ArrayList<String> tgtLanguages = languages.get(srcLanguage);
                if (tgtLanguages == null) {
                    tgtLanguages = new ArrayList<String>();
                    languages.put(srcLanguage, tgtLanguages);
                }
                if (!tgtLanguages.contains(tgtLanguage)) {
                    // 未包含,就添加进去
                    tgtLanguages.add(tgtLanguage);
                }
            }
        } catch (XPathParseException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
        } catch (XPathEvalException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
        } catch (NavException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
        }
    }
    return languages;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) TreeMap(java.util.TreeMap) VTDNav(com.ximpleware.VTDNav)

Example 57 with XPathParseException

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

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

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

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

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