Search in sources :

Example 21 with AutoPilot

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();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) NavException(com.ximpleware.NavException) FileNotFoundException(java.io.FileNotFoundException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 22 with AutoPilot

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();
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav)

Example 23 with AutoPilot

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();
}
Also used : AutoPilot(com.ximpleware.AutoPilot) TmxNote(net.heartsome.cat.common.bean.TmxNote) VTDNav(com.ximpleware.VTDNav)

Example 24 with AutoPilot

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;
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) LinkedList(java.util.LinkedList) NavException(com.ximpleware.NavException) EncodingException(com.ximpleware.EncodingException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) ParseException(com.ximpleware.ParseException) VTDException(com.ximpleware.VTDException)

Example 25 with AutoPilot

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();
}
Also used : AutoPilot(com.ximpleware.AutoPilot) VTDNav(com.ximpleware.VTDNav) TmxSegement(net.heartsome.cat.common.bean.TmxSegement)

Aggregations

AutoPilot (com.ximpleware.AutoPilot)308 VTDNav (com.ximpleware.VTDNav)173 NavException (com.ximpleware.NavException)150 XPathParseException (com.ximpleware.XPathParseException)145 XPathEvalException (com.ximpleware.XPathEvalException)137 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)112 IOException (java.io.IOException)103 ModifyException (com.ximpleware.ModifyException)95 TranscodeException (com.ximpleware.TranscodeException)82 CoreException (org.eclipse.core.runtime.CoreException)76 UnsupportedEncodingException (java.io.UnsupportedEncodingException)58 VTDGen (com.ximpleware.VTDGen)50 FileNotFoundException (java.io.FileNotFoundException)49 XMLModifier (com.ximpleware.XMLModifier)46 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)44 ArrayList (java.util.ArrayList)42 HashMap (java.util.HashMap)39 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)34 LinkedList (java.util.LinkedList)25