Search in sources :

Example 1 with ModifyException

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

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

use of com.ximpleware.ModifyException 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 4 with ModifyException

use of com.ximpleware.ModifyException 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 5 with ModifyException

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

Aggregations

ModifyException (com.ximpleware.ModifyException)35 NavException (com.ximpleware.NavException)27 XMLModifier (com.ximpleware.XMLModifier)25 AutoPilot (com.ximpleware.AutoPilot)20 XPathParseException (com.ximpleware.XPathParseException)20 XPathEvalException (com.ximpleware.XPathEvalException)18 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)18 TranscodeException (com.ximpleware.TranscodeException)17 IOException (java.io.IOException)17 VTDNav (com.ximpleware.VTDNav)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 FileOutputStream (java.io.FileOutputStream)12 BufferedOutputStream (java.io.BufferedOutputStream)10 ArrayList (java.util.ArrayList)7 ParseException (com.ximpleware.ParseException)6 VTDGen (com.ximpleware.VTDGen)6 List (java.util.List)6 LinkedList (java.util.LinkedList)5 FileNotFoundException (java.io.FileNotFoundException)4 HashMap (java.util.HashMap)4