Search in sources :

Example 31 with XMLModifier

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

the class XLFHandler method addNote.

/**
	 * 添加批注
	 * @param rowId
	 *            行的唯一标识
	 * @param note
	 *            批注内容;
	 */
public void addNote(Map<String, List<String>> mapRowIdByFileName, String note, String from, boolean isApplyCurrent) {
    try {
        StringBuffer insertValue = new StringBuffer("<note");
        if (from != null) {
            insertValue.append(" from='").append(from).append("'");
        }
        if (!isApplyCurrent) {
            insertValue.append(" hs:apply-current='No'");
        }
        insertValue.append(">").append(StringUtilsBasic.checkNullStr(note)).append("</note>");
        VTDUtils vu = new VTDUtils();
        XMLModifier xm = new XMLModifier();
        Iterator<Entry<String, List<String>>> it = mapRowIdByFileName.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, List<String>> entry = it.next();
            String fileName = entry.getKey();
            VTDNav vn = vnMap.get(fileName);
            vu.bind(vn);
            xm.bind(vn);
            for (String rowId : entry.getValue()) {
                StringBuffer xpath = new StringBuffer(RowIdUtil.parseRowIdToXPath(rowId));
                xm = vu.insert(null, xm, xpath.append("/text()").toString(), insertValue.toString());
            }
            saveAndReparse(xm, fileName);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) Entry(java.util.Map.Entry) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) VTDNav(com.ximpleware.VTDNav)

Example 32 with XMLModifier

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

Example 33 with XMLModifier

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

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

the class QAXmlHandler method deleteAllNode.

/**
	 * 根据指定的 xpaht 删除所有的节点
	 * @param filePath
	 * @param nodeXpath
	 * @return
	 */
public boolean deleteAllNode(String filePath, String nodeXpath) {
    VTDNav vn = vnMap.get(filePath);
    Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + filePath);
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(nodeXpath);
        XMLModifier xm = new XMLModifier(vn);
        boolean hasRemoved = false;
        while (ap.evalXPath() != -1) {
            xm.remove();
            hasRemoved = true;
        }
        if (hasRemoved) {
            return saveAndReparse(xm, filePath);
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("", e);
    }
    return false;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) 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 35 with XMLModifier

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

the class QAXmlHandler method addAspellConfig.

/**
	 * 将语言与词典对添加到 Aspell 配置文件中
	 * @throws Exception
	 */
public void addAspellConfig(String xmlPath, String lang, String dic, boolean isUpdate) throws Exception {
    VTDNav vn = vnMap.get(xmlPath);
    XMLModifier xm = null;
    VTDUtils vu = new VTDUtils(vn);
    if (isUpdate) {
        String xpath = "/aspell/aspellDictionaries/" + lang + "/text()";
        xm = vu.update(xpath, dic);
    } else {
        String xpath = "/aspell/aspellDictionaries/text()";
        String insertValue = "\n<" + lang + ">" + dic + "</" + lang + ">\n";
        xm = vu.insert(xpath, insertValue);
    }
    vu.bind(xm.outputAndReparse());
    saveAndReparse(xm, xmlPath);
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) VTDNav(com.ximpleware.VTDNav)

Aggregations

XMLModifier (com.ximpleware.XMLModifier)80 NavException (com.ximpleware.NavException)54 VTDNav (com.ximpleware.VTDNav)53 AutoPilot (com.ximpleware.AutoPilot)50 ModifyException (com.ximpleware.ModifyException)50 XPathParseException (com.ximpleware.XPathParseException)42 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)41 XPathEvalException (com.ximpleware.XPathEvalException)40 IOException (java.io.IOException)38 TranscodeException (com.ximpleware.TranscodeException)34 VTDGen (com.ximpleware.VTDGen)23 UnsupportedEncodingException (java.io.UnsupportedEncodingException)22 CoreException (org.eclipse.core.runtime.CoreException)17 FileNotFoundException (java.io.FileNotFoundException)15 FileOutputStream (java.io.FileOutputStream)15 ParseException (com.ximpleware.ParseException)13 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)13 ArrayList (java.util.ArrayList)12 File (java.io.File)11 XQException (javax.xml.xquery.XQException)11