Search in sources :

Example 1 with TranscodeException

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

the class VTDUtils method updateVTDNav.

/**
	 * 保存修改并更新 VTDNav 实例
	 * @param xm
	 *            XMLModifier 实例
	 * @param filePath
	 *            文件路径
	 * @return VTDNav 实例
	 */
public VTDNav updateVTDNav(XMLModifier xm, String filePath) {
    try {
        File file = new File(filePath);
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        xm.output(bos);
        bos.close();
        fos.close();
        VTDGen vg = new VTDGen();
        if (vg.parseFile(filePath, true)) {
            // 重新加载
            vn = vg.getNav();
        }
    } catch (ModifyException e) {
        LOGGER.error("", e);
    } catch (TranscodeException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    }
    return vn;
}
Also used : FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TranscodeException(com.ximpleware.TranscodeException)

Example 2 with TranscodeException

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

the class ExportTbxImpl method filterLangSet.

/**
	 * 过滤从库中导出的langSet节点<br>
	 * 在库中TremEntry是以整个节点进行存储的,因此,在导出后也是整个节点导出,所以会将无关的语言也导出来。<br>
	 * 在导出后,生成的TBX文件中进语言进行过滤
	 * @param filePath
	 *            导出后生成的TBX文件路径
	 * @param srcLang
	 *            源语言
	 * @param needLang
	 *            当前需要导出的语言;
	 */
private void filterLangSet(String filePath, String srcLang, List<String> needLang) {
    try {
        VTDGen vg = new VTDGen();
        if (vg.parseFile(filePath, true)) {
            VTDUtils vu = new VTDUtils(vg.getNav());
            StringBuffer xpath = new StringBuffer("/martif/text/body/termEntry/langSet[");
            String noteXpathtemp = "starts-with(@id,'__LANG__,') or ends-with(@id,',__LANG__')";
            StringBuffer noteTgtXpath = new StringBuffer();
            for (String lang : needLang) {
                xpath.append("not(@xml:lang='" + lang + "') and ");
                if (!lang.equals(srcLang)) {
                    noteTgtXpath.append(noteXpathtemp.replace("__LANG__", lang)).append(" or ");
                }
            }
            String tgtLangXpath = noteTgtXpath.substring(0, noteTgtXpath.lastIndexOf("or"));
            StringBuffer noteXpath = new StringBuffer();
            noteXpath.append("/martif/text/body/termEntry/note[");
            noteXpath.append("not(");
            noteXpath.append("(").append(noteXpathtemp.replace("__LANG__", srcLang)).append(")");
            noteXpath.append(" and ");
            noteXpath.append("(").append(tgtLangXpath).append(")");
            noteXpath.append(")");
            noteXpath.append("]");
            String xpathStr = xpath.substring(0, xpath.lastIndexOf("and")) + "]";
            XMLModifier xm = new XMLModifier(vu.getVTDNav());
            AutoPilot ap = new AutoPilot(vu.getVTDNav());
            ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
            xm = vu.delete(ap, xm, xpathStr, VTDUtils.PILOT_TO_END);
            xm = vu.delete(ap, xm, noteXpath.toString(), VTDUtils.PILOT_TO_END);
            FileOutputStream fos = new FileOutputStream(filePath);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            // 写入文件
            xm.output(bos);
            bos.close();
            fos.close();
        }
    } catch (NavException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (ModifyException 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();
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) NavException(com.ximpleware.NavException) FileNotFoundException(java.io.FileNotFoundException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with TranscodeException

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

the class DatabaseConfiger method saveToFile.

/**
	 * 保存文件
	 * @param xm
	 *            XMLModifier对象
	 * @param fileName
	 *            文件名
	 * @return 是否保存成功;
	 */
private boolean saveToFile(XMLModifier xm, File file) {
    try {
        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 写入文件
        xm.output(bos);
        bos.close();
        fos.close();
        return true;
    } catch (ModifyException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        logger.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        logger.error("", e);
        e.printStackTrace();
    }
    return false;
}
Also used : FileOutputStream(java.io.FileOutputStream) ModifyException(com.ximpleware.ModifyException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) TranscodeException(com.ximpleware.TranscodeException)

Example 4 with TranscodeException

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

the class WebSearchPreferencStore method storeConfig.

/**
	 * 保存设置值
	 * @param searchEntry
	 *            ;
	 */
public void storeConfig(List<SearchEntry> searchEntry) {
    FileOutputStream out = null;
    boolean hasError = false;
    try {
        out = new FileOutputStream(customConfigPath);
        writeContent(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        writeContent(out, "<WebSearchInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instancexmlns:xsd=http://www.w3.org/2001/XMLSchema\">\r\n");
        writeContent(out, "	<Providers>\r\n");
        if (null != searchEntry) {
            for (SearchEntry s : searchEntry) {
                writeContent(out, convert2Xml(s));
            }
        }
        writeContent(out, "	</Providers>\r\n </WebSearchInfo>");
        out.flush();
        support.firePropertyChange("URL", null, getUseredConfig(searchEntry));
    } catch (FileNotFoundException e) {
        hasError = true;
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        hasError = true;
        e.printStackTrace();
    } catch (IOException e) {
        hasError = true;
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (hasError) {
            XMLModifier modifier;
            try {
                modifier = new XMLModifier(customVu.getVTDNav());
                modifier.output(customConfigPath);
            } catch (ModifyException e) {
                e.printStackTrace();
                logger.error("", e);
            } catch (TranscodeException e) {
                e.printStackTrace();
                logger.error("", e);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("", e);
            }
        } else {
            try {
                customVu.parseFile(customConfigPath, false);
            } catch (ParseException e) {
                e.printStackTrace();
                logger.error("", e);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("", e);
            }
        }
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ModifyException(com.ximpleware.ModifyException) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) SearchEntry(net.heartsome.cat.ts.websearch.bean.SearchEntry) TranscodeException(com.ximpleware.TranscodeException)

Example 5 with TranscodeException

use of com.ximpleware.TranscodeException 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)

Aggregations

ModifyException (com.ximpleware.ModifyException)19 TranscodeException (com.ximpleware.TranscodeException)19 IOException (java.io.IOException)19 FileOutputStream (java.io.FileOutputStream)14 NavException (com.ximpleware.NavException)12 XPathParseException (com.ximpleware.XPathParseException)12 XMLModifier (com.ximpleware.XMLModifier)11 BufferedOutputStream (java.io.BufferedOutputStream)10 AutoPilot (com.ximpleware.AutoPilot)9 XPathEvalException (com.ximpleware.XPathEvalException)9 VTDGen (com.ximpleware.VTDGen)7 ParseException (com.ximpleware.ParseException)6 VTDNav (com.ximpleware.VTDNav)6 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)5 FileNotFoundException (java.io.FileNotFoundException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 File (java.io.File)3 OutputStream (java.io.OutputStream)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 IFile (org.eclipse.core.resources.IFile)2