Search in sources :

Example 1 with TxtRowReader

use of net.heartsome.cat.document.txt.TxtRowReader in project translationstudio8 by heartsome.

the class Txt2TbxConverter method doConvert.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.document.converter.AbstractConverter#doConvert(java.lang.String,
	 *      org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public void doConvert(String targetFile, IProgressMonitor monitor) throws Exception {
    try {
        monitor.beginTask("", 10);
        monitor.worked(2);
        out = new FileOutputStream(new File(targetFile));
        File f = new File(txtFile);
        String encoding = FileEncodingDetector.detectFileEncoding(f);
        TxtRowReader reader = new TxtRowReader(f, encoding);
        int rowsNum = reader.getLineNumber();
        if (rowsNum < 2) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        // read header
        List<String[]> h = reader.read(1);
        if (h == null || h.size() == 0) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        String[] hv = h.get(0);
        if (hv == null || hv.length < 2) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        Map<String, Language> defaultLanguage = LocaleService.getDefaultLanguage();
        List<String> _temp = new ArrayList<String>();
        for (String s : hv) {
            s = LanguageUtils.convertLangCode(s);
            if (_temp.contains(s)) {
                throw new Exception(Messages.getString("converter.common.vaild.duplicatelangcode.error"));
            }
            if (defaultLanguage.get(s) == null) {
                throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
            }
            _temp.add(s);
        }
        if (_temp.size() < 2) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        header = _temp.toArray(new String[] {});
        // generate header
        String s = generateTbxHeader(header[0]);
        writeString(s);
        // generate body
        int readSize = 10;
        List<String[]> rs = null;
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 8);
        subMonitor.beginTask("", rowsNum / readSize == 0 ? 1 : rowsNum / readSize);
        while ((rs = reader.read(readSize)) != null) {
            for (String[] r : rs) {
                if (r == null || r.length < 2) {
                    continue;
                }
                int loopSize = r.length < header.length ? r.length : header.length;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < loopSize; i++) {
                    String value = r[i];
                    String lang = header[i];
                    sb.append("<langSet id=\"_" + (System.currentTimeMillis() + 1) + "\" xml:lang=\"" + lang + "\">\n");
                    sb.append("<tig>\n");
                    sb.append("<term>" + value + "</term>\n");
                    sb.append("</tig>\n");
                    sb.append("</langSet>\n");
                }
                if (sb.length() != 0) {
                    writeString("<termEntry id=\"_" + System.currentTimeMillis() + "\">\n");
                    writeString(sb.toString());
                    writeString("</termEntry>\n");
                }
                subMonitor.worked(1);
            }
            if (subMonitor.isCanceled()) {
                throw new OperationCanceledException();
            }
        }
        // generate end
        s = generateTbxEnd();
        if (s != null && s.length() != 0) {
            writeString(s);
        }
        subMonitor.done();
    } finally {
        out.close();
        monitor.done();
    }
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TxtRowReader(net.heartsome.cat.document.txt.TxtRowReader) IOException(java.io.IOException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) Language(net.heartsome.cat.common.locale.Language) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Language (net.heartsome.cat.common.locale.Language)1 TxtRowReader (net.heartsome.cat.document.txt.TxtRowReader)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1