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