use of com.ximpleware.ParseException in project translationstudio8 by heartsome.
the class ImportAbstract method doImport.
/**
* 导入文件内容
* @param fileContent
* @param srcLang
* @param monitor
* @return ;
* @throws ImportException
*/
public int doImport(String fileContent, String srcLang, IProgressMonitor monitor) throws ImportException {
if (monitor == null) {
this.monitor = new NullProgressMonitor();
} else {
this.monitor = monitor;
}
// 解析文件
String message = "";
VTDGen vg = new VTDGen();
vg.setDoc(fileContent.getBytes());
try {
vg.parse(true);
} catch (EncodingException e) {
logger.error(Messages.getString("document.ImportAbstract.logger1"), e);
message = Messages.getString("document.ImportAbstract.msg1");
throw new ImportException(message + e.getMessage());
} catch (EOFException e) {
logger.error("", e);
message = Messages.getString("document.ImportAbstract.tbx.msg1");
throw new ImportException(message + e.getMessage());
} catch (EntityException e) {
logger.error("", e);
message = Messages.getString("document.ImportAbstract.tbx.msg1");
throw new ImportException(message + e.getMessage());
} catch (ParseException e) {
logger.error(Messages.getString("document.ImportAbstract.logger3"), e);
String errMsg = e.getMessage();
if (errMsg.indexOf("invalid encoding") != -1) {
// 编码异常
message = Messages.getString("document.ImportAbstract.msg1");
} else {
message = Messages.getString("document.ImportAbstract.tbx.msg1");
}
throw new ImportException(message + e.getMessage());
}
try {
// 构建VTD解析工具
vu = new VTDUtils(vg.getNav());
dbOperator.beginTransaction();
// 执行导入
executeImport(srcLang);
dbOperator.commit();
} catch (VTDException e) {
logger.error("", e);
try {
dbOperator.rollBack();
} catch (SQLException e1) {
logger.error("", e);
}
return FAILURE_4;
} catch (SQLException e) {
logger.error("", e);
try {
dbOperator.rollBack();
} catch (SQLException e1) {
logger.error("", e1);
}
throw new ImportException(Messages.getString("document.ImportAbstract.tbx.importDbError") + e.getMessage());
} catch (OperationCanceledException e) {
logger.error("", e);
try {
dbOperator.rollBack();
} catch (SQLException e1) {
logger.error("", e1);
return CANCEL;
}
} catch (Exception e) {
logger.error("", e);
try {
dbOperator.rollBack();
} catch (SQLException e1) {
logger.error("", e1);
}
throw new ImportException(Messages.getString("document.ImportAbstract.tbx.importDbError") + e.getMessage());
}
return SUCCESS;
}
use of com.ximpleware.ParseException 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.ParseException in project translationstudio8 by heartsome.
the class TmxReader method paseFile.
/**
* Parse file with VTD-XML
* @param file
* @return
* @throws TmxReadException
* All Exception come from VTDExcetpion;
*/
private VTDGen paseFile(File file) throws TmxReadException {
String encoding = FileEncodingDetector.detectFileEncoding(file);
VTDGen vg = new VTDGen();
FileInputStream fis = null;
String message = "";
try {
fis = new FileInputStream(file);
byte[] bArr = new byte[(int) file.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 (bArr.length - offset < numOfBytes) {
numOfBytes = bArr.length - offset;
}
while (offset < bArr.length && (numRead = fis.read(bArr, offset, numOfBytes)) >= 0) {
offset += numRead;
if (bArr.length - offset < numOfBytes) {
numOfBytes = bArr.length - offset;
}
}
// clean invalid XML character
if (!(encoding.equalsIgnoreCase("UTF-16LE") || encoding.equalsIgnoreCase("UTF-16BE"))) {
byte[] _bArr = new byte[bArr.length];
int _bArrIndx = 0;
int type = 0;
for (int i = 0; i < bArr.length; i++) {
byte b = bArr[i];
if ((b >= type && b <= 8) || b == 11 || b == 12 || (b >= 14 && b <= 31)) {
continue;
} else if (b == 38 && i + 1 < bArr.length && bArr[i + 1] == 35 && i + 2 < bArr.length) {
// &#
List<Byte> entis = new ArrayList<Byte>();
entis.add((byte) 38);
entis.add((byte) 35);
int j = i + 2;
if (bArr[j] == 120) {
// x
entis.add((byte) 120);
while (true) {
j++;
if (j >= bArr.length) {
entis.clear();
b = bArr[i];
break;
}
b = bArr[j];
if ((b >= 48 && b <= 57) || (b >= 97 && b <= 102) || (b >= 65 && b <= 70)) {
entis.add(b);
} else if (b == 59) {
entis.add(b);
i = j;
break;
} else if (j - i > 10) {
entis.clear();
b = bArr[i];
break;
} else {
entis.clear();
b = bArr[i];
break;
}
}
} else {
while (true) {
b = bArr[j];
if ((b >= 48 && b <= 57)) {
entis.add(b);
} else if (b == 59) {
entis.add(b);
i = j;
break;
} else if (j - i > 10) {
entis.clear();
b = bArr[i];
break;
} else {
entis.clear();
b = bArr[i];
break;
}
j++;
if (j >= bArr.length) {
entis.clear();
b = bArr[i];
break;
}
}
}
if (!entis.isEmpty()) {
byte[] t = new byte[entis.size()];
for (int ti = 0; ti < entis.size(); ti++) {
t[ti] = entis.get(ti);
}
String s = new String(t);
if (s.matches("((&#[x]?)(([0]?([0-8]|[BbCcEe]))|(1[0-9])|(1[a-fA-F]));)")) {
continue;
}
}
}
_bArr[_bArrIndx++] = b;
}
bArr = null;
bArr = Arrays.copyOf(_bArr, _bArrIndx);
}
// use vtd parse
vg.setDoc(bArr);
vg.parse(true);
} catch (IOException e) {
logger.error(Messages.getString("document.DocUtils.logger1"), e);
throw new TmxReadException(Messages.getString("document.TmxReader.parseTmxFileError"));
} catch (EncodingException e) {
logger.error(Messages.getString("document.ImportAbstract.logger1"), e);
message = Messages.getString("document.ImportAbstract.msg1");
throw new TmxReadException(message + e.getMessage());
} catch (ParseException e) {
logger.error(Messages.getString("document.ImportAbstract.logger3"), e);
String errMsg = e.getMessage();
if (errMsg.indexOf("invalid encoding") != -1) {
// 编码异常
message = Messages.getString("document.ImportAbstract.msg1");
} else {
message = Messages.getString("document.ImportAbstract.msg3");
}
throw new TmxReadException(message + e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
}
}
}
return vg;
}
use of com.ximpleware.ParseException 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();
}
}
use of com.ximpleware.ParseException in project translationstudio8 by heartsome.
the class TextUtil method loadLanguages.
private static void loadLanguages() {
descriptions = null;
isBidi = null;
descriptions = new Hashtable<String, String>();
isBidi = new Hashtable<String, String>();
String strFile = CoreActivator.LANGUAGE_CODE_PATH;
VTDGen vg = new VTDGen();
try {
vg.setDoc(readBytesFromIS(CoreActivator.getConfigurationFileInputStream(strFile)));
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;
int bidiIndex;
String bidi = null;
String langName;
while ((ap.evalXPath()) != -1) {
codeIndex = vn.getAttrVal("code");
if (codeIndex != -1) {
code = vn.toString(codeIndex);
}
bidiIndex = vn.getAttrVal("bidi");
if (bidiIndex != -1) {
bidi = vn.toString(bidiIndex);
}
langName = vu.getElementPureText();
if (code != null && langName != null) {
descriptions.put(code, langName);
}
if (code != null && bidi != null) {
isBidi.put(code, bidi);
}
}
ap.resetXPath();
} catch (NavException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} catch (XPathParseException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} catch (XPathEvalException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} catch (EncodingException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} catch (EOFException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} catch (EntityException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} catch (ParseException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} catch (IOException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("util.TextUtil.logger1");
Object[] args = { strFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
} finally {
vg.clear();
}
}
Aggregations