Search in sources :

Example 6 with TranscodeException

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

the class ProjectConfiger method saveProjectConfigInfo.

/**
	 * 保存项目的配置信息
	 * @param bean
	 *            {@link ProjectInfoBean} 项目配置信息bean
	 * @throws NavException
	 * @throws ParseException
	 * @throws TranscodeException
	 * @throws ModifyException
	 * @throws IOException
	 *             ;
	 */
public boolean saveProjectConfigInfo(ProjectInfoBean bean) {
    try {
        StringBuffer content = new StringBuffer();
        content.append("\n\t<hs>");
        content.append(createProjectNameNode(bean.getProjectName()));
        content.append(createFieldListNode(bean.getMapField()));
        content.append(createAttrListNode(bean.getMapAttr()));
        content.append(createLanguageNode(bean.getSourceLang(), bean.getTargetLang()));
        content.append(createTmNode(bean.getTmDb()));
        content.append(createTbNode(bean.getTbDb()));
        content.append("\n\t</hs>");
        XMLModifier xm = vu.update("/projectDescription/text()", content.toString());
        save(xm, this.projCfgFile);
        return true;
    } catch (Exception e) {
        logger.error(Messages.getString("file.ProjectConfiger.logger1"), e);
        e.printStackTrace();
        return false;
    }
}
Also used : XMLModifier(com.ximpleware.XMLModifier) NavException(com.ximpleware.NavException) TranscodeException(com.ximpleware.TranscodeException) XPathEvalException(com.ximpleware.XPathEvalException) XPathParseException(com.ximpleware.XPathParseException) IOException(java.io.IOException) ModifyException(com.ximpleware.ModifyException) ParseException(com.ximpleware.ParseException)

Example 7 with TranscodeException

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

the class TSFileHandler method fileAnalysis.

/**
	 * 分析指定名称的 XLIFF 文件或项目文件,获取其进度报告。
	 * 
	 * @param xliff
	 *            要分析的文件名。
	 * @param reportFormat
	 *            报告的格式。
	 * @return Map 中 ReportPath 键值表示最终生成的报告路径。
	 * */
public Map<String, Object> fileAnalysis(String filename, int reportFormat, int analysisMode, float eqvFactor) {
    File file = new File(filename);
    String filepath = file.getAbsolutePath();
    if (file == null || !file.exists()) {
        String msg = Messages.getString("file.TSFileHandler.logger21");
        logger.error(msg);
        return getErrorResult(msg, null);
    }
    // 解析文件,判断其文件类型,并调用不同的分析方法。
    VTDGen vg = new VTDGen();
    if (vg.parseFile(filepath, true)) {
        VTDNav vn = vg.getNav();
        int inx = vn.getRootIndex();
        try {
            String rootName = vn.toString(inx);
            if (rootName.equals("xliff")) {
                return xliffAnalysis(file, analysisMode, eqvFactor);
            } else if (rootName.equals("hsts-project")) {
                return projectAnalysis(file);
            } else {
                String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger22"), filepath);
                logger.error(msg);
                return getErrorResult(msg, null);
            }
        } catch (NavException e) {
            String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger23"), filepath);
            logger.error(msg, e);
            return getErrorResult(msg, e);
        } catch (ModifyException e) {
            String msg = Messages.getString("file.TSFileHandler.logger24");
            logger.error(msg, e);
            return getErrorResult(msg, e);
        } catch (UnsupportedEncodingException e) {
            String msg = Messages.getString("file.TSFileHandler.logger25");
            logger.error(msg, e);
            return getErrorResult(msg, e);
        } catch (TranscodeException e) {
            String msg = Messages.getString("file.TSFileHandler.logger26");
            logger.error(msg, e);
            return getErrorResult(msg, e);
        } catch (IOException e) {
            String msg = Messages.getString("file.TSFileHandler.logger27");
            logger.error(msg, e);
            return getErrorResult(msg, e);
        } catch (XPathParseException e) {
            String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger23"), filepath);
            logger.error(msg, e);
            return getErrorResult(msg, e);
        } catch (XPathEvalException e) {
            String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger23"), filepath);
            logger.error(msg, e);
            return getErrorResult(msg, e);
        }
    } else {
        String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger28"), filepath);
        logger.error(msg);
        return getErrorResult(msg, null);
    }
}
Also used : XPathParseException(com.ximpleware.XPathParseException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) ModifyException(com.ximpleware.ModifyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) File(java.io.File) VTDNav(com.ximpleware.VTDNav) TranscodeException(com.ximpleware.TranscodeException)

Example 8 with TranscodeException

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

the class TSFileHandler method openFile.

@Override
public Map<String, Object> openFile(File file, int tuCount) {
    long start = System.currentTimeMillis();
    // 验证文件是否存在
    if (file == null || !file.exists()) {
        String errorMsg = Messages.getString("file.TSFileHandler.logger5");
        logger.error(errorMsg);
        return getErrorResult(errorMsg, null);
    }
    // 判断是否还有缓存空间。
    boolean canCache = tuCount < TU_CACHE_SIZE;
    // 当前文件中解析并缓存翻译单元计数器。
    int parsedTuCount = 0;
    // 当前文件未解析缓存的翻译单元计数器。
    int noParseTuCount = 0;
    String filename = file.getAbsolutePath();
    int fileIndex = 1;
    // 解析文件并获取索引
    VTDGen vgRead = new VTDGen();
    if (vgRead.parseFile(filename, true)) {
        VTDNav vnRead = vgRead.getNav();
        VTDUtils vu = null;
        try {
            vu = new VTDUtils(vnRead);
            // 创建临时文件
            File tmpFile = createTmpFile();
            XMLModifier xm = new XMLModifier(vnRead);
            FileOutputStream fos = new FileOutputStream(tmpFile);
            xm.output(fos);
            fos.close();
            tmpFileMap.put(filename, tmpFile.getAbsolutePath());
            filesChangeStatus.put(filename, false);
        } catch (ModifyException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger6"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (TranscodeException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger7"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (IOException e) {
            String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger8"), filename);
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (NavException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger9");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        }
        // 创建翻译单元集合缓存。
        Vector<TransUnitBean> tusCache = new Vector<TransUnitBean>();
        // 创建翻译单元索引集合缓存。
        Vector<String> tuIndexCache = new Vector<String>();
        // 初始化文件节点属性集合。
        fileAttrs = new Hashtable<Integer, Hashtable<String, String>>();
        AutoPilot apFile = new AutoPilot(vnRead);
        String fileNode = "/xliff/file";
        try {
            apFile.selectXPath(fileNode);
            while (apFile.evalXPath() != -1) {
                fileAttrs.put(fileIndex, vu.getCurrentElementAttributs());
                AutoPilot apTU = new AutoPilot(vnRead);
                apTU.selectXPath("body//trans-unit");
                vnRead.push();
                while (apTU.evalXPath() != -1) {
                    // 如果缓冲区未满,则解析文件内容并缓存,否则只计数,不解析内容。
                    if (canCache) {
                        String tuid = "";
                        String srcText = "";
                        String srcContent = "";
                        String tgtText = "";
                        String tgtContent = "";
                        Hashtable<String, String> srcProps = null;
                        Hashtable<String, String> tgtProps = null;
                        // 取翻译单元所有属性
                        String tmpNode = "";
                        vnRead.push();
                        Hashtable<String, String> tuProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        tuid = tuProps.get("id");
                        // 取翻译单元源节点完整文本,含内部标记。
                        vnRead.push();
                        tmpNode = "./source";
                        srcContent = vu.getElementContent(tmpNode);
                        // vnRead.pop();
                        // 取翻译单元源文本。
                        // vnRead.push();
                        srcText = vu.getElementPureText();
                        // 取翻译单元源节点属性。
                        srcProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        // 取翻译单元目标节点完整文本,含内部标记。
                        vnRead.push();
                        tmpNode = "./target";
                        tgtContent = vu.getElementContent(tmpNode);
                        // vnRead.pop();
                        // 取翻译单元目标文本。
                        // vnRead.push();
                        tgtText = vu.getElementPureText();
                        // 取翻译单元目标节点属性。
                        tgtProps = vu.getCurrentElementAttributs();
                        vnRead.pop();
                        // 获取所有的 alttrans 匹配节点。
                        vnRead.push();
                        Vector<AltTransBean> matches = getAltTrans(vu);
                        vnRead.pop();
                        // 构建翻译单元对象,存储节点信息
                        TransUnitBean tub = new TransUnitBean(tuid, srcContent, srcText);
                        tub.setTuProps(tuProps);
                        tub.setSrcProps(srcProps);
                        tub.setTgtContent(tgtContent);
                        tub.setTgtText(tgtText);
                        tub.setTgtProps(tgtProps);
                        tub.setMatches(matches);
                        vnRead.push();
                        tub.setNotes(getNotes(vu));
                        vnRead.pop();
                        vnRead.push();
                        tub.setPropgroups(getPrpoGroups(vu));
                        vnRead.pop();
                        tusCache.add(tub);
                        tuIndexCache.add(filename + ";" + fileIndex + ";" + tuid);
                        // 解析的翻译单元节点计数
                        parsedTuCount++;
                        if (tuCount + parsedTuCount == TU_CACHE_SIZE) {
                            canCache = false;
                        }
                    } else {
                        // 未解析的翻译单元节点计数
                        noParseTuCount++;
                    }
                }
                vnRead.pop();
                // 文件节点索引计数
                fileIndex++;
            }
            transunits.put(filename, tusCache);
            tuIndexs.put(filename, tuIndexCache);
            actualTuCount.put(filename, parsedTuCount + noParseTuCount);
            accessHistory.put(filename, "");
        } catch (XPathEvalException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger10");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (NavException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger11");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        } catch (XPathParseException e) {
            String errorMsg = Messages.getString("file.TSFileHandler.logger12");
            logger.error(errorMsg, e);
            return getErrorResult(errorMsg, e);
        }
    } else {
        String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger13"), filename);
        logger.error(errorMsg);
        return getErrorResult(errorMsg, null);
    }
    long end = System.currentTimeMillis();
    // 输出结果
    long resultMS = end - start;
    long resultS = resultMS / 1000;
    long resultM = resultMS / (1000 * 60);
    System.gc();
    logger.info(Messages.getString("file.TSFileHandler.logger14"), new Object[] { resultM, resultS, resultMS });
    Map<String, Object> result = getSuccessResult();
    result.put("CurCachedTuCount", Integer.valueOf(parsedTuCount));
    result.put("TotalCachedTuCount", Integer.valueOf(parsedTuCount + tuCount));
    return result;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) XPathParseException(com.ximpleware.XPathParseException) TransUnitBean(net.heartsome.cat.ts.core.bean.TransUnitBean) AltTransBean(net.heartsome.cat.ts.core.bean.AltTransBean) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) ModifyException(com.ximpleware.ModifyException) Vector(java.util.Vector) Hashtable(java.util.Hashtable) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException) AutoPilot(com.ximpleware.AutoPilot) FileOutputStream(java.io.FileOutputStream) VTDNav(com.ximpleware.VTDNav) File(java.io.File)

Example 9 with TranscodeException

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

the class XLPHandler method getItems.

/**
	 * 得到配置文件中记录的所有项
	 * @param attrName
	 *            属性名
	 * @param values
	 *            属性值
	 * @return ;
	 */
public List<Map<String, String>> getItems(String attrName, List<String> values) {
    HashMap<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
    AutoPilot ap = new AutoPilot(vn);
    try {
        VTDUtils vu = new VTDUtils(vn);
        ap.selectXPath("/xlfedit-project/item[@" + attrName + "]");
        XMLModifier xm = new XMLModifier(vn);
        boolean isModified = false;
        while (ap.evalXPath() != -1) {
            int index = vn.getAttrVal(attrName);
            String value = index != -1 ? vn.toString(index) : "";
            if (values.contains(value) && !map.containsKey(value)) {
                Map<String, String> item = vu.getCurrentElementAttributs();
                if (item == null) {
                    item = new Hashtable<String, String>();
                }
                map.put(value, item);
            } else {
                // 去除无用的记录
                xm.remove();
                isModified = true;
            }
        }
        if (isModified) {
            save(xm);
        }
        for (String value : values) {
            if (!map.containsKey(value)) {
                HashMap<String, String> item = new HashMap<String, String>();
                item.put(attrName, value);
                map.put(value, item);
            }
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ModifyException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (ParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (TranscodeException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (IOException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    // 按顺序添加
    ArrayList<Map<String, String>> items = new ArrayList<Map<String, String>>();
    for (String value : values) {
        items.add(map.get(value));
    }
    return items;
}
Also used : XMLModifier(com.ximpleware.XMLModifier) HashMap(java.util.HashMap) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TranscodeException(com.ximpleware.TranscodeException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) ModifyException(com.ximpleware.ModifyException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with TranscodeException

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

the class PreMachineTranslation method executeTranslation.

/**
	 * 根据构建参数执行预翻译 ;
	 * @throws InterruptedException
	 */
public List<PreMachineTranslationCounter> executeTranslation(IProgressMonitor monitor) throws InterruptedException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    simpleMatchers = getMacthers();
    if (null == simpleMatchers || simpleMatchers.isEmpty()) {
        return this.transCounters;
    }
    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 PreMachineTranslationCounter(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;
                }
                keepCurrentMatchs(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) PreMachineTranslationCounter(net.heartsome.cat.ts.machinetranslation.bean.PreMachineTranslationCounter) 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) 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