Search in sources :

Example 81 with VTDNav

use of com.ximpleware.VTDNav 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 82 with VTDNav

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

the class XLFHandler method getNodeAttribute.

/**
	 * 获取某属性名的属性值
	 * @param xlfPath
	 * @param nodeXpath
	 * @param attrName
	 * @return robert 2011-11-02
	 */
public String getNodeAttribute(String xlfPath, String nodeXpath, String attrName) {
    String attribute = "";
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    try {
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            attribute = vu.getCurrentElementAttribut(attrName, "");
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return attribute;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) XQException(javax.xml.xquery.XQException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 83 with VTDNav

use of com.ximpleware.VTDNav 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 84 with VTDNav

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

the class XLFHandler method getNodeAttributes.

/**
	 * 获取指定xpath节点的所有属性,如果没有属性,则返回null
	 * @param xlfPath
	 * @param nodeXpath
	 * @return ;
	 */
public Hashtable<String, String> getNodeAttributes(String xlfPath, String nodeXpath) {
    Hashtable<String, String> attributes = new Hashtable<String, String>();
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    try {
        AutoPilot ap = new AutoPilot(vn);
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            attributes = vu.getCurrentElementAttributs();
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return attributes;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) Hashtable(java.util.Hashtable) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) XQException(javax.xml.xquery.XQException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 85 with VTDNav

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

the class XLFHandler method addDataToXlf.

/**
	 * 将数据添加到切割后新生成的文件中,并且是添加到指定节点的尾部
	 * @param newXlfPath
	 * @param data
	 *            要添加的内容
	 * @param toXpath
	 *            要添加的位置 robert 2011-10-21
	 */
public void addDataToXlf(String newXlfPath, String data, String toXpath) {
    VTDNav vn = vnMap.get(newXlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + newXlfPath);
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(toXpath);
        if (ap.evalXPath() != -1) {
            XMLModifier xm = new XMLModifier(vn);
            xm.insertBeforeTail((data + "\n").getBytes("UTF8"));
            // VTDUtils vUtils = new VTDUtils(vn);
            // 更新新生成的xliff文件,并重新加载并更新VTDVNav
            // vnMap.put(newXlfPath, vUtils.updateVTDNav(xm, newXlfPath));
            saveAndReparse(xm, newXlfPath);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) XQException(javax.xml.xquery.XQException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Aggregations

VTDNav (com.ximpleware.VTDNav)206 AutoPilot (com.ximpleware.AutoPilot)177 NavException (com.ximpleware.NavException)128 XPathParseException (com.ximpleware.XPathParseException)115 XPathEvalException (com.ximpleware.XPathEvalException)110 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)99 IOException (java.io.IOException)85 ModifyException (com.ximpleware.ModifyException)81 TranscodeException (com.ximpleware.TranscodeException)69 CoreException (org.eclipse.core.runtime.CoreException)69 XMLModifier (com.ximpleware.XMLModifier)53 UnsupportedEncodingException (java.io.UnsupportedEncodingException)49 FileNotFoundException (java.io.FileNotFoundException)46 VTDGen (com.ximpleware.VTDGen)45 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)43 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)27 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)25 LinkedList (java.util.LinkedList)23