Search in sources :

Example 11 with NavException

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;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) XMLModifier(com.ximpleware.XMLModifier) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) TranscodeException(com.ximpleware.TranscodeException) XPathParseException(com.ximpleware.XPathParseException) PreTranslationCounter(net.heartsome.cat.ts.pretranslation.bean.PreTranslationCounter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) VTDNav(com.ximpleware.VTDNav) BufferedOutputStream(java.io.BufferedOutputStream)

Example 12 with NavException

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) {
        }
    });
}
Also used : NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FileDialog(org.eclipse.swt.widgets.FileDialog) VTDNav(com.ximpleware.VTDNav) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 13 with NavException

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));
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) Language(net.heartsome.cat.common.locale.Language) ProjectConfiger(net.heartsome.cat.ts.core.file.ProjectConfiger) LanguageLabelProvider(net.heartsome.cat.ts.ui.composite.LanguageLabelProvider) ArrayList(java.util.ArrayList) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException)

Example 14 with NavException

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;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Example 15 with NavException

use of com.ximpleware.NavException in project translationstudio8 by heartsome.

the class XliffUtil method getOriginalFiles.

/**
	 * @param vu
	 *            XLIFF 文件对应的 VTDUtils
	 * @return List&lt;String&gt;
	 */
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;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Aggregations

NavException (com.ximpleware.NavException)93 AutoPilot (com.ximpleware.AutoPilot)70 XPathParseException (com.ximpleware.XPathParseException)70 XPathEvalException (com.ximpleware.XPathEvalException)67 VTDNav (com.ximpleware.VTDNav)63 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)58 XMLModifier (com.ximpleware.XMLModifier)31 ModifyException (com.ximpleware.ModifyException)29 ArrayList (java.util.ArrayList)22 IOException (java.io.IOException)21 VTDGen (com.ximpleware.VTDGen)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)13 TranscodeException (com.ximpleware.TranscodeException)12 ParseException (com.ximpleware.ParseException)11 FileOutputStream (java.io.FileOutputStream)11 List (java.util.List)10 LinkedList (java.util.LinkedList)9 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8