use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method getSplitTuData.
/**
* 针对xliff文件分割,获取相应的trans-unit的内容,包括其他结点,例如group。这种处理方式,针对body第一级子节点。robert 2012-05-11
* 备注:先通过file与body第一级子节点定位到起始位置,再通过startRowId进行判断要获取的节点。直到endRowId或file节点结束时为止。
* @param xlfPath
* @param fileIdx
* 要获取节点内容的当前file节点的序列号(从1开始)
* @param startBodyChildIdx
* 要获取节点内容的起始位置(针对body节点的第一级子节点,从1开始)
* @param startRowId
* 起始rowId或者标记(如notFirst,这表示为file节点的第一个位置开始)
* @param endRowId
* 结束位置的rowId
* @param isFirstFileNode
* 是否是新生成的分割文件的第一个file节点。
* @param isLastOfFile
* 是否是被分割xliff文件的最后,因为这时要处理一些在分割点之外的节点。
* @return
*/
public String getSplitTuData(String xlfPath, int fileIdx, int startBodyChildIdx, String startRowId, String endRowId, boolean isFirstFileNode, boolean isLastOfFile) {
StringBuffer dataSB = new StringBuffer();
List<String[]> removeTuList;
boolean isEnd = false;
VTDNav vn = vnMap.get(xlfPath);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
AutoPilot ap = new AutoPilot(vn);
AutoPilot checkAp = new AutoPilot(vn);
// 从这个tuXpath所定位的tu节点开始,到endRowId所定位的tu节点结束,或到一个file节点的尾端结束
String tuXpath = "/xliff/file[" + fileIdx + "]/body/node()[" + startBodyChildIdx + "]";
try {
ap.selectXPath(tuXpath);
if (ap.evalXPath() != -1) {
VTDUtils vu = new VTDUtils(vn);
// 起始结点的数据也要计算在内,也要判断有tu子节点的情况
String firstNodeFrag = vu.getElementFragment();
// 判断当前节点是否是tu节点,如果是,则判断是否等于startRowId,如果不是,那就进行其子节点,获取相关节点
int index = vn.getCurrentIndex();
// 是否结束,效果与isEnd相似,但是针对情况不同。
boolean isTheLast = false;
if (index != -1) {
if ("trans-unit".equals(vn.toString(index))) {
if (isFirstFileNode) {
if (!RowIdUtil.getRowId(vn, xlfPath).equals(startRowId)) {
firstNodeFrag = "";
}
}
// 针对两个分割点相连的情况,判断起始rowId是否等于终止rowId,如果是,则退出程序
if (endRowId.equals(RowIdUtil.getRowId(vn, xlfPath))) {
isTheLast = true;
}
} else {
removeTuList = new LinkedList<String[]>();
// 开始循环tu节点
vn.push();
checkAp.resetXPath();
checkAp.selectXPath("descendant::trans-unit");
boolean isStart = false;
while (checkAp.evalXPath() != -1) {
// 如果这是分割后的文件的第一个file节点,那么,它的起始rowId才不会为空。
String curRowId = RowIdUtil.getRowId(vn, xlfPath);
if (isFirstFileNode) {
if (!isStart && startRowId.equals(curRowId)) {
isStart = true;
}
if (!isStart) {
removeTuList.add(new String[] { "", vu.getElementFragment() });
}
}
// 在没有开始,或已经结束这个区间之外的所有TU节点都要被删除,注意这个开始节点与结束节点的判断位置,因为结束点也必须包括在内
if (isEnd) {
// 由于文件名变更以后,rowId也会变更,故存入的格式为original与tuID
String original = RowIdUtil.getOriginalByRowId(curRowId);
String tuId = RowIdUtil.getTUIdByRowId(curRowId);
removeTuList.add(new String[] { RowIdUtil.getRowId("{0}", original, tuId), vu.getElementFragment() });
}
if (!isEnd && endRowId.equals(RowIdUtil.getRowId(vn, xlfPath))) {
isEnd = true;
}
}
if (removeTuList.size() >= 1) {
for (String[] tuRowIdAndFrag : removeTuList) {
String tuPlaceHolder = "";
if (!"".equals(tuRowIdAndFrag[0])) {
tuPlaceHolder = "<hs:TuPlaceHolder rowId=\"" + tuRowIdAndFrag[0] + "\" />";
}
firstNodeFrag = firstNodeFrag.replace(tuRowIdAndFrag[1], tuPlaceHolder);
}
}
vn.pop();
}
}
dataSB.append(firstNodeFrag);
if (isTheLast || isEnd) {
return dataSB.toString();
}
// 开始向下循环每一个body的第一级子节点
String followNodeXpath = "following-sibling::node()";
ap.resetXPath();
ap.selectXPath(followNodeXpath);
while (ap.evalXPath() != -1) {
String curNodeFrag = vu.getElementFragment();
index = -1;
index = vn.getCurrentIndex();
if (index != -1) {
String nodeName = vn.toString(vn.getCurrentIndex());
// 如果名称等于trans-unit,那么标志这个节点就是tu节点
if ("trans-unit".equals(nodeName)) {
String rowId = RowIdUtil.getRowId(vn, xlfPath);
if (endRowId.equals(rowId)) {
// 如果这是最后一处分割节点,那么,继续循环,获取最后几个在分割点RowId之外的非TU节点
if (!isLastOfFile) {
dataSB.append(curNodeFrag);
break;
}
}
} else {
// 否则,循环其子节点,查看其中是否存在子节点
isEnd = false;
removeTuList = new LinkedList<String[]>();
vn.push();
checkAp.selectXPath("descendant::trans-unit");
while (checkAp.evalXPath() != -1) {
String curRowId = RowIdUtil.getRowId(vn, xlfPath);
if (isEnd) {
String original = RowIdUtil.getOriginalByRowId(curRowId);
String tuId = RowIdUtil.getTUIdByRowId(curRowId);
removeTuList.add(new String[] { RowIdUtil.getRowId("{0}", original, tuId), vu.getElementFragment() });
} else {
if (endRowId.equals(curRowId)) {
isEnd = true;
}
}
}
checkAp.resetXPath();
vn.pop();
if (isEnd) {
// 如果已经到达结束点,那么删除该删除的tu节点
for (String[] tuRowIdAndFrag : removeTuList) {
String tuPlaceHolder = "<hs:TuPlaceHolder rowId=\"" + tuRowIdAndFrag[0] + "\" />";
curNodeFrag = curNodeFrag.replace(tuRowIdAndFrag[1], tuPlaceHolder);
}
if (!isLastOfFile) {
dataSB.append(curNodeFrag);
break;
}
}
}
}
dataSB.append(curNodeFrag);
}
}
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
return dataSB.toString();
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method getSortRowIdList.
/**
* 获取排序的 rowId 集合 robert 2013-02-18
* @param textMap
* @param isAsc
* @return
*/
public List<String> getSortRowIdList(HashSet<String> rowIdSet, String columnName, final boolean isAsc) {
List<String> sortList = new LinkedList<String>();
// 先通过界面上所显示的文本段,
Map<String, String> textMap = new HashMap<String, String>();
AutoPilot ap = new AutoPilot();
AutoPilot childAP = new AutoPilot();
for (Entry<String, VTDNav> entry : vnMap.entrySet()) {
String filePath = entry.getKey();
VTDNav vn = entry.getValue();
ap.bind(vn);
childAP.bind(vn);
try {
String xpath = XPATH_ALL_TU;
ap.selectXPath(xpath);
while (ap.evalXPath() != -1) {
String rowId = RowIdUtil.getRowId(vn, filePath);
if (rowIdSet.contains(rowId)) {
vn.push();
childAP.selectXPath("./" + columnName);
String tgtText = "";
if (childAP.evalXPath() != -1) {
tgtText = getTUPureText(vn);
}
// 排序时应忽略段首段末空格,忽略大小写
textMap.put(rowId, tgtText.trim().toLowerCase());
vn.pop();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
List<Entry<String, String>> mapList = new ArrayList<Entry<String, String>>(textMap.entrySet());
//排序
Collections.sort(mapList, new Comparator<Entry<String, String>>() {
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
int result = o1.getValue().compareTo(o2.getValue());
return isAsc ? result : -result;
}
});
for (Entry<String, String> entry : mapList) {
sortList.add(entry.getKey());
}
return sortList;
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method validateMultiFileNodes.
/**
* 检验多个 file 节点是否存在 document 属性组,以及该属性组下是否存在 original 属性(用于转换 XLIFF 的源文件为 OpenOffice 和 MSOffice2007 的情况)
* @param fileName
* @return ;
*/
public boolean validateMultiFileNodes(String fileName) {
VTDNav vn = vnMap.get(fileName);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + fileName);
try {
AutoPilot subAp = new AutoPilot(vn);
subAp.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
subAp.selectXPath("./header/hs:prop-group[@name=\"document\"]/hs:prop[@prop-type=\"original\"]");
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/xliff/file");
while (ap.evalXPath() != -1) {
vn.push();
subAp.resetXPath();
if (subAp.evalXPath() != -1) {
String documentOriginal = vu.getElementContent();
if (documentOriginal == null || documentOriginal.equals("")) {
return false;
}
} else {
return false;
}
vn.pop();
}
return true;
} 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 false;
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method getProps.
// 获取当前节点下的属性元素子节点。
private Vector<PropBean> getProps(VTDUtils vu) throws XPathParseException, XPathEvalException, NavException {
VTDNav vn = vu.getVTDNav();
Vector<PropBean> props = new Vector<PropBean>();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("./prop");
while (ap.evalXPath() != -1) {
String proptype = vu.getCurrentElementAttribut("prop-type", null);
String value = vu.getElementContent();
String lang = vu.getCurrentElementAttribut("xml:lang", null);
PropBean prop = new PropBean(proptype, value, lang);
props.add(prop);
}
ap.resetXPath();
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
ap.selectXPath("./hs:prop");
while (ap.evalXPath() != -1) {
String proptype = vu.getCurrentElementAttribut("prop-type", null);
String value = vu.getElementContent();
String lang = vu.getCurrentElementAttribut("xml:lang", null);
PropBean prop = new PropBean(proptype, value, lang);
props.add(prop);
}
if (props.isEmpty()) {
props = null;
}
return props;
}
use of com.ximpleware.VTDNav in project translationstudio8 by heartsome.
the class XLFHandler method vnToXpath.
/**
* 将当前的vn定位到指的xpath Add by Jason
* @param xlfPath
* 文件路径
* @param xpath
* 如/xliff/file/body/trans-unit[1],则将当前VN定位到第一个trans-unit节点;
*/
public void vnToXpath(String xlfPath, String xpath) {
VTDNav vn = vnMap.get(xlfPath);
Assert.isNotNull(vn, Messages.getString("file.XLFHandler.msg4") + xlfPath);
try {
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath(xpath);
ap.evalXPath();
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
Aggregations