use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class TMXValidator method validSrcLanguage.
/**
* 循环每一个tu节点,进一步判断源语言
* @param tmxLocation
*/
private void validSrcLanguage(String tmxLocation, String srcLanguage) throws Exception {
VTDNav vn = vnMap.get(tmxLocation);
Assert.isNotNull(vn, MessageFormat.format(Messages.getString("plugin.TMXValidator.msg11"), tmxLocation));
AutoPilot ap = new AutoPilot(vn);
AutoPilot tuvAp = new AutoPilot(vn);
ap.selectXPath("/tmx/body/tu");
int index;
while (ap.evalXPath() != -1) {
boolean found = false;
vn.push();
tuvAp.selectXPath("./tuv");
while (tuvAp.evalXPath() != -1) {
String lang = "";
if ((index = vn.getAttrVal("xml:lang")) != -1) {
lang = vn.toString(index);
}
if ("".equals(lang)) {
if (version.equals("1.1") || version.equals("1.2")) {
if ((index = vn.getAttrVal("lang")) != -1) {
//$NON-NLS-1$
lang = vn.toString(index);
}
} else {
throw new Exception(Messages.getString("plugin.TMXValidator.msg12"));
}
}
if (lang.equals("")) {
throw new Exception(Messages.getString("plugin.TMXValidator.msg12"));
}
if (lang.equals(srcLanguage)) {
found = true;
}
}
if (!found) {
throw new Exception(MessageFormat.format(Messages.getString("plugin.TMXValidator.msg13"), srcLanguage));
}
vn.pop();
}
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class TMXValidator method validTmxRoot.
/**
* 通过验证指定文件的头元素是否是TMX,来验证该文件是不是一个TMX文件
* @return
* @throws Exception ;
*/
private boolean validTmxRoot(String tmxLocation) throws Exception {
VTDNav vn = vnMap.get(tmxLocation);
AutoPilot ap = new AutoPilot(vn);
Assert.isNotNull(vn, MessageFormat.format(Messages.getString("plugin.TMXValidator.msg11"), tmxLocation));
ap.selectXPath("/tmx");
if (ap.evalXPath() != -1) {
return true;
}
return false;
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class ReverseConversionValidateWithLibrary3 method reBuildXlf.
/**
* <div style='color:red;'>代替上面的 reBuildXlf 方法</div>
* 将要逆转换的 hsxliff 文件生成临时文件,再将这个临时文件进行处理,比如将分割的文本段拆分开来 robert 2012-11-28
* @param tmpXLFFile
*/
private void reBuildXlf(String xliffPath, File tmpXLFFile) {
//先将所有合并的文本段进行恢复成原来的样子
try {
ResourceUtils.copyFile(new File(xliffPath), tmpXLFFile);
VTDGen vg = new VTDGen();
if (!vg.parseFile(xliffPath, true)) {
LOGGER.error(MessageFormat.format("{0} parse error!", xliffPath));
return;
}
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
AutoPilot childAP = new AutoPilot(vn);
ap.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
childAP.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
VTDUtils vu = new VTDUtils(vn);
XMLModifier xm = new XMLModifier(vn);
// 先找出所有的分割与合并信息,再依序列号从高到低依次分解,合并信息是<ph id="hs-merge0~1" splitMergeIndex="0"> 这种标记
NavigableMap<Long, SegMergeInfoBean> infoMap = new TreeMap<Long, SegMergeInfoBean>();
ap.selectXPath("/xliff/file/body/descendant::node()" + "[(name()='group' and @ts='hs-split') or (name()='ph' and contains(@id, 'hs-merge'))]");
int idx = -1;
while (ap.evalXPath() != -1) {
String nodeName = vn.toString(vn.getCurrentIndex());
long index = -1;
if ((idx = vn.getAttrVal("splitMergeIndex")) != -1) {
index = Long.parseLong(vn.toString(idx));
}
boolean isMerge = false;
// 如果是 ph 节点,那么这个就是合并信息
if ("ph".equals(nodeName)) {
isMerge = true;
String phFrag = vu.getElementFragment();
String phID = vn.toString(vn.getAttrVal("id"));
String[] tuIds = vn.toString(vn.getAttrVal("id")).replace("hs-merge", "").split("~");
String mergeFirstId = tuIds[0].trim();
String mergeSecondId = tuIds[1].trim();
System.out.println("mergeFirstId = " + mergeFirstId);
System.out.println("mergeSecondId = " + mergeSecondId);
infoMap.put(index, new SegMergeInfoBean(isMerge, phFrag, phID, mergeFirstId, mergeSecondId));
} else {
infoMap.put(index, new SegMergeInfoBean(isMerge));
}
}
for (Entry<Long, SegMergeInfoBean> entry : infoMap.descendingMap().entrySet()) {
Long index = entry.getKey();
SegMergeInfoBean bean = entry.getValue();
if (bean.isMerge()) {
resetMerge(ap, vn, vu, xm, index, bean);
} else {
resetSplit(ap, childAP, vn, vu, xm, index);
}
vn = xm.outputAndReparse();
xm.bind(vn);
ap.bind(vn);
childAP.bind(vn);
vu.bind(vn);
}
xm.output(tmpXLFFile.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class TSFileHandler method getNotes.
// 获取当前节点的所有批注。
private Vector<NoteBean> getNotes(VTDUtils vu) throws XPathEvalException, NavException, XPathParseException {
Vector<NoteBean> notes = new Vector<NoteBean>();
VTDNav vn = vu.getVTDNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("./note");
while (ap.evalXPath() != -1) {
NoteBean note = new NoteBean(vu.getElementContent());
int attInx = vn.getAttrVal("xml:lang");
if (attInx != -1) {
note.setLang(vn.toString(attInx));
}
attInx = vn.getAttrVal("from");
if (attInx != -1) {
note.setFrom(vn.toString(attInx));
}
attInx = vn.getAttrVal("priority");
if (attInx != -1) {
note.setPriority(vn.toString(attInx));
}
attInx = vn.getAttrVal("annotates");
if (attInx != -1) {
note.setAnnotates(vn.toString(attInx));
}
notes.add(note);
}
if (notes.isEmpty()) {
notes = null;
}
return notes;
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class QAXmlHandler method getAllRowIdsByLanguages.
/**
* 通过源与目标语言得到所有当前打开的文件包含的 RowId
* ///xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]
* @return ;
*/
public List<String> getAllRowIdsByLanguages(String srcLanguage, String tgtLanguage) {
List<String> allRowIds = new LinkedList<String>();
AutoPilot ap = new AutoPilot();
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
VTDUtils vu = new VTDUtils();
for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
String fileName = entry.getKey();
VTDNav vn = entry.getValue();
ap.bind(vn);
try {
vu.bind(vn);
// 查询相同目标与相同源文本的tu
String XPATH_ALL_TU_BYLANGUAGE = "/xliff/file[upper-case(@source-language)=''{0}'' and upper-case(@target-language)=''{1}'']/body/descendant::trans-unit[(source/text()!='''' or source/*)]";
String xpath = MessageFormat.format(XPATH_ALL_TU_BYLANGUAGE, new Object[] { srcLanguage, tgtLanguage });
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
String rowId = RowIdUtil.getRowId(vn, fileName);
if (rowId != null) {
allRowIds.add(rowId);
}
}
} catch (NavException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
} catch (XPathParseException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
} catch (XPathEvalException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
}
}
return allRowIds;
}
Aggregations