Search in sources :

Example 1 with AutoPilot

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

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

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

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

the class VTDUtils method getChildrenContent.

/**
	 * 获取当前节点下所有指定元素名称的文本内容,含内部标记等子节点内容。
	 * @param elementName
	 *            子节点名称
	 * @throws XPathParseException
	 * @throws NavException
	 * @throws XPathEvalException
	 */
public Vector<String> getChildrenContent(String elementName) throws XPathParseException, XPathEvalException, NavException {
    Vector<String> texts = new Vector<String>();
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath("./" + elementName);
    while (ap.evalXPath() != -1) {
        vn.push();
        texts.add(getElementContent());
        vn.pop();
    }
    if (texts.isEmpty()) {
        texts = null;
    }
    return texts;
}
Also used : AutoPilot(com.ximpleware.AutoPilot) Vector(java.util.Vector)

Example 5 with AutoPilot

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

the class VTDUtils method getChildElementsCount.

/**
	 * 获得指定节点下一级子节点的个数
	 * @param xpath
	 *            指定节点的 XPath
	 * @return
	 * @throws XPathParseException
	 * @throws XPathEvalException
	 * @throws NavException
	 *             ;
	 */
public int getChildElementsCount(String xpath) throws XPathParseException, XPathEvalException, NavException {
    int result = 0;
    AutoPilot ap = new AutoPilot(vn);
    ap.selectXPath(xpath);
    vn.push();
    if (ap.evalXPath() != -1) {
        result = getChildElementsCount();
    }
    vn.pop();
    return result;
}
Also used : AutoPilot(com.ximpleware.AutoPilot)

Aggregations

AutoPilot (com.ximpleware.AutoPilot)312 VTDNav (com.ximpleware.VTDNav)177 NavException (com.ximpleware.NavException)152 XPathParseException (com.ximpleware.XPathParseException)147 XPathEvalException (com.ximpleware.XPathEvalException)138 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)112 IOException (java.io.IOException)106 ModifyException (com.ximpleware.ModifyException)97 TranscodeException (com.ximpleware.TranscodeException)84 CoreException (org.eclipse.core.runtime.CoreException)76 UnsupportedEncodingException (java.io.UnsupportedEncodingException)58 VTDGen (com.ximpleware.VTDGen)54 XMLModifier (com.ximpleware.XMLModifier)50 FileNotFoundException (java.io.FileNotFoundException)49 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)44 ArrayList (java.util.ArrayList)42 HashMap (java.util.HashMap)40 XQException (javax.xml.xquery.XQException)37 LinkedHashMap (java.util.LinkedHashMap)34 LinkedList (java.util.LinkedList)25