use of com.ximpleware.XMLModifier in project translationstudio8 by heartsome.
the class QAXmlHandler method removeAspellConfig.
/**
* 删除 Aspell 配置文件的内容
* @param xmlPath
* @param lang
* @param dic
*/
public void removeAspellConfig(String xmlPath, String lang) throws Exception {
VTDNav vn = vnMap.get(xmlPath);
AutoPilot ap = new AutoPilot(vn);
XMLModifier xm = new XMLModifier(vn);
String xpath = "/aspell/aspellDictionaries/" + lang + "";
ap.selectXPath(xpath);
if (ap.evalXPath() != -1) {
xm.remove();
saveAndReparse(xm, xmlPath);
}
}
use of com.ximpleware.XMLModifier in project translationstudio8 by heartsome.
the class QAXmlHandler method deleteNode.
/**
* 删除指定的节点,且并重新解析
* @param filePath
* @param nodeXpath
* @return
*/
public boolean deleteNode(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);
if (ap.evalXPath() != -1) {
VTDUtils vu = new VTDUtils(vn);
XMLModifier xm = vu.update(nodeXpath, "");
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 XLFValidator method validateXliffFile.
/**
* 验证 XLIFF 文件的语言对是否与项目的语言对一致。
* @param iFile
* ;
* @throws XPathEvalException
* @throws XPathParseException
* @throws NavException
*/
public static boolean validateXliffFile(IFile iFile) {
shell = Display.getDefault().getActiveShell();
try {
Object[] arrObj = getProjectLang(iFile);
if (arrObj == null) {
return false;
}
String xlfFolderPath = iFile.getProject().getFullPath().append(Constant.FOLDER_XLIFF).toOSString();
String xlfFullPath = iFile.getFullPath().toOSString();
if (!xlfFullPath.startsWith(xlfFolderPath) || iFile.getParent().getFullPath().toOSString().equals(xlfFolderPath)) {
// 该 XLIFF 文件是 XLIFF 目录的直接子文件或者不在 XLIFF 的目录下
if (!blnMsg1) {
MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg1"), xlfFullPath), Messages.getString("file.XLFValidator.toggleStateMsg"), false, null, null);
blnMsg1 = dialog.getToggleState();
}
return false;
}
Language projectSrcLang = (Language) arrObj[0];
@SuppressWarnings("unchecked") List<Language> lstProjectTgtLang = (List<Language>) arrObj[1];
// /test/XLIFF/zh-CN/split/test.xlf (zh-CN 在第三级目录下)
String parentName = xlfFullPath.split(System.getProperty("file.separator").replaceAll("\\\\", "\\\\\\\\"))[3];
Vector<String> languageVector = new Vector<String>();
languageVector.add(parentName);
if (LocaleService.verifyLanguages(languageVector)) {
boolean flag = false;
for (Language lang : lstProjectTgtLang) {
if (lang.getCode().equalsIgnoreCase(parentName)) {
flag = true;
break;
}
}
if (!flag) {
if (!blnMsg1) {
MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg2"), xlfFullPath), Messages.getString("file.XLFValidator.toggleStateMsg"), false, null, null);
blnMsg1 = dialog.getToggleState();
}
return false;
}
} else {
if (!blnMsg1) {
MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg3"), xlfFullPath), Messages.getString("file.XLFValidator.toggleStateMsg"), false, null, null);
blnMsg1 = dialog.getToggleState();
}
return false;
}
String xlfSrcLang = null;
String xlfTgtLang = null;
VTDGen vg = new VTDGen();
XMLModifier xm = null;
boolean isConfirmSrc = false;
boolean isConfirmTgt = false;
String fileOsPath = ResourceUtils.iFileToOSPath(iFile);
boolean result = false;
try {
result = vg.parseFile(fileOsPath, true);
} catch (Exception e) {
}
if (!result) {
MessageDialog.openError(shell, Messages.getString("file.XLFValidator.errorTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.parseError"), fileOsPath));
return false;
}
VTDNav vn = vg.getNav();
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
ap.selectXPath("/xliff/file");
String original = null;
int tempi = ap.evalXPath();
if (tempi == -1) {
MessageDialog.openError(shell, Messages.getString("file.XLFValidator.errorTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.parseError"), fileOsPath));
return false;
}
do {
xlfSrcLang = vu.getCurrentElementAttribut("source-language", null);
xlfTgtLang = vu.getCurrentElementAttribut("target-language", null);
original = vu.getCurrentElementAttribut("original", null);
if (original == null || original.trim().isEmpty()) {
MessageDialog.openWarning(shell, Messages.getString("file.XLFValidator.warningTitle"), MessageFormat.format(Messages.getString("file.XLFValidator.msg10"), xlfFullPath));
return false;
}
String msg = null;
// XLIFF 源语言为空或与项目源语言不一致;
if (xlfSrcLang == null || !xlfSrcLang.equalsIgnoreCase(projectSrcLang.getCode())) {
if (!blnIsOpenConfirmSrc && !isConfirmSrc) {
if (xlfSrcLang == null) {
msg = MessageFormat.format(Messages.getString("file.XLFValidator.msg4"), xlfFullPath, projectSrcLang.getCode());
} else {
msg = MessageFormat.format(Messages.getString("file.XLFValidator.msg5"), xlfFullPath, xlfSrcLang.toLowerCase(), projectSrcLang.getCode());
}
MessageDialogWithToggle dialog = new MessageDialogWithToggle(shell, Messages.getString("file.XLFValidator.msgTitle2"), null, msg, MessageDialog.CONFIRM, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0, Messages.getString("file.XLFValidator.toggleStateMsg"), false);
int returnCode = dialog.open();
if (returnCode == IDialogConstants.YES_ID) {
isConfirmSrc = true;
blnIsOpenConfirmSrcY = true;
} else if (returnCode == IDialogConstants.NO_ID) {
isConfirmSrc = false;
blnIsOpenConfirmSrcY = false;
}
blnIsOpenConfirmSrc = dialog.getToggleState();
}
if ((isConfirmSrc || (blnIsOpenConfirmSrc && blnIsOpenConfirmSrcY)) && xlfSrcLang != null) {
xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@source-language", projectSrcLang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST);
} else if (!isConfirmSrc && !(blnIsOpenConfirmSrc && blnIsOpenConfirmSrcY)) {
return false;
}
}
// XLIFF 目标语言为空,(且其所在的 XLIFF 一级子目录名称是项目的目标语言代码之一,已在上面验证)直接设置
if (xlfTgtLang == null || xlfSrcLang == null) {
// XLIFF 文件中源与目标都为空时,由于 VTD 要求不能在一个位置修改两次,因此使用下面的方式插入源与目标到 file 节点中
if (xlfTgtLang == null && xlfSrcLang == null) {
String attrFragment = new StringBuffer(" source-language=\"").append(projectSrcLang.getCode()).append("\" target-language=\"").append(parentName).append("\"").toString();
// attrName="attrValue"
// ”
// 得到开始标记的结束位置
long i = vn.getOffsetAfterHead();
if (xm == null) {
xm = new XMLModifier(vn);
}
if (vn.getEncoding() < VTDNav.FORMAT_UTF_16BE) {
xm.insertBytesAt((int) i - 1, attrFragment.getBytes());
} else {
xm.insertBytesAt(((int) i - 1) << 1, attrFragment.getBytes());
}
} else if (xlfTgtLang == null) {
xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@target-language", parentName, VTDUtils.CREATE_IF_NOT_EXIST);
} else if (xlfSrcLang == null) {
xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@source-language", projectSrcLang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST);
}
}
if (xlfTgtLang != null) {
// XLIFF 目标语言非空,但未放在对应的目录下。
boolean flag = false;
for (Language lang : lstProjectTgtLang) {
if (lang.getCode().equalsIgnoreCase(xlfTgtLang)) {
flag = true;
break;
}
}
String message = null;
if (!flag) {
message = MessageFormat.format(Messages.getString("file.XLFValidator.msg6"), xlfFullPath, xlfTgtLang, parentName);
} else if (!xlfTgtLang.equalsIgnoreCase(parentName)) {
message = MessageFormat.format(Messages.getString("file.XLFValidator.msg7"), xlfFullPath, xlfTgtLang, parentName);
}
if (!blnIsOpenConfirmTgt && !isConfirmTgt && message != null) {
MessageDialogWithToggle dialog = new MessageDialogWithToggle(shell, Messages.getString("file.XLFValidator.msgTitle2"), null, message, MessageDialog.CONFIRM, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0, Messages.getString("file.XLFValidator.toggleStateMsg"), false);
int returnCode = dialog.open();
if (returnCode == IDialogConstants.YES_ID) {
isConfirmTgt = true;
blnIsOpenConfirmTgtY = true;
} else if (returnCode == IDialogConstants.NO_ID) {
isConfirmTgt = false;
blnIsOpenConfirmTgtY = false;
}
blnIsOpenConfirmTgt = dialog.getToggleState();
}
if ((blnIsOpenConfirmTgt && blnIsOpenConfirmTgtY) || isConfirmTgt) {
xm = vu.update(null, xm, "/xliff/file[@original=\"" + original + "\"]/@target-language", parentName, VTDUtils.CREATE_IF_NOT_EXIST | VTDUtils.PILOT_TO_END);
// vu.bind(xm.outputAndReparse());
} else if (message != null) {
return false;
}
}
// Bug #2329:文件语言更改成项目语言时应同时更改 source 节点的 xml:lang 属性值,如果 target 节点有 xml:lang 属性,也要修改成项目语言
AutoPilot tempAp = new AutoPilot(vn);
tempAp.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
xm = vu.update(tempAp, xm, "/xliff/file[@original=\"" + original + "\"]/body//trans-unit/source/@xml:lang", projectSrcLang.getCode(), VTDUtils.CREATE_IF_NOT_EXIST | VTDUtils.PILOT_TO_END);
xm = vu.update(tempAp, xm, "/xliff/file[@original=\"" + original + "\"]/body//trans-unit/target/@xml:lang", parentName, VTDUtils.PILOT_TO_END);
} while (ap.evalXPath() != -1);
if (xm != null) {
// vu.bind(xm.outputAndReparse());
vu.bind(vu.updateVTDNav(xm, ResourceUtils.iFileToOSPath(iFile)));
}
vg.clear();
iFile.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
} catch (XPathParseException e) {
e.printStackTrace();
logger.error(Messages.getString("file.XLFValidator.logger1"), e);
MessageDialog.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), Messages.getString("file.XLFValidator.msg8"));
return false;
} catch (XPathEvalException e) {
e.printStackTrace();
logger.error(Messages.getString("file.XLFValidator.logger1"), e);
MessageDialog.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), Messages.getString("file.XLFValidator.msg8"));
return false;
} catch (NavException e) {
e.printStackTrace();
logger.error(Messages.getString("file.XLFValidator.logger1"), e);
MessageDialog.openInformation(shell, Messages.getString("file.XLFValidator.msgTitle"), Messages.getString("file.XLFValidator.msg8"));
return false;
} catch (CoreException e) {
logger.error("", e);
} catch (ModifyException e) {
logger.error("", e);
}
return true;
}
use of com.ximpleware.XMLModifier in project translationstudio8 by heartsome.
the class DatabaseConfiger method updateServerConfigById.
/**
* 更新指定id的server配置信息
* @param id
* @param serverBean
* ;
*/
public void updateServerConfigById(String id, DatabaseModelBean serverBean) {
String newValue = generateServerNode(serverBean.getDbType(), serverBean);
XMLModifier xm = vu.update("/servers/server[@id='" + id + "']", newValue);
try {
vu.bind(xm.outputAndReparse());
saveToFile(xm, serverConfigFile);
} catch (Exception e) {
logger.error(Messages.getString("core.DatabaseConfiger.logger2"), e);
}
}
use of com.ximpleware.XMLModifier in project translationstudio8 by heartsome.
the class DatabaseConfiger method addServerConfig.
/**
* 添加一个全新的服务器到配置文件
* @param serverBean
* ;
*/
public void addServerConfig(DatabaseModelBean serverBean) {
serverBean.setId(this.generateServerId());
String content = generateServerNode(serverBean.getDbType(), serverBean);
if (!content.equals("")) {
try {
XMLModifier xm = vu.insert("/servers/text()", content.toString());
vu.bind(xm.outputAndReparse());
saveToFile(xm, serverConfigFile);
} catch (Exception e) {
logger.error("", e);
}
ap.resetXPath();
}
}
Aggregations