Search in sources :

Example 6 with XPathParseException

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

the class XliffUtil method getOriginalFiles.

/**
	 * @param vu
	 *            XLIFF 文件对应的 VTDUtils
	 * @return List<String>
	 */
public static List<String> getOriginalFiles(VTDUtils vu) {
    ArrayList<String> list = new ArrayList<String>();
    String xpath = "/xliff/file";
    AutoPilot ap = new AutoPilot(vu.getVTDNav());
    try {
        ap.selectXPath(xpath);
        while (ap.evalXPath() != -1) {
            list.add(vu.getElementAttribute(".", "original"));
        }
    } catch (XPathParseException e) {
        e.printStackTrace();
    } catch (XPathEvalException e) {
        e.printStackTrace();
    } catch (NavException e) {
        e.printStackTrace();
    }
    return list;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) ArrayList(java.util.ArrayList) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Example 7 with XPathParseException

use of com.ximpleware.XPathParseException 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 8 with XPathParseException

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

the class QAXmlHandler method openFile.

/**
	 * 解析文件(同时操作进度条)
	 * @param file
	 * @param monitor
	 * @param totalWork
	 * @return ;
	 */
private Map<String, Object> openFile(File file, IProgressMonitor monitor) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        monitor.beginTask(MessageFormat.format(Messages.getString("qa.QAXmlHandler.task1"), file.getName()), 10);
        String filename = file.getAbsolutePath();
        // 解析文件并获取索引
        VTDGen vgRead = new VTDGen();
        if (vgRead.parseFile(filename, true)) {
            vnRead = vgRead.getNav();
            if (monitor.isCanceled()) {
                return getReturnResult();
            }
            monitor.worked(3);
            try {
                AutoPilot ap = new AutoPilot(vnRead);
                apMap.put(filename, ap);
                // 记录xliff文件命名空间
                //			ap.selectXPath("namespace-uri(/xliff)");
                //			String xmlns;
                //			if ((xmlns = ap.evalXPathToString()) != null) {
                //				xliffXmlnsMap.put(filename, xmlns);
                //			} else {
                //				String errorMsg = "文件“" + filename + "”,不是合法的 XLIFF 文件!";
                //				return getErrorResult(errorMsg, null);
                //			}
                monitor.worked(1);
                ap.resetXPath();
                if (monitor.isCanceled()) {
                    return getReturnResult();
                }
                ap.selectXPath("count(" + XPATH_ALL_TU + ")");
                // 整个xliff文件中的trans-unit节点的个数
                int countAllTU = (int) ap.evalXPathToNumber();
                monitor.worked(6);
                tuSizeMap.put(filename, countAllTU);
                vnMap.put(filename, vnRead);
            } catch (XPathParseException e) {
                String errorMsg = Messages.getString("qa.QAXmlHandler.logger1");
                logger.error(errorMsg, e);
                return getErrorResult(errorMsg, e);
            }
            accessHistory.put(filename, "");
        } else {
            String errorMsg = MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger2"), filename);
            logger.error(errorMsg);
            return getErrorResult(errorMsg, null);
        }
    } finally {
        monitor.done();
    }
    return getSuccessResult();
}
Also used : XPathParseException(com.ximpleware.XPathParseException) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) AutoPilot(com.ximpleware.AutoPilot) VTDGen(com.ximpleware.VTDGen)

Example 9 with XPathParseException

use of com.ximpleware.XPathParseException 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 10 with XPathParseException

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

XPathParseException (com.ximpleware.XPathParseException)71 NavException (com.ximpleware.NavException)66 XPathEvalException (com.ximpleware.XPathEvalException)66 AutoPilot (com.ximpleware.AutoPilot)65 VTDNav (com.ximpleware.VTDNav)46 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)36 ModifyException (com.ximpleware.ModifyException)17 ArrayList (java.util.ArrayList)17 XMLModifier (com.ximpleware.XMLModifier)15 IOException (java.io.IOException)15 VTDGen (com.ximpleware.VTDGen)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 HashMap (java.util.HashMap)9 ParseException (com.ximpleware.ParseException)8 TranscodeException (com.ximpleware.TranscodeException)7 FileNotFoundException (java.io.FileNotFoundException)7 FileOutputStream (java.io.FileOutputStream)6 EOFException (com.ximpleware.EOFException)5 EncodingException (com.ximpleware.EncodingException)5 EntityException (com.ximpleware.EntityException)5