Search in sources :

Example 6 with VTDNav

use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.

the class DocUtils method isTBX.

/**
	 * 判断是否是正确的 TBX 文件
	 * @param fileName
	 *            TBX 文件的全路径
	 * @return 反回null,验证失败
	 * @throws ParseException
	 * @throws EntityException
	 * @throws EOFException
	 * @throws EncodingException
	 * @throws FileNotFoundException
	 */
public static VTDUtils isTBX(String fileName) throws EncodingException, ParseException, FileNotFoundException {
    VTDGen vg = new VTDGen();
    FileInputStream fis = null;
    File f = null;
    try {
        f = new File(fileName);
        fis = new FileInputStream(f);
        byte[] b = new byte[(int) f.length()];
        int offset = 0;
        int numRead = 0;
        // I choose this value randomally,
        int numOfBytes = 1048576;
        // any other (not too big) value also can be here.
        if (b.length - offset < numOfBytes) {
            numOfBytes = b.length - offset;
        }
        while (offset < b.length && (numRead = fis.read(b, offset, numOfBytes)) >= 0) {
            offset += numRead;
            if (b.length - offset < numOfBytes) {
                numOfBytes = b.length - offset;
            }
        }
        vg.setDoc(b);
        vg.parse(true);
    } catch (IOException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger1"), e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    String rootPath = "/martif";
    VTDUtils vtdUtils = new VTDUtils();
    try {
        vtdUtils.bind(vn);
        ap.selectXPath(rootPath);
        if (ap.evalXPath() == -1) {
            // } else {
            return null;
        }
    } catch (NavException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathEvalException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathParseException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } finally {
        vg.clear();
    }
    return vtdUtils;
}
Also used : NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) NavException(com.ximpleware.NavException) IOException(java.io.IOException) EncodingException(com.ximpleware.EncodingException) FileNotFoundException(java.io.FileNotFoundException) ParseException(com.ximpleware.ParseException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 7 with VTDNav

use of com.ximpleware.VTDNav 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 8 with VTDNav

use of com.ximpleware.VTDNav 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 9 with VTDNav

use of com.ximpleware.VTDNav 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 10 with VTDNav

use of com.ximpleware.VTDNav 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

VTDNav (com.ximpleware.VTDNav)206 AutoPilot (com.ximpleware.AutoPilot)177 NavException (com.ximpleware.NavException)128 XPathParseException (com.ximpleware.XPathParseException)115 XPathEvalException (com.ximpleware.XPathEvalException)110 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)99 IOException (java.io.IOException)85 ModifyException (com.ximpleware.ModifyException)81 TranscodeException (com.ximpleware.TranscodeException)69 CoreException (org.eclipse.core.runtime.CoreException)69 XMLModifier (com.ximpleware.XMLModifier)53 UnsupportedEncodingException (java.io.UnsupportedEncodingException)49 FileNotFoundException (java.io.FileNotFoundException)46 VTDGen (com.ximpleware.VTDGen)45 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)43 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)27 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)25 LinkedList (java.util.LinkedList)23