use of com.ximpleware.AutoPilot 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.AutoPilot 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.AutoPilot in project translationstudio8 by heartsome.
the class ProjectConfiger method getAttrMap.
/**
* 获取配置文件中的项目属性字段信息
* @return
* @throws XPathParseException
* @throws XPathEvalException
* @throws NavException ;
*/
private LinkedHashMap<String, Object[]> getAttrMap() throws XPathParseException, XPathEvalException, NavException {
AutoPilot ap = new AutoPilot(vu.getVTDNav());
Map<String, Object[]> mapAttr = new HashMap<String, Object[]>();
ap.selectXPath("/projectDescription/hs//attributeList/attribute");
AutoPilot ap2 = new AutoPilot(vu.getVTDNav());
final Collator collatorChinese = Collator.getInstance(java.util.Locale.CHINA);
while (ap.evalXPath() != -1) {
String attrName = TextUtil.stringToXML(vu.getCurrentElementAttribut("name", ""));
String attrSelVal = TextUtil.stringToXML(vu.getCurrentElementAttribut("selection", ""));
vu.getVTDNav().push();
ap2.selectXPath("./item");
ArrayList<String> lstAttrVal = new ArrayList<String>();
while (ap2.evalXPath() != -1) {
lstAttrVal.add(vu.getElementContent());
}
vu.getVTDNav().pop();
Collections.sort(lstAttrVal, collatorChinese);
mapAttr.put(attrName, new Object[] { attrSelVal, lstAttrVal });
}
List<Entry<String, Object[]>> lstAttr = new ArrayList<Entry<String, Object[]>>(mapAttr.entrySet());
Collections.sort(lstAttr, new Comparator<Entry<String, Object[]>>() {
public int compare(Entry<String, Object[]> arg0, Entry<String, Object[]> arg1) {
return collatorChinese.compare(arg0.getKey(), arg1.getKey());
}
});
LinkedHashMap<String, Object[]> linkedMapAttr = new LinkedHashMap<String, Object[]>();
for (Entry<String, Object[]> entry : lstAttr) {
linkedMapAttr.put(entry.getKey(), entry.getValue());
}
return linkedMapAttr;
}
use of com.ximpleware.AutoPilot in project translationstudio8 by heartsome.
the class ProjectConfiger method getProjectName.
/**
* 获取项目名称,如果在设置项目信息时,未填写该值返回将为空串
* @return 如果返回null表示为获取到项目名称;
* @throws XPathParseException
* @throws XPathEvalException
* @throws NavException
*/
public String getProjectName() throws XPathParseException, XPathEvalException, NavException {
AutoPilot ap = new AutoPilot(vu.getVTDNav());
ap.selectXPath("/projectDescription/hs");
if (ap.evalXPath() != -1) {
return TextUtil.resetSpecialString(vu.getChildContent("projectname"));
}
return null;
}
use of com.ximpleware.AutoPilot in project translationstudio8 by heartsome.
the class ProjectConfiger method getTargetlanguages.
/**
* 获取项目的所有目标语言
* @return 如果返回空的list表示没有获取到项目的目标语言;
* @throws XPathParseException
* @throws NavException
* @throws XPathEvalException
*/
public List<Language> getTargetlanguages() throws XPathParseException, NavException, XPathEvalException {
AutoPilot ap = new AutoPilot(vu.getVTDNav());
ap.selectXPath("/projectDescription/hs/language/target");
List<Language> targetLangs = new ArrayList<Language>();
while (ap.evalXPath() != -1) {
String code = vu.getCurrentElementAttribut("code", "");
String name = vu.getElementContent();
String image = vu.getCurrentElementAttribut("image", "");
String isBidi = vu.getCurrentElementAttribut("isbidi", "false");
targetLangs.add(new Language(code, name, image, isBidi.equals("false") ? false : true));
}
return targetLangs;
}
Aggregations