use of net.heartsome.cat.document.TmxReader 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");
}
}
}
use of net.heartsome.cat.document.TmxReader in project translationstudio8 by heartsome.
the class Tmx2xlsx method doCovnerter.
public void doCovnerter(String tmxFile, File targetFile, IProgressMonitor monitor) throws Exception {
writer = new Excel2007Writer(targetFile, size);
try {
tmxReader = new TmxReader(new File(tmxFile));
} catch (TmxReadException e) {
LOGGER.error("", e);
throw new Exception(Messages.getString("tmxdata.TmxFileContainer.parseTmxFileError"));
}
TmxTU tuTemp = null;
List<TmxTU> tus = new ArrayList<TmxTU>(100);
int total = tmxReader.getTotalTu() / size;
monitor.beginTask("", total == 0 ? 1 : total);
try {
while (true) {
tuTemp = tmxReader.read().getTu();
if (null == tuTemp) {
break;
}
if (tus.size() < size) {
tus.add(tuTemp);
continue;
}
try {
if (monitor.isCanceled()) {
return;
}
writer.witerTmxTU(tus, false);
monitor.worked(1);
tus.clear();
} catch (IOException e) {
LOGGER.error("写入文件出错", e);
throw new Exception(Messages.getString("converter.tmx2xlsx.writeTmx.error"));
}
}
if (!tus.isEmpty()) {
try {
if (monitor.isCanceled()) {
return;
}
writer.witerTmxTU(tus, false);
monitor.done();
tus.clear();
} catch (IOException e) {
LOGGER.error("写入文件出错", e);
throw new Exception(Messages.getString("converter.tmx2xlsx.writeTmx.error"));
}
}
} catch (Exception e) {
throw e;
} finally {
writer.outZip();
monitor.done();
}
}
Aggregations