Search in sources :

Example 56 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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 57 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.

the class XLFHandler method getNotes.

public Vector<NoteBean> getNotes(String rowId) throws NavException, XPathParseException, XPathEvalException {
    Vector<NoteBean> notes = new Vector<NoteBean>();
    VTDNav vn = getVTDNavByRowId(rowId);
    VTDUtils vu = new VTDUtils(vn);
    AutoPilot ap = new AutoPilot(vn);
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
    ap.selectXPath(tuXPath + "/note");
    while (ap.evalXPath() != -1) {
        NoteBean note = new NoteBean(vu.getElementContent());
        note.setLang(vu.getCurrentElementAttribut("xml:lang", null));
        note.setFrom(vu.getCurrentElementAttribut("from", null));
        note.setPriority(vu.getCurrentElementAttribut("priority", null));
        note.setAnnotates(vu.getCurrentElementAttribut("annotates", null));
        note.setApplyCurrent(vu.getCurrentElementAttribut("hs:apply-current", "Yes"));
        notes.add(0, note);
    }
    if (notes.isEmpty()) {
        notes = null;
    }
    return notes;
}
Also used : NoteBean(net.heartsome.cat.ts.core.bean.NoteBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) Vector(java.util.Vector) VTDNav(com.ximpleware.VTDNav)

Example 58 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils 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 59 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.

the class QAXmlHandler method getFilteredTUText.

/**
	 * 获取trans-unit节点过滤后的值,过滤条件为不包括上下文匹配,不包括完全匹配,不包括已锁文本,过滤条件在首选项中设置
	 * @return Map<String, String > 六个键值对,srcPureText --> 源文本的纯文本,tarPureText --> 目标文本的纯文本, srcContent -->
	 *         源文本的内容,tarContent --> 目标文本的内容 , srcTag --> 源文本的标记, tarTag --> 目标文本的标记;
	 *         如果返回的是null,则标志source节点无内容,这对与行号就不自增
	 * @param xlfPath	
	 * @param nodeXpath	
	 * @param filterMap	过滤条件
	 * @return ;
	 */
public QATUDataBean getFilteredTUText(String xlfPath, String nodeXpath, Map<String, Boolean> filterMap) {
    QATUDataBean bean = new QATUDataBean();
    VTDNav vn = vnMap.get(xlfPath);
    AutoPilot ap = new AutoPilot(vn);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
    AutoPilot childAp = new AutoPilot(vn);
    try {
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            String rowId = RowIdUtil.getRowId(vn, xlfPath);
            bean.setRowId(rowId);
            vn.push();
            //取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回null,没有source节点,也返回null
            childAp.selectXPath("./source");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个source,因此此处用if不用while
                String srcContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回null
                if (srcContent == null || "".equals(srcContent)) {
                    return null;
                } else {
                    bean.setSrcPureText(getTUPureText(vn));
                    bean.setSrcContent(srcContent);
                }
            } else {
                return null;
            }
            childAp.resetXPath();
            vn.pop();
            //首先过滤,如果有不应包括的文本段,则返回一个空对象
            if (!filterTheTU(vn, filterMap)) {
                bean.setPassFilter(false);
                return bean;
            }
            //下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
            childAp.selectXPath("./target");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个target,因此此处用if不用while
                String tarContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回空对象
                if (tarContent == null || "".equals(tarContent)) {
                    bean.setTgtContent(tarContent);
                    return bean;
                } else {
                    bean.setTgtContent(tarContent);
                    bean.setTgtPureText(getTUPureText(vn));
                }
            } else {
                return bean;
            }
            childAp.resetXPath();
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger13"), e);
    }
    return bean;
}
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) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Example 60 with VTDUtils

use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.

the class QAXmlHandler method getFilteredTUPureText.

/**
	 * 获取trans-unit节点过滤后的值,过滤条件为不包括上下文匹配,不包括完全匹配,不包括已锁文本,过滤条件在首选项中设置
	 * @return	Map<String, String >	两个键值对,srcPureText --> 源文本的纯文本,tarPureText --> 目标文本的纯文本
	 * 如果返回的是null,则标志source节点无内容,这对与行号就不自增
	 */
public Map<String, String> getFilteredTUPureText(String xlfPath, String nodeXpath, Map<String, Boolean> filterMap) {
    Map<String, String> filteredTuPureTextMap = new HashMap<String, String>();
    VTDNav vn = vnMap.get(xlfPath);
    AutoPilot ap = new AutoPilot(vn);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
    AutoPilot childAp = new AutoPilot(vn);
    try {
        VTDUtils vUtils = new VTDUtils(vn);
        ap.selectXPath(nodeXpath);
        while (ap.evalXPath() != -1) {
            vn.push();
            //取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回,没有source节点,也返回null
            childAp.selectXPath("./source");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个source,因此此处用if不用while
                String srcContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回null
                if (srcContent == null || "".equals(srcContent)) {
                    return null;
                } else {
                    filteredTuPureTextMap.put("srcPureText", getTUPureText(vn));
                }
            } else {
                return null;
            }
            childAp.resetXPath();
            vn.pop();
            //首先过滤,如果有不应包括的文本段,则返回空
            if (!filterTheTU(vn, filterMap)) {
                return filteredTuPureTextMap;
            }
            //下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
            childAp.selectXPath("./target");
            if (childAp.evalXPath() != -1) {
                //因为标准里面只有一个target,因此此处用if不用while
                String tarContent = vUtils.getElementContent();
                //如果源文本为空或无值,则返回null
                if (tarContent == null || "".equals(tarContent)) {
                    return filteredTuPureTextMap;
                } else {
                    filteredTuPureTextMap.put("tarPureText", getTUPureText(vn));
                }
            } else {
                return filteredTuPureTextMap;
            }
            childAp.resetXPath();
        }
    } catch (Exception e) {
        logger.error(Messages.getString("qa.QAXmlHandler.logger12"), e);
    }
    return filteredTuPureTextMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) NavException(com.ximpleware.NavException) CoreException(org.eclipse.core.runtime.CoreException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException)

Aggregations

VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)137 AutoPilot (com.ximpleware.AutoPilot)112 NavException (com.ximpleware.NavException)103 VTDNav (com.ximpleware.VTDNav)99 XPathParseException (com.ximpleware.XPathParseException)83 XPathEvalException (com.ximpleware.XPathEvalException)81 IOException (java.io.IOException)64 ModifyException (com.ximpleware.ModifyException)62 TranscodeException (com.ximpleware.TranscodeException)49 CoreException (org.eclipse.core.runtime.CoreException)45 XMLModifier (com.ximpleware.XMLModifier)41 VTDGen (com.ximpleware.VTDGen)33 FileNotFoundException (java.io.FileNotFoundException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)29 HashMap (java.util.HashMap)27 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)27 LinkedHashMap (java.util.LinkedHashMap)25 ArrayList (java.util.ArrayList)23 XQException (javax.xml.xquery.XQException)20 LinkedList (java.util.LinkedList)14