Search in sources :

Example 31 with NavException

use of com.ximpleware.NavException 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 32 with NavException

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

the class XLFHandler method getTgtPropValue.

/**
	 * 得到Target节点属性的值
	 * @param rowId
	 *            行的唯一标识
	 * @return ;
	 */
public String getTgtPropValue(String rowId, String propName) {
    VTDNav vn = getVTDNavByRowId(rowId);
    String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
    try {
        VTDUtils vu = new VTDUtils(vn);
        return vu.getValue(tuXPath + "/target/@" + propName);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return null;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 33 with NavException

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

Example 34 with NavException

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

the class QAXmlHandler method getTUPureTextOrContent.

/**
	 * 根据需求获取trans-unit下source或target的纯文本,或者整体内容
	 * @return	如果返回null,则证明这个节点是个空节点,要么没有这个节点,要么这个节点没有值
	 */
public String getTUPureTextOrContent(String xlfPath, String nodeXpath, boolean ignoreTag) {
    VTDNav vn = vnMap.get(xlfPath);
    AutoPilot ap = apMap.get(xlfPath);
    validNull(vn, ap, xlfPath);
    try {
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            String content = vUtils.getElementContent();
            if (content == null || "".equals(content)) {
                return null;
            }
            // 如果忽略标记,就返回纯文本,否则返回整体内容
            if (ignoreTag) {
                return getTUPureText(vn);
            }
            return content;
        }
    } catch (NavException e) {
        e.printStackTrace();
        logger.error("", e);
    } catch (XPathParseException e) {
        e.printStackTrace();
        logger.error("", e);
    } catch (XPathEvalException e) {
        e.printStackTrace();
        logger.error("", e);
    }
    return null;
}
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 35 with NavException

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

the class QAXmlHandler method getTransUnitContext.

/**
	 * 通过rowId获取当前翻译单元的上下文
	 * @param rowId 
	 * @param num 上下文个数
	 * @return ;
	 */
public Map<String, String> getTransUnitContext(String rowId, int num) {
    Map<String, String> result = new HashMap<String, String>();
    if (tuSizeMap == null || rowId == null) {
        return null;
    }
    VTDUtils vu = null;
    VTDNav vn = getVTDNavByRowId(rowId);
    try {
        vu = new VTDUtils(vn);
    } catch (NavException e1) {
        String errorMsg = Messages.getString("qa.QAXmlHandler.logger21");
        logger.error(errorMsg, e1);
        return null;
    }
    AutoPilot ap = new AutoPilot(vu.getVTDNav());
    result.put("x-preContext", getContext(vu, ap, num, true));
    result.put("x-nextContext", getContext(vu, ap, num, false));
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Aggregations

NavException (com.ximpleware.NavException)93 AutoPilot (com.ximpleware.AutoPilot)70 XPathParseException (com.ximpleware.XPathParseException)70 XPathEvalException (com.ximpleware.XPathEvalException)67 VTDNav (com.ximpleware.VTDNav)63 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)58 XMLModifier (com.ximpleware.XMLModifier)31 ModifyException (com.ximpleware.ModifyException)29 ArrayList (java.util.ArrayList)22 IOException (java.io.IOException)21 VTDGen (com.ximpleware.VTDGen)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)13 TranscodeException (com.ximpleware.TranscodeException)12 ParseException (com.ximpleware.ParseException)11 FileOutputStream (java.io.FileOutputStream)11 List (java.util.List)10 LinkedList (java.util.LinkedList)9 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8