use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method splitSegment.
/**
* 分割文本段
* @param rowId
* 要分割的行的RowId
* @param spltOffset
* 分割的位置;
* @return ;
*/
public String splitSegment(String rowId, int spltOffset) {
String fileName = RowIdUtil.getFileNameByRowId(rowId);
VTDNav vn = vnMap.get(fileName);
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
String xpath = RowIdUtil.parseRowIdToXPath(rowId);
try {
VTDUtils vu = new VTDUtils(vn);
ap.selectXPath(xpath);
if (ap.evalXPath() == -1) {
return "";
}
String tuid = vu.getCurrentElementAttribut("id", "");
String xmlSpace = vu.getCurrentElementAttribut("xml:space", "preserve");
String approved = vu.getCurrentElementAttribut("approved", null);
String sendToTM = vu.getCurrentElementAttribut("hs:send-to-tm", null);
String needReview = vu.getCurrentElementAttribut("hs:needs-review", null);
String tuHead = vu.getElementHead();
// 删除 approved 属性
if (approved != null) {
tuHead = tuHead.replace(" approved=\"" + approved + "\"", "");
tuHead = tuHead.replace(" approved='" + approved + "'", "");
}
// tuHead2 删除 hs:send-to-tm, hs:needs-review 属性
String tuHead2 = tuHead;
if (sendToTM != null) {
tuHead2 = tuHead2.replace(" hs:send-to-tm=\"" + sendToTM + "\"", "");
tuHead2 = tuHead2.replace(" hs:send-to-tm='" + sendToTM + "'", "");
}
if (needReview != null) {
tuHead2 = tuHead2.replace(" hs:needs-review=\"" + needReview + "\"", "");
tuHead2 = tuHead2.replace(" hs:needs-review='" + needReview + "'", "");
}
StringBuffer tu1;
StringBuffer tu2;
if (tuHead.contains("id=\"" + tuid + "\"")) {
tu1 = new StringBuffer(tuHead.replace("id=\"" + tuid + "\"", "id=\"" + tuid + "-1\""));
tu2 = new StringBuffer(tuHead2.replace("id=\"" + tuid + "\"", "id=\"" + tuid + "-2\""));
} else if (tuHead.contains("id='" + tuid + "'")) {
tu1 = new StringBuffer(tuHead.replace("id='" + tuid + "'", "id=\"" + tuid + "-1\""));
tu2 = new StringBuffer(tuHead2.replace("id='" + tuid + "'", "id=\"" + tuid + "-2\""));
} else {
// 不存在 id 属性
return "";
}
String sourceFragment1 = null;
String sourceFragment2 = null;
String targetFragment1 = null;
String targetFragment2 = null;
ap.selectXPath(xpath + "/source");
if (ap.evalXPath() != -1) {
String sourceHead = vu.getElementHead();
if (sourceHead != null) {
// source节点的内容
String sourceContent = vu.getElementContent();
// 处理光标在 g 标记内的情况 --robert 2012-11-15
List<Map<String, String>> tagLocationList = getTagLocation(vn, sourceContent);
String srcAddStr1 = "";
String srcAddStr2 = "";
for (Map<String, String> map : tagLocationList) {
String tagHeader = map.get("tagHeader");
String tagTail = map.get("tagTail");
int headerIdx = Integer.parseInt(map.get("headerIdx"));
int tailIdx = Integer.parseInt(map.get("tailIdx"));
if (headerIdx < spltOffset && spltOffset <= tailIdx) {
srcAddStr1 = tagTail + srcAddStr1;
srcAddStr2 += tagHeader;
}
}
sourceFragment1 = sourceHead + sourceContent.substring(0, spltOffset) + srcAddStr1 + "</source>";
sourceFragment2 = sourceHead + srcAddStr2 + sourceContent.substring(spltOffset) + "</source>";
}
}
ap.selectXPath(xpath + "/target");
if (ap.evalXPath() != -1) {
String state = vu.getCurrentElementAttribut("state", null);
String targetHead = vu.getElementHead();
if (targetHead != null) {
if (state != null && !state.equalsIgnoreCase("new")) {
targetHead = targetHead.replace(" state=\"" + state + "\"", " state=\"new\"");
targetHead = targetHead.replace(" state='" + state + "'", " state=\"new\"");
targetFragment1 = targetHead + vu.getElementContent() + "</target>";
} else {
// target节点的段落
targetFragment1 = vu.getElementFragment();
}
// targetFragment2 = targetHead + "</target>";
// modify by peason---- Bug #1048
targetFragment2 = "<target></target>";
}
}
if (sourceFragment1 != null) {
tu1.append(sourceFragment1);
}
if (targetFragment1 != null) {
tu1.append(targetFragment1);
}
if (sourceFragment2 != null) {
tu2.append(sourceFragment2);
}
if (targetFragment2 != null) {
tu2.append(targetFragment2);
}
// 批注信息添加到分割后的第一个文本段中
ap.selectXPath(xpath + "/note");
while (ap.evalXPath() != -1) {
tu1.append(vu.getElementFragment());
}
tu1.append("</trans-unit>");
tu2.append("</trans-unit>");
StringBuffer group = new StringBuffer("<group ");
group.append("id=\"" + tuid + "\" ");
group.append("ts=\"hs-split\" ");
group.append("splitMergeIndex=\"" + System.nanoTime() + "\" ");
group.append("xml:space=\"" + xmlSpace + "\">");
group.append(tu1).append(tu2);
group.append("</group>");
String tuFragment = "";
ap.selectXPath(xpath);
if (ap.evalXPath() != -1) {
XMLModifier xm = new XMLModifier(vn);
xm.insertBeforeElement(group.toString());
// 保存修改前的内容
tuFragment = vu.getElementFragment();
xm.remove();
// 保存并更新VTDNav对象
saveAndReparse(xm, fileName);
int index = rowIds.indexOf(rowId);
// 移除分割前的RowId
rowIds.remove(index);
// 添加分割后的RowId
rowIds.add(index, rowId + "-2");
rowIds.add(index, rowId + "-1");
int tuSize = tuSizeMap.get(fileName);
// 翻译单元总数加1
tuSizeMap.put(fileName, tuSize + 1);
}
return tuFragment;
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return "";
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method getXliffInfo.
/**
* 检查所有已读取文件的file节点是否有target-language属性
* @return key:filename,value: map(key:file节点的original属性
* @throws XPathParseException
* @throws NavException
* @throws XPathEvalException
*/
private Map<String, List<XliffBean>> getXliffInfo(boolean checkTgtLang) {
Map<String, List<XliffBean>> fileNameXliffBeansMap = new HashMap<String, List<XliffBean>>();
VTDUtils vu = new VTDUtils();
AutoPilot subAp = new AutoPilot();
for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
String fileName = entry.getKey();
VTDNav vn = entry.getValue();
try {
vu.bind(vn);
subAp.bind(vn);
subAp.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
subAp.selectXPath("./header/hs:prop-group[@name=\"document\"]/hs:prop[@prop-type=\"sourcefile\"]");
AutoPilot ap = new AutoPilot(vn);
String xpath;
if (checkTgtLang) {
xpath = "/xliff/file[not(@target-language)]";
} else {
xpath = "/xliff/file";
}
// 不存在 target-language 属性。
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
vn.push();
List<XliffBean> xliffBeans = fileNameXliffBeansMap.get(fileName);
if (xliffBeans == null) {
xliffBeans = new ArrayList<XliffBean>();
fileNameXliffBeansMap.put(fileName, xliffBeans);
}
String sourceLanguage = vu.getCurrentElementAttribut("source-language", "");
String targetLanguage = vu.getCurrentElementAttribut("target-language", "");
// 如果是
String original = vu.getCurrentElementAttribut("original", "");
// 源文件名
String sourceFile;
String datatype = vu.getCurrentElementAttribut("datatype", null);
if (datatype != null && (datatype.contains("openoffice") || datatype.contains("msoffice2007"))) {
// 重置 AutoPilot 对象的储存的信息。
subAp.resetXPath();
if (subAp.evalXPath() != -1) {
sourceFile = vu.getElementContent();
} else {
sourceFile = original;
}
} else {
sourceFile = original;
}
XliffBean xliff = null;
for (XliffBean bean : xliffBeans) {
if (bean.getSourceFile().equals(sourceFile)) {
xliff = bean;
}
}
if (xliff == null) {
xliff = new XliffBean(datatype, sourceFile, sourceLanguage, targetLanguage, original, fileName);
xliffBeans.add(xliff);
} else {
xliff.addOriginal(original);
}
vn.pop();
}
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
return fileNameXliffBeansMap;
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method getLanguages.
/**
* 得到所有语言对
* @return 语言对的Map<br/>
* key: 源语言;value: 对应的目标语言(可以是多个)
*/
public Map<String, ArrayList<String>> getLanguages() {
TreeMap<String, ArrayList<String>> languages = new TreeMap<String, ArrayList<String>>();
AutoPilot ap = new AutoPilot();
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
VTDUtils vu = new VTDUtils();
for (VTDNav vn : vnMap.values()) {
ap.bind(vn);
try {
vu.bind(vn);
ap.selectXPath("/xliff/file");
while (ap.evalXPath() != -1) {
String srcLanguage = vu.getCurrentElementAttribut("source-language", null);
String tgtLanguage = vu.getCurrentElementAttribut("target-language", null);
if (srcLanguage == null) {
// TODO 该file节点不存在“source-language”属性,提醒添加
continue;
}
if (tgtLanguage == null) {
// TODO 该file节点不存在“target-language”属性,提醒添加
continue;
}
ArrayList<String> tgtLanguages = languages.get(srcLanguage);
if (tgtLanguages == null) {
tgtLanguages = new ArrayList<String>();
languages.put(srcLanguage, tgtLanguages);
}
if (!tgtLanguages.contains(tgtLanguage)) {
// 未包含,就添加进去
tgtLanguages.add(tgtLanguage);
}
}
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
return languages;
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method generateTMXFileContent.
/**
* 根据同一个 XLIFF 文件中的 rowId 生成 TMX 文件, 对于不添加到库中的文本段将被忽略
* @param list
* rowId
* @param srcLang
* 源语言
* @param monitor
* 进度条
* @return TMX 文件
*/
public StringBuffer generateTMXFileContent(String systemUser, List<String> list, String srcLang, String tgtLang, IProgressMonitor monitor, int contextSize, IProject project) {
if (list == null || list.size() == 0) {
return null;
}
// long start = System.currentTimeMillis();
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.setTaskName(Messages.getString("file.XLFHandler.task4"));
monitor.beginTask(Messages.getString("file.XLFHandler.task4"), list.size());
if (monitor.isCanceled()) {
monitor.setTaskName(Messages.getString("file.XLFHandler.task5"));
throw new OperationCanceledException();
}
StringBuffer re = new StringBuffer();
re.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
re.append("<tmx version=\"1.4b\">");
re.append("<header creationtool=\"" + Constants.TMX_CREATIONTOOL + "\" creationtoolversion=\"" + Constants.TMX_CREATIONTOOLVERSION + "\" srclang=\"" + srcLang + "\" adminlang=\"en\" datatype=\"xml\" o-tmf=\"XLIFF\" segtype=\"paragraph\">");
re.append("</header>");
re.append("<body>");
VTDNav vn = getVTDNavByRowId(list.get(0)).duplicateNav();
AutoPilot ap = new AutoPilot(vn);
VTDUtils vu = null;
try {
vu = new VTDUtils(vn);
} catch (NavException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger15"), e);
}
String strChangeBy = systemUser;
String strProp = getProjectProp(project);
boolean isNull = true;
AutoPilot ap2 = new AutoPilot(vn);
ap2.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
for (String rowId : list) {
if (monitor.isCanceled()) {
re = null;
monitor.setTaskName(Messages.getString("file.XLFHandler.task5"));
throw new OperationCanceledException();
}
String tgtText = getTgtContent(rowId);
String srcText = getSrcContent(rowId);
if (srcText == null || srcText.trim().equals("") || tgtText == null || tgtText.trim().equals("")) {
monitor.worked(1);
continue;
}
if (isSendToTM(rowId)) {
monitor.worked(1);
continue;
}
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
try {
ap.selectXPath(tuXPath);
ap.evalXPath();
} catch (XPathParseException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
monitor.done();
} catch (XPathEvalException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
monitor.done();
} catch (NavException e) {
LOGGER.error(Messages.getString("file.XLFHandler.logger16"), e);
monitor.done();
}
re.append("<tu ");
// 添加tu的属性
String id = vu.getValue("./@id");
String creationDate = CommonFunction.retTMXDate();
String changeDate = creationDate;
re.append("tuid=\"" + genTuId(rowId, id) + "\" ");
re.append("changedate=\"" + changeDate + "\" changeid=\"" + strChangeBy + "\" ");
re.append("creationdate=\"" + creationDate + "\" creationid=\"" + strChangeBy + "\" >\n");
try {
// Fixed Bug #2290 入库时未添加批注信息 by Jason
Vector<NoteBean> notes = getNotes(rowId);
if (notes != null) {
for (NoteBean bean : notes) {
String lang = bean.getLang();
if (lang != null && !lang.equals("")) {
re.append("<note xml:lang='" + lang + "'>" + bean.getNoteText() + "</note>");
} else {
re.append("<note>" + bean.getNoteText() + "</note>");
}
}
}
} catch (NavException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
} catch (XPathParseException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
} catch (XPathEvalException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
}
// 添加上下文prop
re.append("<prop type=\"x-preContext\">");
vn.push();
try {
ap2.selectXPath(tuXPath);
} catch (XPathParseException e) {
LOGGER.error("XPathParseException", e);
e.printStackTrace();
}
re.append(getContext(vu, ap2, contextSize, true));
re.append("</prop>");
vn.pop();
vn.push();
re.append("<prop type=\"x-nextContext\">");
ap2.resetXPath();
re.append(getContext(vu, ap2, contextSize, false));
re.append("</prop>");
vn.pop();
// 添加自定义属性
re.append(strProp);
re.append("<tuv xml:lang=\"" + srcLang + "\">\n");
// String sourceTextXp = "./source/text()";
// re.append("<seg>").append(vu.getValue(ap, sourceTextXp)).append("</seg>");
re.append("<seg>").append(srcText.trim()).append("</seg>\n");
re.append("</tuv>\n");
// 添加tuv(target)
re.append("<tuv xml:lang=\"" + tgtLang + "\">\n");
// String targetTextXp = "./target/text()";
// re.append("<seg>").append(vu.getValue(ap, targetTextXp)).append("</seg>");
re.append("<seg>").append(tgtText.trim()).append("</seg>\n");
re.append("</tuv>\n");
re.append("</tu>\n");
monitor.worked(1);
isNull = false;
}
re.append("</body>");
re.append("</tmx>");
monitor.done();
if (isNull) {
return null;
} else {
return re;
}
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method generateTMXString.
public String generateTMXString(String systemUser, String filePath, String srcLang, String tgtLang, String original, VTDNav vn, boolean isAddApprove, boolean isAddSignedOff, boolean isAddTranslate, boolean isAddDraft, boolean isAddLocked, int contextSize, IProject project) {
String strChangeBy = systemUser;
StringBuffer re = new StringBuffer();
String xpath = "/xliff/file[@original='" + original + "' and @source-language='" + srcLang + "' and @target-language='" + tgtLang + "']/body//trans-unit[source/text()!='' or source/*]";
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
AutoPilot ap2 = new AutoPilot(vn);
ap2.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
VTDUtils vu = null;
boolean isNull = true;
boolean blnLocked = false;
boolean blnDraft = false;
boolean blnTranslate = false;
boolean blnApproved = false;
boolean blnSignedOff = false;
String srcText;
String tgtText;
String sendToTM;
String locked;
String state;
String approved;
String id;
String creationDate;
String changeDate;
// 添加自定义属性
String strProp = getProjectProp(project);
try {
ap.selectXPath(xpath);
vu = new VTDUtils(vn);
vn.push();
while (ap.evalXPath() != -1) {
srcText = vu.getValue("./source/text()");
tgtText = vu.getValue("./target/text()");
// 源文或目标文本段为空(包括空格),不添加到记忆库
if (srcText == null || srcText.trim().equals("") || tgtText == null || tgtText.trim().equals("")) {
continue;
}
// 标记为“不添加到记忆库”状态的文本段不添加到记忆库(默认为 yes,表示添加到记忆库)。
sendToTM = vu.getCurrentElementAttribut("hs:send-to-tm", "yes");
if (sendToTM.equalsIgnoreCase("no")) {
continue;
}
// 锁定状态
locked = vu.getCurrentElementAttribut("translate", "yes");
blnLocked = false;
if (locked.equalsIgnoreCase("no")) {
blnLocked = isAddLocked;
}
blnDraft = false;
blnTranslate = false;
blnApproved = false;
blnSignedOff = false;
if (locked.equalsIgnoreCase("yes")) {
state = vu.getValue("./target/@state");
approved = vu.getCurrentElementAttribut("approved", "no");
// 是否添加草稿到记忆库
if (state != null && state.equalsIgnoreCase("new") && approved.equalsIgnoreCase("no")) {
blnDraft = isAddDraft;
}
// 是否添加完成翻译文本段到记忆库
if (approved.equalsIgnoreCase("no") && state != null && state.equalsIgnoreCase("translated")) {
blnTranslate = isAddTranslate;
}
// 是否添加已批准文本段到记忆库
if (approved.equalsIgnoreCase("yes") && (state == null || !state.equalsIgnoreCase("signed-off"))) {
blnApproved = isAddApprove;
}
// 是否添加已签发文本段到记忆库
if (state != null && state.equalsIgnoreCase("signed-off")) {
blnSignedOff = isAddSignedOff;
}
}
if (!(blnLocked || blnDraft || blnTranslate || blnApproved || blnSignedOff)) {
continue;
}
id = vu.getCurrentElementAttribut("id", null);
re.append("<tu ");
// 添加tu的属性
creationDate = CommonFunction.retTMXDate();
changeDate = creationDate;
re.append("tuid=\"" + (original.hashCode() + "-" + id.hashCode()) + "\" ");
re.append("changedate=\"" + changeDate + "\" changeid=\"" + strChangeBy + "\" ");
re.append("creationdate=\"" + creationDate + "\" creationid=\"" + strChangeBy + "\" >\n");
// 添加上下文prop
re.append("<prop type=\"x-preContext\">");
ap2.selectXPath("/xliff/file[@original='" + original + "' and @source-language='" + srcLang + "' and @target-language='" + tgtLang + "']/body//trans-unit[@id='" + id + "']");
re.append(getContext(vu, ap2, contextSize, true));
re.append("</prop>\n");
re.append("<prop type=\"x-nextContext\">");
re.append(getContext(vu, ap2, contextSize, false));
re.append("</prop>\n");
re.append(strProp);
re.append("<tuv xml:lang=\"" + srcLang + "\" >\n");
re.append("<seg>").append(srcText.trim()).append("</seg>\n");
re.append("</tuv>\n");
// 添加tuv(target)
re.append("<tuv xml:lang=\"" + tgtLang + "\">\n");
re.append("<seg>").append(tgtText.trim()).append("</seg>\n");
re.append("</tuv>\n");
re.append("</tu>\n");
isNull = false;
}
} catch (XPathEvalException e) {
LOGGER.error(MessageFormat.format(Messages.getString("file.XLFHandler.logger17"), filePath), e);
} catch (NavException e) {
LOGGER.error(MessageFormat.format(Messages.getString("file.XLFHandler.logger17"), filePath), e);
} catch (XPathParseException e) {
LOGGER.error(MessageFormat.format(Messages.getString("file.XLFHandler.logger17"), filePath), e);
} finally {
vn.pop();
}
if (isNull) {
return null;
} else {
return re.toString();
}
}
Aggregations