Search in sources :

Example 76 with NavException

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

the class XLFHandler method handleSomeSegment.

/**
	 * 处理多个文本段
	 * @param rowIds
	 *            行的唯一标识
	 * @param handler
	 *            单个文本段的处理实现 ;
	 */
private Map<String, Object> handleSomeSegment(List<String> rowIds, PerSegmentHandler handler) {
    if (rowIds == null || rowIds.isEmpty()) {
        return getSuccessResult();
    }
    VTDNav vn = null;
    VTDUtils vu = new VTDUtils();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIds);
    String errorMsg = null;
    Throwable error = null;
    for (Entry<String, List<String>> entry : map.entrySet()) {
        String fileName = entry.getKey();
        List<String> rowIdList = entry.getValue();
        vn = vnMap.get(fileName);
        vn.push();
        try {
            vu.bind(vn);
            ap.bind(vn);
            xm.bind(vn);
            for (String rowId : rowIdList) {
                handler.handle(rowId, vu, ap, xm);
            }
            // 保存并更新VTDNav对象
            saveAndReparse(xm, fileName);
        // return getSuccessResult(); //robert注释
        } catch (XPathParseException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger11");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (XPathEvalException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger12");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (NavException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger12");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (ModifyException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger13");
            error = e;
            LOGGER.error(errorMsg, e);
        } catch (UnsupportedEncodingException e) {
            errorMsg = Messages.getString("file.XLFHandler.logger14");
            error = e;
            LOGGER.error(errorMsg, e);
        } finally {
            vn.pop();
        }
    }
    vu = null;
    ap = null;
    xm = null;
    if (errorMsg != null) {
        return getErrorResult(Messages.getString("file.XLFHandler.msg3"), error);
    } else {
        return getSuccessResult();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ModifyException(com.ximpleware.ModifyException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) VTDNav(com.ximpleware.VTDNav)

Example 77 with NavException

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

the class XLPHandler method saveXliffInfo.

/**
	 * 保存 XLIFF 信息
	 * @param xliffInfo
	 *            XLIFF 信息 ;
	 */
public void saveXliffInfo(List<? extends Map<String, String>> xliffInfo) {
    try {
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        StringBuffer items = new StringBuffer();
        String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" />";
        ap.selectXPath("/xlfedit-project");
        if (ap.evalXPath() != -1) {
            for (Map<String, String> map : xliffInfo) {
                vn.push();
                String xliff = map.get(ATTR_XLIFF);
                String tgtEnc = map.get(ATTR_TGT_ENC);
                String xpath = "./item[@" + ATTR_XLIFF + "='" + xliff + "']";
                ap.selectXPath(xpath);
                if (ap.evalXPath() != -1) {
                    updateAttribute(xm, ATTR_TGT_ENC, tgtEnc);
                } else {
                    String item = MessageFormat.format(pattern, ATTR_XLIFF, xliff, ATTR_TGT_ENC, tgtEnc);
                    items.append(item).append(LINE_SEPARATOR);
                }
                vn.pop();
            }
            if (items.length() > 1) {
                xm.insertBeforeTail(items.toString());
            }
            save(xm);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException)

Example 78 with NavException

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

the class XLPHandler method saveSourceInfo.

/**
	 * 保存 XLIFF 信息
	 * @param sourceInfo
	 *            源文件信息 ;
	 */
public void saveSourceInfo(List<? extends Map<String, String>> sourceInfo) {
    try {
        XMLModifier xm = new XMLModifier(vn);
        AutoPilot ap = new AutoPilot(vn);
        StringBuffer items = new StringBuffer();
        String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" {6}=\"{7}\" />";
        ap.selectXPath("/xlfedit-project");
        if (ap.evalXPath() != -1) {
            for (Map<String, String> map : sourceInfo) {
                vn.push();
                String source = map.get(ATTR_SOURCE);
                String format = map.get(ATTR_FORMAT);
                String srcLang = map.get(ATTR_SRC_LANG);
                String srcEnc = map.get(ATTR_SRC_ENC);
                String xpath = "./item[@" + ATTR_SOURCE + "=\"" + source + "\"]";
                ap.selectXPath(xpath);
                if (ap.evalXPath() != -1) {
                    updateAttribute(xm, ATTR_FORMAT, format);
                    updateAttribute(xm, ATTR_SRC_LANG, srcLang);
                    updateAttribute(xm, ATTR_SRC_ENC, srcEnc);
                } else {
                    String item = MessageFormat.format(pattern, ATTR_SOURCE, source, ATTR_FORMAT, format, ATTR_SRC_LANG, srcLang, ATTR_SRC_ENC, srcEnc);
                    ap.selectXPath(xpath);
                    items.append(item).append(LINE_SEPARATOR);
                }
                vn.pop();
            }
            if (items.length() > 1) {
                xm.insertBeforeTail(items.toString());
            }
            save(xm);
        }
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException)

Example 79 with NavException

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

the class XLFHandler 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);
        String tuXpath = RowIdUtil.parseRowIdToXPath(rowId);
        vu.pilot(tuXpath);
    } catch (NavException e1) {
        String errorMsg = Messages.getString("file.XLFHandler.logger4");
        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)

Example 80 with NavException

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

the class XLFHandler method handleAllSegment.

/**
	 * 处理所有的文本段
	 * @param handler
	 *            单个文件的处理实现 ;
	 */
private void handleAllSegment(PerFileHandler handler) {
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    XMLModifier xm = new XMLModifier();
    VTDUtils vu = new VTDUtils();
    VTDNav vn;
    for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
        String fileName = entry.getKey();
        vn = entry.getValue();
        vn.push();
        try {
            ap.bind(vn);
            xm.bind(vn);
            vu.bind(vn);
            // 针对每个文件的VTDNav对象进行操作
            handler.handle(fileName, vu, ap, xm);
        } catch (ModifyException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } 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 (UnsupportedEncodingException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } finally {
            vn.pop();
        }
    }
    ap = null;
    xm = null;
    vu = null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) XMLModifier(com.ximpleware.XMLModifier) 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) 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