use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method getNodeAttributes.
/**
* 获取指定xpath节点的所有属性,如果没有属性,则返回null
* @param xlfPath
* @param nodeXpath
* @return ;
*/
public Hashtable<String, String> getNodeAttributes(String xlfPath, String nodeXpath) {
Hashtable<String, String> attributes = new Hashtable<String, String>();
VTDNav vn = vnMap.get(xlfPath);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
try {
AutoPilot ap = new AutoPilot(vn);
VTDUtils vu = new VTDUtils(vn);
ap.selectXPath(nodeXpath);
while (ap.evalXPath() != -1) {
attributes = vu.getCurrentElementAttributs();
}
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
return attributes;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLFHandler method getNotes.
public Vector<NoteBean> getNotes(String rowId) throws NavException, XPathParseException, XPathEvalException {
Vector<NoteBean> notes = new Vector<NoteBean>();
VTDNav vn = getVTDNavByRowId(rowId);
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
ap.selectXPath(tuXPath + "/note");
while (ap.evalXPath() != -1) {
NoteBean note = new NoteBean(vu.getElementContent());
note.setLang(vu.getCurrentElementAttribut("xml:lang", null));
note.setFrom(vu.getCurrentElementAttribut("from", null));
note.setPriority(vu.getCurrentElementAttribut("priority", null));
note.setAnnotates(vu.getCurrentElementAttribut("annotates", null));
note.setApplyCurrent(vu.getCurrentElementAttribut("hs:apply-current", "Yes"));
notes.add(0, note);
}
if (notes.isEmpty()) {
notes = null;
}
return notes;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class XLPHandler method getItems.
/**
* 得到配置文件中记录的所有项
* @param attrName
* 属性名
* @param values
* 属性值
* @return ;
*/
public List<Map<String, String>> getItems(String attrName, List<String> values) {
HashMap<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
AutoPilot ap = new AutoPilot(vn);
try {
VTDUtils vu = new VTDUtils(vn);
ap.selectXPath("/xlfedit-project/item[@" + attrName + "]");
XMLModifier xm = new XMLModifier(vn);
boolean isModified = false;
while (ap.evalXPath() != -1) {
int index = vn.getAttrVal(attrName);
String value = index != -1 ? vn.toString(index) : "";
if (values.contains(value) && !map.containsKey(value)) {
Map<String, String> item = vu.getCurrentElementAttributs();
if (item == null) {
item = new Hashtable<String, String>();
}
map.put(value, item);
} else {
// 去除无用的记录
xm.remove();
isModified = true;
}
}
if (isModified) {
save(xm);
}
for (String value : values) {
if (!map.containsKey(value)) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(attrName, value);
map.put(value, item);
}
}
} 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 (ParseException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (IOException e) {
LOGGER.error("", e);
e.printStackTrace();
}
// 按顺序添加
ArrayList<Map<String, String>> items = new ArrayList<Map<String, String>>();
for (String value : values) {
items.add(map.get(value));
}
return items;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getFilteredTUText.
/**
* 获取trans-unit节点过滤后的值,过滤条件为不包括上下文匹配,不包括完全匹配,不包括已锁文本,过滤条件在首选项中设置
* @return Map<String, String > 六个键值对,srcPureText --> 源文本的纯文本,tarPureText --> 目标文本的纯文本, srcContent -->
* 源文本的内容,tarContent --> 目标文本的内容 , srcTag --> 源文本的标记, tarTag --> 目标文本的标记;
* 如果返回的是null,则标志source节点无内容,这对与行号就不自增
* @param xlfPath
* @param nodeXpath
* @param filterMap 过滤条件
* @return ;
*/
public QATUDataBean getFilteredTUText(String xlfPath, String nodeXpath, Map<String, Boolean> filterMap) {
QATUDataBean bean = new QATUDataBean();
VTDNav vn = vnMap.get(xlfPath);
AutoPilot ap = new AutoPilot(vn);
Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
AutoPilot childAp = new AutoPilot(vn);
try {
VTDUtils vUtils = new VTDUtils(vn);
ap.selectXPath(nodeXpath);
while (ap.evalXPath() != -1) {
String rowId = RowIdUtil.getRowId(vn, xlfPath);
bean.setRowId(rowId);
vn.push();
//取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回null,没有source节点,也返回null
childAp.selectXPath("./source");
if (childAp.evalXPath() != -1) {
//因为标准里面只有一个source,因此此处用if不用while
String srcContent = vUtils.getElementContent();
//如果源文本为空或无值,则返回null
if (srcContent == null || "".equals(srcContent)) {
return null;
} else {
bean.setSrcPureText(getTUPureText(vn));
bean.setSrcContent(srcContent);
}
} else {
return null;
}
childAp.resetXPath();
vn.pop();
//首先过滤,如果有不应包括的文本段,则返回一个空对象
if (!filterTheTU(vn, filterMap)) {
bean.setPassFilter(false);
return bean;
}
//下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
childAp.selectXPath("./target");
if (childAp.evalXPath() != -1) {
//因为标准里面只有一个target,因此此处用if不用while
String tarContent = vUtils.getElementContent();
//如果源文本为空或无值,则返回空对象
if (tarContent == null || "".equals(tarContent)) {
bean.setTgtContent(tarContent);
return bean;
} else {
bean.setTgtContent(tarContent);
bean.setTgtPureText(getTUPureText(vn));
}
} else {
return bean;
}
childAp.resetXPath();
}
} catch (Exception e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger13"), e);
}
return bean;
}
use of net.heartsome.xml.vtdimpl.VTDUtils in project translationstudio8 by heartsome.
the class QAXmlHandler method getFilteredTUPureText.
/**
* 获取trans-unit节点过滤后的值,过滤条件为不包括上下文匹配,不包括完全匹配,不包括已锁文本,过滤条件在首选项中设置
* @return Map<String, String > 两个键值对,srcPureText --> 源文本的纯文本,tarPureText --> 目标文本的纯文本
* 如果返回的是null,则标志source节点无内容,这对与行号就不自增
*/
public Map<String, String> getFilteredTUPureText(String xlfPath, String nodeXpath, Map<String, Boolean> filterMap) {
Map<String, String> filteredTuPureTextMap = new HashMap<String, String>();
VTDNav vn = vnMap.get(xlfPath);
AutoPilot ap = new AutoPilot(vn);
Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
AutoPilot childAp = new AutoPilot(vn);
try {
VTDUtils vUtils = new VTDUtils(vn);
ap.selectXPath(nodeXpath);
while (ap.evalXPath() != -1) {
vn.push();
//取出源文本的纯文本之前,先查看其内容是否为空,若为空,则返回,没有source节点,也返回null
childAp.selectXPath("./source");
if (childAp.evalXPath() != -1) {
//因为标准里面只有一个source,因此此处用if不用while
String srcContent = vUtils.getElementContent();
//如果源文本为空或无值,则返回null
if (srcContent == null || "".equals(srcContent)) {
return null;
} else {
filteredTuPureTextMap.put("srcPureText", getTUPureText(vn));
}
} else {
return null;
}
childAp.resetXPath();
vn.pop();
//首先过滤,如果有不应包括的文本段,则返回空
if (!filterTheTU(vn, filterMap)) {
return filteredTuPureTextMap;
}
//下面获取目标文本的纯文本,在之前先检查目标文本是否为空或为空值,若是,则返回null,若没有target节点,也返回空
childAp.selectXPath("./target");
if (childAp.evalXPath() != -1) {
//因为标准里面只有一个target,因此此处用if不用while
String tarContent = vUtils.getElementContent();
//如果源文本为空或无值,则返回null
if (tarContent == null || "".equals(tarContent)) {
return filteredTuPureTextMap;
} else {
filteredTuPureTextMap.put("tarPureText", getTUPureText(vn));
}
} else {
return filteredTuPureTextMap;
}
childAp.resetXPath();
}
} catch (Exception e) {
logger.error(Messages.getString("qa.QAXmlHandler.logger12"), e);
}
return filteredTuPureTextMap;
}
Aggregations