use of com.ximpleware.AutoPilot in project translationstudio8 by heartsome.
the class ExportTbxImpl method filterLangSet.
/**
* 过滤从库中导出的langSet节点<br>
* 在库中TremEntry是以整个节点进行存储的,因此,在导出后也是整个节点导出,所以会将无关的语言也导出来。<br>
* 在导出后,生成的TBX文件中进语言进行过滤
* @param filePath
* 导出后生成的TBX文件路径
* @param srcLang
* 源语言
* @param needLang
* 当前需要导出的语言;
*/
private void filterLangSet(String filePath, String srcLang, List<String> needLang) {
try {
VTDGen vg = new VTDGen();
if (vg.parseFile(filePath, true)) {
VTDUtils vu = new VTDUtils(vg.getNav());
StringBuffer xpath = new StringBuffer("/martif/text/body/termEntry/langSet[");
String noteXpathtemp = "starts-with(@id,'__LANG__,') or ends-with(@id,',__LANG__')";
StringBuffer noteTgtXpath = new StringBuffer();
for (String lang : needLang) {
xpath.append("not(@xml:lang='" + lang + "') and ");
if (!lang.equals(srcLang)) {
noteTgtXpath.append(noteXpathtemp.replace("__LANG__", lang)).append(" or ");
}
}
String tgtLangXpath = noteTgtXpath.substring(0, noteTgtXpath.lastIndexOf("or"));
StringBuffer noteXpath = new StringBuffer();
noteXpath.append("/martif/text/body/termEntry/note[");
noteXpath.append("not(");
noteXpath.append("(").append(noteXpathtemp.replace("__LANG__", srcLang)).append(")");
noteXpath.append(" and ");
noteXpath.append("(").append(tgtLangXpath).append(")");
noteXpath.append(")");
noteXpath.append("]");
String xpathStr = xpath.substring(0, xpath.lastIndexOf("and")) + "]";
XMLModifier xm = new XMLModifier(vu.getVTDNav());
AutoPilot ap = new AutoPilot(vu.getVTDNav());
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
xm = vu.delete(ap, xm, xpathStr, VTDUtils.PILOT_TO_END);
xm = vu.delete(ap, xm, noteXpath.toString(), VTDUtils.PILOT_TO_END);
FileOutputStream fos = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入文件
xm.output(bos);
bos.close();
fos.close();
}
} catch (NavException e) {
logger.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
logger.error("", e);
e.printStackTrace();
} catch (FileNotFoundException e) {
logger.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
logger.error("", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("", e);
e.printStackTrace();
}
}
use of com.ximpleware.AutoPilot in project translationstudio8 by heartsome.
the class TmxReader method readTuElementAttribute.
private void readTuElementAttribute(TmxTU tu) throws VTDException {
VTDNav vn = vu.getVTDNav();
vn.push();
AutoPilot apAttributes = new AutoPilot(vu.getVTDNav());
apAttributes.selectXPath("@*");
int inx = -1;
while ((inx = apAttributes.evalXPath()) != -1) {
String name = vn.toString(inx);
inx = vn.getAttrVal(name);
String value = inx != -1 ? vn.toString(inx) : "";
// creationid, changedate, segtype, changeid, o-tmf, srclang.
if (name.equals("tuid")) {
tu.setTuId(value);
} else if (name.equals("creationtool")) {
tu.setCreationTool(value);
} else if (name.equals("creationtoolversion")) {
tu.setCreationToolVersion(value);
} else if (name.equals("creationdate")) {
tu.setCreationDate(value);
} else if (name.equals("creationid")) {
tu.setCreationUser(value);
} else if (name.equals("changedate")) {
tu.setChangeDate(value);
} else if (name.equals("changeid")) {
tu.setChangeUser(value);
} else {
tu.appendAttribute(name, value);
}
}
vn.pop();
}
use of com.ximpleware.AutoPilot in project translationstudio8 by heartsome.
the class TmxReader method readTuNoteElement.
private void readTuNoteElement(TmxTU tu) throws VTDException {
VTDNav vn = vu.getVTDNav();
vn.push();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("./note");
while (ap.evalXPath() != -1) {
String fragment = vu.getElementFragment();
TmxNote note = new TmxNote();
note.setContent(fragment);
int inx = vn.getAttrVal("xml:lang");
String value = inx != -1 ? vn.toString(inx) : null;
if (value != null) {
note.setXmlLang(value);
}
inx = vn.getAttrVal("o-encoding");
value = inx != -1 ? vn.toString(inx) : null;
if (value != null) {
note.setXmlLang(value);
}
tu.appendNote(note);
}
vn.pop();
}
use of com.ximpleware.AutoPilot in project translationstudio8 by heartsome.
the class TmxReader method getLangs.
/**
* 获取 tmxfile 中的所有语言
* @return
*/
public List<String> getLangs() {
VTDNav vn = vu.getVTDNav();
vn.push();
List<String> langs = new LinkedList<String>();
langs.add(header.getSrclang());
AutoPilot ap = new AutoPilot(vn);
try {
ap.selectXPath("/tmx/body/tu/tuv");
String lang;
int index = -1;
while (ap.evalXPath() != -1) {
index = vn.getAttrVal("xml:lang");
if (index == -1)
continue;
lang = vn.toRawString(index);
if (!langs.contains(lang)) {
langs.add(lang);
}
}
} catch (Exception e) {
e.printStackTrace();
}
vn.pop();
return langs;
}
use of com.ximpleware.AutoPilot in project translationstudio8 by heartsome.
the class TmxReader method readTuTuvElement.
private void readTuTuvElement(TmxTU tu) throws VTDException {
VTDNav vn = vu.getVTDNav();
vn.push();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("./tuv");
// TUV 节点下的Note,Prop节点暂时不处理,所以此处暂时不解析
while (ap.evalXPath() != -1) {
int inx = vn.getAttrVal("xml:lang");
inx = inx == -1 ? vn.getAttrVal("lang") : inx;
String lang = inx != -1 ? vn.toString(inx) : null;
if (lang == null) {
continue;
}
vn.push();
if (vu.pilot("./seg") != -1) {
String fullText = vu.getElementContent().trim();
String pureText = DocUtils.getTmxTbxPureText(vu).trim();
if (fullText == null || pureText == null || fullText.equals("") || pureText.equals("")) {
// fix Bug #2928 by Jason SQLite--导入TMX异常, 导入程序正常退出,但是未完全导入所有内容,此处在continue时应该先调用vn.pop()
vn.pop();
continue;
}
TmxSegement segment = new TmxSegement();
segment.setLangCode(Utils.convertLangCode(lang));
// fix Bug #3406 by yule --xliff中的标记可能与TMX标记不兼容。
if (tmxFilter == null)
// segment.setFullText(InnerTagClearUtil.clearXliffTag4Tmx(fullText));
// 不在导入时清理
segment.setFullText(fullText);
else {
String text = tmxFilter.clearString(fullText);
// text = InnerTagClearUtil.clearXliffTag4Tmx(text);// 不在导入时清理
segment.setFullText(text);
}
segment.setPureText(pureText);
if (lang.equalsIgnoreCase(header.getSrclang())) {
tu.setSource(segment);
} else {
tu.appendSegement(segment);
}
}
vn.pop();
}
vn.pop();
}
Aggregations