use of com.ximpleware.NavException 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();
}
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class XLFHandler method deleteAndSave.
/**
* 删除翻译单元内容并保存
* @param rowId
* @param subXPath
* @param newValue
* ;
*/
private void deleteAndSave(String rowId, String subXPath) {
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
String fileName = RowIdUtil.getFileNameByRowId(rowId);
VTDNav vn = vnMap.get(fileName);
try {
VTDUtils vu = new VTDUtils(vn);
XMLModifier xm = vu.delete(tuXPath + subXPath);
saveAndReparse(xm, fileName);
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class XLFHandler method getTuProp.
/**
* 得到翻译单元的属性值
* @param rowId
* 行的唯一标识
* @param propName
* 属性名
* @return 属性值;
*/
public String getTuProp(String rowId, String propName) {
VTDNav vn = getVTDNavByRowId(rowId);
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
try {
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vu.getVTDNav());
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
return vu.getValue(ap, tuXPath + "/@" + propName);
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return null;
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class XLFHandler method getSrcContent.
/**
* 得到源的完整内容
* @param rowId
* 行的唯一标识
* @return 源的完整内容;
*/
public String getSrcContent(String rowId) {
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
VTDNav vn = getVTDNavByRowId(rowId);
try {
VTDUtils vu = new VTDUtils(vn);
return vu.getValue(tuXPath + "/source/text()");
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return null;
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class XLFHandler method mergeSegment.
/**
* 合并两个文本段(注意:rowId1 和 rowId2 有可能不相邻,中间可能会间隔几个源文为空的文本段)
* @param rowId1
* 第一个文本段的 rowId
* @param rowId2
* 第二个文本段的 rowId;
*/
public String mergeSegment(String rowId1, String rowId2) {
// if (rowIndex + 1 >= rowIds.size()) {
// return "";
// }
// String rowId1 = getRowId(rowIndex);
// String rowId2 = getRowId(rowIndex + 1);
String fileName1 = RowIdUtil.getFileNameByRowId(rowId1);
String fileName2 = RowIdUtil.getFileNameByRowId(rowId2);
if (fileName1 == null || fileName2 == null || !fileName1.equals(fileName2)) {
return "";
}
VTDNav vn = vnMap.get(fileName1);
// TransUnitBean tuTop = getTransUnit(rowIndex);
// TransUnitBean tuBottom = getTransUnit(rowIndex + 1);
// String srcContent = tuTop.getSrcContent() + "<ph id=\"hs-merge" +
// tuTop.getId() + "~" + tuBottom.getId()
// + "\"/>" + tuBottom.getSrcContent();
// tuTop.setSrcContent(srcContent);
// String tgtContent = tuTop.getTgtContent() + "<ph id=\"hs-merge" +
// tuTop.getId() + "~" + tuBottom.getId()
// + "\"/>" + tuBottom.getTgtContent();
// tuTop.setTgtContent(tgtContent);
// tuTop.setMatches(null);
//
// String tuXPath1 = RowIdUtil.parseRowIdToXPath(rowId1);
// String tuXPath2 = RowIdUtil.parseRowIdToXPath(rowId2);
// AutoPilot ap = new AutoPilot(vn);
//
// XMLModifier xm = new XMLModifier(vn);
// delete(ap, xm, fileName2, tuXPath2, true);
// update(ap, xm, fileName1, tuXPath2, tuTop.toXMLString(), true);
// ap.selectXPath(tuXPath2);
//
// saveAndReparse(xm, fileName1);
String tuXPath1 = RowIdUtil.parseRowIdToXPath(rowId1);
String tuXPath2 = RowIdUtil.parseRowIdToXPath(rowId2);
String tuid1 = RowIdUtil.getTUIdByRowId(rowId1);
String tuid2 = RowIdUtil.getTUIdByRowId(rowId2);
// 不需加数据库
boolean isNotSendToTM = false;
// 是否是疑问行
boolean isNeeds_review = false;
// 是否添加属性“不需添加到数据库”
boolean isAddNotSendToTm = false;
// 是否添加属性“疑问行”
boolean isAddNeeds_review = false;
StringBuffer nodeSB = new StringBuffer();
AutoPilot ap = new AutoPilot(vn);
try {
VTDUtils vu = new VTDUtils(vn);
String oldElementFragment = "";
ap.selectXPath(tuXPath1);
String xmlSpace = null;
if (ap.evalXPath() != -1) {
xmlSpace = vu.getCurrentElementAttribut("xml:space", "preserve");
oldElementFragment += vu.getElementFragment();
// 获取当前tu节点的属性 hs:send-to-tm="no" hs:needs-review="yes"
isNotSendToTM = "no".equals(vu.getCurrentElementAttribut("hs:send-to-tm", ""));
isNeeds_review = "yes".equals(vu.getCurrentElementAttribut("hs:needs-review", ""));
}
ap.selectXPath(tuXPath2);
if (ap.evalXPath() != -1) {
oldElementFragment += vu.getElementFragment();
if (!isNotSendToTM) {
isAddNotSendToTm = "no".equals(vu.getCurrentElementAttribut("hs:send-to-tm", ""));
}
if (!isNeeds_review) {
isAddNeeds_review = "yes".equals(vu.getCurrentElementAttribut("hs:needs-review", ""));
}
// 开始获取批注
ap.selectXPath("./note");
while (ap.evalXPath() != -1) {
nodeSB.append(vu.getElementFragment());
}
}
XMLModifier xm = new XMLModifier(vn);
String sourceContent2 = "";
String targetContent2 = "";
ap.selectXPath(tuXPath2 + "/source");
String srcLang = null;
if (ap.evalXPath() != -1) {
srcLang = vu.getCurrentElementAttribut("xml:lang", null);
sourceContent2 = vu.getElementContent();
sourceContent2 = sourceContent2 == null ? "" : sourceContent2;
}
ap.selectXPath(tuXPath2 + "/target");
if (ap.evalXPath() != -1) {
targetContent2 = vu.getElementContent();
targetContent2 = targetContent2 == null ? "" : targetContent2;
}
String curTime = "" + System.nanoTime();
ap.selectXPath(tuXPath1 + "/source");
if (ap.evalXPath() != -1) {
String sourceContent1 = vu.getElementContent();
sourceContent1 = sourceContent1 == null ? "" : sourceContent1;
String newValue = sourceContent1 + "<ph id=\"hs-merge" + tuid1 + "~" + tuid2 + "\" splitMergeIndex=\"" + curTime + "\"/>" + sourceContent2;
vu.update(ap, xm, tuXPath1 + "/source/text()", newValue);
}
ap.selectXPath(tuXPath1 + "/target");
if (ap.evalXPath() != -1) {
String targetContent1 = vu.getElementContent();
targetContent1 = targetContent1 == null ? "" : targetContent1;
String newValue = targetContent1 + "<ph id=\"hs-merge" + tuid1 + "~" + tuid2 + "\" splitMergeIndex=\"" + curTime + "\"/>" + targetContent2;
vu.update(ap, xm, tuXPath1 + "/target/text()", newValue);
} else {
String newValue = "<ph id=\"hs-merge" + tuid1 + "~" + tuid2 + "\" splitMergeIndex=\"" + curTime + "\"/>" + targetContent2;
vu.insert(ap, xm, tuXPath1 + "/target/text()", newValue);
}
// 移除alt-trans节点
ap.selectXPath(tuXPath1 + "/alt-trans");
while (ap.evalXPath() != -1) {
xm.remove();
}
ap.selectXPath(tuXPath2);
if (ap.evalXPath() != -1) {
// xm.remove();
// Bug #1054:合并文本段时不应丢失第二个文本段的 trans-unit 节点
String transUnit = "<trans-unit id=\"" + tuid2 + "\" xml:space=\"" + xmlSpace + "\"><target/>" + "<source xml:lang=\"" + srcLang + "\"/></trans-unit>";
vu.update(ap, xm, tuXPath2, transUnit);
}
// 整合并集中的属性以及批注
ap.selectXPath(tuXPath1);
if (ap.evalXPath() != -1) {
// 注意添加属性时必须保持应有的空格
String insertAttri = isAddNotSendToTm ? " hs:send-to-tm=\"no\" " : "";
insertAttri += isAddNeeds_review ? " hs:needs-review=\"yes\" " : "";
if (insertAttri.length() > 0) {
xm.insertAttribute(insertAttri);
}
// 开始添加批注
if (nodeSB.length() > 0) {
xm.insertBeforeTail(nodeSB.toString().getBytes("UTF-8"));
}
vn.push();
// 开始处理合并后的状态,保持草稿状态
ap.selectXPath("./target");
if (ap.evalXPath() != -1) {
int index = vn.getAttrVal("state");
if (index != -1) {
xm.updateToken(index, "new");
} else {
xm.insertAttribute(" state=\"new\" ");
}
}
vn.pop();
// 删除批准状态
int index = vn.getAttrVal("approved");
if (index != -1) {
xm.removeAttribute(index - 1);
}
}
saveAndReparse(xm, fileName1);
// 删除下面的行
rowIds.remove(rowId2);
tuSizeMap.put(fileName1, tuSizeMap.get(fileName1) - 1);
return oldElementFragment;
} 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 (UnsupportedEncodingException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return "";
}
Aggregations