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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations