use of com.ximpleware.ParseException in project translationstudio8 by heartsome.
the class XLPHandler method saveSourceInfo.
/**
* 保存 XLIFF 信息
* @param sourceInfo
* 源文件信息 ;
*/
public void saveSourceInfo(List<? extends Map<String, String>> sourceInfo) {
try {
XMLModifier xm = new XMLModifier(vn);
AutoPilot ap = new AutoPilot(vn);
StringBuffer items = new StringBuffer();
String pattern = "<item {0}=\"{1}\" {2}=\"{3}\" {4}=\"{5}\" {6}=\"{7}\" />";
ap.selectXPath("/xlfedit-project");
if (ap.evalXPath() != -1) {
for (Map<String, String> map : sourceInfo) {
vn.push();
String source = map.get(ATTR_SOURCE);
String format = map.get(ATTR_FORMAT);
String srcLang = map.get(ATTR_SRC_LANG);
String srcEnc = map.get(ATTR_SRC_ENC);
String xpath = "./item[@" + ATTR_SOURCE + "=\"" + source + "\"]";
ap.selectXPath(xpath);
if (ap.evalXPath() != -1) {
updateAttribute(xm, ATTR_FORMAT, format);
updateAttribute(xm, ATTR_SRC_LANG, srcLang);
updateAttribute(xm, ATTR_SRC_ENC, srcEnc);
} else {
String item = MessageFormat.format(pattern, ATTR_SOURCE, source, ATTR_FORMAT, format, ATTR_SRC_LANG, srcLang, ATTR_SRC_ENC, srcEnc);
ap.selectXPath(xpath);
items.append(item).append(LINE_SEPARATOR);
}
vn.pop();
}
if (items.length() > 1) {
xm.insertBeforeTail(items.toString());
}
save(xm);
}
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (ParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (IOException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
use of com.ximpleware.ParseException in project translationstudio8 by heartsome.
the class LanguageConfiger method parseFile.
private void parseFile() {
VTDGen vg = new VTDGen();
try {
if (vg.parseFile(langConfigFile.getPath(), true)) {
vu = new VTDUtils(vg.getNav());
ap = new AutoPilot(vu.getVTDNav());
} else {
throw new ParseException();
}
} catch (NavException e) {
LOGGER.error("", e);
} catch (ParseException e) {
LOGGER.error("", e);
}
}
use of com.ximpleware.ParseException in project translationstudio8 by heartsome.
the class VTDLoader method loadVTDGen.
/**
* 使用 VTD 解析 XML 文件,返回 VTDGen 对象<br>
* 解析 XML 时忽略掉了 XML 标准中的非法字符
* @param file
* @return
* @throws ParseException
* @throws IOException
* @throws Exception
* ;
*/
public static VTDGen loadVTDGen(File f, String fileEncoding) throws ParseException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), fileEncoding));
try {
char[] cbuf = new char[8192];
int count = reader.read(cbuf);
if (count < 20) {
// Error Empty File
throw new ParseException("Other Error: XML not starting properly");
}
StringBuilder _sb = new StringBuilder();
// 取前500个字符进行判断
_sb.append(cbuf, 0, count < 500 ? count : 500);
int stIdx = _sb.indexOf("<?xml");
if (stIdx != -1) {
// is xml
int endIdx = _sb.indexOf("?>");
if (endIdx != -1 && _sb.indexOf("encoding") != -1) {
endIdx += 2;
String s = "<?xml version=\"1.0\" ?>";
char[] temp = new char[cbuf.length - endIdx + s.length()];
char[] schar = s.toCharArray();
System.arraycopy(schar, 0, temp, 0, schar.length);
System.arraycopy(cbuf, endIdx, temp, schar.length, cbuf.length - endIdx);
cbuf = temp;
count = count - endIdx + s.length();
}
} else {
throw new ParseException("Other Error: XML not starting properly");
}
byte[] bArr = new byte[(int) f.length()];
int i = 0;
int idx = 0;
while (count > 0) {
int tempIdx = 0;
char[] tempChar = new char[count];
for (; i < count; i++) {
char c = cbuf[i];
if (!XMLChar.isValidChar(c)) {
continue;
} else if (c == '&') {
StringBuilder sb = new StringBuilder();
int val = 0;
int j = i + 1;
if (j >= count) {
c = (char) reader.read();
} else {
c = cbuf[j++];
}
sb.append(c);
if (c == '#') {
if (j >= count) {
c = (char) reader.read();
} else {
c = cbuf[j++];
}
sb.append(c);
if (c == 'x') {
while (true) {
if (j >= count) {
c = (char) reader.read();
} else {
c = cbuf[j++];
}
sb.append(c);
if (c >= '0' && c <= '9') {
val = (val << 4) + (c - '0');
} else if (c >= 'a' && c <= 'f') {
val = (val << 4) + (c - 'a' + 10);
} else if (c >= 'A' && c <= 'F') {
val = (val << 4) + (c - 'A' + 10);
} else if (c == ';') {
break;
} else {
break;
}
}
} else {
while (true) {
if (c >= '0' && c <= '9') {
val = val * 10 + (c - '0');
} else if (c == ';') {
break;
} else {
break;
}
if (j >= count) {
c = (char) reader.read();
} else {
c = cbuf[j++];
}
sb.append(c);
}
}
if (!XMLChar.isValidChar(val)) {
if (j <= count) {
i = j;
}
System.out.println((char) val + " " + val + " " + sb);
continue;
} else {
c = cbuf[i];
tempChar[tempIdx++] = c;
for (int t = 0; t < sb.length(); t++) {
if (tempIdx >= tempChar.length) {
tempChar = Arrays.copyOf(tempChar, tempChar.length + 1);
count = tempChar.length;
}
tempChar[tempIdx++] = sb.charAt(t);
i++;
}
}
} else {
c = cbuf[i];
tempChar[tempIdx++] = c;
for (int t = 0; t < sb.length(); t++) {
if (tempIdx >= tempChar.length) {
tempChar = Arrays.copyOf(tempChar, tempChar.length + 1);
count = tempChar.length;
}
tempChar[tempIdx++] = sb.charAt(t);
i++;
}
}
} else {
tempChar[tempIdx++] = c;
}
}
if (tempChar.length != 0) {
byte[] temp = getBytes(tempChar, 0, tempIdx);
System.arraycopy(temp, 0, bArr, idx, temp.length);
idx += temp.length;
}
count = reader.read(cbuf);
i = 0;
}
// FileOutputStream os = new FileOutputStream("C:\\Users\\Jason\\Desktop\\Trados 2007.tmxsasas");
// os.write(bArr, 0, idx);
// os.close();
VTDGen vg = new VTDGen();
vg.setDoc(Arrays.copyOf(bArr, idx));
vg.parse(true);
return vg;
} finally {
reader.close();
}
}
use of com.ximpleware.ParseException in project translationstudio8 by heartsome.
the class DocUtils method isTMX.
/**
* 判断是否是正确的 TMX 文件
* @param fileName
* @return ;
* @throws FileNotFoundException
* @throws ParseException
* @throws EntityException
* @throws EOFException
* @throws EncodingException
*/
public static VTDUtils isTMX(String fileName) throws FileNotFoundException, EncodingException, ParseException {
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 = "/tmx";
VTDUtils vu = new VTDUtils();
try {
vu.bind(vn);
ap.selectXPath(rootPath);
if (ap.evalXPath() == -1) {
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 vu;
}
use of com.ximpleware.ParseException in project translationstudio8 by heartsome.
the class ProjectConfiger method save.
/**
* 保存文件
* @param xm
* XMLModifier对象
* @param fileName
* 文件名
* @return 是否保存成功;
*/
private boolean save(XMLModifier xm, File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入文件
xm.output(bos);
bos.close();
fos.close();
VTDGen vg = new VTDGen();
try {
if (vg.parseFile(projCfgFile.getPath(), true)) {
vu.bind(vg.getNav());
} else {
throw new ParseException();
}
} catch (NavException e) {
logger.error("", e);
} catch (ParseException e) {
logger.error("", e);
}
return true;
} catch (ModifyException e) {
logger.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
logger.error("", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("", e);
e.printStackTrace();
}
return false;
}
Aggregations