Search in sources :

Example 16 with NavException

use of com.ximpleware.NavException 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 17 with NavException

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

the class QAXmlHandler method getAllRowIdsByLanguages.

/**
	 * 通过源与目标语言得到所有当前打开的文件包含的 RowId
	 * ///xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]
	 * @return ;
	 */
public List<String> getAllRowIdsByLanguages(String srcLanguage, String tgtLanguage) {
    List<String> allRowIds = new LinkedList<String>();
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    VTDUtils vu = new VTDUtils();
    for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
        String fileName = entry.getKey();
        VTDNav vn = entry.getValue();
        ap.bind(vn);
        try {
            vu.bind(vn);
            // 查询相同目标与相同源文本的tu
            String XPATH_ALL_TU_BYLANGUAGE = "/xliff/file[upper-case(@source-language)=''{0}'' and upper-case(@target-language)=''{1}'']/body/descendant::trans-unit[(source/text()!='''' or source/*)]";
            String xpath = MessageFormat.format(XPATH_ALL_TU_BYLANGUAGE, new Object[] { srcLanguage, tgtLanguage });
            ap.selectXPath(xpath);
            while (ap.evalXPath() != -1) {
                String rowId = RowIdUtil.getRowId(vn, fileName);
                if (rowId != null) {
                    allRowIds.add(rowId);
                }
            }
        } catch (NavException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
        } catch (XPathParseException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
        } catch (XPathEvalException e) {
            e.printStackTrace();
            logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
        }
    }
    return allRowIds;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDNav(com.ximpleware.VTDNav) LinkedList(java.util.LinkedList)

Example 18 with NavException

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

the class QAXmlHandler method getTuProp.

/**
	 * 得到翻译单元的属性值	,来自XLFHandler
	 * @param rowId
	 *            行的唯一标识
	 * @param propName
	 *            属性名
	 * @return 属性值;
	 */
public String getTuProp(String rowId, String propName) {
    VTDNav vn = getVTDNavByRowId(rowId);
    String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
    try {
        VTDUtils vu = new VTDUtils(vn);
        return vu.getValue(tuXPath + "/@" + propName);
    } catch (NavException e) {
        e.printStackTrace();
        logger.error(Messages.getString("qa.QAXmlHandler.logger7"), e);
    }
    return null;
}
Also used : VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 19 with NavException

use of com.ximpleware.NavException 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 20 with NavException

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

NavException (com.ximpleware.NavException)93 AutoPilot (com.ximpleware.AutoPilot)70 XPathParseException (com.ximpleware.XPathParseException)70 XPathEvalException (com.ximpleware.XPathEvalException)67 VTDNav (com.ximpleware.VTDNav)63 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)58 XMLModifier (com.ximpleware.XMLModifier)31 ModifyException (com.ximpleware.ModifyException)29 ArrayList (java.util.ArrayList)22 IOException (java.io.IOException)21 VTDGen (com.ximpleware.VTDGen)17 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)13 TranscodeException (com.ximpleware.TranscodeException)12 ParseException (com.ximpleware.ParseException)11 FileOutputStream (java.io.FileOutputStream)11 List (java.util.List)10 LinkedList (java.util.LinkedList)9 FileNotFoundException (java.io.FileNotFoundException)8 LinkedHashMap (java.util.LinkedHashMap)8