Search in sources :

Example 21 with XPathEvalException

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

the class XLFHandler method validateMultiFileNodes.

/**
	 * 检验多个 file 节点是否存在 document 属性组,以及该属性组下是否存在 original 属性(用于转换 XLIFF 的源文件为 OpenOffice 和 MSOffice2007 的情况)
	 * @param fileName
	 * @return ;
	 */
public boolean validateMultiFileNodes(String fileName) {
    VTDNav vn = vnMap.get(fileName);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + fileName);
    try {
        AutoPilot subAp = new AutoPilot(vn);
        subAp.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        subAp.selectXPath("./header/hs:prop-group[@name=\"document\"]/hs:prop[@prop-type=\"original\"]");
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/xliff/file");
        while (ap.evalXPath() != -1) {
            vn.push();
            subAp.resetXPath();
            if (subAp.evalXPath() != -1) {
                String documentOriginal = vu.getElementContent();
                if (documentOriginal == null || documentOriginal.equals("")) {
                    return false;
                }
            } else {
                return false;
            }
            vn.pop();
        }
        return true;
    } 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) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 22 with XPathEvalException

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

the class XLFHandler 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("file.XLFHandler.msg4") + 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) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    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)

Example 23 with XPathEvalException

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

the class XLFHandler method getSegmentIndex.

/**
	 * 得到文本段的索引
	 * @param currentRowIndex
	 * @param relativeXPath
	 * @param isNext
	 *            是否是找下一个。<br/>
	 *            <code>true</code>: 下一个;<code>false</code>: 上一个。
	 * @return 文本段的索引;
	 */
private int getSegmentIndex(int currentRowIndex, String relativeXPath, boolean isNext) {
    int step = isNext ? 1 : -1;
    currentRowIndex += step;
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    while (currentRowIndex >= 0 && currentRowIndex < rowIds.size()) {
        String rowId = getRowId(currentRowIndex);
        VTDNav vn = getVTDNavByRowId(rowId);
        ap.bind(vn);
        String xpath = RowIdUtil.parseRowIdToXPath(rowId);
        try {
            ap.selectXPath(xpath + relativeXPath);
            if (ap.evalXPath() != -1) {
                return currentRowIndex;
            }
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
        currentRowIndex += step;
    }
    return -1;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 24 with XPathEvalException

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

the class XLFHandler method getPropValue.

/**
	 * 得到TU下指定节点的指定属性值
	 * @param rowIdList
	 *            用来生成Xpath的rowId集合
	 * @param subXPath
	 *            定位到tu下的子节点属性的xpath,例如:"/target/@state"
	 * @return 由rowId与得到的属性值的映射map。<br/>
	 *         key: rowId; value: 属性值;
	 */
private Map<String, String> getPropValue(List<String> rowIdList, String subXPath) {
    if (rowIdList == null) {
        return null;
    }
    if (subXPath.lastIndexOf('/') > subXPath.lastIndexOf('@')) {
        // 此subXPath要获取的并不是属性
        LOGGER.error(Messages.getString("file.XLFHandler.logger9"));
        return null;
    }
    Map<String, String> propValueMap = new HashMap<String, String>();
    AutoPilot ap = new AutoPilot();
    try {
        ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        int index = -1;
        for (String rowId : rowIdList) {
            VTDNav vn = getVTDNavByRowId(rowId);
            vn.push();
            ap.bind(vn);
            // 根据RowId得到定位到该翻译单元的XPath
            String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
            // 可以定位TU下的任何子节点的属性
            String propXpath = tuXPath + subXPath;
            ap.selectXPath(propXpath);
            if ((index = ap.evalXPath()) != -1) {
                propValueMap.put(rowId, vn.toNormalizedString(index + 1));
            } else {
                propValueMap.put(rowId, null);
            }
            vn.pop();
        }
    } 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 propValueMap;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 25 with XPathEvalException

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

the class XLPHandler method getItems.

/**
	 * 得到配置文件中记录的所有项
	 * @param attrName
	 *            属性名
	 * @param values
	 *            属性值
	 * @return ;
	 */
public List<Map<String, String>> getItems(String attrName, List<String> values) {
    HashMap<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
    AutoPilot ap = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath("/xlfedit-project/item[@" + attrName + "]");
        XMLModifier xm = new XMLModifier(vn);
        boolean isModified = false;
        while (ap.evalXPath() != -1) {
            int index = vn.getAttrVal(attrName);
            String value = index != -1 ? vn.toString(index) : "";
            if (values.contains(value) && !map.containsKey(value)) {
                Map<String, String> item = vu.getCurrentElementAttributs();
                if (item == null) {
                    item = new Hashtable<String, String>();
                }
                map.put(value, item);
            } else {
                // 去除无用的记录
                xm.remove();
                isModified = true;
            }
        }
        if (isModified) {
            save(xm);
        }
        for (String value : values) {
            if (!map.containsKey(value)) {
                HashMap<String, String> item = new HashMap<String, String>();
                item.put(attrName, value);
                map.put(value, item);
            }
        }
    } 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 (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    // 按顺序添加
    ArrayList<Map<String, String>> items = new ArrayList<Map<String, String>>();
    for (String value : values) {
        items.add(map.get(value));
    }
    return items;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) HashMap(java.util.HashMap) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

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