use of net.heartsome.cat.ts.ui.docx.common.FlagErrorException in project translationstudio8 by heartsome.
the class ImportDocxDialog method beginImport.
/**
* 开始导入功能
*/
public boolean beginImport(final String docxPath) throws Exception {
if (xliffEditor == null || xliffEditor.getXLFHandler() == null) {
xlfHandler = new XLFHandler();
} else {
xlfHandler = xliffEditor.getXLFHandler();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
HsMultiActiveCellEditor.commit(true);
}
});
xm = new XMLModifier(xlfHandler.getVnMap().get(xliffFullPath));
xlfHandler.saveAndReparse(xm, xliffFullPath);
}
// 开始解析 xliff 文件
tempXlfHandler = new XLFHandler();
parseXliff(tempXlfHandler);
// UNDO 这里还应判断导入时两个文件是否对应,
try {
ImportDocx importWord = new ImportDocx(docxPath, xliffFullPath);
List<RowBean> rowList = importWord.getDataFromWord();
hasComment = importWord.isHasComment();
// 现在开始判断每个tu 节点的标记是否异常。若有异常进行提示
rowListFor: for (RowBean rowBean : rowList) {
String rowId = rowBean.getRowId();
if (rowId == null || rowId.length() <= 0) {
continue rowListFor;
}
if (curRowIsLocked(rowId)) {
rowBean.setLocked(true);
continue rowListFor;
}
String srcText = getSrcTextFromXliffByRowId(rowId);
List<String> xliffSrcTagList = getTagFromSrcText(srcText);
List<String> xliffTgtTagList = new ArrayList<String>();
xliffTgtTagList.addAll(xliffSrcTagList);
// 获取 rowBean 中源文的标记
List<TagBean> rowSrcTagList = new ArrayList<TagBean>();
for (Object object : rowBean.getSrcElement()) {
if (object instanceof TagBean) {
rowSrcTagList.add((TagBean) object);
}
}
// 0、首先验证 word 文档中的当前行能否找到与之对应的 xliff 文件
if (srcText == null) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_notFindTU));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
// 1、首先验证源文标记是否缺失
if (rowSrcTagList.size() < xliffSrcTagList.size()) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagLose));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
// 2、验证word源文标记是否多出
if (rowSrcTagList.size() > xliffSrcTagList.size()) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagMore));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
// UNDO 这里还应确定导出的标记是否发生改变
for (TagBean bean : rowSrcTagList) {
String rowTagText = bean.getText();
for (int i = 0; i < xliffSrcTagList.size(); i++) {
String xlfTagText = xliffSrcTagList.get(i);
if (xlfTagText.indexOf(rowTagText) == 0) {
if (!xlfTagText.equals(rowTagText)) {
bean.setText(xlfTagText);
}
xliffSrcTagList.remove(i);
bean.setTagType(DocxCommonFuction.getTagType(xlfTagText));
break;
}
}
}
if (xliffSrcTagList.size() > 0) {
// docx 文档中的标记被更换了
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagNotSame));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
int startTag = 0;
for (TagBean bean : rowSrcTagList) {
if (bean.getTagType() == DocxConstant.PAIRSTAR) {
startTag++;
} else if (bean.getTagType() == DocxConstant.PAIREND) {
startTag--;
}
if (startTag < 0) {
errorList.add(new ErrorBean(rowBean.getIndex(), true, DocxConstant.ERROR_tagPostionError));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
}
// 4、验证 目标文本段中标记错误,或者位置不对应的情况
// 先获得word 中目标文本中的标记
// 获取 rowBean 中源文的标记
List<TagBean> rowTgtTagList = new ArrayList<TagBean>();
for (Object object : rowBean.getTgtElement()) {
if (object instanceof TagBean) {
rowTgtTagList.add((TagBean) object);
}
}
int modifiedTagSum = 0;
for (TagBean bean : rowTgtTagList) {
String rowTagText = bean.getText();
// 因为标记不允许修改,因此在 xliff 中,目标文本中的标记就是源文中的标记
for (int i = 0; i < xliffTgtTagList.size(); i++) {
String xlfTagText = xliffTgtTagList.get(i);
if (xlfTagText.indexOf(rowTagText) == 0) {
if (!xlfTagText.equals(rowTagText)) {
bean.setText(xlfTagText);
}
xliffTgtTagList.remove(i);
modifiedTagSum++;
bean.setTagType(DocxCommonFuction.getTagType(xlfTagText));
break;
}
}
}
if (modifiedTagSum != rowTgtTagList.size()) {
// docx 文档中的标记被更换了
errorList.add(new ErrorBean(rowBean.getIndex(), false, DocxConstant.ERROR_tagNotSame));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
startTag = 0;
for (TagBean bean : rowTgtTagList) {
if (bean.getTagType() == DocxConstant.PAIRSTAR) {
startTag++;
} else if (bean.getTagType() == DocxConstant.PAIREND) {
startTag--;
}
if (startTag < 0) {
errorList.add(new ErrorBean(rowBean.getIndex(), false, DocxConstant.ERROR_tagPostionError));
errorRowSet.add(rowBean.getRowId());
continue rowListFor;
}
}
if (startTag != 0) {
errorList.add(new ErrorBean(rowBean.getIndex(), false, DocxConstant.ERROR_pairTagError));
errorRowSet.add(rowBean.getRowId());
}
}
// 验证完后,开始导入功能,如果有错误提示信息,开始提示
if (errorList.size() > 0) {
StringBuffer errorSB = new StringBuffer();
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip1"));
for (ErrorBean bean : errorList) {
errorSB.append(MessageFormat.format(Messages.getString("ImportDocxDialog.import.errorTip2"), bean.getIndex()));
if (bean.getErrorType() == DocxConstant.ERROR_notFindTU) {
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip3"));
continue;
}
if (bean.isSrc()) {
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip4"));
} else {
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip5"));
}
switch(bean.getErrorType()) {
case DocxConstant.ERROR_tagLose:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip6"));
break;
case DocxConstant.ERROR_tagMore:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip7"));
break;
case DocxConstant.ERROR_tagPostionError:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip8"));
break;
case DocxConstant.ERROR_tagNotSame:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip9"));
break;
case DocxConstant.ERROR_pairTagError:
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip11"));
break;
default:
break;
}
}
errorSB.append(Messages.getString("ImportDocxDialog.import.errorTip10"));
final String errorTip = errorSB.toString();
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
ErrorTipDialog errorDialog = new ErrorTipDialog(getShell(), errorTip);
int result = errorDialog.open();
if (result == IDialogConstants.CANCEL_ID) {
continuImport = false;
}
}
});
if (!continuImport) {
return false;
}
String rowId = "";
// 先将 有错误的文本段进行清除
for (int i = 0; i < rowList.size(); i++) {
rowId = rowList.get(i).getRowId();
if (errorRowSet.contains(rowId)) {
rowList.remove(i);
i--;
}
}
}
importDocxToXliffList(rowList);
if (xliffEditor != null) {
parseXliff(xlfHandler);
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
xliffEditor.reloadData();
HsMultiCellEditorControl.activeSourceAndTargetCell(xliffEditor);
}
});
}
} catch (final FlagErrorException e) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openWarning(Display.getDefault().getActiveShell(), Messages.getString("all.dialog.warning"), e.getMessage());
}
});
LOGGER.error(e.getMessage(), e);
return false;
} catch (Exception e) {
LOGGER.error(Messages.getString("ImportDocxDialog.LOGGER.logger2"), e);
}
return true;
}
use of net.heartsome.cat.ts.ui.docx.common.FlagErrorException in project translationstudio8 by heartsome.
the class ImportDocx method getDataFromWord.
public List<RowBean> getDataFromWord() throws FlagErrorException {
List<RowBean> rowList = new ArrayList<RowBean>();
// 文件标识正确
boolean flagRight = false;
try {
tempFolder = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + new File(docxPath).getName();
docxFolderPath = ZipUtil.upZipFile(docxPath, tempFolder);
documentXmlPath = docxFolderPath + File.separator + "word" + File.separator + "document.xml";
VTDGen vg = new VTDGen();
boolean parseResult = vg.parseFile(documentXmlPath, true);
if (!parseResult) {
throw new Exception(MessageFormat.format(Messages.getString("ImportDocxDialog.ok.parseError"), docxPath));
}
vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
AutoPilot childAP = new AutoPilot(vn);
extendAP = new AutoPilot(vn);
otherAP = new AutoPilot(vn);
commentAP = new AutoPilot(vn);
vu = new VTDUtils(vn);
ap.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
childAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
extendAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
otherAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
commentAP.declareXPathNameSpace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
String xpath = "/w:document/w:body/w:tbl/w:tr";
ap.selectXPath(xpath);
boolean isHeader = true;
while (ap.evalXPath() != -1) {
RowBean bean = new RowBean();
int tcIdx = 0;
vn.push();
childAP.selectXPath("./w:tc");
// 备注:状态只写不读。故,不需要读状态一栏。
while (childAP.evalXPath() != -1) {
tcIdx++;
// 表头是没有数据的,故不读取数据
if (tcIdx == 1) {
// 先取第一列,获取其行号,以及 rowId,如果是头,里面保存的是固定值:heartsomeExportDocxFlag,若未找到,直接退出程序。
if (isHeader) {
boolean validResult = validFileLegalized();
if (!validResult) {
flagRight = true;
throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
} else {
flagRight = true;
}
} else {
getIndexAndRowId(bean);
}
} else if (tcIdx == 2) {
// 第二列,为源文本
if (isHeader) {
String columnType = getColumnType();
if (columnType == null || columnType.trim().isEmpty() || !columnType.contains(DocxConstant.COLUMN_TYPE_src)) {
throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
}
} else {
bean.setSrcElement(getSrcOrTgtText());
}
} else if (tcIdx == 3) {
// 第三列,为目标文本
if (isHeader) {
String columnType = getColumnType();
if (columnType == null || columnType.trim().isEmpty() || !columnType.contains(DocxConstant.COLUMN_TYPE_tgt)) {
throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
}
} else {
bean.setTgtElement(getSrcOrTgtText());
}
} else if (tcIdx == 4) {
if (isHeader) {
String typeStr = getColumnType();
if (typeStr.contains(DocxConstant.COLUMN_TYPE_comment)) {
hasComment = true;
} else if (typeStr.contains(DocxConstant.COLUMN_TYPE_status)) {
hasStatus = true;
}
} else {
if (hasComment && hasStatus) {
getComments(bean);
} else if (hasComment) {
getComments(bean);
}
}
}
}
isHeader = false;
vn.pop();
rowList.add(bean);
}
} catch (FlagErrorException e) {
throw new FlagErrorException(e.getMessage());
} catch (Exception e) {
LOGGER.error("", e);
} finally {
if (!flagRight) {
throw new FlagErrorException(MessageFormat.format(Messages.getString("ImportDocx.msg.flagError"), docxPath));
}
if (new File(docxFolderPath).exists()) {
deleteFileOrFolder(new File(docxFolderPath));
}
}
return rowList;
}
Aggregations