use of com.ximpleware.XMLModifier in project translationstudio8 by heartsome.
the class XLFHandler method addNote.
/**
* 添加批注
* @param rowId
* 行的唯一标识
* @param note
* 批注内容;
*/
public void addNote(Map<String, List<String>> mapRowIdByFileName, String note, String from, boolean isApplyCurrent) {
try {
StringBuffer insertValue = new StringBuffer("<note");
if (from != null) {
insertValue.append(" from='").append(from).append("'");
}
if (!isApplyCurrent) {
insertValue.append(" hs:apply-current='No'");
}
insertValue.append(">").append(StringUtilsBasic.checkNullStr(note)).append("</note>");
VTDUtils vu = new VTDUtils();
XMLModifier xm = new XMLModifier();
Iterator<Entry<String, List<String>>> it = mapRowIdByFileName.entrySet().iterator();
while (it.hasNext()) {
Entry<String, List<String>> entry = it.next();
String fileName = entry.getKey();
VTDNav vn = vnMap.get(fileName);
vu.bind(vn);
xm.bind(vn);
for (String rowId : entry.getValue()) {
StringBuffer xpath = new StringBuffer(RowIdUtil.parseRowIdToXPath(rowId));
xm = vu.insert(null, xm, xpath.append("/text()").toString(), insertValue.toString());
}
saveAndReparse(xm, fileName);
}
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
use of com.ximpleware.XMLModifier 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();
}
}
use of com.ximpleware.XMLModifier 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 com.ximpleware.XMLModifier in project translationstudio8 by heartsome.
the class QAXmlHandler method deleteAllNode.
/**
* 根据指定的 xpaht 删除所有的节点
* @param filePath
* @param nodeXpath
* @return
*/
public boolean deleteAllNode(String filePath, String nodeXpath) {
VTDNav vn = vnMap.get(filePath);
Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + filePath);
try {
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath(nodeXpath);
XMLModifier xm = new XMLModifier(vn);
boolean hasRemoved = false;
while (ap.evalXPath() != -1) {
xm.remove();
hasRemoved = true;
}
if (hasRemoved) {
return saveAndReparse(xm, filePath);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("", e);
}
return false;
}
use of com.ximpleware.XMLModifier in project translationstudio8 by heartsome.
the class QAXmlHandler method addAspellConfig.
/**
* 将语言与词典对添加到 Aspell 配置文件中
* @throws Exception
*/
public void addAspellConfig(String xmlPath, String lang, String dic, boolean isUpdate) throws Exception {
VTDNav vn = vnMap.get(xmlPath);
XMLModifier xm = null;
VTDUtils vu = new VTDUtils(vn);
if (isUpdate) {
String xpath = "/aspell/aspellDictionaries/" + lang + "/text()";
xm = vu.update(xpath, dic);
} else {
String xpath = "/aspell/aspellDictionaries/text()";
String insertValue = "\n<" + lang + ">" + dic + "</" + lang + ">\n";
xm = vu.insert(xpath, insertValue);
}
vu.bind(xm.outputAndReparse());
saveAndReparse(xm, xmlPath);
}
Aggregations