use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class PreTranslation method executeTranslation.
/**
* 根据构建参数执行预翻译 ;
* @throws InterruptedException
*/
public List<PreTranslationCounter> executeTranslation(IProgressMonitor monitor) throws InterruptedException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask("", this.xlfFiles.size());
monitor.setTaskName(Messages.getString("pretranslation.PreTranslation.task1"));
try {
for (String xlfPath : xlfFiles) {
if (monitor != null && monitor.isCanceled()) {
throw new InterruptedException();
}
currentCounter = new PreTranslationCounter(xlfPath);
this.transCounters.add(currentCounter);
VTDNav vn = xlfHandler.getVnMap().get(xlfPath);
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vu.getVTDNav());
int tuNumber = xlfHandler.getNodeCount(xlfPath, "/xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]");
currentCounter.setTuNumber(tuNumber);
ap.selectXPath("/xliff/file");
String srcLang = "";
String tgtLang = "";
XMLModifier xm = new XMLModifier(vn);
IProgressMonitor monitor2 = new SubProgressMonitor(monitor, 1);
monitor2.beginTask(Messages.getString("pretranslation.PreTranslation.task2"), tuNumber);
while (ap.evalXPath() != -1) {
// 循环 file 节点
String _srcLang = vu.getCurrentElementAttribut("source-language", "");
String _tgtLang = vu.getCurrentElementAttribut("target-language", "");
if (!_srcLang.equals("")) {
srcLang = _srcLang;
}
if (!_tgtLang.equals("")) {
tgtLang = _tgtLang;
}
if (srcLang.equals("") || tgtLang.equals("")) {
continue;
}
if (updateStrategy == PreTransParameters.KEEP_OLD_TARGET) {
keepCurrentMatchs(vu, _srcLang, _tgtLang, xm, monitor2);
} else if (updateStrategy == PreTransParameters.KEEP_BEST_MATCH_TARGET) {
keepHigherMatchs(vu, _srcLang, _tgtLang, xm, monitor2);
} else if (updateStrategy == PreTransParameters.KEEP_NEW_TARGET) {
overwriteMatchs(vu, srcLang, tgtLang, xm, monitor2);
}
}
monitor2.done();
FileOutputStream fos = new FileOutputStream(xlfPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入文件
xm.output(bos);
bos.close();
fos.close();
}
} catch (XPathParseException e) {
logger.error("", e);
e.printStackTrace();
} catch (NavException e) {
logger.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
logger.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
logger.error("", e);
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
logger.error("", e);
e.printStackTrace();
} catch (FileNotFoundException e) {
logger.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
logger.error("", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("", e);
e.printStackTrace();
}
monitor.done();
return this.transCounters;
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class ColumnTypeDialog method initListener.
/**
* 初始化加载配置和保存配置按钮的监听 ;
*/
private void initListener() {
btnLoadConfiguration.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
fd.setText(Messages.getString("dialog.ColumnTypeDialog.fdTitle"));
//$NON-NLS-1$ //$NON-NLS-2$
String[] extensions = { "*.ctc", "*" };
String[] names = { Messages.getString("dialog.ColumnTypeDialog.filterName1"), //$NON-NLS-1$ //$NON-NLS-2$
Messages.getString("dialog.ColumnTypeDialog.filterName2") };
fd.setFilterExtensions(extensions);
fd.setFilterNames(names);
fd.setFilterPath(System.getProperty("user.home"));
String name = fd.open();
if (name == null) {
return;
}
try {
VTDGen vg = new VTDGen();
if (vg.parseFile(name, true)) {
VTDNav vn = vg.getNav();
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/CSV2TBX-configuration");
ap.evalXPath();
int count = vu.getChildElementsCount();
if (count != arrCmbLangs.length) {
MessageDialog.openInformation(getShell(), Messages.getString("dialog.ColumnTypeDialog.msgTitle"), Messages.getString("dialog.ColumnTypeDialog.msg1"));
return;
}
String xpath = "/CSV2TBX-configuration/item";
ap.selectXPath(xpath);
int i = 0;
while (ap.evalXPath() != -1) {
String propLevel = vu.getCurrentElementAttribut("propLevel", "");
//$NON-NLS-1$ //$NON-NLS-2$
String propType = vu.getCurrentElementAttribut("propType", "");
//$NON-NLS-1$ //$NON-NLS-2$
String lang = vu.getCurrentElementAttribut("propLang", "");
String propName = vu.getCurrentElementAttribut("propName", "");
arrCmbPropsLevel[i].setItems(levelValues);
arrCmbPropsLevel[i].select(0);
if (!propLevel.equals("")) {
//$NON-NLS-1$
arrCmbPropsLevel[i].setText(propLevel);
if (propLevel.equals(ColProperties.conceptLevel)) {
arrCmbLangs[i].setEnabled(false);
arrCmbPropsName[i].setItems(conceptPropValues);
arrCmbPropsName[i].select(0);
arrCmbPropsType[i].setItems(conceptPropTypeValues);
arrCmbPropsType[i].select(0);
}
if (propLevel.equals(ColProperties.langLevel)) {
arrCmbLangs[i].setEnabled(true);
arrCmbPropsName[i].setItems(TranslationPropValues);
arrCmbPropsName[i].select(0);
arrCmbPropsType[i].setItems(termDescripPropTypeValues);
arrCmbPropsType[i].select(0);
}
}
// Update content for Prop Name combo
if (!propName.equals("")) {
//$NON-NLS-1$
arrCmbPropsName[i].setText(propName);
}
if (!propLevel.equals("")) {
//$NON-NLS-1$
if (propLevel.equals(ColProperties.conceptLevel)) {
arrCmbPropsType[i].setEnabled(propName.equals(ColProperties.descripName));
arrCmbPropsType[i].setItems(conceptPropTypeValues);
arrCmbPropsType[i].select(0);
}
if (propLevel.equals(ColProperties.langLevel)) {
arrCmbPropsType[i].setEnabled(!propName.equals(ColProperties.termName));
if (propName.equals(ColProperties.descripName)) {
arrCmbPropsType[i].setItems(termDescripPropTypeValues);
} else {
arrCmbPropsType[i].setItems(termTermNotePropTypeValues);
}
arrCmbPropsType[i].select(0);
}
}
// Update content for Prop Type combo
if (!propType.equals("")) {
//$NON-NLS-1$
arrCmbPropsType[i].setText(propType);
}
// Update content for Language Combo
arrCmbLangs[i].setItems(LocaleService.getLanguages());
arrCmbLangs[i].select(0);
if (!lang.equals("")) {
//$NON-NLS-1$
arrCmbLangs[i].setText(lang);
}
i++;
}
}
} catch (XPathParseException e) {
LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger1"), e);
} catch (NavException e) {
LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger1"), e);
} catch (XPathEvalException e) {
LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger1"), e);
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
btnSaveConfiguration.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
fd.setText(Messages.getString("dialog.ColumnTypeDialog.savefdTitle"));
//$NON-NLS-1$ //$NON-NLS-2$
String[] extensions = { "*.ctc", "*.*" };
String[] names = { Messages.getString("dialog.ColumnTypeDialog.filterName1"), //$NON-NLS-1$ //$NON-NLS-2$
Messages.getString("dialog.ColumnTypeDialog.filterName2") };
fd.setFilterExtensions(extensions);
fd.setFilterNames(names);
fd.setFilterPath(System.getProperty("user.home"));
String name = fd.open();
if (name == null) {
return;
}
try {
FileOutputStream output = new FileOutputStream(name);
//$NON-NLS-1$ //$NON-NLS-2$
output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes("UTF-8"));
//$NON-NLS-1$ //$NON-NLS-2$
output.write("<CSV2TBX-configuration>\n".getBytes("UTF-8"));
for (int i = 0; i < arrCmbLangs.length; i++) {
String strItem = //$NON-NLS-1$
"<item propLang=\"" + arrCmbLangs[i].getText() + "\" propName=\"" + //$NON-NLS-1$
arrCmbPropsName[i].getText() + "\" propType=\"" + //$NON-NLS-1$
arrCmbPropsType[i].getText() + "\" propLevel=\"" + //$NON-NLS-1$
arrCmbPropsLevel[i].getText() + //$NON-NLS-1$
"\"/>\n";
//$NON-NLS-1$
output.write(strItem.getBytes("UTF-8"));
}
//$NON-NLS-1$ //$NON-NLS-2$
output.write("</CSV2TBX-configuration>\n".getBytes("UTF-8"));
output.close();
} catch (FileNotFoundException e1) {
LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger2"), e);
} catch (UnsupportedEncodingException e1) {
LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger2"), e);
} catch (IOException e1) {
LOGGER.error(Messages.getString("dialog.ColumnTypeDialog.logger2"), e);
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class AddTermToTBDialog method initProperty.
/**
* 初始化各个控件的值 ;
*/
public void initProperty() {
if (srcText != null && !srcText.equals("")) {
txtSrc.setText(srcText);
txtSrc.setSelection(0, srcText.length());
txtSrc.setFocus();
}
if (tgtText != null && !tgtText.equals("")) {
txtTgt.setText(tgtText);
txtTgt.setSelection(0, tgtText.length());
txtTgt.setFocus();
}
if (getPropertyValue() != null) {
txtProperty.setText(getPropertyValue());
}
ProjectConfiger projectConfig = ProjectConfigerFactory.getProjectConfiger(project);
List<Language> rsLstSrcLangs = new ArrayList<Language>();
List<Language> rsLstTgtLangs = null;
try {
rsLstSrcLangs.add(projectConfig.getSourceLanguage());
rsLstTgtLangs = projectConfig.getTargetlanguages();
} catch (XPathParseException e) {
LOGGER.error(Messages.getString("dialog.AddTermToTBDialog.logger"), e);
} catch (NavException e) {
LOGGER.error(Messages.getString("dialog.AddTermToTBDialog.logger"), e);
} catch (XPathEvalException e) {
LOGGER.error(Messages.getString("dialog.AddTermToTBDialog.logger"), e);
}
ArrayList<String> lstTgtLangs = new ArrayList<String>();
if (rsLstTgtLangs != null) {
for (Language lang : rsLstTgtLangs) {
lstTgtLangs.add(lang.getCode());
}
}
cmbSrcLang.setLabelProvider(new LanguageLabelProvider());
cmbSrcLang.setInput(rsLstSrcLangs);
cmbSrcLang.getTableCombo().select(0);
cmbTgtLang.setLabelProvider(new LanguageLabelProvider());
cmbTgtLang.setInput(rsLstTgtLangs);
if (tgtLang != null) {
cmbTgtLang.getTableCombo().select(lstTgtLangs.indexOf(tgtLang));
}
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class XliffUtil method getLangPairOfFile.
/**
* @param xlfFile
* @param filePath
* @return String[] 第一个元素为源语言,第二个为目标语言
*/
public static String[] getLangPairOfFile(String xlfFile, String filePath) {
VTDUtils vu = getVU(xlfFile);
String xpath = "/xliff/file[@original=\"" + filePath + "\"]";
String[] langPair = new String[2];
try {
langPair[0] = vu.getElementAttribute(xpath, "source-language");
langPair[1] = vu.getElementAttribute(xpath, "target-language");
} catch (XPathParseException e) {
e.printStackTrace();
} catch (XPathEvalException e) {
e.printStackTrace();
} catch (NavException e) {
e.printStackTrace();
}
return langPair;
}
use of com.ximpleware.NavException in project translationstudio8 by heartsome.
the class XliffUtil method getOriginalFiles.
/**
* @param vu
* XLIFF 文件对应的 VTDUtils
* @return List<String>
*/
public static List<String> getOriginalFiles(VTDUtils vu) {
ArrayList<String> list = new ArrayList<String>();
String xpath = "/xliff/file";
AutoPilot ap = new AutoPilot(vu.getVTDNav());
try {
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
list.add(vu.getElementAttribute(".", "original"));
}
} catch (XPathParseException e) {
e.printStackTrace();
} catch (XPathEvalException e) {
e.printStackTrace();
} catch (NavException e) {
e.printStackTrace();
}
return list;
}
Aggregations