use of net.heartsome.cat.converter.Converter in project translationstudio8 by heartsome.
the class ConverterViewModel method convert.
/**
* 正向转换
* @param sourceItem
* @param monitor
* @return ;
*/
private Map<String, String> convert(final IConversionItem sourceItem, IProgressMonitor monitor) throws ConverterException {
Map<String, String> result = null;
boolean convertFlg = false;
String xliffDir = ConverterUtil.toLocalPath(configBean.getXliffDir());
String targetFile = ConverterUtil.toLocalPath(configBean.getTarget());
String skeletonFile = ConverterUtil.toLocalPath(configBean.getSkeleton());
// 转换前的准备
ConverterContext converterContext = new ConverterContext(configBean);
final Map<String, String> configuration = converterContext.getConvertConfiguration();
// 转换前,生成临时的XLIFF文件,用此文件生成指定目标语言的XLIFF文件
File targetTempFile = null;
try {
targetTempFile = File.createTempFile("tempxlf", "xlf");
} catch (IOException e) {
LOGGER.error(Messages.getString("model.ConverterViewModel.msg10"), e);
}
configuration.put(Converter.ATTR_XLIFF_FILE, targetTempFile.getAbsolutePath());
if (configBean.getFileType().equals(FileFormatUtils.MS)) {
IPreferenceStore ps = net.heartsome.cat.ts.ui.Activator.getDefault().getPreferenceStore();
String path = ps.getString(ITranslationPreferenceConstants.PATH_OF_OPENOFFICE);
String port = ps.getString(ITranslationPreferenceConstants.PORT_OF_OPENOFFICE);
configuration.put("ooPath", path);
configuration.put("ooPort", port);
}
// 创建skeleton文件
File skeleton = new File(skeletonFile);
if (!skeleton.exists()) {
try {
File parent = skeleton.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
skeleton.createNewFile();
} catch (IOException e) {
String message = MessageFormat.format(Messages.getString("model.ConverterViewModel.msg11"), skeletonFile);
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterException(status);
}
}
try {
// 执行转换
Converter converter = getConverter();
if (converter == null) {
// Build a message
String message = Messages.getString("model.ConverterViewModel.msg2") + configBean.getFileType();
// Build a new IStatus
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
throw new ConverterException(status);
}
result = converter.convert(configuration, monitor);
final String alert = result.get("ttx2xlfAlert39238409230481092830");
if (result.containsKey("ttx2xlfAlert39238409230481092830")) {
//ttx 转 xlf 时,提示含有未预翻译,不推荐,但没办法。
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), Messages.getString("handler.PreviewTranslationHandler.msgTitle"), alert);
}
});
}
// 处理骨架文件,将骨架文件路径修改为项目相对路径,此路径写入external-file节点的href属性
String projectPath = sourceItem.getProject().getLocation().toOSString();
String sklPath = skeletonFile.replace(projectPath, "");
// 处理目标语言, 创建多个目标语言的文件
List<Language> tgtLang = configBean.getHasSelTgtLangList();
if (tgtLang != null && tgtLang.size() > 0) {
// 解析XLIFF文件
File f = new File(targetTempFile.getAbsolutePath());
FileInputStream is = null;
byte[] b = new byte[(int) f.length()];
try {
is = new FileInputStream(f);
is.read(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
VTDGen vg = new VTDGen();
vg.setDoc(b);
try {
vg.parse(true);
} catch (VTDException e) {
String message = Messages.getString("model.ConverterViewModel.msg12");
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterException(status);
}
VTDNav vn = vg.getNav();
VTDUtils vu = new VTDUtils();
// 生成多个XLIFF文件,只是修改目标语言和骨架文件路径
for (Language lang : tgtLang) {
// 修复 bug 2949 ,当文件名中出现 XLIFF 时,文件名获取失败,下面注释代码为之前的代码。 --robert 2013-04-01
// String[] pathArray = targetFile.split(Constant.FOLDER_XLIFF);
// StringBuffer xlffPath = new StringBuffer(pathArray[0]);
// xlffPath.append(Constant.FOLDER_XLIFF).append(File.separator).append(lang.getCode())
// .append(pathArray[1]);
String fileName = targetFile.substring(xliffDir.length());
StringBuffer xlfPahtSB = new StringBuffer();
xlfPahtSB.append(xliffDir);
xlfPahtSB.append(File.separator);
xlfPahtSB.append(lang.getCode());
xlfPahtSB.append(fileName);
File tmpFile = new File(xlfPahtSB.toString());
generateTgtFileList.add(tmpFile);
if (!tmpFile.exists()) {
File parent = tmpFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try {
tmpFile.createNewFile();
} catch (IOException e) {
String message = Messages.getString("model.ConverterViewModel.msg13");
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterException(status);
}
}
try {
vu.bind(vn.duplicateNav());
} catch (NavException e) {
LOGGER.error("", e);
}
XMLModifier xm = vu.update("/xliff/file/@target-language", lang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST);
xm = vu.update(null, xm, "/xliff/file/header/skl/external-file/@href", TextUtil.cleanString(sklPath));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(tmpFile);
// 写入文件
xm.output(fos);
} catch (Exception e) {
String message = Messages.getString("model.ConverterViewModel.msg13");
LOGGER.error(message, e);
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message + "\n" + e.getMessage());
throw new ConverterNotFoundException(status);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
}
}
vg.clear();
}
convertFlg = true;
} catch (OperationCanceledException e) {
LOGGER.info("ConverterViewerModel: 取消转换");
} finally {
if (!convertFlg) {
for (File f : generateTgtFileList) {
if (f != null && f.exists()) {
f.delete();
}
}
if (skeleton != null && skeleton.exists()) {
skeleton.delete();
}
}
targetTempFile.delete();
sourceItem.refresh();
}
return result;
}
use of net.heartsome.cat.converter.Converter in project translationstudio8 by heartsome.
the class ConverterViewModel method reverseConvert.
/**
* 逆向转换
* @param sourceItem
* @param monitor
* @return ;
*/
private Map<String, String> reverseConvert(IConversionItem sourceItem, IProgressMonitor monitor) throws ConverterException {
Map<String, String> result = null;
String sourcePathStr = configBean.getSource();
String targetFile = ConverterUtil.toLocalPath(configBean.getTarget());
boolean convertSuccessful = false;
File target = null;
try {
target = new File(targetFile);
if (!target.exists()) {
try {
File parent = target.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
target.createNewFile();
} catch (IOException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("model.ConverterViewModel.logger4");
Object[] args = { targetFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
}
}
Converter converter = getConverter();
if (converter == null) {
// Build a message
String message = Messages.getString("model.ConverterViewModel.msg3") + configBean.getFileType();
// Build a new IStatus
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
throw new ConverterNotFoundException(status);
}
ConverterContext converterContext = new ConverterContext(configBean);
final Map<String, String> configuration = converterContext.getReverseConvertConfiguraion();
if (configBean.getFileType().equals("x-msoffice2003")) {
IPreferenceStore ps = net.heartsome.cat.ts.ui.Activator.getDefault().getPreferenceStore();
String path = ps.getString(ITranslationPreferenceConstants.PATH_OF_OPENOFFICE);
String port = ps.getString(ITranslationPreferenceConstants.PORT_OF_OPENOFFICE);
configuration.put("ooPath", path);
configuration.put("ooPort", port);
}
result = converter.convert(configuration, monitor);
convertSuccessful = true;
} catch (OperationCanceledException e) {
// 捕获用户取消操作的异常
LOGGER.info(Messages.getString("model.ConverterViewModel.logger2"), e);
throw e;
} catch (ConverterNotFoundException e) {
// Let the StatusManager handle the Status and provide a hint
StatusManager.getManager().handle(e.getStatus(), StatusManager.LOG | StatusManager.SHOW);
if (LOGGER.isErrorEnabled()) {
LOGGER.error(e.getMessage(), e);
}
throw e;
} catch (ConverterException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error(MessageFormat.format(Messages.getString("model.ConverterViewModel.logger3"), sourcePathStr), e);
}
throw e;
} finally {
if (!convertSuccessful) {
// 在转换失败或用户取消转换时,清除目标文件和骨架文件
if (target != null && target.exists()) {
target.delete();
}
}
sourceItem.refresh();
}
return result;
}
use of net.heartsome.cat.converter.Converter in project translationstudio8 by heartsome.
the class Activator method start.
/**
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
* @param context
* @throws Exception
*/
public void start(BundleContext context) throws Exception {
plugin = this;
bundleContext = context;
// tracker the open office converter service
String positiveFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, OpenOffice2Xliff.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_POSITIVE)).toString();
positiveTracker = new ServiceTracker(context, context.createFilter(positiveFilter), new OpenOfficePositiveCustomizer());
positiveTracker.open();
String reverseFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, Xliff2OpenOffice.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_REVERSE)).toString();
reverseTracker = new ServiceTracker(context, context.createFilter(reverseFilter), new OpenOfficeReverseCustomize());
reverseTracker.open();
}
use of net.heartsome.cat.converter.Converter in project translationstudio8 by heartsome.
the class Activator method start.
/**
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
* @param context
* @throws Exception
*/
public void start(BundleContext context) throws Exception {
plugin = this;
// register the converter services
Converter rc2Xliff = new Rc2Xliff();
Properties properties = new Properties();
properties.put(Converter.ATTR_NAME, Rc2Xliff.NAME_VALUE);
properties.put(Converter.ATTR_TYPE, Rc2Xliff.TYPE_VALUE);
properties.put(Converter.ATTR_TYPE_NAME, Rc2Xliff.TYPE_NAME_VALUE);
rc2XliffSR = ConverterRegister.registerPositiveConverter(context, rc2Xliff, properties);
Converter xliff2Rc = new Xliff2Rc();
properties = new Properties();
properties.put(Converter.ATTR_NAME, Xliff2Rc.NAME_VALUE);
properties.put(Converter.ATTR_TYPE, Xliff2Rc.TYPE_VALUE);
properties.put(Converter.ATTR_TYPE_NAME, Xliff2Rc.TYPE_NAME_VALUE);
xliff2RcSR = ConverterRegister.registerReverseConverter(context, xliff2Rc, properties);
}
use of net.heartsome.cat.converter.Converter in project translationstudio8 by heartsome.
the class Activator method start.
/**
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
* @param context
* @throws Exception
*/
public void start(BundleContext context) throws Exception {
plugin = this;
bundleContext = context;
// tracker the xml converter service
String positiveFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, Xml2Xliff.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_POSITIVE)).toString();
positiveTracker = new ServiceTracker(context, context.createFilter(positiveFilter), new XmlPositiveCustomizer());
positiveTracker.open();
String reverseFilter = new AndFilter(new EqFilter(Constants.OBJECTCLASS, Converter.class.getName()), new EqFilter(Converter.ATTR_TYPE, Xliff2Xml.TYPE_VALUE), new EqFilter(Converter.ATTR_DIRECTION, Converter.DIRECTION_REVERSE)).toString();
reverseTracker = new ServiceTracker(context, context.createFilter(reverseFilter), new XmlReverseCustomize());
reverseTracker.open();
}
Aggregations