Search in sources :

Example 1 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class XLFHandler method updateLanguages.

/**
	 * 设置指定 XLIFF 文件中指定 file 节点的语言代码信息(source-language、target-language 属性值)
	 * @param fileName
	 *            指定文件名
	 * @param XliffBeans
	 *            XliffBean 集合;
	 */
public void updateLanguages(String fileName, List<XliffBean> xliffBeans) {
    if (xliffBeans == null || xliffBeans.isEmpty()) {
        return;
    }
    VTDNav vn = vnMap.get(fileName);
    AutoPilot ap = new AutoPilot(vn);
    AutoPilot subAp = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        XMLModifier xm = new XMLModifier(vn);
        for (XliffBean bean : xliffBeans) {
            Set<String> originals = bean.getOriginals();
            for (String original : originals) {
                int index = vu.pilot(subAp, "/xliff/file[@original='" + original + "']");
                if (index != -1) {
                    xm = vu.update(ap, xm, "./@source-language", bean.getSourceLanguage(), VTDUtils.CREATE_IF_NOT_EXIST);
                    xm = vu.update(ap, xm, "./@target-language", bean.getTargetLanguage(), VTDUtils.CREATE_IF_NOT_EXIST);
                }
            }
        }
        saveAndReparse(xm, fileName);
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) ModifyException(com.ximpleware.ModifyException) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) VTDNav(com.ximpleware.VTDNav)

Example 2 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class PreMachineTranslationDialog method getTableViewerInput.

/**
	 * 从当前的数据库获取需要显示到界面上的数据
	 * @return ;
	 */
private String[][] getTableViewerInput() {
    String wPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
    Iterator<Entry<String, List<XliffBean>>> it = xliffInofs.entrySet().iterator();
    List<String[]> rows = new ArrayList<String[]>();
    int index = 1;
    while (it.hasNext()) {
        Entry<String, List<XliffBean>> entry = it.next();
        String filePath = entry.getKey();
        // 获取到项目为根的路径
        String iFilePath = filePath.replace(wPath, "");
        List<XliffBean> xliffBeans = entry.getValue();
        // for (int i = 0; i < xliffBeans.size(); i++) {
        XliffBean xliffBean = xliffBeans.get(0);
        String srcLang = xliffBean.getSourceLanguage();
        String tagLang = xliffBean.getTargetLanguage();
        String[] rowValue = new String[] { (index++) + "", iFilePath, srcLang, tagLang };
        rows.add(rowValue);
    // }
    }
    return rows.toArray(new String[][] {});
}
Also used : Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) Point(org.eclipse.swt.graphics.Point)

Example 3 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class PreTranslationDialog method getTableViewerInput.

/**
	 * 从当前的数据库获取需要显示到界面上的数据
	 * @return ;
	 */
private String[][] getTableViewerInput() {
    String wPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
    Iterator<Entry<String, List<XliffBean>>> it = xliffInofs.entrySet().iterator();
    List<String[]> rows = new ArrayList<String[]>();
    int index = 1;
    while (it.hasNext()) {
        Entry<String, List<XliffBean>> entry = it.next();
        String filePath = entry.getKey();
        // 获取到项目为根的路径
        String iFilePath = filePath.replace(wPath, "");
        List<XliffBean> xliffBeans = entry.getValue();
        // for (int i = 0; i < xliffBeans.size(); i++) {
        XliffBean xliffBean = xliffBeans.get(0);
        String srcLang = xliffBean.getSourceLanguage();
        String tagLang = xliffBean.getTargetLanguage();
        String[] rowValue = new String[] { (index++) + "", iFilePath, srcLang, tagLang };
        rows.add(rowValue);
    // }
    }
    return rows.toArray(new String[][] {});
}
Also used : Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) Point(org.eclipse.swt.graphics.Point)

Example 4 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class AbstractExportHandler method addXLFFile.

private void addXLFFile(IFile file) {
    if (!fileSet.contains(file.getFullPath())) {
        // 防止又选项目,又选文件夹,又选文件
        IPath path = file.getFullPath();
        if (path.segment(1).equals(XLF)) {
            // 必须存放在 XLIFF 中(should check??)
            if (file.getFileExtension() != null && CommonFunction.validXlfExtension(file.getFileExtension())) {
                fileSet.add(file.toString());
                XliffBean bean = getXlfBean(file);
                if (bean != null) {
                    config.addXlfBean(file.getProject(), bean);
                }
            }
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean)

Example 5 with XliffBean

use of net.heartsome.cat.ts.core.bean.XliffBean in project translationstudio8 by heartsome.

the class ExportConfig method transunitLooper.

private void transunitLooper(ExportWriter exper) {
    if (projects == null) {
        return;
    }
    final int steps = 100;
    int totalFile = mapSaveAs.size();
    String baseXpath1 = "/xliff/file";
    String baseXpath = "./body/descendant::trans-unit";
    int index = 0;
    for (IProject project : projects) {
        inner: for (XliffBean bean : mapXlfBeans.get(project)) {
            String saveAs = mapSaveAs.get(bean);
            if (!setSaveAs.contains(saveAs)) {
                return;
            }
            String xlfFileName = bean.getXliffFile().substring(bean.getXliffFile().lastIndexOf(File.separator) + 1);
            IProgressMonitor subMonitor = null;
            // 进度条信息
            if (monitor != null) {
                monitor.beginTask("[" + ++index + "/" + totalFile + "]", 1);
            }
            try {
                exper.init(project, bean);
            } catch (OperationCanceledException e) {
                continue;
            } catch (ExportExternalException e) {
                LOGGER.error("", e);
                openErrorDialog(e);
                return;
            } catch (ExportCanceledException e) {
                LOGGER.error("", e);
                openErrorDialog(e);
                continue;
            }
            VTDUtils vu = new VTDUtils();
            try {
                vu.parseFile(bean.getXliffFile(), true);
            } catch (Exception e) {
                if (openErrorDialog(MessageFormat.format(Messages.getString("ExportDocxDialog.ok.parseError"), xlfFileName))) {
                    continue;
                }
            }
            try {
                VTDNav vn = vu.getVTDNav();
                AutoPilot apFile = new AutoPilot(vn);
                // 总控制
                AutoPilot ap = new AutoPilot(vn);
                // 临时用
                AutoPilot _ap = new AutoPilot(vn);
                apFile.declareXPathNameSpace("hs", ExportExternal.NAMESPACE_HS);
                ap.declareXPathNameSpace("hs", ExportExternal.NAMESPACE_HS);
                _ap.declareXPathNameSpace("hs", ExportExternal.NAMESPACE_HS);
                int page = 0;
                double total = 0;
                int worked = 0;
                int count = 0;
                int tmp = 0;
                if (monitor != null) {
                    _ap.selectXPath("count(/xliff/file/body/descendant::trans-unit)");
                    total = _ap.evalXPathToNumber();
                    if (total == 0) {
                        exper.canceled();
                        continue;
                    }
                    subMonitor = new SubProgressMonitor(monitor, 1);
                    subMonitor.setTaskName("[" + index + "/" + totalFile + "]" + Messages.getString("ExportDocxDialog.ok.monitor.msg0") + xlfFileName);
                    subMonitor.beginTask("subtask", (total > steps ? steps : (int) total) + 1);
                }
                apFile.selectXPath(baseXpath1);
                while (apFile.evalXPath() != -1) {
                    page++;
                    ap.selectXPath(baseXpath);
                    while (ap.evalXPath() != -1) {
                        // monitor
                        if (subMonitor != null) {
                            if (subMonitor.isCanceled()) {
                                exper.canceled();
                                return;
                            }
                            tmp = (int) ((count++ / total) * steps);
                            if (tmp > worked) {
                                subMonitor.worked(tmp - worked);
                                worked = tmp;
                            }
                        }
                        if (filterAllTrans) {
                            // continue if bingo (排除此项)
                            if (exceptLocked && testXpath(_ap, "./@translate='no'")) {
                                continue;
                            }
                            // should we use machine translation??
                            if (exceptFullMatch && testXpath(_ap, "./target/@hs:matchType and ./target/@hs:quality='100'")) {
                                continue;
                            }
                            // should we except machine translation ??
                            if (exceptContextMatch && testXpath(_ap, "./target/@hs:matchType and ./target/@hs:quality='101'")) {
                                continue;
                            }
                        } else if (filterSpecial) {
                            // just contains <note> or state='need-review'
                            boolean bingo = false;
                            if (withNote) {
                                bingo = testXpath(_ap, "./note");
                            }
                            if (!bingo && withNeedReview) {
                                bingo = testXpath(_ap, "./@hs:needs-review='yes'");
                            }
                            if (!bingo) {
                                continue;
                            }
                        } else if (filterHasState) {
                            // when tu not/new/has translated, or approved
                            boolean bingo = false;
                            if (!bingo && noTrans) {
                                bingo = testXpath(_ap, "string-length(./target/text()) < 1") && !testXpath(_ap, "./target/*");
                            }
                            if (!bingo && newTrans) {
                                bingo = testXpath(_ap, "./target/@state='new'");
                            }
                            if (!bingo && transed) {
                                bingo = testXpath(_ap, "./target/@state='translated'") && !testXpath(_ap, "./@approved='yes'");
                            }
                            if (!bingo && approved) {
                                bingo = testXpath(_ap, "./@approved='yes'");
                            }
                            if (!bingo) {
                                continue;
                            }
                        }
                        try {
                            exper.append(vu, count, page);
                        } catch (ExportExternalException e) {
                            openErrorDialog(e);
                            continue inner;
                        }
                    }
                }
                try {
                    exper.flush();
                } catch (ExportExternalException e) {
                    openErrorDialog(e);
                    continue;
                }
                if (subMonitor != null) {
                    subMonitor.done();
                }
            } catch (Exception e) {
                e.printStackTrace();
                openErrorDialog(e);
                return;
            }
        }
    }
    exper.close();
    if (monitor != null) {
        monitor.done();
    }
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IProject(org.eclipse.core.resources.IProject) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) XPathParseException(com.ximpleware.XPathParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XliffBean(net.heartsome.cat.ts.core.bean.XliffBean) VTDNav(com.ximpleware.VTDNav)

Aggregations

XliffBean (net.heartsome.cat.ts.core.bean.XliffBean)9 ArrayList (java.util.ArrayList)5 List (java.util.List)5 XPathParseException (com.ximpleware.XPathParseException)4 AutoPilot (com.ximpleware.AutoPilot)3 VTDNav (com.ximpleware.VTDNav)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)3 IProject (org.eclipse.core.resources.IProject)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 NavException (com.ximpleware.NavException)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 LinkedHashSet (java.util.LinkedHashSet)2 Entry (java.util.Map.Entry)2 Point (org.eclipse.swt.graphics.Point)2