Search in sources :

Example 1 with XPathParseException

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

the class LanguageConfiger method getAllLanguage.

/**
	 * 返回所有语言列表,key:code,value:<code>Language</code>
	 */
public Map<String, Language> getAllLanguage() {
    Map<String, Language> result = new HashMap<String, Language>();
    AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
    String codeAttr = "code";
    String bidiAttr = "bidi";
    String imgAttr = "image";
    String bidiYes = "Yes";
    try {
        tempAp.selectXPath("/languages/lang");
        while (tempAp.evalXPath() != -1) {
            Map<String, String> attrs = vu.getCurrentElementAttributs();
            String code = attrs.get(codeAttr);
            String bidi = attrs.get(bidiAttr);
            String img = attrs.get(imgAttr);
            String langName = vu.getElementPureText();
            boolean isBidi = false;
            if (code != null && langName != null) {
                if (bidi != null && bidi.equals(bidiYes)) {
                    isBidi = true;
                }
                result.put(code, new Language(code, langName, img == null ? "" : img, isBidi));
            }
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
    } catch (NavException e) {
        LOGGER.error("", e);
    }
    return result;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) Language(net.heartsome.cat.common.locale.Language) HashMap(java.util.HashMap) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Example 2 with XPathParseException

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

the class LanguageConfiger method getLanguageByCode.

/**
	 * 返回Code对应的Language对象,如果在配置中找到则返回null
	 * @param code
	 * @return ;
	 */
public Language getLanguageByCode(String code) {
    AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
    String bidiAttr = "bidi";
    String imgAttr = "image";
    String bidiYes = "Yes";
    try {
        tempAp.selectXPath("/languages/lang[@code='" + code + "']");
        if (tempAp.evalXPath() != -1) {
            Map<String, String> attrs = vu.getCurrentElementAttributs();
            String bidi = attrs.get(bidiAttr);
            String img = attrs.get(imgAttr);
            String langName = vu.getElementPureText();
            boolean isBidi = false;
            if (code != null && langName != null) {
                if (bidi != null && bidi.equals(bidiYes)) {
                    isBidi = true;
                }
                return new Language(code, langName, img == null ? "" : img, isBidi);
            }
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
    } catch (NavException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) Language(net.heartsome.cat.common.locale.Language) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Example 3 with XPathParseException

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

the class VTDUtils method getEvalValue.

/**
	 * 获取非节点 XPath 表达式计算的值。<br/>
	 * 非节点 XPath:导航到的位置不是简单的 XML 内容。
	 * <p>
	 * 例如“count”等函数的返回值。 <br/>
	 * <code>Double count = getEvalValue(Double.class, "count(//a)");</code>
	 * </p>
	 * @param <T>
	 *            结果类型
	 * @param clazz
	 *            结果类型
	 * @param ap
	 *            AutoPilot 实例
	 * @param xpath
	 *            XPath 表达式
	 * @return XPath 表达式计算的值;
	 */
@SuppressWarnings("unchecked")
public <T> T getEvalValue(Class<T> clazz, AutoPilot ap, String xpath) {
    if (ap == null) {
        ap = new AutoPilot(vn);
    }
    try {
        vn.push();
        ap.selectXPath(xpath);
        if (String.class.equals(clazz)) {
            String value;
            if ((value = ap.evalXPathToString()) != null) {
                return (T) value;
            }
        } else if (Double.class.equals(clazz)) {
            Double value;
            if ((value = ap.evalXPathToNumber()) != -1) {
                return (T) value;
            }
        } else if (Boolean.class.equals(clazz)) {
            Boolean value;
            if ((value = ap.evalXPathToBoolean()) != false) {
                return (T) value;
            }
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
    } finally {
        vn.pop();
    }
    return null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) AutoPilot(com.ximpleware.AutoPilot)

Example 4 with XPathParseException

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

the class DocUtils method isTBX.

/**
	 * 判断是否是正确的 TBX 文件
	 * @param fileName
	 *            TBX 文件的全路径
	 * @return 反回null,验证失败
	 * @throws ParseException
	 * @throws EntityException
	 * @throws EOFException
	 * @throws EncodingException
	 * @throws FileNotFoundException
	 */
public static VTDUtils isTBX(String fileName) throws EncodingException, ParseException, FileNotFoundException {
    VTDGen vg = new VTDGen();
    FileInputStream fis = null;
    File f = null;
    try {
        f = new File(fileName);
        fis = new FileInputStream(f);
        byte[] b = new byte[(int) f.length()];
        int offset = 0;
        int numRead = 0;
        // I choose this value randomally,
        int numOfBytes = 1048576;
        // any other (not too big) value also can be here.
        if (b.length - offset < numOfBytes) {
            numOfBytes = b.length - offset;
        }
        while (offset < b.length && (numRead = fis.read(b, offset, numOfBytes)) >= 0) {
            offset += numRead;
            if (b.length - offset < numOfBytes) {
                numOfBytes = b.length - offset;
            }
        }
        vg.setDoc(b);
        vg.parse(true);
    } catch (IOException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger1"), e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
    VTDNav vn = vg.getNav();
    AutoPilot ap = new AutoPilot(vn);
    String rootPath = "/martif";
    VTDUtils vtdUtils = new VTDUtils();
    try {
        vtdUtils.bind(vn);
        ap.selectXPath(rootPath);
        if (ap.evalXPath() == -1) {
            // } else {
            return null;
        }
    } catch (NavException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathEvalException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } catch (XPathParseException e) {
        LOGGER.error(Messages.getString("document.DocUtils.logger2"), e);
        return null;
    } finally {
        vg.clear();
    }
    return vtdUtils;
}
Also used : NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) NavException(com.ximpleware.NavException) IOException(java.io.IOException) EncodingException(com.ximpleware.EncodingException) FileNotFoundException(java.io.FileNotFoundException) ParseException(com.ximpleware.ParseException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) File(java.io.File) VTDNav(com.ximpleware.VTDNav)

Example 5 with XPathParseException

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

the class TextUtil method loadISOLang.

private static void loadISOLang(String strLangFile) {
    if (strLangFile == null) {
        return;
    }
    ISOLang = new Hashtable<String, String>();
    VTDGen vg = new VTDGen();
    // vg.setDoc(strLangFile.getBytes());
    try {
        vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(strLangFile)));
        vg.parse(true);
        VTDNav vn = vg.getNav();
        VTDUtils vu = new VTDUtils(vn);
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/languages/lang");
        int codeIndex;
        String code = null;
        String langName;
        while ((ap.evalXPath()) != -1) {
            codeIndex = vn.getAttrVal("code");
            if (codeIndex != -1) {
                code = vn.toString(codeIndex);
            }
            langName = vu.getElementPureText();
            if (code != null && langName != null) {
                ISOLang.put(code, langName);
            }
        }
        ap.resetXPath();
    } catch (NavException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (XPathEvalException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EncodingException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EOFException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (EntityException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (ParseException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } catch (IOException e) {
        if (LOGGER.isErrorEnabled()) {
            String msg = Messages.getString("util.TextUtil.logger1");
            Object[] args = { strLangFile };
            LOGGER.error(new MessageFormat(msg).format(args), e);
        }
    } finally {
        vg.clear();
    }
}
Also used : MessageFormat(java.text.MessageFormat) EncodingException(com.ximpleware.EncodingException) NavException(com.ximpleware.NavException) XPathEvalException(com.ximpleware.XPathEvalException) EntityException(com.ximpleware.EntityException) VTDGen(com.ximpleware.VTDGen) IOException(java.io.IOException) XPathParseException(com.ximpleware.XPathParseException) VTDUtils(net.heartsome.xml.vtdimpl.VTDUtils) AutoPilot(com.ximpleware.AutoPilot) EOFException(com.ximpleware.EOFException) XPathParseException(com.ximpleware.XPathParseException) ParseException(com.ximpleware.ParseException) VTDNav(com.ximpleware.VTDNav)

Aggregations

XPathParseException (com.ximpleware.XPathParseException)73 NavException (com.ximpleware.NavException)68 AutoPilot (com.ximpleware.AutoPilot)67 XPathEvalException (com.ximpleware.XPathEvalException)67 VTDNav (com.ximpleware.VTDNav)48 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)36 ModifyException (com.ximpleware.ModifyException)19 XMLModifier (com.ximpleware.XMLModifier)17 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)17 VTDGen (com.ximpleware.VTDGen)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 TranscodeException (com.ximpleware.TranscodeException)9 HashMap (java.util.HashMap)9 ParseException (com.ximpleware.ParseException)8 FileOutputStream (java.io.FileOutputStream)8 FileNotFoundException (java.io.FileNotFoundException)7 EOFException (com.ximpleware.EOFException)5 EncodingException (com.ximpleware.EncodingException)5 EntityException (com.ximpleware.EntityException)5