use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class QAXmlHandler method getNodeFrag.
/**
* 获取整个节点,包括其头部,其子节点,其文本
* @param xlfPath
* @param nodeXPath 节点的xpath
* @return
* robert 2011-10-21
*/
public String getNodeFrag(String xlfPath, String nodeXPath) {
VTDNav vn = vnMap.get(xlfPath);
Assert.isNotNull(vn, Messages.getString("qa.QAXmlHandler.msg1") + xlfPath);
String xliffNodeContent = "";
try {
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath(nodeXPath);
VTDUtils vu = new VTDUtils(vn);
if (ap.evalXPath() != -1) {
xliffNodeContent = vu.getElementFragment();
}
} catch (XPathParseException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger9"), e);
} catch (NavException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger11"), e);
} catch (XPathEvalException e) {
e.printStackTrace();
logger.error(Messages.getString("qa.QAXmlHandler.logger10"), e);
}
return xliffNodeContent;
}
use of com.ximpleware.XPathParseException 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.XPathParseException in project translationstudio8 by heartsome.
the class DatabaseConfiger method getAllServerConfig.
/**
* 获取数据库服务器配置文件中保存的所有服务器信息
* @return Map<数据库类型,List<数据库配置信息> 参考{@link DatabaseModelBean};
*/
public Map<String, List<DatabaseModelBean>> getAllServerConfig() {
Map<String, List<DatabaseModelBean>> map = new HashMap<String, List<DatabaseModelBean>>();
try {
AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
tempAp.selectXPath("/servers/server");
while (tempAp.evalXPath() != -1) {
String type = vu.getCurrentElementAttribut("type", "");
if (map.containsKey(type)) {
continue;
} else {
map.put(type, getServersConfigByType(type));
}
}
} catch (XPathParseException e) {
logger.error("", e);
} catch (XPathEvalException e) {
logger.error("", e);
} catch (NavException e) {
logger.error("", e);
}
return map;
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class ExportFilterStoreConfiger method getFilterRule.
/**
* 获取过滤规则
* @param ruleType
* "TMX" 或者 "TBX"
* @return 空的集合或者带有内容的集合;
*/
public List<ExportFilterBean> getFilterRule(String ruleType) {
Assert.isLegal(ruleType.equals("TMX") || ruleType.equals("TBX"), Messages.getString("core.ExportFilterStoreConfiger.msg1"));
List<ExportFilterBean> filterList = new ArrayList<ExportFilterBean>();
try {
AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
tempAp.selectXPath("/filter/" + ruleType + "/content");
while (tempAp.evalXPath() != -1) {
Hashtable<String, String> attrs = vu.getCurrentElementAttributs();
ExportFilterBean bean = new ExportFilterBean();
bean.setFilterType(ruleType);
bean.setFilterName(attrs.get("name"));
bean.setFilterConnector(attrs.get("type"));
AutoPilot ap = new AutoPilot(vu.getVTDNav());
ap.selectXPath("./option");
List<ExportFilterComponentBean> cBeanList = new ArrayList<ExportFilterComponentBean>();
while (ap.evalXPath() != -1) {
Hashtable<String, String> oAttrs = vu.getCurrentElementAttributs();
ExportFilterComponentBean cBean = new ExportFilterComponentBean(ruleType);
cBean.setOptionName(oAttrs.get("name"));
cBean.setCurrentExpression(oAttrs.get("operator"));
cBean.setFilterVlaue(oAttrs.get("value"));
cBeanList.add(cBean);
}
if (cBeanList.size() == 0) {
// deleteFilterRuleByName(bean.getFilterName(),ruleType);
continue;
}
bean.setFilterOption(cBeanList);
filterList.add(bean);
}
} catch (XPathParseException e) {
logger.error("", e);
} catch (XPathEvalException e) {
logger.error("", e);
} catch (NavException e) {
logger.error("", e);
}
return filterList;
}
use of com.ximpleware.XPathParseException in project translationstudio8 by heartsome.
the class ExportFilterStoreConfiger method isFilterNameExist.
/**
* 检查filterName是否已经存在
* @param filterName
* @param ruleType
* "TMX" 或者 "TBX"
* @return ;
*/
public boolean isFilterNameExist(String filterName, String ruleType) {
Assert.isLegal(ruleType.equals("TMX") || ruleType.equals("TBX"), Messages.getString("core.ExportFilterStoreConfiger.msg1"));
try {
AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
tempAp.selectXPath("/filter/" + ruleType + "/content[@name='" + filterName + "']");
if (tempAp.evalXPath() != -1) {
return true;
}
} catch (XPathParseException e) {
logger.error("", e);
} catch (XPathEvalException e) {
logger.error("", e);
} catch (NavException e) {
logger.error("", e);
}
return false;
}
Aggregations