use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method updateAltTrans.
/**
* 更新alt-trans节点,删除指定类型的alt-trans(以toolid区分类型),重新写入新的alt-trans 内容
* @param rowId
* 当前标识
* @param newAltTrans
* 新的alt-trans内容
* @param oldAltTransToolId
* 旧的alt-trans toolId,会删除此toolid的所有alt-trans;
*/
public void updateAltTrans(String rowId, List<AltTransBean> newAltTrans, List<String> oldAltTransToolId) {
String fileName = RowIdUtil.getFileNameByRowId(rowId);
VTDUtils vu;
XMLModifier xm = null;
try {
vu = new VTDUtils(vnMap.get(fileName));
xm = new XMLModifier(vu.getVTDNav());
updateAltTrans(vu, xm, rowId, newAltTrans, oldAltTransToolId);
} catch (NavException e) {
LOGGER.error("", e);
} catch (ModifyException e) {
LOGGER.error("", e);
}
saveAndReparse(xm, fileName);
}
use of net.heartsome.xml.vtdimpl.VTDUtils 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 net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method deleteNote.
/**
* 删除批注
* @param rowId
* 行的唯一标识
* @param note
* 批注;
*/
public void deleteNote(final HashMap<String, Vector<NoteBean>> mapNote) {
HashMap<String, List<String>> mapRowIdAndXpath = new HashMap<String, List<String>>();
Iterator<Entry<String, Vector<NoteBean>>> it = mapNote.entrySet().iterator();
List<String> lstRowId = new ArrayList<String>();
while (it.hasNext()) {
Entry<String, Vector<NoteBean>> entry = (Entry<String, Vector<NoteBean>>) it.next();
String rowId = entry.getKey();
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
for (NoteBean bean : entry.getValue()) {
String from = bean.getFrom();
String applyCurrent = bean.getApplyCurrent();
StringBuffer subXPath = new StringBuffer();
if (from != null && !from.equals("")) {
subXPath.append("@from='").append(from).append("' and ");
}
// 只有是所有句段时才加该属性
if (applyCurrent != null && applyCurrent.equalsIgnoreCase("No")) {
subXPath.append("@hs:apply-current='").append(applyCurrent).append("' and ");
}
subXPath.insert(0, "/note[");
subXPath.append("text()='");
if (bean.getApplyCurrent().equals("Yes")) {
List<String> lstXpath = mapRowIdAndXpath.get(rowId);
if (lstXpath == null) {
lstXpath = new ArrayList<String>();
mapRowIdAndXpath.put(rowId, lstXpath);
}
subXPath.insert(0, tuXPath);
subXPath.append(bean.getNoteText()).append("']");
lstXpath.add(subXPath.toString());
if (!lstRowId.contains(rowId)) {
lstRowId.add(rowId);
}
} else {
// 删除所有句段的批注
List<String> rowIds = getRowIds();
if (!lstRowId.containsAll(rowIds)) {
lstRowId.clear();
lstRowId.addAll(rowIds);
}
for (String strRowId : rowIds) {
StringBuffer strTuXPath = new StringBuffer(RowIdUtil.parseRowIdToXPath(strRowId));
List<String> lstXpath = mapRowIdAndXpath.get(strRowId);
if (lstXpath == null) {
lstXpath = new ArrayList<String>();
mapRowIdAndXpath.put(strRowId, lstXpath);
}
lstXpath.add(strTuXPath.append(subXPath).append(bean.getNoteText()).append("']").toString());
}
}
}
}
XMLModifier xm = new XMLModifier();
VTDUtils vu = new VTDUtils();
Map<String, List<String>> map = RowIdUtil.groupRowIdByFileName(lstRowId);
Iterator<Entry<String, List<String>>> iterator = map.entrySet().iterator();
try {
while (iterator.hasNext()) {
Entry<String, List<String>> entry = (Entry<String, List<String>>) iterator.next();
String fileName = entry.getKey();
VTDNav vn = vnMap.get(fileName);
xm.bind(vn);
vu.bind(vn);
List<String> rowIdList = entry.getValue();
for (String rowId : rowIdList) {
List<String> lstXpath = mapRowIdAndXpath.get(rowId);
if (lstXpath != null) {
AutoPilot ap = new AutoPilot(vu.getVTDNav());
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
for (String xpath : lstXpath) {
xm = vu.delete(ap, xm, xpath);
}
}
}
saveAndReparse(xm, fileName);
}
} catch (ModifyException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
use of net.heartsome.xml.vtdimpl.VTDUtils 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;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method saveAsHtml.
/**
* 存为HTML
* @param fileName
* @param out
* @param elementName
* @return ;
*/
public boolean saveAsHtml(String fileName, String out, String elementName) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(out));
//$NON-NLS-1$
bos.write("<html>\n".getBytes());
//$NON-NLS-1$
bos.write(" <head>\n".getBytes());
//$NON-NLS-1$
bos.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n".getBytes());
//$NON-NLS-1$ //$NON-NLS-2$
bos.write((" <title>" + fileName + "</title>\n").getBytes());
//$NON-NLS-1$
bos.write(" </head>\n".getBytes());
//$NON-NLS-1$
bos.write("<body>\n".getBytes());
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(("<p>" + value + "</p>\n").getBytes());
}
}
}
bos.write("</body>\n</html>\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();
}
}
}
}
Aggregations