use of com.ximpleware.ModifyException in project translationstudio8 by heartsome.
the class XLFHandler method updateNote.
public void updateNote(Map<String, List<String>> mapRowIdByFileName, String oldFrom, String oldText, String newFrom, String newText) {
oldFrom = StringUtilsBasic.checkNullStr(oldFrom);
oldText = StringUtilsBasic.checkNullStr(oldText);
newFrom = StringUtilsBasic.checkNullStr(newFrom);
newText = StringUtilsBasic.checkNullStr(newText);
StringBuffer subXPath = new StringBuffer();
if (!oldFrom.equals("")) {
subXPath.append("@from='" + oldFrom + "' and ");
}
subXPath.insert(0, "/note[");
subXPath.append("text()='");
try {
XMLModifier xm = new XMLModifier();
VTDUtils vu = new VTDUtils();
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);
xm.bind(vn);
vu.bind(vn);
for (String rowId : entry.getValue()) {
StringBuffer sbTuXPath = new StringBuffer(RowIdUtil.parseRowIdToXPath(rowId));
xm = vu.update(null, xm, sbTuXPath.append(subXPath).append(oldText).append("']/text()").toString(), newText);
xm = vu.update(null, xm, sbTuXPath.append(subXPath).append(oldText).append("']/@from").toString(), newFrom, VTDUtils.CREATE_IF_NOT_EXIST);
}
saveAndReparse(xm, fileName);
}
} catch (NavException e) {
LOGGER.error("", e);
e.printStackTrace();
} catch (ModifyException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
use of com.ximpleware.ModifyException in project translationstudio8 by heartsome.
the class XLFHandler method updateDocumentInfo.
/**
* 修改文档属性
* @param fileInfo
* file节点的信息
* @param projectInfo
* project prop-group 的信息
* @param customerInfo
* customer prop-group 的信息;
*/
public void updateDocumentInfo(String fileName, Map<String, String> fileInfo, Map<String, String> projectInfo, Map<String, String> customerInfo) {
if (fileInfo == null || fileInfo.size() == 0) {
return;
}
Object value = fileInfo.get(DocumentPropertiesKeys.ORIGINAL);
if (value != null) {
VTDNav vn = vnMap.get(fileName);
AutoPilot ap = new AutoPilot(vn);
String original = escapeTag(value.toString());
try {
ap.selectXPath("//file[@original='" + original + "']");
if (ap.evalXPath() != -1) {
// 定位到相应的file节点
XMLModifier xm = new XMLModifier(vn);
value = fileInfo.get(DocumentPropertiesKeys.TARGET_LANGUAGE);
if (value != null) {
// 修改 target-language 属性
String targetLanguage = escapeTag(value.toString());
int attrIdx = vn.getAttrVal("target-language");
if (attrIdx != -1) {
xm.updateToken(attrIdx, targetLanguage);
} else {
xm.insertAttribute(" target-language=\"" + targetLanguage + "\" ");
}
}
// 定位到 header 节点
ap.selectXPath("./header");
if (ap.evalXPath() == -1) {
saveAndReparse(xm, fileName);
return;
}
value = fileInfo.get(DocumentPropertiesKeys.SKL);
if (value != null) {
// 修改 skl 骨架文件路径
String skl = escapeTag(value.toString());
vn.push();
ap.selectXPath("./skl/external-file");
if (ap.evalXPath() != -1) {
int attrIdx = vn.getAttrVal("href");
if (attrIdx != -1) {
xm.updateToken(attrIdx, skl);
} else {
xm.insertAttribute(" href=\"" + skl + "\" ");
}
}
vn.pop();
}
ap.declareXPathNameSpace(hsNSPrefix, hsR7NSUrl);
String propGroup = "";
if (projectInfo != null && projectInfo.size() > 0) {
vn.push();
// 先删除之前的 project prop-group.
ap.selectXPath("./hs:prop-group[@name='project']");
if (ap.evalXPath() != -1) {
xm.remove(vn.getElementFragment());
}
vn.pop();
// 保存新的 project prop-group.
propGroup += "<hs:prop-group name=\"project\">";
for (Entry<String, String> entry : projectInfo.entrySet()) {
propGroup += "<hs:prop prop-type=\"" + escapeTag(entry.getKey()) + "\">" + escapeTag(entry.getValue()) + "</hs:prop>";
}
propGroup += "</hs:prop-group>";
}
if (customerInfo != null && customerInfo.size() > 0) {
// 先删除之前的 customer prop-group.
vn.push();
ap.selectXPath("./hs:prop-group[@name='customer']");
if (ap.evalXPath() != -1) {
xm.remove(vn.getElementFragment());
}
vn.pop();
// 保存新的 customer prop-group.
propGroup += "<hs:prop-group name=\"customer\">";
for (Entry<String, String> entry : customerInfo.entrySet()) {
propGroup += "<hs:prop prop-type=\"" + escapeTag(entry.getKey()) + "\">" + escapeTag(entry.getValue()) + "</hs:prop>";
}
propGroup += "</hs:prop-group>";
}
// 执行修改
if (!"".equals(propGroup)) {
xm.insertAfterHead(propGroup);
ap.selectXPath("//hs:prop-group");
if (ap.evalXPath() == -1) {
// 不存在namespace“xmlns:hs”
ap.selectXPath("/xliff");
if (ap.evalXPath() != -1) {
xm.insertAttribute(" xmlns:hs=\"" + hsR7NSUrl + "\" ");
}
}
}
// 保存文件并更新VTDNav对象
saveAndReparse(xm, fileName);
}
} 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 (UnsupportedEncodingException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
}
use of com.ximpleware.ModifyException 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 com.ximpleware.ModifyException 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 com.ximpleware.ModifyException in project translationstudio8 by heartsome.
the class ProjectConfiger method save.
/**
* 保存文件
* @param xm
* XMLModifier对象
* @param fileName
* 文件名
* @return 是否保存成功;
*/
private boolean save(XMLModifier xm, File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入文件
xm.output(bos);
bos.close();
fos.close();
VTDGen vg = new VTDGen();
try {
if (vg.parseFile(projCfgFile.getPath(), true)) {
vu.bind(vg.getNav());
} else {
throw new ParseException();
}
} catch (NavException e) {
logger.error("", e);
} catch (ParseException e) {
logger.error("", e);
}
return true;
} catch (ModifyException e) {
logger.error("", e);
e.printStackTrace();
} catch (TranscodeException e) {
logger.error("", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("", e);
e.printStackTrace();
}
return false;
}
Aggregations