use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getFilteredTUTags.
/**
* 备注:--nonUse
* 获取经过过滤后的trans-unit节点的标记,过滤条件为,过滤掉不包括的文本段,如不包括上下文匹配,不包括完全匹配,不包括已锁文本,这些条件在首选项中设置。
* 若返回的为null, 则标志没有source节点或者source节点值为空
* @return filteredTuTagMap 两个键值对,srcTag --> 源文本的标签,tarTag --> 目标文本的标签
*/
public Map<String, List<Map<String, String>>> getFilteredTUTags(String xlfPath, String nodeXpath, Map<String, Boolean> filterMap) {
Map<String, List<Map<String, String>>> filteredTuTagMap = new HashMap<String, List<Map<String, String>>>();
VTDNav vn = vnMap.get(xlfPath);
AutoPilot ap = new AutoPilot(vn);
Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
AutoPilot childAp = new AutoPilot(vn);
try {
VTDUtils vUtils = new VTDUtils(vn);
ap.selectXPath(nodeXpath);
while (ap.evalXPath() != -1) {
vn.push();
//取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回,没有source节点,也返回null
childAp.selectXPath("./source");
if (childAp.evalXPath() != -1) {
//因为标准里面只有一个source,因此此处用if不用while
String srcContent = vUtils.getElementContent();
//如果源文本为空或无值,则返回null
if (srcContent == null || "".equals(srcContent)) {
return null;
} else {
filteredTuTagMap.put("srcTag", getTUTag(vn));
}
} else {
return null;
}
childAp.resetXPath();
vn.pop();
//首先过滤,如果有不应包括的文本段,则返回空
if (!filterTheTU(vn, filterMap)) {
return filteredTuTagMap;
}
//下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
childAp.selectXPath("./target");
if (childAp.evalXPath() != -1) {
//因为标准里面只有一个target,因此此处用if不用while
String tarContent = vUtils.getElementContent();
//如果源文本为空或无值,则返回null
if (tarContent == null || "".equals(tarContent)) {
return filteredTuTagMap;
} else {
filteredTuTagMap.put("tarTag", getTUTag(vn));
}
} else {
return filteredTuTagMap;
}
childAp.resetXPath();
}
} catch (Exception e) {
logger.error(Messages.getString("qa.QAXmlHandler.logger18"), e);
}
return filteredTuTagMap;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getTransProgressData.
/**
* 针对文件分析的翻译进度分析,获取每个节点的未翻译文本段数、字数,已翻译文本段数、字数。
* 如果返回为null,则标志用户退出分析
* 备注:此处的算法与逻辑,要与XLIFFEditorImplWithNatTable中的 updateStatusLine方法保持一致
* @param filePath
* 要处理的文件的路径
* @param monitor
* 进度条
* @param ignoreTag
* 是否忽略标记
* @param workInterval
* 进度条前进一格的间隔
* @param traversalTuIndex
* 循环trans-unit节点的序列号
* @return transProgDataMap,下面是其健值对详解 key: notTransPara --> 未翻译文本段数 key: translatedPara --> 已翻译文本段数 key:
* notTransWords --> 未翻译字数 key: translatedWords --> 已翻译字数
*/
public Map<String, Integer> getTransProgressData(String filePath, IProgressMonitor monitor, int workInterval, int traversalTuIndex) {
Map<String, Integer> transProgDataMap = new HashMap<String, Integer>();
int notTransPara = 0;
int translatedPara = 0;
int lockedPara = 0;
int notTransWords = 0;
int translatedWords = 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 ./*]");
AutoPilot targetAp = new AutoPilot(vn);
targetAp.selectXPath("./target");
String srcLang = vUtils.getElementAttribute("/xliff/file[1]", "source-language");
ap.selectXPath(XPATH_ALL_TU);
while (ap.evalXPath() != -1) {
traversalTuIndex++;
int curWordsNum = 0;
int inx = vn.getAttrVal("approved");
String attriApproved = "";
if (inx != -1) {
attriApproved = vn.toString(inx);
}
//判断是否锁定
boolean isLocked = false;
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();
// 查询该trans-unit节点下是否有target节点或者target节点的内容是否为空
vn.push();
if (targetAp.evalXPath() != -1) {
String attriState = "";
int stateInx = vn.getAttrVal("state");
if (stateInx != -1) {
attriState = vn.toString(stateInx);
}
if ("yes".equals(attriApproved) && (!"translated".equals(attriState) || !"signed-off".equals(attriState))) {
translatedPara++;
translatedWords += curWordsNum;
} else {
if ("translated".equals(attriState) || "signed-off".equals(attriState)) {
translatedPara++;
translatedWords += curWordsNum;
} else {
notTransWords += curWordsNum;
notTransPara++;
}
}
} else {
notTransWords += curWordsNum;
notTransPara++;
}
targetAp.resetXPath();
vn.pop();
if (isLocked) {
lockedPara++;
lockedWords += curWordsNum;
}
if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
return null;
}
}
} catch (Exception e) {
if (!monitorWork(monitor, traversalTuIndex, workInterval, false)) {
return null;
}
e.printStackTrace();
logger.error(MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger23"), filePath), e);
}
transProgDataMap.put("notTransPara", notTransPara);
transProgDataMap.put("translatedPara", translatedPara);
transProgDataMap.put("lockedPara", lockedPara);
transProgDataMap.put("notTransWords", notTransWords);
transProgDataMap.put("translatedWords", translatedWords);
transProgDataMap.put("lockedWords", lockedWords);
return transProgDataMap;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getNonTransElements.
/**
* 从.nonTransElement文件里面获取非译元素
* @param filePath
* @param nodeXpath
* @return
*/
public List<Map<String, String>> getNonTransElements(String filePath, String nodeXpath) {
List<Map<String, String>> list = new LinkedList<Map<String, String>>();
VTDNav vn = vnMap.get(filePath);
AutoPilot ap = new AutoPilot(vn);
validNull(vn, ap, filePath);
try {
VTDUtils vUtils = new VTDUtils(vn);
ap.selectXPath(nodeXpath);
AutoPilot _ap = new AutoPilot(vUtils.getVTDNav());
_ap.selectXPath("./*");
while (ap.evalXPath() != -1) {
Map<String, String> map = new HashMap<String, String>();
map.put("id", vn.toString(vn.getAttrVal("id")));
while (_ap.evalXPath() != -1) {
String nodeName = vUtils.getCurrentElementName();
if ("name".equals(nodeName)) {
map.put("name", vUtils.getElementContent());
}
if ("content".equals(nodeName)) {
map.put("content", vUtils.getElementContent());
} else if ("regular".equals(vUtils.getCurrentElementName())) {
map.put("regular", vUtils.getElementContent());
}
}
_ap.resetXPath();
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger19"), e);
}
return list;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getNodeContent.
/**
* 获取指定文件的指定节点的内容(除节点头与尾之外)
* @param xlfPath
* @param nodeXpath
* @return
* robert 2011-10-26
*/
public String getNodeContent(String xlfPath, String nodeXpath) {
String nodeContent = "";
VTDNav vn = vnMap.get(xlfPath);
Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
try {
AutoPilot ap = apMap.get(xlfPath);
VTDUtils vUtils = new VTDUtils(vn);
ap.selectXPath(nodeXpath);
while (ap.evalXPath() != -1) {
nodeContent = vUtils.getElementContent();
}
} catch (Exception e) {
logger.error(Messages.getString("qa.QAXmlHandler.logger16"), e);
e.printStackTrace();
}
return nodeContent;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getTUTag.
/**
* {@link #getTuTagStr(String, String)}
* <div style='color:red'>该方法与 getTuTagStr 方法类似,如修改应保持一致</div>
* 获取tu节点下的标记
* @param ap
* @param vn
* @return
*/
public List<Map<String, String>> getTUTag(VTDNav vn) {
List<Map<String, String>> tagList = new LinkedList<Map<String, String>>();
Map<String, String> tagMap;
try {
AutoPilot ap = new AutoPilot(vn);
VTDUtils vUtils = new VTDUtils(vn);
//如果没有相关标记,退出程序
if (vUtils.getChildElementsCount() <= 0) {
return tagList;
}
ap.selectXPath("./*");
//如果子节点大于0,那继续处理
if (vUtils.getChildElementsCount() > 0) {
while (ap.evalXPath() != -1) {
String childNodeName = vUtils.getCurrentElementName();
if (QAConstant.QA_mrk.equals(childNodeName)) {
String tagStr = vUtils.getElementHead() + "</" + childNodeName + ">";
tagMap = new HashMap<String, String>();
tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(tagStr));
tagList.add(tagMap);
//如果该节点下还有子节点标记,再获取其子节点标记
if (vUtils.getChildElementsCount() > 0) {
List<Map<String, String>> childTagList = getTUTag(vn);
for (int index = 0; index < childTagList.size(); index++) {
tagList.add(childTagList.get(index));
}
}
} else if (QAConstant.QA_g.equals(childNodeName) || QAConstant.QA_sub.equals(childNodeName)) {
String tagStr = vUtils.getElementHead() + "</" + childNodeName + ">";
tagMap = new HashMap<String, String>();
tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(tagStr));
tagList.add(tagMap);
if (vUtils.getChildElementsCount() > 0) {
List<Map<String, String>> childTagList = getTUTag(vn);
for (int index = 0; index < childTagList.size(); index++) {
tagList.add(childTagList.get(index));
}
}
} else {
//ph节点的值为code data或者一个sub节点,因此,要考虑到sub节点的情况
if (vUtils.getChildElementsCount() <= 0) {
//其他节点,比如ph,etp,btp,it内都没有可翻译文本,故获取其整体文本段即可
String childFrag = vUtils.getElementHead() + vUtils.getElementContent() + "</" + childNodeName + ">";
tagMap = new HashMap<String, String>();
tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(childFrag));
tagList.add(tagMap);
} else {
//先将该标记的纯文本获取出来
String childFrag = vUtils.getElementHead() + vUtils.getElementContent() + "</" + childNodeName + ">";
String childContent = vUtils.getElementContent();
String pureCode = "";
//获取该节点的纯文本
vn.push();
String txtNode = "./text()";
AutoPilot childAp = new AutoPilot(vn);
childAp.selectXPath(txtNode);
int txtIndex = -1;
while ((txtIndex = childAp.evalXPath()) != -1) {
pureCode += (" " + vn.toString(txtIndex));
}
vn.pop();
String tagStr = childFrag.replace(childContent == null ? "" : childContent, pureCode == null ? "" : pureCode);
tagMap = new HashMap<String, String>();
tagMap.put(QAConstant.QA_TAGNAME, childNodeName);
tagMap.put(QAConstant.QA_TAGCONTENT, deleteBlank(tagStr));
tagList.add(tagMap);
//下面添加该节点内的子节点sub标记
List<Map<String, String>> childTagList = getSubNodeTag(vn);
for (int index = 0; index < childTagList.size(); index++) {
tagList.add(childTagList.get(index));
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger18"), e);
}
return tagList;
}
Aggregations