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();
}
}
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();
}
}
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();
}
}
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;
}
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;
}
Aggregations