Search in sources :

Example 96 with NullProgressMonitor

use of org.eclipse.core.runtime.NullProgressMonitor in project translationstudio8 by heartsome.

the class XliffReader method readXliffFile.

/**
	 * After parse file, this method read the xliff file from the parse info
	 * @param xlfOs
	 *            the xliff file out put stream
	 * @param iniFile
	 *            the configure file of mif,witch define the ESC
	 * @param isReadMasterPage
	 *            xliff contain the masterpage or not
	 * @param monitor
	 *            the progress monitor
	 * @throws MifParseException
	 * @throws IOException
	 *             ;
	 * @throws UnSuportedFileExcetption
	 */
public void readXliffFile(BufferedWriter xlfOs, String iniFile, boolean isReadMasterPage, IProgressMonitor monitor) throws MifParseException, IOException, UnSuportedFileExcetption {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    rUtil = new ReaderUtil(iniFile);
    File xf = File.createTempFile("xliftemp", ".temp");
    // first read the index
    List<Marker> markers = mpbf.getMarkers();
    if (markers.size() > 0) {
        File f = File.createTempFile("miftemp", ".tmp");
        BufferedWriter tmpWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), mifEncoding));
        try {
            this.xlfOs = new BufferedWriter(new FileWriter(xf));
            // generate the xliff content to temporary file xf
            for (Marker m : markers) {
                outputSegmenet(rUtil.cleanString(m.getContent()), m.getOffset(), m.getEndOffset(), 1);
            }
            // generate temporary file ,the file had extracted index content and contains '%%%index%%%'
            readSkeletonFile(tmpWriter, null);
        } finally {
            if (tmpWriter != null) {
                tmpWriter.close();
            }
            if (this.xlfOs != null) {
                this.xlfOs.close();
            }
        }
        // reload the temporary file
        loadFile(f.getAbsolutePath(), mifEncoding);
        // after reload delete the temporary file
        f.delete();
    }
    // second read the content
    this.xlfOs = xlfOs;
    List<Page> pages = mpbf.getPages();
    monitor.setTaskName(Messages.getString("mif.Mif2Xliff.task3"));
    if (pages.size() == 0) {
        // no pages specified ,direct read the Para statement
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task3"), 1);
        monitor.worked(1);
        readPara();
    } else {
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task3"), pages.size());
        for (Page page : pages) {
            String pageType = page.getPageType().toLowerCase();
            if (pageType.equals("bodypage")) {
                readPage(page);
            } else if (pageType.indexOf("masterpage") != -1 && isReadMasterPage) {
                readPage(page);
            }
            monitor.worked(1);
        }
    }
    BufferedReader r = null;
    try {
        r = new BufferedReader(new FileReader(xf));
        String line = r.readLine();
        while (line != null) {
            this.xlfOs.write(line + "\n");
            line = r.readLine();
        }
    } finally {
        if (r != null) {
            r.close();
        }
        xf.delete();
    }
    monitor.done();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) FileWriter(java.io.FileWriter) ReaderUtil(net.heartsome.cat.converter.mif.common.ReaderUtil) Page(net.heartsome.cat.converter.mif.bean.Page) Marker(net.heartsome.cat.converter.mif.bean.Marker) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) FileReader(java.io.FileReader) File(java.io.File)

Example 97 with NullProgressMonitor

use of org.eclipse.core.runtime.NullProgressMonitor in project translationstudio8 by heartsome.

the class XliffReader method readSkeletonFile.

/**
	 * After parse file, this method read skeleton file from the parse info
	 * @param sklOs
	 *            The skeleton file out put stream
	 * @param monitor
	 *            the progress monitor
	 * @throws IOException
	 *             ;
	 */
public void readSkeletonFile(BufferedWriter sklOs, IProgressMonitor monitor) throws IOException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    List<Object[]> cbfs = mrbf.getBuffer(new Comparator<Object[]>() {

        public int compare(Object[] o1, Object[] o2) {
            Integer v1 = (Integer) o1[1];
            int v2 = (Integer) o2[1];
            return v1.compareTo(v2);
        }
    });
    int off = 0;
    int len = doc.length;
    monitor.setTaskName(Messages.getString("mif.Mif2Xliff.task4"));
    if (cbfs.size() == 0) {
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task4"), 1);
        sklOs.write(doc, off, len);
    } else {
        monitor.beginTask(Messages.getString("mif.Mif2Xliff.task4"), cbfs.size() + 1);
        for (Object[] obj : cbfs) {
            int index = (Integer) obj[0];
            int start = (Integer) obj[1];
            int end = (Integer) obj[2];
            int segNum = (Integer) obj[3];
            sklOs.write(doc, off, start - off);
            off = end;
            sklOs.write("%%%" + index + "%%%");
            while (segNum > 1) {
                sklOs.write("\n%%%" + ++index + "%%%");
                segNum--;
            }
            monitor.worked(1);
        }
        sklOs.write(new String(doc, off, len - off).trim());
        monitor.worked(1);
    }
    monitor.done();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor)

Example 98 with NullProgressMonitor

use of org.eclipse.core.runtime.NullProgressMonitor in project translationstudio8 by heartsome.

the class NewProjectWizard method createProject.

private void createProject(IProject project, IProgressMonitor monitor) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    monitor.beginTask("", 3);
    monitor.setTaskName(Messages.getString("wizard.NewProjectWizard.task1"));
    try {
        project.create(null);
        monitor.worked(1);
        if (!project.isOpen()) {
            project.open(null);
        }
        monitor.worked(1);
        // 创建项目所需要的文件目录
        String[] folderNames = { Constant.FOLDER_INTERMEDDIATE, Constant.FOLDER_SRC, Constant.FOLDER_TGT, Constant.FOLDER_XLIFF };
        createFolders(project, folderNames);
        monitor.worked(1);
        if (monitor.isCanceled()) {
            project.delete(true, monitor);
            throw new OperationCanceledException();
        }
    } catch (CoreException e) {
        logger.error(Messages.getString("wizard.NewProjectWizard.logger5"), e);
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException)

Example 99 with NullProgressMonitor

use of org.eclipse.core.runtime.NullProgressMonitor 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 100 with NullProgressMonitor

use of org.eclipse.core.runtime.NullProgressMonitor in project translationstudio8 by heartsome.

the class ExportDocxDialog method beginExport.

/**
	 * 开始导出功能
	 */
private void beginExport(IProgressMonitor monitor, final String docxPath, boolean exportComment, boolean exportStatus, String expandXpath) throws Exception {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    VTDGen vg = new VTDGen();
    if (!vg.parseFile(strXliffFullPath, true)) {
        final String parseErrorTip = MessageFormat.format(Messages.getString("ExportDocxDialog.parseError"), ResourceUtils.fileToIFile(docxPath).getFullPath().toOSString());
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                MessageDialog.openError(getShell(), Messages.getString("all.dialog.warning"), parseErrorTip);
            }
        });
        throw new Exception(parseErrorTip);
    }
    monitor.worked(1);
    VTDNav vn = vg.getNav();
    VTDUtils vu = new VTDUtils(vn);
    AutoPilot ap = new AutoPilot(vn);
    ap.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
    otherAP = new AutoPilot(vn);
    otherAP.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
    AutoPilot childAP = new AutoPilot(vn);
    childAP.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
    String srcLang = "";
    String tgtLang = "";
    // 备注:目前只支持处理一种目标语言的情况
    ap.selectXPath("/xliff//file[1]");
    if (ap.evalXPath() != -1) {
        srcLang = vu.getCurrentElementAttribut("source-language", "");
        tgtLang = vu.getCurrentElementAttribut("target-language", "");
    }
    if ("".equals(srcLang)) {
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                MessageDialog.openWarning(getShell(), Messages.getString("all.dialog.warning"), Messages.getString("ExportDocxDialog.ok.msg1"));
            }
        });
        LOGGER.error(Messages.getString("ExportDocxDialog.ok.msg1"));
    }
    if ("".equals(tgtLang)) {
        Display.getDefault().syncExec(new Runnable() {

            @Override
            public void run() {
                MessageDialog.openWarning(getShell(), Messages.getString("all.dialog.warning"), Messages.getString("ExportDocxDialog.ok.msg2"));
            }
        });
        LOGGER.error(Messages.getString("ExportDocxDialog.ok.msg2"));
    }
    String xpath = "/xliff/file[@source-language='" + srcLang + "' and @target-language='" + tgtLang + "']/body/descendant::trans-unit[(source/text()!='' or source/*)" + expandXpath + "]";
    ap.selectXPath(xpath);
    List<TUBean> tuDataList = new ArrayList<TUBean>();
    String rowId = "";
    int status = DocxConstant.STATUS_unstrans;
    boolean approved = false;
    boolean isLocked = false;
    boolean isNotSendToTm = false;
    boolean needsReview = false;
    int index = -1;
    while (ap.evalXPath() != -1) {
        status = DocxConstant.STATUS_unstrans;
        approved = false;
        isLocked = false;
        isNotSendToTm = false;
        needsReview = false;
        TUBean bean = new TUBean();
        rowId = RowIdUtil.getSpecialRowId(vn);
        bean.setRowId(rowId);
        // 是否批准
        if ((index = vn.getAttrVal("approved")) != -1) {
            if ("yes".equals(vn.toString(index))) {
                approved = true;
            }
        }
        // 是否锁定
        if ((index = vn.getAttrVal("translate")) != -1) {
            if ("no".equals(vn.toString(index))) {
                isLocked = true;
            }
        }
        // 是否不发送数据库
        if ((index = vn.getAttrVal("hs:send-to-tm")) != -1) {
            if ("no".equals(vn.toString(index))) {
                isNotSendToTm = true;
            }
        }
        // 是否是疑问文本段
        if ((index = vn.getAttrVal("hs:needs-review")) != -1) {
            if ("yes".equals(vn.toString(index))) {
                needsReview = true;
            }
        }
        vn.push();
        childAP.selectXPath("./source");
        if (childAP.evalXPath() != -1) {
            bean.setSrcText(vu.getElementContent());
        }
        vn.pop();
        vn.push();
        childAP.selectXPath("./target");
        if (childAP.evalXPath() != -1) {
            if ((index = vn.getAttrVal("state")) != -1) {
                String stateStr = vn.toString(index);
                if ("new".equals(stateStr)) {
                    // 草稿
                    status = DocxConstant.STATUS_draft;
                } else if ("translated".equals(stateStr)) {
                    if (approved) {
                        // 批准翻译
                        status = DocxConstant.STATUS_approved;
                    } else {
                        // 完成翻译
                        status = DocxConstant.STATUS_translated;
                    }
                } else if ("signed-off".equals(stateStr) && approved) {
                    // 签发
                    status = DocxConstant.STATUS_signedOff;
                }
            }
            bean.setTgtText(vu.getElementContent());
        } else {
            status = DocxConstant.STATUS_unstrans;
        }
        vn.pop();
        // 这里参照界面上状态的写法,分三个部份,第一个为(草稿,已翻译,完成翻译,批注,签发,锁定), 第二部份为不送至库,第三部份为疑问。
        String beanStatus = "";
        if (isLocked) {
            beanStatus += Messages.getString("ExportDocxDialog.ok.status.locked");
        } else {
            switch(status) {
                case DocxConstant.STATUS_unstrans:
                    beanStatus += Messages.getString("ExportDocxDialog.ok.status.unstrans");
                    break;
                case DocxConstant.STATUS_draft:
                    beanStatus += Messages.getString("ExportDocxDialog.ok.status.draft");
                    break;
                case DocxConstant.STATUS_translated:
                    beanStatus += Messages.getString("ExportDocxDialog.ok.status.translated");
                    break;
                case DocxConstant.STATUS_approved:
                    beanStatus += Messages.getString("ExportDocxDialog.ok.status.approved");
                    break;
                case DocxConstant.STATUS_signedOff:
                    beanStatus += Messages.getString("ExportDocxDialog.ok.status.signedOff");
                    break;
                default:
                    break;
            }
        }
        if (isNotSendToTm) {
            beanStatus += "、";
            beanStatus += Messages.getString("ExportDocxDialog.ok.status.NotSendToTm");
        }
        if (needsReview) {
            beanStatus += "、";
            beanStatus += Messages.getString("ExportDocxDialog.ok.status.Review");
        }
        bean.setStatus(beanStatus);
        getComments(vn, vu, bean);
        bean.setIndex("" + (tuDataList.size() + 1));
        tuDataList.add(bean);
    }
    monitor.worked(1);
    // 开始导出操作。规划出 subMonitor
    IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 18, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
    ExportDocx wordOutput = new ExportDocx(srcLang, tgtLang, docxPath, getShell(), exportComment, exportStatus);
    wordOutput.output(tuDataList, subMonitor);
    subMonitor.done();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ArrayList(java.util.ArrayList) VTDGen(com.ximpleware.VTDGen) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) TUBean(net.heartsome.cat.ts.ui.docx.common.TUBean) AutoPilot(com.ximpleware.AutoPilot) ExportDocx(net.heartsome.cat.ts.ui.docx.ExportDocx) VTDNav(com.ximpleware.VTDNav)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1614 CoreException (org.eclipse.core.runtime.CoreException)416 Test (org.junit.Test)382 IFile (org.eclipse.core.resources.IFile)342 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)267 IProject (org.eclipse.core.resources.IProject)258 File (java.io.File)205 IPath (org.eclipse.core.runtime.IPath)182 IOException (java.io.IOException)153 Path (org.eclipse.core.runtime.Path)132 ArrayList (java.util.ArrayList)122 IStatus (org.eclipse.core.runtime.IStatus)113 IFolder (org.eclipse.core.resources.IFolder)87 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)69 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)63 InvocationTargetException (java.lang.reflect.InvocationTargetException)60 List (java.util.List)59 IProjectDescription (org.eclipse.core.resources.IProjectDescription)57 IType (org.eclipse.jdt.core.IType)57 Status (org.eclipse.core.runtime.Status)55