Search in sources :

Example 16 with XPathParseException

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

the class XLFHandler method validateMultiFileNodes.

/**
	 * 检验多个 file 节点是否存在 document 属性组,以及该属性组下是否存在 original 属性(用于转换 XLIFF 的源文件为 OpenOffice 和 MSOffice2007 的情况)
	 * @param fileName
	 * @return ;
	 */
public boolean validateMultiFileNodes(String fileName) {
    VTDNav vn = vnMap.get(fileName);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + fileName);
    try {
        AutoPilot subAp = new AutoPilot(vn);
        subAp.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        subAp.selectXPath("./header/hs:prop-group[@name=\"document\"]/hs:prop[@prop-type=\"original\"]");
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/xliff/file");
        while (ap.evalXPath() != -1) {
            vn.push();
            subAp.resetXPath();
            if (subAp.evalXPath() != -1) {
                String documentOriginal = vu.getElementContent();
                if (documentOriginal == null || documentOriginal.equals("")) {
                    return false;
                }
            } else {
                return false;
            }
            vn.pop();
        }
        return true;
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return false;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 17 with XPathParseException

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

the class XLFHandler method getNodeFrag.

/**
	 * 获取整个节点,包括其头部,其子节点,其文本
	 * @param xlfPath
	 * @param nodeXPath
	 *            节点的xpath
	 * @return robert 2011-10-21
	 */
public String getNodeFrag(String xlfPath, String nodeXPath) {
    VTDNav vn = vnMap.get(xlfPath);
    Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
    String xliffNodeContent = "";
    try {
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(nodeXPath);
        VTDUtils vu = new VTDUtils(vn);
        if (ap.evalXPath() != -1) {
            xliffNodeContent = vu.getElementFragment();
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return xliffNodeContent;
}
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)

Example 18 with XPathParseException

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

the class XLFHandler method openFile.

/**
	 * 解析文件(同时操作进度条)
	 * @param file
	 * @param monitor
	 * @param totalWork
	 * @return ;
	 */
private Map<String, Object> openFile(File file, IProgressMonitor monitor) {
    boolean isStarting = PlatformUI.getWorkbench().isStarting();
    if (isStarting) {
        monitor = null;
    } else if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        if (!isStarting) {
            monitor.beginTask(MessageFormat.format(Messages.getString("file.XLFHandler.task2"), file.getName()), 10);
        }
        String filename = file.getAbsolutePath();
        // updateXLIFFVersion(filename); // 更新 XLIFF 文件版本为 1.2
        // monitor.worked(1);
        // 解析文件并获取索引
        VTDGen vgRead = new VTDGen();
        if (vgRead.parseFile(filename, true)) {
            vnRead = vgRead.getNav();
            if (!isStarting) {
                if (monitor.isCanceled()) {
                    return getErrorResult(Messages.getString("file.XLFHandler.msg1"), null);
                }
                monitor.worked(3);
            }
            try {
                // 创建临时文件
                // File tmpFile = createTmpFile();
                // XMLModifier xm = new XMLModifier(vnRead);
                // save(xm, tmpFile);
                //
                // tmpFileMap.put(filename, tmpFile.getAbsolutePath());
                // filesChangeStatus.put(filename, false);
                // monitor.worked(1);
                AutoPilot ap = new AutoPilot(vnRead);
                // 记录xliff文件命名空间
                ap.selectXPath("namespace-uri(/xliff)");
                String xmlns;
                if ((xmlns = ap.evalXPathToString()) != null) {
                    xliffXmlnsMap.put(filename, xmlns);
                } else {
                    String errorMsg = MessageFormat.format(Messages.getString("file.XLFHandler.msg2"), filename);
                    return getErrorResult(errorMsg, null);
                }
                if (!isStarting) {
                    monitor.worked(1);
                }
                ap.resetXPath();
                if (!isStarting) {
                    if (monitor.isCanceled()) {
                        return getErrorResult(Messages.getString("file.XLFHandler.msg1"), null);
                    }
                }
                ap.selectXPath("count(" + XPATH_ALL_TU + ")");
                // 整个xliff文件中的trans-unit节点的个数
                int countAllTU = (int) ap.evalXPathToNumber();
                if (!isStarting) {
                    monitor.worked(6);
                }
                tuSizeMap.put(filename, countAllTU);
                vnMap.put(filename, vnRead);
            }// }
             catch (XPathParseException e) {
                String errorMsg = Messages.getString("file.XLFHandler.logger2");
                LOGGER.error(errorMsg, e);
                return getErrorResult(errorMsg, e);
            }
            // catch (XPathEvalException e) {
            // String errorMsg = "XPath 求值时出错,定位到file节点失败。";
            // LOGGER.error(errorMsg, e);
            // return getErrorResult(errorMsg, e);
            // }
            accessHistory.put(filename, "");
        } else {
            String errorMsg = MessageFormat.format(Messages.getString("file.XLFHandler.logger3"), filename);
            LOGGER.error(errorMsg);
            return getErrorResult(errorMsg, null);
        }
    } finally {
        if (!isStarting) {
            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 19 with XPathParseException

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

the class XLFHandler method getSegmentIndex.

/**
	 * 得到文本段的索引
	 * @param currentRowIndex
	 * @param relativeXPath
	 * @param isNext
	 *            是否是找下一个。<br/>
	 *            <code>true</code>: 下一个;<code>false</code>: 上一个。
	 * @return 文本段的索引;
	 */
private int getSegmentIndex(int currentRowIndex, String relativeXPath, boolean isNext) {
    int step = isNext ? 1 : -1;
    currentRowIndex += step;
    AutoPilot ap = new AutoPilot();
    ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
    while (currentRowIndex >= 0 && currentRowIndex < rowIds.size()) {
        String rowId = getRowId(currentRowIndex);
        VTDNav vn = getVTDNavByRowId(rowId);
        ap.bind(vn);
        String xpath = RowIdUtil.parseRowIdToXPath(rowId);
        try {
            ap.selectXPath(xpath + relativeXPath);
            if (ap.evalXPath() != -1) {
                return currentRowIndex;
            }
        } catch (XPathEvalException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (NavException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        } catch (XPathParseException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
        currentRowIndex += step;
    }
    return -1;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

Example 20 with XPathParseException

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

the class XLFHandler method getPropValue.

/**
	 * 得到TU下指定节点的指定属性值
	 * @param rowIdList
	 *            用来生成Xpath的rowId集合
	 * @param subXPath
	 *            定位到tu下的子节点属性的xpath,例如:"/target/@state"
	 * @return 由rowId与得到的属性值的映射map。<br/>
	 *         key: rowId; value: 属性值;
	 */
private Map<String, String> getPropValue(List<String> rowIdList, String subXPath) {
    if (rowIdList == null) {
        return null;
    }
    if (subXPath.lastIndexOf('/') > subXPath.lastIndexOf('@')) {
        // 此subXPath要获取的并不是属性
        LOGGER.error(Messages.getString("file.XLFHandler.logger9"));
        return null;
    }
    Map<String, String> propValueMap = new HashMap<String, String>();
    AutoPilot ap = new AutoPilot();
    try {
        ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
        ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
        int index = -1;
        for (String rowId : rowIdList) {
            VTDNav vn = getVTDNavByRowId(rowId);
            vn.push();
            ap.bind(vn);
            // 根据RowId得到定位到该翻译单元的XPath
            String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
            // 可以定位TU下的任何子节点的属性
            String propXpath = tuXPath + subXPath;
            ap.selectXPath(propXpath);
            if ((index = ap.evalXPath()) != -1) {
                propValueMap.put(rowId, vn.toNormalizedString(index + 1));
            } else {
                propValueMap.put(rowId, null);
            }
            vn.pop();
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    } catch (NavException e) {
        LOGGER.error("", e);
        e.printStackTrace();
    }
    return propValueMap;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException) VTDNav(com.ximpleware.VTDNav)

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