use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method getTMXFileContent.
/**
* 根据同一个 XLIFF 文件中的 rowId 生成 TMX 文件
* @param list
* rowId
* @param creationTool
* 创建工具
* @param creationToolVersion
* 创建工具版本
* @param srcName
* 源语言
* @param monitor
* 进度条
* @return TMX 文件
*/
public StringBuffer getTMXFileContent(List<String> list, String creationTool, String creationToolVersion, String srcName, IProgressMonitor monitor) {
if (list == null || list.size() == 0) {
return new StringBuffer();
}
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask(Messages.getString("file.XLFHandler.task3"), list.size());
if (monitor.isCanceled())
return new StringBuffer();
StringBuffer re = new StringBuffer();
re.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
re.append("<tmx version=\"1.4b\">");
re.append("<header creationtool=\"" + creationTool + "\" creationtoolversion=\"" + creationToolVersion + "\" srclang=\"" + srcName + "\" adminlang=\"en\" datatype=\"xml\" o-tmf=\"XLIFF\" segtype=\"paragraph\">");
re.append("</header>");
re.append("<body>");
VTDNav vn = getVTDNavByRowId(list.get(0));
AutoPilot ap = new AutoPilot(vn);
VTDUtils vu = null;
try {
vu = new VTDUtils(vn);
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
monitor.done();
}
String defaultSrcLang = "";
String defaultTgtLang = "";
String fileXPath = RowIdUtil.getFileXpathByRowId(list.get(0));
if (vu.pilot(ap, fileXPath) != -1) {
defaultSrcLang = vu.getValue(ap, "./@source-language");
defaultTgtLang = vu.getValue(ap, "./@target-language");
}
if (defaultTgtLang == null) {
defaultTgtLang = "";
}
for (String rowId : list) {
if (monitor.isCanceled())
return new StringBuffer();
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
try {
ap.selectXPath(tuXPath);
ap.evalXPath();
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
monitor.done();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
monitor.done();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
monitor.done();
}
re.append("<tu ");
// 添加tu的属性
boolean hasTuId = false;
String id = null;
String attrNameXp = "./@*[name]";
String attrValueXp = "./@";
List<String> tuPropNames = vu.getValues(ap, attrNameXp);
for (String name : tuPropNames) {
String value = vu.getValue(ap, attrValueXp + name);
re.append(name + "=\"" + value + "\" ");
if (name.equals("id")) {
id = value;
}
if (!hasTuId) {
if (name.equals("tuid")) {
hasTuId = true;
}
}
}
if (!hasTuId) {
re.append("tuid=\"" + genTuId(rowId, id) + "\"");
}
re.append(">");
String typeXp = "./prop-group/prop/@prop-type";
List<String> typeValues = vu.getValues(ap, typeXp);
if (typeValues != null) {
for (String typeValue : typeValues) {
re.append("<prop type=\"" + typeValue + "\">");
re.append(vu.getValue(ap, "./prop-group/prop[@prop-type='" + typeValue + "']/text()"));
re.append("</prop>");
}
}
// HS的自定义属性组
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
String hsPropXp = "./hs:prop-group/prop/@prop-type";
typeValues = vu.getValues(ap, hsPropXp);
if (typeValues != null) {
for (String typeValue : typeValues) {
re.append("<prop type=\"" + typeValue + "\">");
re.append(vu.getValue(ap, "./hs:prop-group/prop[@prop-type='" + typeValue + "']/text()"));
re.append("</prop>");
}
}
// 添加tuv(source)
re.append("<tuv ");
// 添加tuv(source)属性
attrNameXp = "./source/@*[name]";
attrValueXp = "./source/@";
List<String> sourcePropNames = vu.getValues(ap, attrNameXp);
if (sourcePropNames != null) {
for (String name : sourcePropNames) {
String value = vu.getValue(ap, attrValueXp + name);
re.append(name + "=\"" + value + "\" ");
}
}
/*
* source节点可能没有xml:lang属性,如果没有,则要去file节点去取source-language(Required)属性的值
*/
if (sourcePropNames == null || !sourcePropNames.contains("xml:lang")) {
re.append("xml:lang=\"" + defaultSrcLang + "\"");
}
re.append(">");
// 添加上下文prop
re.append("<prop type=\"x-preContext\">");
re.append(getContext(vu, ap, contextNum, true));
re.append("</prop>");
re.append("<prop type=\"x-nextContext\">");
re.append(getContext(vu, ap, contextNum, false));
re.append("</prop>");
String sourceTextXp = "./source/text()";
re.append("<seg>").append(vu.getValue(ap, sourceTextXp)).append("</seg>");
re.append("</tuv>");
// 添加tuv(target)
re.append("<tuv ");
// 添加tuv(target)属性
attrNameXp = "./target/@*[name]";
attrValueXp = "./target/@";
List<String> targetPropNames = vu.getValues(ap, attrNameXp);
if (targetPropNames != null) {
for (String name : targetPropNames) {
String value = vu.getValue(ap, attrValueXp + name);
re.append(name + "=\"" + value + "\" ");
}
}
/*
* target节点可能没有xml:lang属性,如果没有,则要去file节点去取target-language(Optional)属性的值
*/
if (targetPropNames == null || !targetPropNames.contains("xml:lang")) {
re.append("xml:lang=\"" + defaultTgtLang + "\"");
}
re.append(">");
String targetTextXp = "./target/text()";
re.append("<seg>").append(vu.getValue(ap, targetTextXp)).append("</seg>");
re.append("</tuv>");
re.append("</tu>");
monitor.worked(1);
}
re.append("</body>");
re.append("</tmx>");
monitor.done();
return re;
}
use of com.ximpleware.VTDNav 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 com.ximpleware.VTDNav 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 com.ximpleware.VTDNav 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 com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method resetMergeSegment.
/**
* 合并文本段的撤销功能 robert 2012-11-06
*/
public void resetMergeSegment(Map<String, String> oldSegFragMap) {
String xlfPath = "";
VTDNav vn = null;
AutoPilot ap = new AutoPilot();
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
XMLModifier xm = new XMLModifier();
try {
// 由于合并的文本段都是同一个文件,因此此处不做 文件名 的判断
for (Entry<String, String> entry : oldSegFragMap.entrySet()) {
String rowId = entry.getKey();
if ("".equals(xlfPath) || xlfPath == null || vn == null) {
xlfPath = RowIdUtil.getFileNameByRowId(rowId);
vn = vnMap.get(xlfPath);
ap.bind(vn);
xm.bind(vn);
}
ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
if (ap.evalXPath() != -1) {
xm.remove();
xm.insertAfterElement(entry.getValue());
}
}
saveAndReparse(xm, xlfPath);
resetRowIdsToUnsorted();
} catch (Exception e) {
LOGGER.error("", e);
}
}
Aggregations