/** * Validate TMX Format,and pilot to Body XMLElement * * @param vg * @throws TmxReadException ; */ private void validateTmxAndParseHeader(VTDGen vg) throws TmxReadException { VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); String rootPath = "/tmx"; vu = new VTDUtils(); try { vu.bind(vn); ap.selectXPath(rootPath); if (ap.evalXPath() == -1) { throw new TmxReadException(Messages.getString("document.TmxReader.validateTmxFileError")); } ap.resetXPath(); ap.selectXPath("/tmx/header"); if (ap.evalXPath() == -1) { throw new TmxReadException(Messages.getString("document.TmxReader.validateTmxFileError")); } int id = vu.getVTDNav().getAttrVal("srclang"); if (id == -1) { throw new TmxReadException(Messages.getString("document.TmxReader.validateTmxFileError")); } header.setSrclang(vu.getVTDNav().toString(id).trim()); if (vu.pilot("/tmx/body") == -1) { throw new TmxReadException(Messages.getString("document.TmxReader.validateTmxFileError")); } // compute total tu number this.totalTu = vu.getChildElementsCount(); } catch (VTDException e) { logger.error("", e); throw new TmxReadException( Messages.getString("document.TmxReader.parseTmxFileError") + e.getMessage()); } finally { vg.clear(); } }
/** * 获取 tmxfile 中的所有语言 * * @return */ public List<String> getLangs() { List<String> langs = new LinkedList<String>(); langs.add(LanguageUtils.convertLangCode(header.getSrclang())); VTDNav vn = vu.getVTDNav(); vn.push(); 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) { index = vn.getAttrVal("lang"); // version 1.1 if (index == -1) { continue; } } lang = LanguageUtils.convertLangCode(vn.toRawString(index)); if (!langs.contains(lang)) { langs.add(lang); } } } catch (Exception e) { e.printStackTrace(); } vn.pop(); return langs; }
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)); if (tmxFilter == null) segment.setFullText(fullText); else { String text = tmxFilter.clearString(fullText); segment.setFullText(text); } segment.setPureText(pureText); if (lang.equalsIgnoreCase(header.getSrclang())) { tu.setSource(segment); } else { tu.appendSegement(segment); } } vn.pop(); } vn.pop(); }