use of com.ximpleware.XPathParseException 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.XPathParseException 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.XPathParseException 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;
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class XLFHandler method getCustomerInfo.
/**
* 得到用户定制的信息(customer prop-group 的信息)
* @param fileName
* @return 以{ {key1, value1}, {key2, value2} }二维数组的形式返回;
*/
public String[][] getCustomerInfo(String fileName) {
ArrayList<String[]> customerInfo = new ArrayList<String[]>();
VTDNav vn = vnMap.get(fileName);
AutoPilot ap = new AutoPilot(vn);
try {
VTDUtils vu = new VTDUtils(vn);
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
ap.selectXPath("/xliff/file/header/hs:prop-group[@name='customer']/hs:prop");
while (ap.evalXPath() != -1) {
String key = vu.getCurrentElementAttribut("prop-type", "");
String value = vn.toString(vn.getText());
String[] entry = { key, value };
customerInfo.add(entry);
}
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return customerInfo.toArray(new String[][] {});
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class XLFHandler method changeSomeTranslateProp.
/**
* 若当前目标文本段(target)内容不为空,则自动将其 state 属性值设为“translated”
* @param rowIdList
* 要修改的翻译单元Id的集合;
*/
public void changeSomeTranslateProp(List<String> rowIdList) {
if (rowIdList == null) {
return;
}
Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(rowIdList);
VTDNav vn = null;
AutoPilot ap = new AutoPilot();
for (Entry<String, List<String>> entry : map.entrySet()) {
String fileName = entry.getKey();
List<String> rowIds = entry.getValue();
vn = vnMap.get(fileName);
ap.bind(vn);
boolean isNew;
for (int i = 0; i < rowIds.size(); i++) {
ap.resetXPath();
String rowId = rowIds.get(i);
String tuXPath = RowIdUtil.parseRowIdToXPathWithCondition(rowId, "(not(source='')) and (not(target=''))");
try {
isNew = false;
ap.selectXPath(tuXPath + "/target/@state");
int stateIdx = -1;
if ((stateIdx = ap.evalXPath()) != -1) {
String state = vn.toNormalizedString(stateIdx + 1);
if ("new".equalsIgnoreCase(state)) {
isNew = true;
}
}
if (!isNew && rowIdList.size() > i) {
// state不为“new”
rowIdList.remove(i);
}
// else { // state为“new”,则继续找下一个
//
// }
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
}
if (rowIdList.size() > 0) {
changeTgtPropValue(rowIdList, "state", "translated", "new");
}
}
Aggregations