use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method getPropValue.
/**
* 得到TU下指定节点的指定属性值
* @param rowIdList
* 用来生成Xpath的rowId集合
* @param subXPath
* 定位到tu下的子节点属性的xpath,例如:"/target/@state"
* @return 由rowId与得到的属性值的映射map。<br/>
* key: rowId; value: 属性值;
*/
private Map<String, String> getPropValue(List<String> rowIdList, String subXPath) {
if (rowIdList == null) {
return null;
}
if (subXPath.lastIndexOf('/') > subXPath.lastIndexOf('@')) {
// 此subXPath要获取的并不是属性
LOGGER.error(Messages.getString("file.XLFHandler.logger9"));
return null;
}
Map<String, String> propValueMap = new HashMap<String, String>();
AutoPilot ap = new AutoPilot();
try {
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
int index = -1;
for (String rowId : rowIdList) {
VTDNav vn = getVTDNavByRowId(rowId);
vn.push();
ap.bind(vn);
// 根据RowId得到定位到该翻译单元的XPath
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
// 可以定位TU下的任何子节点的属性
String propXpath = tuXPath + subXPath;
ap.selectXPath(propXpath);
if ((index = ap.evalXPath()) != -1) {
propValueMap.put(rowId, vn.toNormalizedString(index + 1));
} else {
propValueMap.put(rowId, null);
}
vn.pop();
}
} 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 propValueMap;
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method getNodeAttribute.
/**
* 获取某属性名的属性值
* @param xlfPath
* @param nodeXpath
* @param attrName
* @return robert 2011-11-02
*/
public String getNodeAttribute(String xlfPath, String nodeXpath, String attrName) {
String attribute = "";
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) {
attribute = vu.getCurrentElementAttribut(attrName, "");
}
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
return attribute;
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method getTgtPropValue.
/**
* 得到Target节点属性的值
* @param rowId
* 行的唯一标识
* @return ;
*/
public String getTgtPropValue(String rowId, String propName) {
VTDNav vn = getVTDNavByRowId(rowId);
String tuXPath = RowIdUtil.parseRowIdToXPath(rowId);
try {
VTDUtils vu = new VTDUtils(vn);
return vu.getValue(tuXPath + "/target/@" + propName);
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
}
return null;
}
use of com.ximpleware.VTDNav 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 com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method addDataToXlf.
/**
* 将数据添加到切割后新生成的文件中,并且是添加到指定节点的尾部
* @param newXlfPath
* @param data
* 要添加的内容
* @param toXpath
* 要添加的位置 robert 2011-10-21
*/
public void addDataToXlf(String newXlfPath, String data, String toXpath) {
VTDNav vn = vnMap.get(newXlfPath);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + newXlfPath);
try {
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath(toXpath);
if (ap.evalXPath() != -1) {
XMLModifier xm = new XMLModifier(vn);
xm.insertBeforeTail((data + "\n").getBytes("UTF8"));
// VTDUtils vUtils = new VTDUtils(vn);
// 更新新生成的xliff文件,并重新加载并更新VTDVNav
// vnMap.put(newXlfPath, vUtils.updateVTDNav(xm, newXlfPath));
saveAndReparse(xm, newXlfPath);
}
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
Aggregations