use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getEditProgressData.
/**
* 针对文件分析的翻译进度分析,获取每个节点的未翻译文本段数、字数,已翻译文本段数、字数。
* 如果返回为null,则标志用户退出分析
* @param filePath 要处理的文件的路径
* @param monitor 进度条
* @param ignoreTag 是否忽略标记
* @param workInterval 进度条前进一格的间隔
* @param traversalTuIndex 循环trans-unit节点的序列号
* @return editProgDataMap,下面是其健值对详解
* key: notApprovedParas --> 未批准文本段数
* key: approvedParas --> 已批准文本段数
* key: notApprovedWords --> 未批准字数
* key: approvedWords --> 已批准字数
*/
public Map<String, Integer> getEditProgressData(String filePath, IProgressMonitor monitor, int workInterval, int traversalTuIndex) {
Map<String, Integer> editProgDataMap = new HashMap<String, Integer>();
int notApprovedParas = 0;
int approvedParas = 0;
int lockedParas = 0;
int notApprovedWords = 0;
int approvedWords = 0;
int lockedWords = 0;
try {
VTDNav vn = vnMap.get(filePath);
Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + filePath);
AutoPilot ap = new AutoPilot(vn);
VTDUtils vUtils = new VTDUtils(vn);
AutoPilot sourceAp = new AutoPilot(vn);
sourceAp.selectXPath("./source[text()!='' or ./*]");
String srcLang = vUtils.getElementAttribute("/xliff/file[1]", "source-language");
ap.selectXPath(XPATH_ALL_TU);
while (ap.evalXPath() != -1) {
traversalTuIndex++;
int curWordsNum = 0;
//判断是否锁定
boolean isLocked = false;
int inx = vn.getAttrVal("translate");
if (inx != -1 && "no".equals(vn.toString(inx))) {
isLocked = true;
}
//根据是否忽略标记,获取源文本的字数
vn.push();
if (sourceAp.evalXPath() != -1) {
String sourceText = getTUPureText(vn);
curWordsNum = CountWord.wordCount(sourceText, srcLang);
}
sourceAp.resetXPath();
vn.pop();
String approved = "";
int approveIndex = vn.getAttrVal("approved");
if (approveIndex != -1) {
approved = vn.toString(approveIndex);
}
if ("yes".equals(approved)) {
approvedParas++;
approvedWords += curWordsNum;
} else {
notApprovedParas++;
notApprovedWords += curWordsNum;
}
if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
return null;
}
if (isLocked) {
lockedParas++;
lockedWords += curWordsNum;
}
}
} catch (Exception e) {
if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
return null;
}
e.printStackTrace();
logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger24"), filePath), e);
}
editProgDataMap.put("notApprovedParas", notApprovedParas);
editProgDataMap.put("approvedParas", approvedParas);
editProgDataMap.put("lockedParas", lockedParas);
editProgDataMap.put("notApprovedWords", notApprovedWords);
editProgDataMap.put("approvedWords", approvedWords);
editProgDataMap.put("lockedWords", lockedWords);
return editProgDataMap;
}
use of net.heartsome.xml.vtdimpl.VTDUtils 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 net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method addNewInfoToSplitXlf.
/**
* 向新切割的xliff文件添加分割信息
* @param newXlfPath
* @param infoMap
* robert 2011-10-23
*/
public void addNewInfoToSplitXlf(String newXlfPath, Map<String, String> infoMap) {
// --robert split
VTDNav vn = vnMap.get(newXlfPath);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + newXlfPath);
try {
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
ap.selectXPath("/xliff/file/header/hs:splitInfos");
boolean hasSplitInfos = false;
XMLModifier xm = new XMLModifier(vn);
VTDUtils vUtils = new VTDUtils(vn);
String splitInfo = "\n <hs:splitInfo count=\"" + infoMap.get("count") + "\" " + " depth=\"" + infoMap.get("depth") + "\" id=\"" + infoMap.get("id") + "\" " + "index=\"" + infoMap.get("index") + "\" name=\"" + infoMap.get("name") + "\" splitTime=\"" + infoMap.get("splitTime") + "\"/>";
while (ap.evalXPath() != -1) {
xm.insertBeforeTail(splitInfo + "\n");
hasSplitInfos = true;
}
if (hasSplitInfos) {
vnMap.put(newXlfPath, vUtils.updateVTDNav(xm, newXlfPath));
}
// 如果是第一次分割,那么添加相关信息
if (!hasSplitInfos) {
String firstSplitInfo = "\n <hs:splitInfos original=\"" + infoMap.get("original") + "\">" + splitInfo + "</hs:splitInfos>";
AutoPilot ap1 = new AutoPilot(vn);
ap1.selectXPath("/xliff/file/header");
while (ap1.evalXPath() != -1) {
xm.insertBeforeTail(firstSplitInfo + "\n");
}
vnMap.put(newXlfPath, vUtils.updateVTDNav(xm, newXlfPath));
}
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
/*
* VTDNav vn = vnMap.get(newXlfPath); Assert.isNotNull(vn, "未在已解析的缓存中找到该文件:" + newXlfPath); try { AutoPilot ap =
* new AutoPilot(vn); ap.selectXPath("/xliff/file/header/splitInfos"); boolean hasSplitInfos = false;
* XMLModifier xm = new XMLModifier(vn); VTDUtils vUtils = new VTDUtils(vn); String splitInfo =
* "\n <splitInfo count=\"" + infoMap.get("count") + "\" " + " depth=\"" + infoMap.get("depth") + "\" id=\""
* + infoMap.get("id") + "\" " + "index=\"" + infoMap.get("index") + "\" name=\"" + infoMap.get("name") +
* "\" splitTime=\"" + infoMap.get("splitTime") + "\"/>";
*
* while (ap.evalXPath() != -1) { xm.insertBeforeTail(splitInfo + "\n"); hasSplitInfos = true; } if
* (hasSplitInfos) { vnMap.put(newXlfPath, vUtils.updateVTDNav(xm, newXlfPath)); }
*
* // 如果是第一次分割,那么添加相关信息 if (!hasSplitInfos) { String firstSplitInfo = "\n <splitInfos>" + splitInfo +
* "</splitInfos>";
*
* AutoPilot ap1 = new AutoPilot(vn); ap1.selectXPath("/xliff/file/header"); while (ap1.evalXPath() != -1) {
* xm.insertBeforeTail(firstSplitInfo + "\n"); } vnMap.put(newXlfPath, vUtils.updateVTDNav(xm, newXlfPath)); } }
* catch (Exception e) { e.printStackTrace(); }
*/
}
use of net.heartsome.xml.vtdimpl.VTDUtils 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 net.heartsome.xml.vtdimpl.VTDUtils 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;
}
Aggregations