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