Search in sources :

Example 56 with XPathEvalException

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

the class XLPHandler method saveSourceInfo.

/**
	 * 保存 XLIFF 信息
	 * @param sourceInfo
	 *            源文件信息 ;
	 */
public void saveSourceInfo(List<? extends Map<String, String>> sourceInfo) {
    try {
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        StringBuffer items = new StringBuffer();
        String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" {6}=\"{7}\" />";
        ap.selectXPath("/xlfedit-project");
        if (ap.evalXPath() != -1) {
            for (Map<String, String> map : sourceInfo) {
                vn.push();
                String source = map.get(ATTR_SOURCE);
                String format = map.get(ATTR_FORMAT);
                String srcLang = map.get(ATTR_SRC_LANG);
                String srcEnc = map.get(ATTR_SRC_ENC);
                String xpath = "./item[@" + ATTR_SOURCE + "=\"" + source + "\"]";
                ap.selectXPath(xpath);
                if (ap.evalXPath() != -1) {
                    updateAttribute(xm, ATTR_FORMAT, format);
                    updateAttribute(xm, ATTR_SRC_LANG, srcLang);
                    updateAttribute(xm, ATTR_SRC_ENC, srcEnc);
                } else {
                    String item = MessageFormat.format(pattern, ATTR_SOURCE, source, ATTR_FORMAT, format, ATTR_SRC_LANG, srcLang, ATTR_SRC_ENC, srcEnc);
                    ap.selectXPath(xpath);
                    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)

Example 57 with XPathEvalException

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

the class XLFHandler method handleAllSegment.

/**
	 * 处理所有的文本段
	 * @param handler
	 *            单个文件的处理实现 ;
	 */
private void handleAllSegment(PerFileHandler handler) {
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    VTDUtils vu = new VTDUtils();
    VTDNav vn;
    for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
        String fileName = entry.getKey();
        vn = entry.getValue();
        vn.push();
        try {
            ap.bind(vn);
            xm.bind(vn);
            vu.bind(vn);
            // 针对每个文件的VTDNav对象进行操作
            handler.handle(fileName, vu, ap, xm);
        } catch (ModifyException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } 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 (UnsupportedEncodingException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } finally {
            vn.pop();
        }
    }
    ap = null;
    xm = null;
    vu = null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) 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 58 with XPathEvalException

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

the class XLFHandler method getCustomerInfo.

/**
	 * 得到用户定制的信息(customer prop-group 的信息)
	 * @param fileName
	 * @return 以{ {key1, value1}, {key2, value2} }二维数组的形式返回;
	 */
public String[][] getCustomerInfo(String fileName) {
    ArrayList<String[]> customerInfo = new ArrayList<String[]>();
    VTDNav vn = vnMap.get(fileName);
    AutoPilot ap = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        ap.selectXPath("/xliff/file/header/hs:prop-group[@name='customer']/hs:prop");
        while (ap.evalXPath() != -1) {
            String key = vu.getCurrentElementAttribut("prop-type", "");
            String value = vn.toString(vn.getText());
            String[] entry = { key, value };
            customerInfo.add(entry);
        }
    } 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 customerInfo.toArray(new String[][] {});
}
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) VTDNav(com.ximpleware.VTDNav)

Example 59 with XPathEvalException

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

the class XLFHandler method changeSomeTranslateProp.

/**
	 * 若当前目标文本段(target)内容不为空,则自动将其 state 属性值设为“translated”
	 * @param rowIdList
	 *            要修改的翻译单元Id的集合;
	 */
public void changeSomeTranslateProp(List<String> rowIdList) {
    if (rowIdList == null) {
        return;
    }
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIdList);
    VTDNav vn = null;
    AutoPilot ap = new AutoPilot();
    for (Entry<String, List<String>> entry : map.entrySet()) {
        String fileName = entry.getKey();
        List<String> rowIds = entry.getValue();
        vn = vnMap.get(fileName);
        ap.bind(vn);
        boolean isNew;
        for (int i = 0; i < rowIds.size(); i++) {
            ap.resetXPath();
            String rowId = rowIds.get(i);
            String tuXPath = RowIdUtil.parseRowIdToXPathWithCondition(rowId, "(not(source='')) and (not(target=''))");
            try {
                isNew = false;
                ap.selectXPath(tuXPath + "/target/@state");
                int stateIdx = -1;
                if ((stateIdx = ap.evalXPath()) != -1) {
                    String state = vn.toNormalizedString(stateIdx + 1);
                    if ("new".equalsIgnoreCase(state)) {
                        isNew = true;
                    }
                }
                if (!isNew && rowIdList.size() > i) {
                    // state不为“new”
                    rowIdList.remove(i);
                }
            // else { // state为“new”,则继续找下一个
            //
            // }
            } catch (XPathParseException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            } catch (XPathEvalException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            } catch (NavException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
    }
    if (rowIdList.size() > 0) {
        changeTgtPropValue(rowIdList, "state", "translated", "new");
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) VTDNav(com.ximpleware.VTDNav)

Example 60 with XPathEvalException

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

the class QAXmlHandler method getNodeFrag.

/**
	 * 获取整个节点,包括其头部,其子节点,其文本
	 * @param xlfPath
	 * @param nodeXPath	节点的xpath
	 * @return
	 * robert	2011-10-21
	 */
public String getNodeFrag(String xlfPath, String nodeXPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
    String xliffNodeContent = "";
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(nodeXPath);
        VTDUtils vu = new VTDUtils(vn);
        if (ap.evalXPath() != -1) {
            xliffNodeContent = vu.getElementFragment();
        }
    } catch (XPathParseException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
    } catch (NavException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
    } catch (XPathEvalException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
    }
    return xliffNodeContent;
}
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)

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