use of net.heartsome.xml.vtdimpl.VTDUtils 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 net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler 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);
// Add by Jason
vn.push();
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
try {
AutoPilot ap = new AutoPilot(vn);
VTDUtils vUtils = new VTDUtils(vn);
ap.selectXPath(nodeXpath);
while (ap.evalXPath() != -1) {
nodeContent = vUtils.getElementContent();
}
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
// Add by Jason
vn.pop();
return nodeContent;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method deleteLastSplitInfo.
/**
* 删除目标文件的最后一条切割信息
* @param xlfPath
* robert 2011-10-26
*/
public void deleteLastSplitInfo(String xlfPath) {
VTDNav vn = vnMap.get(xlfPath);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
try {
// --robert split
String xPath = "/xliff/file/header/hs:splitInfos/hs:splitInfo[last()]";
// String xPath = "/xliff/file/header/splitInfos/splitInfo[last()]";
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
XMLModifier xm = vu.delete(ap, null, xPath, VTDUtils.PILOT_TO_END);
saveAndReparse(xm, xlfPath);
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method getDocumentInfo.
/**
* 得到文档属性的信息
* @param fileName
* 文件名
* @return 多个文件的文档属性集合(一个 file 节点文档的属性为一个 HashMap);
*/
public List<HashMap<String, String>> getDocumentInfo(String fileName) {
ArrayList<HashMap<String, String>> fileList = new ArrayList<HashMap<String, String>>();
VTDNav vn = vnMap.get(fileName);
AutoPilot apFile = new AutoPilot(vn);
try {
apFile.selectXPath("/xliff/file");
String[] fileAttrNames = { DocumentPropertiesKeys.ORIGINAL, DocumentPropertiesKeys.DATA_TYPE, DocumentPropertiesKeys.SOURCE_LANGUAGE, DocumentPropertiesKeys.TARGET_LANGUAGE };
String[] propTypes = new String[] { DocumentPropertiesKeys.PROJECT_REF, DocumentPropertiesKeys.JOB_REF, DocumentPropertiesKeys.JOB_DATE, DocumentPropertiesKeys.JOB_OWNER, DocumentPropertiesKeys.CLIENT };
VTDUtils vu = new VTDUtils(vn);
while (apFile.evalXPath() != -1) {
String value = "";
HashMap<String, String> fileAttrs = new HashMap<String, String>();
for (String attrName : fileAttrNames) {
value = vu.getCurrentElementAttribut(attrName, "");
fileAttrs.put(attrName, value);
}
AutoPilot ap = new AutoPilot(vn);
vn.push();
value = "";
ap.selectXPath("./header/skl");
if (ap.evalXPath() != -1) {
ap.selectXPath("./external-file");
if (ap.evalXPath() != -1) {
value = vu.getCurrentElementAttribut("href", "");
} else {
ap.selectXPath("./internal-file");
if (ap.evalXPath() != -1) {
value = Constant.SKL_INTERNAL_FILE;
}
}
}
// vn.push();
// ap.selectXPath("./header/skl/external-file");
// value = "";
// if (ap.evalXPath() != -1) {
// int attrIdx = vn.getAttrVal("href");
// value = attrIdx != -1 ? vn.toString(attrIdx) : "";
// }
fileAttrs.put(DocumentPropertiesKeys.SKL, value);
vn.pop();
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
vn.push();
ap.selectXPath("./header/hs:prop-group[@name='encoding']/hs:prop[@prop-type='encoding']");
value = "";
if (ap.evalXPath() != -1) {
value = vn.toString(vn.getText());
}
fileAttrs.put(DocumentPropertiesKeys.ENCODING, value);
vn.pop();
for (String attrName : propTypes) {
vn.push();
ap.selectXPath("./header/hs:prop-group[@name=\"project\"]/hs:prop[@prop-type=\"" + attrName + "\"]");
value = "";
if (ap.evalXPath() != -1) {
value = vn.toString(vn.getText());
}
if ("".equals(value) && DocumentPropertiesKeys.JOB_DATE.equals(attrName)) {
value = sdf.format(new Date());
}
fileAttrs.put(attrName, value);
vn.pop();
}
fileList.add(fileAttrs);
}
} 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 fileList;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method getTagLocation.
/**
* <div style='color:red'>此方法有复制到 convert.ui 插件的 ReverseConversionValidateWithLibrary3 类中。故若修改,注意保持同步 --robert 2012-11-29</div>
* 获取每个标记 header 与 tail 在文本中的 index,此方法主要针对文本段分割,分割点在g、mrk标记里面。robert 2012-11-15
* @param vn
*/
private List<Map<String, String>> getTagLocation(VTDNav vn, String srcContent) {
List<Map<String, String>> tagLoctionList = new LinkedList<Map<String, String>>();
vn.push();
AutoPilot ap = new AutoPilot(vn);
String xpath = "./descendant::node()";
try {
VTDUtils vu = new VTDUtils(vn);
ap.selectXPath(xpath);
int lastIdx = 0;
while (ap.evalXPath() != -1) {
Map<String, String> tagLocationMap = new HashMap<String, String>();
String tagName = vn.toString(vn.getCurrentIndex());
if (!("g".equals(tagName) || "mrk".equals(tagName) || "sub".equals(tagName))) {
continue;
}
String tagHeader = vu.getElementHead();
String tagTail = vu.getElementFragment().replace(tagHeader, "").replace(vu.getElementContent(), "");
int headerIdx = srcContent.indexOf(tagHeader, lastIdx);
int tailIdx = headerIdx + tagHeader.length() + vu.getElementContent().length();
lastIdx = headerIdx;
tagLocationMap.put("tagHeader", tagHeader);
tagLocationMap.put("tagTail", tagTail);
tagLocationMap.put("headerIdx", "" + headerIdx);
tagLocationMap.put("tailIdx", "" + tailIdx);
tagLoctionList.add(tagLocationMap);
}
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
vn.pop();
return tagLoctionList;
}
Aggregations