use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class XliffUtil method getOriginalFiles.
/**
* @param vu
* XLIFF 文件对应的 VTDUtils
* @return List<String>
*/
public static List<String> getOriginalFiles(VTDUtils vu) {
ArrayList<String> list = new ArrayList<String>();
String xpath = "/xliff/file";
AutoPilot ap = new AutoPilot(vu.getVTDNav());
try {
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
list.add(vu.getElementAttribute(".", "original"));
}
} catch (XPathParseException e) {
e.printStackTrace();
} catch (XPathEvalException e) {
e.printStackTrace();
} catch (NavException e) {
e.printStackTrace();
}
return list;
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class QAXmlHandler method getAllRowIdsByLanguages.
/**
* 通过源与目标语言得到所有当前打开的文件包含的 RowId
* ///xliff/file//descendant::trans-unit[(source/text()!='' or source/*)]
* @return ;
*/
public List<String> getAllRowIdsByLanguages(String srcLanguage, String tgtLanguage) {
List<String> allRowIds = new LinkedList<String>();
AutoPilot ap = new AutoPilot();
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
VTDUtils vu = new VTDUtils();
for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
String fileName = entry.getKey();
VTDNav vn = entry.getValue();
ap.bind(vn);
try {
vu.bind(vn);
// 查询相同目标与相同源文本的tu
String XPATH_ALL_TU_BYLANGUAGE = "/xliff/file[upper-case(@source-language)=''{0}'' and upper-case(@target-language)=''{1}'']/body/descendant::trans-unit[(source/text()!='''' or source/*)]";
String xpath = MessageFormat.format(XPATH_ALL_TU_BYLANGUAGE, new Object[] { srcLanguage, tgtLanguage });
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
String rowId = RowIdUtil.getRowId(vn, fileName);
if (rowId != null) {
allRowIds.add(rowId);
}
}
} catch (NavException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
} catch (XPathParseException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
} catch (XPathEvalException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
}
}
return allRowIds;
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class QAXmlHandler method openFile.
/**
* 解析文件(同时操作进度条)
* @param file
* @param monitor
* @param totalWork
* @return ;
*/
private Map<String, Object> openFile(File file, IProgressMonitor monitor) {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
monitor.beginTask(MessageFormat.format(Messages.getString("qa.QAXmlHandler.task1"), file.getName()), 10);
String filename = file.getAbsolutePath();
// 解析文件并获取索引
VTDGen vgRead = new VTDGen();
if (vgRead.parseFile(filename, true)) {
vnRead = vgRead.getNav();
if (monitor.isCanceled()) {
return getReturnResult();
}
monitor.worked(3);
try {
AutoPilot ap = new AutoPilot(vnRead);
apMap.put(filename, ap);
// 记录xliff文件命名空间
// ap.selectXPath("namespace-uri(/xliff)");
// String xmlns;
// if ((xmlns = ap.evalXPathToString()) != null) {
// xliffXmlnsMap.put(filename, xmlns);
// } else {
// String errorMsg = "文件“" + filename + "”,不是合法的 XLIFF 文件!";
// return getErrorResult(errorMsg, null);
// }
monitor.worked(1);
ap.resetXPath();
if (monitor.isCanceled()) {
return getReturnResult();
}
ap.selectXPath("count(" + XPATH_ALL_TU + ")");
// 整个xliff文件中的trans-unit节点的个数
int countAllTU = (int) ap.evalXPathToNumber();
monitor.worked(6);
tuSizeMap.put(filename, countAllTU);
vnMap.put(filename, vnRead);
} catch (XPathParseException e) {
String errorMsg = Messages.getString("qa.QAXmlHandler.logger1");
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
}
accessHistory.put(filename, "");
} else {
String errorMsg = MessageFormat.format(Messages.getString("qa.QAXmlHandler.logger2"), filename);
logger.error(errorMsg);
return getErrorResult(errorMsg, null);
}
} finally {
monitor.done();
}
return getSuccessResult();
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class TSFileHandler method fileAnalysis.
/**
* 分析指定名称的 XLIFF 文件或项目文件,获取其进度报告。
*
* @param xliff
* 要分析的文件名。
* @param reportFormat
* 报告的格式。
* @return Map 中 ReportPath 键值表示最终生成的报告路径。
* */
public Map<String, Object> fileAnalysis(String filename, int reportFormat, int analysisMode, float eqvFactor) {
File file = new File(filename);
String filepath = file.getAbsolutePath();
if (file == null || !file.exists()) {
String msg = Messages.getString("file.TSFileHandler.logger21");
logger.error(msg);
return getErrorResult(msg, null);
}
// 解析文件,判断其文件类型,并调用不同的分析方法。
VTDGen vg = new VTDGen();
if (vg.parseFile(filepath, true)) {
VTDNav vn = vg.getNav();
int inx = vn.getRootIndex();
try {
String rootName = vn.toString(inx);
if (rootName.equals("xliff")) {
return xliffAnalysis(file, analysisMode, eqvFactor);
} else if (rootName.equals("hsts-project")) {
return projectAnalysis(file);
} else {
String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger22"), filepath);
logger.error(msg);
return getErrorResult(msg, null);
}
} catch (NavException e) {
String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger23"), filepath);
logger.error(msg, e);
return getErrorResult(msg, e);
} catch (ModifyException e) {
String msg = Messages.getString("file.TSFileHandler.logger24");
logger.error(msg, e);
return getErrorResult(msg, e);
} catch (UnsupportedEncodingException e) {
String msg = Messages.getString("file.TSFileHandler.logger25");
logger.error(msg, e);
return getErrorResult(msg, e);
} catch (TranscodeException e) {
String msg = Messages.getString("file.TSFileHandler.logger26");
logger.error(msg, e);
return getErrorResult(msg, e);
} catch (IOException e) {
String msg = Messages.getString("file.TSFileHandler.logger27");
logger.error(msg, e);
return getErrorResult(msg, e);
} catch (XPathParseException e) {
String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger23"), filepath);
logger.error(msg, e);
return getErrorResult(msg, e);
} catch (XPathEvalException e) {
String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger23"), filepath);
logger.error(msg, e);
return getErrorResult(msg, e);
}
} else {
String msg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger28"), filepath);
logger.error(msg);
return getErrorResult(msg, null);
}
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class TSFileHandler method openFile.
@Override
public Map<String, Object> openFile(File file, int tuCount) {
long start = System.currentTimeMillis();
// 验证文件是否存在
if (file == null || !file.exists()) {
String errorMsg = Messages.getString("file.TSFileHandler.logger5");
logger.error(errorMsg);
return getErrorResult(errorMsg, null);
}
// 判断是否还有缓存空间。
boolean canCache = tuCount < TU_CACHE_SIZE;
// 当前文件中解析并缓存翻译单元计数器。
int parsedTuCount = 0;
// 当前文件未解析缓存的翻译单元计数器。
int noParseTuCount = 0;
String filename = file.getAbsolutePath();
int fileIndex = 1;
// 解析文件并获取索引
VTDGen vgRead = new VTDGen();
if (vgRead.parseFile(filename, true)) {
VTDNav vnRead = vgRead.getNav();
VTDUtils vu = null;
try {
vu = new VTDUtils(vnRead);
// 创建临时文件
File tmpFile = createTmpFile();
XMLModifier xm = new XMLModifier(vnRead);
FileOutputStream fos = new FileOutputStream(tmpFile);
xm.output(fos);
fos.close();
tmpFileMap.put(filename, tmpFile.getAbsolutePath());
filesChangeStatus.put(filename, false);
} catch (ModifyException e) {
String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger6"), filename);
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
} catch (TranscodeException e) {
String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger7"), filename);
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
} catch (IOException e) {
String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger8"), filename);
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
} catch (NavException e) {
String errorMsg = Messages.getString("file.TSFileHandler.logger9");
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
}
// 创建翻译单元集合缓存。
Vector<TransUnitBean> tusCache = new Vector<TransUnitBean>();
// 创建翻译单元索引集合缓存。
Vector<String> tuIndexCache = new Vector<String>();
// 初始化文件节点属性集合。
fileAttrs = new Hashtable<Integer, Hashtable<String, String>>();
AutoPilot apFile = new AutoPilot(vnRead);
String fileNode = "/xliff/file";
try {
apFile.selectXPath(fileNode);
while (apFile.evalXPath() != -1) {
fileAttrs.put(fileIndex, vu.getCurrentElementAttributs());
AutoPilot apTU = new AutoPilot(vnRead);
apTU.selectXPath("body//trans-unit");
vnRead.push();
while (apTU.evalXPath() != -1) {
// 如果缓冲区未满,则解析文件内容并缓存,否则只计数,不解析内容。
if (canCache) {
String tuid = "";
String srcText = "";
String srcContent = "";
String tgtText = "";
String tgtContent = "";
Hashtable<String, String> srcProps = null;
Hashtable<String, String> tgtProps = null;
// 取翻译单元所有属性
String tmpNode = "";
vnRead.push();
Hashtable<String, String> tuProps = vu.getCurrentElementAttributs();
vnRead.pop();
tuid = tuProps.get("id");
// 取翻译单元源节点完整文本,含内部标记。
vnRead.push();
tmpNode = "./source";
srcContent = vu.getElementContent(tmpNode);
// vnRead.pop();
// 取翻译单元源文本。
// vnRead.push();
srcText = vu.getElementPureText();
// 取翻译单元源节点属性。
srcProps = vu.getCurrentElementAttributs();
vnRead.pop();
// 取翻译单元目标节点完整文本,含内部标记。
vnRead.push();
tmpNode = "./target";
tgtContent = vu.getElementContent(tmpNode);
// vnRead.pop();
// 取翻译单元目标文本。
// vnRead.push();
tgtText = vu.getElementPureText();
// 取翻译单元目标节点属性。
tgtProps = vu.getCurrentElementAttributs();
vnRead.pop();
// 获取所有的 alttrans 匹配节点。
vnRead.push();
Vector<AltTransBean> matches = getAltTrans(vu);
vnRead.pop();
// 构建翻译单元对象,存储节点信息
TransUnitBean tub = new TransUnitBean(tuid, srcContent, srcText);
tub.setTuProps(tuProps);
tub.setSrcProps(srcProps);
tub.setTgtContent(tgtContent);
tub.setTgtText(tgtText);
tub.setTgtProps(tgtProps);
tub.setMatches(matches);
vnRead.push();
tub.setNotes(getNotes(vu));
vnRead.pop();
vnRead.push();
tub.setPropgroups(getPrpoGroups(vu));
vnRead.pop();
tusCache.add(tub);
tuIndexCache.add(filename + ";" + fileIndex + ";" + tuid);
// 解析的翻译单元节点计数
parsedTuCount++;
if (tuCount + parsedTuCount == TU_CACHE_SIZE) {
canCache = false;
}
} else {
// 未解析的翻译单元节点计数
noParseTuCount++;
}
}
vnRead.pop();
// 文件节点索引计数
fileIndex++;
}
transunits.put(filename, tusCache);
tuIndexs.put(filename, tuIndexCache);
actualTuCount.put(filename, parsedTuCount + noParseTuCount);
accessHistory.put(filename, "");
} catch (XPathEvalException e) {
String errorMsg = Messages.getString("file.TSFileHandler.logger10");
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
} catch (NavException e) {
String errorMsg = Messages.getString("file.TSFileHandler.logger11");
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
} catch (XPathParseException e) {
String errorMsg = Messages.getString("file.TSFileHandler.logger12");
logger.error(errorMsg, e);
return getErrorResult(errorMsg, e);
}
} else {
String errorMsg = MessageFormat.format(Messages.getString("file.TSFileHandler.logger13"), filename);
logger.error(errorMsg);
return getErrorResult(errorMsg, null);
}
long end = System.currentTimeMillis();
// 输出结果
long resultMS = end - start;
long resultS = resultMS / 1000;
long resultM = resultMS / (1000 * 60);
System.gc();
logger.info(Messages.getString("file.TSFileHandler.logger14"), new Object[] { resultM, resultS, resultMS });
Map<String, Object> result = getSuccessResult();
result.put("CurCachedTuCount", Integer.valueOf(parsedTuCount));
result.put("TotalCachedTuCount", Integer.valueOf(parsedTuCount + tuCount));
return result;
}
Aggregations