Search in sources :

Example 1 with TmxReaderEvent

use of net.heartsome.cat.document.TmxReaderEvent in project translationstudio8 by heartsome.

the class Tmx2Txt method doCovnerter.

public void doCovnerter(String tmxFile, File targetFile, IProgressMonitor monitor) throws Exception {
    //1. 解析文件
    monitor.beginTask(Messages.getString("converter.common.monitor.info.start"), 100);
    monitor.setTaskName(Messages.getString("converter.common.monitor.info.start"));
    if (monitor.isCanceled()) {
        return;
    }
    monitor.worked(1);
    try {
        tmxReader = new TmxReader(new File(tmxFile));
    } catch (TmxReadException e) {
        LOGGER.error(Messages.getString("tmxdata.TmxFileContainer.parseTmxFileError"), e);
        throw e;
    }
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    TmxReaderEvent event = null;
    List<String> langs = tmxReader.getLangs();
    Map<String, TmxSegement> map = new HashMap<String, TmxSegement>();
    if (monitor.isCanceled()) {
        return;
    }
    //done 1
    monitor.worked(9);
    try {
        fos = new FileOutputStream(targetFile, true);
    } catch (FileNotFoundException e) {
        LOGGER.error("", e);
        throw e;
    }
    bos = new BufferedOutputStream(fos);
    try {
        for (String str : langs) {
            bos.write(str.getBytes());
            bos.write(SEPARATOR);
        }
        bos.write(CRLF);
        // 2. 处理内容
        double total = tmxReader.getTotalTu();
        //已完成
        int worked = 0;
        //计数
        int count = 0;
        int tmp = 0;
        IProgressMonitor submMonitor = new SubProgressMonitor(monitor, 90);
        submMonitor.beginTask("", 100);
        submMonitor.setTaskName(Messages.getString("converter.common.monitor.info.readtmx"));
        loop: while (true) {
            if (monitor.isCanceled()) {
                return;
            }
            event = tmxReader.read();
            switch(event.getState()) {
                case TmxReaderEvent.END_FILE:
                    break loop;
                case TmxReaderEvent.NORMAL_READ:
                    // 清除
                    map.clear();
                    if (event.getTu().getSource() == null) {
                        break;
                    }
                    map.put(langs.get(0).toLowerCase(), event.getTu().getSource());
                    if (event.getTu().getSegments() != null) {
                        for (TmxSegement seg : event.getTu().getSegments()) {
                            map.put(seg.getLangCode().toLowerCase(), seg);
                        }
                    }
                    // 生成每列
                    for (String lang : langs) {
                        if (map.containsKey(lang.toLowerCase())) {
                            bos.write(encodeTab(map.get(lang.toLowerCase()).getPureText(), null));
                        }
                        bos.write(SEPARATOR);
                    }
                    bos.write(CRLF);
                    break;
                default:
                    continue;
            }
            tmp = (int) ((count++ / total) * 100);
            if (tmp > worked) {
                submMonitor.worked(tmp - worked);
                worked = tmp;
            }
        }
        bos.flush();
        bos.close();
        submMonitor.done();
        monitor.done();
    } catch (IOException e) {
        LOGGER.error("", e);
        throw e;
    } finally {
        try {
            fos.close();
            bos.close();
        } catch (IOException e) {
            LOGGER.error("close io");
        }
    }
}
Also used : HashMap(java.util.HashMap) TmxReader(net.heartsome.cat.document.TmxReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) TmxReaderEvent(net.heartsome.cat.document.TmxReaderEvent) TmxSegement(net.heartsome.cat.common.bean.TmxSegement) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileOutputStream(java.io.FileOutputStream) AbstractTmx2File(net.heartsome.cat.tmx.converter.AbstractTmx2File) File(java.io.File) TmxReadException(net.heartsome.cat.document.TmxReadException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 TmxSegement (net.heartsome.cat.common.bean.TmxSegement)1 TmxReadException (net.heartsome.cat.document.TmxReadException)1 TmxReader (net.heartsome.cat.document.TmxReader)1 TmxReaderEvent (net.heartsome.cat.document.TmxReaderEvent)1 AbstractTmx2File (net.heartsome.cat.tmx.converter.AbstractTmx2File)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1