use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method getNodeHeader.
/**
* 获取指定节点的头
* @param xlfPath
* @param nodeName
* 节点名称
* @param nodeXPath
* 节点路径
* @return robert 2011-10-20
*/
public String getNodeHeader(String xlfPath, String nodeName, String nodeXPath) {
VTDNav vn = vnMap.get(xlfPath);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
String xliffNodeHeader = "";
try {
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath(nodeXPath);
VTDUtils vu = new VTDUtils(vn);
while (ap.evalXPath() != -1) {
xliffNodeHeader = vu.getElementHead();
}
xliffNodeHeader += "\n</" + nodeName + ">";
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return xliffNodeHeader;
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method resetSplitSegment.
/**
* 恢复分割的文本段
* @param rowId
* 唯一标识
* @param tuFragment
* 翻译单元的原始XML文本;
*/
public void resetSplitSegment(String rowId, String tuFragment) {
String groupXPath = RowIdUtil.parseRowIdToGroupXPath(rowId);
String fileName = RowIdUtil.getFileNameByRowId(rowId);
VTDNav vn = vnMap.get(fileName);
AutoPilot ap = new AutoPilot(vn);
try {
ap.selectXPath(groupXPath);
if (ap.evalXPath() != -1) {
XMLModifier xm = new XMLModifier(vn);
// 添加 trans-unit 节点
xm.insertBeforeElement(tuFragment);
// 删除 group 节点
xm.remove(vn.getElementFragment());
saveAndReparse(xm, fileName);
int index = rowIds.indexOf(rowId + "-1");
// 添加分割前的RowId
rowIds.add(index, rowId);
rowIds.remove(rowId + "-1");
// 移除分割后的RowId
rowIds.remove(rowId + "-2");
}
} 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();
}
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method updateDocumentInfo.
/**
* 修改文档属性
* @param fileInfo
* file节点的信息
* @param projectInfo
* project prop-group 的信息
* @param customerInfo
* customer prop-group 的信息;
*/
public void updateDocumentInfo(String fileName, Map<String, String> fileInfo, Map<String, String> projectInfo, Map<String, String> customerInfo) {
if (fileInfo == null || fileInfo.size() == 0) {
return;
}
Object value = fileInfo.get(DocumentPropertiesKeys.ORIGINAL);
if (value != null) {
VTDNav vn = vnMap.get(fileName);
AutoPilot ap = new AutoPilot(vn);
String original = escapeTag(value.toString());
try {
ap.selectXPath("//file[@original='" + original + "']");
if (ap.evalXPath() != -1) {
// 定位到相应的file节点
XMLModifier xm = new XMLModifier(vn);
value = fileInfo.get(DocumentPropertiesKeys.TARGET_LANGUAGE);
if (value != null) {
// 修改 target-language 属性
String targetLanguage = escapeTag(value.toString());
int attrIdx = vn.getAttrVal("target-language");
if (attrIdx != -1) {
xm.updateToken(attrIdx, targetLanguage);
} else {
xm.insertAttribute(" target-language=\"" + targetLanguage + "\" ");
}
}
// 定位到 header 节点
ap.selectXPath("./header");
if (ap.evalXPath() == -1) {
saveAndReparse(xm, fileName);
return;
}
value = fileInfo.get(DocumentPropertiesKeys.SKL);
if (value != null) {
// 修改 skl 骨架文件路径
String skl = escapeTag(value.toString());
vn.push();
ap.selectXPath("./skl/external-file");
if (ap.evalXPath() != -1) {
int attrIdx = vn.getAttrVal("href");
if (attrIdx != -1) {
xm.updateToken(attrIdx, skl);
} else {
xm.insertAttribute(" href=\"" + skl + "\" ");
}
}
vn.pop();
}
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
String propGroup = "";
if (projectInfo != null && projectInfo.size() > 0) {
vn.push();
// 先删除之前的 project prop-group.
ap.selectXPath("./hs:prop-group[@name='project']");
if (ap.evalXPath() != -1) {
xm.remove(vn.getElementFragment());
}
vn.pop();
// 保存新的 project prop-group.
propGroup += "<hs:prop-group name=\"project\">";
for (Entry<String, String> entry : projectInfo.entrySet()) {
propGroup += "<hs:prop prop-type=\"" + escapeTag(entry.getKey()) + "\">" + escapeTag(entry.getValue()) + "</hs:prop>";
}
propGroup += "</hs:prop-group>";
}
if (customerInfo != null && customerInfo.size() > 0) {
// 先删除之前的 customer prop-group.
vn.push();
ap.selectXPath("./hs:prop-group[@name='customer']");
if (ap.evalXPath() != -1) {
xm.remove(vn.getElementFragment());
}
vn.pop();
// 保存新的 customer prop-group.
propGroup += "<hs:prop-group name=\"customer\">";
for (Entry<String, String> entry : customerInfo.entrySet()) {
propGroup += "<hs:prop prop-type=\"" + escapeTag(entry.getKey()) + "\">" + escapeTag(entry.getValue()) + "</hs:prop>";
}
propGroup += "</hs:prop-group>";
}
// 执行修改
if (!"".equals(propGroup)) {
xm.insertAfterHead(propGroup);
ap.selectXPath("//hs:prop-group");
if (ap.evalXPath() == -1) {
// 不存在namespace“xmlns:hs”
ap.selectXPath("/xliff");
if (ap.evalXPath() != -1) {
xm.insertAttribute(" xmlns:hs=\"" + hsR7NSUrl + "\" ");
}
}
}
// 保存文件并更新VTDNav对象
saveAndReparse(xm, fileName);
}
} 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();
}
}
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method saveAsText.
/**
* 存为纯文本
* @param fileName
* @param out
* @param elementName
* @return ;
*/
public boolean saveAsText(String fileName, String out, String elementName) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(out));
VTDNav vn = vnMap.get(fileName);
AutoPilot ap = new AutoPilot(vn);
VTDUtils vu = new VTDUtils(vn);
ap.selectXPath("/xliff/file");
while (ap.evalXPath() != -1) {
AutoPilot subAp = new AutoPilot(vn);
subAp.selectXPath("descendant::trans-unit[source/text()!='']");
while (subAp.evalXPath() != -1) {
if ("target".equals(elementName)) {
String approvedValue = vu.getValue("./@approved");
approvedValue = approvedValue == null ? "no" : approvedValue;
if (!"yes".equalsIgnoreCase(approvedValue)) {
// 未批准
continue;
}
}
String value = vu.getValue("./" + elementName + "/text()");
if (value != null) {
bos.write((value + "\n").getBytes());
}
}
}
return true;
} catch (FileNotFoundException e) {
LOGGER.error("", e);
e.printStackTrace();
return false;
} catch (IOException e) {
LOGGER.error("", e);
e.printStackTrace();
return false;
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
return false;
} catch (XPathParseException e) {
LOGGER.error("", e);
e.printStackTrace();
return false;
} catch (XPathEvalException e) {
LOGGER.error("", e);
e.printStackTrace();
return false;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
}
}
use of com.ximpleware.XPathEvalException in project translationstudio8 by heartsome.
the class XLFHandler method getTransUnit.
/**
* 获取指定索引的翻译单元对象。
* @param rowId
* 行的唯一标识,由三部分拼成
*/
public TransUnitBean getTransUnit(String rowId) {
if (tuSizeMap == null || rowId == null) {
return null;
}
String tuNode = RowIdUtil.parseRowIdToXPath(rowId);
if (tuNode == null) {
return null;
}
VTDUtils vu = null;
VTDNav vn = getVTDNavByRowId(rowId);
try {
vu = new VTDUtils(vn);
} catch (NavException e1) {
String errorMsg = Messages.getString("file.XLFHandler.logger4");
LOGGER.error(errorMsg, e1);
return null;
}
try {
if (vu.pilot(tuNode) != -1) {
// 导航到 trans-unit 节点
String tuid = "";
String srcText = "";
String srcContent = "";
String tgtText = "";
String tgtContent = "";
Hashtable<String, String> srcProps = new Hashtable<String, String>();
Hashtable<String, String> tgtProps = new Hashtable<String, String>();
// 取翻译单元所有属性
vn.push();
Hashtable<String, String> tuProps = vu.getCurrentElementAttributs();
vn.pop();
if (tuProps != null) {
tuid = tuProps.get("id");
}
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
// 取翻译单元源节点完整文本,含内部标记。
vn.push();
if (vu.pilot(ap, "./source") != -1) {
// 导航到 Source 子节点
// 源节点完整内容。
srcContent = vu.getElementContent();
// 源节点纯文本内容。
srcText = getTUPureText(vu.getVTDNav());
// 源节点属性集合。
srcProps = vu.getCurrentElementAttributs();
}
ap.resetXPath();
vn.pop();
// 取翻译单元目标节点完整文本,含内部标记。
vn.push();
if (vu.pilot(ap, "./target") != -1) {
// 导航到 Target 子节点
// 目标节点完整内容。
tgtContent = vu.getElementContent();
// 目标节点纯文本内容。
tgtText = getTUPureText(vu.getVTDNav());
// 目标节点属性集合。
tgtProps = vu.getCurrentElementAttributs();
}
vn.pop();
// 获取所有的 alttrans 匹配节点。
vn.push();
Vector<AltTransBean> matches = getAltTrans(vu);
vn.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);
vn.push();
tub.setNotes(getNotes(vu));
vn.pop();
vn.push();
tub.setPropgroups(getPrpoGroups(vu));
vn.pop();
return tub;
}
} catch (XPathEvalException e) {
String errorMsg = Messages.getString("file.XLFHandler.logger5");
LOGGER.error(errorMsg, e);
return null;
} catch (NavException e) {
String errorMsg = Messages.getString("file.XLFHandler.logger6");
LOGGER.error(errorMsg, e);
return null;
} catch (XPathParseException e) {
String errorMsg = Messages.getString("file.XLFHandler.logger7");
LOGGER.error(errorMsg, e);
return null;
}
return null;
}
Aggregations