Search in sources :

Example 6 with ModifyException

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

the class XLFHandler method resetTuNodes.

/**
	 * 重置 TU 子节点的 XML 内容。
	 * @param subNodesOfTU
	 *            TU 子节点的 XML 内容
	 */
public void resetTuNodes(Map<String, String> subNodesOfTU) {
    if (subNodesOfTU == null || subNodesOfTU.size() == 0) {
        return;
    }
    List<String> rowIdList = new ArrayList<String>(subNodesOfTU.keySet());
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIdList);
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    VTDUtils vu = new VTDUtils();
    for (Entry<String, List<String>> entry : map.entrySet()) {
        String fileName = entry.getKey();
        List<String> rowIds = entry.getValue();
        VTDNav vn = vnMap.get(fileName);
        try {
            ap.bind(vn);
            xm.bind(vn);
            vu.bind(vn);
            for (String rowId : rowIds) {
                String tgtNode = subNodesOfTU.get(rowId);
                String xpath = RowIdUtil.parseRowIdToXPath(rowId);
                xm = vu.update(ap, xm, xpath, tgtNode);
            }
            // 保存并更新 VTDNav
            saveAndReparse(xm, fileName);
        } catch (ModifyException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) 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 7 with ModifyException

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

the class XLFHandler method getMatchOfSegments.

/**
	 * 获取翻译匹配率达到 <code>minMatchQuality</code> 的匹配
	 * @param minMatchQuality
	 *            最低匹配率
	 */
public Map<String, String> getMatchOfSegments(final int minMatchQuality) {
    final HashMap<String, String> map = new HashMap<String, String>();
    handleAllSegment(new PerFileHandler() {

        public void handle(String fileName, VTDUtils vu, AutoPilot ap, XMLModifier xm) throws ModifyException, XPathParseException, XPathEvalException, NavException, UnsupportedEncodingException {
            ap.selectXPath("/xliff/file/body//trans-unit[translate(alt-trans/@match-quality, '%', '')>=" + minMatchQuality + "]");
            AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
            while (ap.evalXPath() != -1) {
                String rowId = RowIdUtil.getRowId(vu.getVTDNav(), fileName);
                if (isApproved(rowId) || isLocked(rowId)) {
                    // 已经批准或者锁定的,跳过。
                    continue;
                }
                String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
                if (vu.pilot(tempAp, tuXPath) != -1) {
                    String tgt = vu.getValue(tempAp, "./alt-trans[translate(@match-quality, '%', '')>=" + minMatchQuality + "]/target/text()");
                    if (tgt != null) {
                        // 当前 Target 的值
                        String currentTgt = vu.getValue(tempAp, "./target/text()");
                        if (!tgt.equals(currentTgt)) {
                            map.put(rowId, tgt);
                        }
                    }
                }
            }
            saveAndReparse(xm, fileName);
        }
    });
    return map;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with ModifyException

use of com.ximpleware.ModifyException 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 9 with ModifyException

use of com.ximpleware.ModifyException 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 10 with ModifyException

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

the class XLFHandler method updateLanguages.

/**
	 * 设置指定 XLIFF 文件中指定 file 节点的语言代码信息(source-language、target-language 属性值)
	 * @param fileName
	 *            指定文件名
	 * @param XliffBeans
	 *            XliffBean 集合;
	 */
public void updateLanguages(String fileName, List<XliffBean> xliffBeans) {
    if (xliffBeans == null || xliffBeans.isEmpty()) {
        return;
    }
    VTDNav vn = vnMap.get(fileName);
    AutoPilot ap = new AutoPilot(vn);
    AutoPilot subAp = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = new XMLModifier(vn);
        for (XliffBean bean : xliffBeans) {
            Set<String> originals = bean.getOriginals();
            for (String original : originals) {
                int index = vu.pilot(subAp, "/xliff/file[@original='" + original + "']");
                if (index != -1) {
                    xm = vu.update(ap, xm, "./@source-language", bean.getSourceLanguage(), VTDUtils.CREATE_IF_NOT_EXIST);
                    xm = vu.update(ap, xm, "./@target-language", bean.getTargetLanguage(), VTDUtils.CREATE_IF_NOT_EXIST);
                }
            }
        }
        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) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) VTDNav(com.ximpleware.VTDNav)

Aggregations

ModifyException (com.ximpleware.ModifyException)35 NavException (com.ximpleware.NavException)27 XMLModifier (com.ximpleware.XMLModifier)25 AutoPilot (com.ximpleware.AutoPilot)20 XPathParseException (com.ximpleware.XPathParseException)20 XPathEvalException (com.ximpleware.XPathEvalException)18 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)18 TranscodeException (com.ximpleware.TranscodeException)17 IOException (java.io.IOException)17 VTDNav (com.ximpleware.VTDNav)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 FileOutputStream (java.io.FileOutputStream)12 BufferedOutputStream (java.io.BufferedOutputStream)10 ArrayList (java.util.ArrayList)7 ParseException (com.ximpleware.ParseException)6 VTDGen (com.ximpleware.VTDGen)6 List (java.util.List)6 LinkedList (java.util.LinkedList)5 FileNotFoundException (java.io.FileNotFoundException)4 HashMap (java.util.HashMap)4