use of net.heartsome.cat.ts.ui.docx.common.TagBean in project translationstudio8 by heartsome.
the class ImportDocx method getSrcOrTgtText.
/**
* UNDO 导入时,如果行为空的情况未判断
* 获取源文及译文的内容
*
* 标记是以超链接的形式出现的,超链接有两种形式,第二种为
<w:r w:rsidR="00690EF7">
<w:fldChar w:fldCharType="begin" />
</w:r>
<w:r w:rsidR="00690EF7">
<w:instrText xml:space="preserve"> HYPERLINK "http://null" \o "<g id='0'>" </w:instrText>
</w:r>
<w:r w:rsidR="00690EF7">
<w:fldChar w:fldCharType="separate" />
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:color w:val="7F7F7F" />
<w:sz w:val="22" />
</w:rPr>
<w:t>►</w:t>
</w:r>
<w:r w:rsidR="00690EF7">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" />
<w:color w:val="7F7F7F" />
<w:sz w:val="22" />
</w:rPr>
<w:fldChar w:fldCharType="end" />
</w:r>
* @param bean
*/
private List<Object> getSrcOrTgtText() throws Exception {
Map<Integer, Object> elementMap = new TreeMap<Integer, Object>();
vn.push();
boolean isTag = false;
extendAP.selectXPath("./descendant::node()[name()!='w:hyperlink']/node()[name()='w:r' or name()='w:hyperlink']");
int index = -1;
String text = "";
while (extendAP.evalXPath() != -1) {
String nodeName = vn.toString(vn.getCurrentIndex());
if ("w:r".equals(nodeName)) {
if (isTag) {
// 处理第二种超链接的情况
vn.push();
otherAP.selectXPath("./w:instrText");
if (otherAP.evalXPath() != -1) {
if ((index = vn.getText()) != -1) {
// 类似这种 HYPERLINK "http://null" \o "<g id='0'>"
String tagStr = text = vn.toRawString(index);
if (tagStr.trim().indexOf("HYPERLINK") == 0) {
tagStr = tagStr.substring(tagStr.indexOf(" \\o ") + 3).trim();
tagStr = tagStr.replace("\"", "");
tagStr = TextUtil.resetSpecialString(tagStr);
elementMap.put(index, new TagBean(tagStr));
}
}
}
vn.pop();
} else {
vn.push();
otherAP.selectXPath("./w:t");
if (otherAP.evalXPath() != -1) {
if ((index = vn.getText()) != -1) {
text = vn.toRawString(index);
if (text.length() > 0) {
elementMap.put(index, text);
}
}
}
vn.pop();
}
vn.push();
otherAP.selectXPath("./w:fldChar");
if (otherAP.evalXPath() != -1) {
if ((index = vn.getAttrVal("w:fldCharType")) != -1) {
if ("begin".equals(vn.toString(index))) {
isTag = true;
} else if ("end".equals(vn.toString(index))) {
isTag = false;
}
}
}
vn.pop();
} else if ("w:hyperlink".equals(nodeName)) {
vn.push();
if ((index = vn.getAttrVal("w:tooltip")) != -1) {
text = vn.toRawString(index);
if (text.length() > 0) {
text = TextUtil.resetSpecialString(text);
elementMap.put(index, new TagBean(text));
}
}
vn.pop();
}
}
vn.pop();
// 开始处理 map,将里面分散的句子连在一起
List<Object> elementList = new ArrayList<Object>();
StringBuffer textSB = new StringBuffer();
for (Entry<Integer, Object> entry : elementMap.entrySet()) {
Object object = entry.getValue();
if (object instanceof TagBean) {
if (textSB.length() > 0) {
elementList.add(textSB.toString());
textSB = new StringBuffer();
}
elementList.add(object);
} else if (object instanceof String) {
textSB.append(object);
}
}
if (textSB.length() > 0) {
elementList.add(textSB.toString());
}
return elementList;
}
use of net.heartsome.cat.ts.ui.docx.common.TagBean in project translationstudio8 by heartsome.
the class ExportDocx method ananysisTextWithTag.
/**
* 处理标记,将 <g> 与 <ph> 等将记换成 docx 支持的标记形式,以及将所有文本段处理成 r 节点形式
* 该方法与 {@link #getTagFromSrcText} 类似
* @return
*/
public String ananysisTextWithTag(String text) {
StringBuffer rNodeStrSB = new StringBuffer();
// this is <g id='1'/>a</g><ph>this is a phTag</ph> test
// 先不管是什么标记,按照 xml 的标记,把文本段中的节点提取出来。
int index = text.indexOf("<");
Map<Integer, TagBean> tagMap = new LinkedHashMap<Integer, TagBean>();
int tagType = -1;
while (index != -1) {
int endIndex = text.length();
int end_1 = text.indexOf(">", index + 1);
int end_2 = text.indexOf("\\>", index + 1);
endIndex = end_1 != -1 ? (endIndex < end_1 ? endIndex : end_1) : endIndex;
endIndex = end_2 != -1 ? (endIndex < end_2 ? endIndex : end_2) : endIndex;
String tagText = text.substring(index, endIndex + 1);
tagType = DocxCommonFuction.getTagType(tagText);
tagMap.put(index, new TagBean(index, endIndex, tagType, tagText));
index = text.indexOf("<", index + 1);
}
// 开始处理 <ph> 标记的特殊情况
TagBean bean = null;
Integer[] keyArray = tagMap.keySet().toArray(new Integer[] {});
int key = -1;
for (int i = 0; i < keyArray.length; i++) {
key = keyArray[i];
bean = tagMap.get(key);
if (bean.getText().indexOf("<ph") != -1 && bean.getTagType() == DocxConstant.PAIRSTAR) {
int start = bean.getStartIndex();
int end = bean.getEndIndex();
int nextPhEndTagIdx = i + 1;
while (nextPhEndTagIdx <= keyArray.length) {
TagBean nextBean = tagMap.get(keyArray[nextPhEndTagIdx]);
tagMap.remove(keyArray[nextPhEndTagIdx]);
if (nextBean.getText().indexOf("</ph") != -1) {
int nextEnd = nextBean.getEndIndex();
end = nextEnd;
String newText = text.substring(start, end + 1);
bean.setTagType(DocxConstant.NOTPAIR);
bean.setEndIndex(end);
bean.setText(newText);
i = nextPhEndTagIdx;
break;
}
nextPhEndTagIdx++;
}
}
}
// 开始将标记与文本段进行拼装 ► ◇ ◄
int textStart = 0;
bean = null;
for (Entry<Integer, TagBean> entry : tagMap.entrySet()) {
// 这是测试用代码
// bean = entry.getValue();
// int tagStart = bean.getStartIndex();
// tagSB.append(text.substring(textStart, tagStart));
//
// tagSB.append("(");
// if (bean.getTagType() == DocxConstant.PAIRSTAR) {
// tagSB.append("►");
// }else if (bean.getTagType() == DocxConstant.PAIREND) {
// tagSB.append("◄");
// }else {
// tagSB.append("◇");
// }
// tagSB.append(bean.getText() + ")");
// textStart = bean.getEndIndex() + 1;
bean = entry.getValue();
int tagStart = bean.getStartIndex();
rNodeStrSB.append(createRNode(text.substring(textStart, tagStart), true, null));
if (bean.getTagType() == DocxConstant.PAIRSTAR) {
rNodeStrSB.append(createStartTag(bean.getText()));
} else if (bean.getTagType() == DocxConstant.PAIREND) {
rNodeStrSB.append(createEndTag(bean.getText()));
} else {
rNodeStrSB.append(createNotPairTag(bean.getText()));
}
textStart = bean.getEndIndex() + 1;
}
rNodeStrSB.append(createRNode(text.substring(textStart, text.length()), true, null));
return rNodeStrSB.toString();
}
use of net.heartsome.cat.ts.ui.docx.common.TagBean 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.TagBean in project translationstudio8 by heartsome.
the class ImportDocxDialog method getTagFromSrcText.
/**
* 该方法与类 {@link #ananysisTextWithTag} 类似
* @param srcText
* @return
*/
private List<String> getTagFromSrcText(String srcText) {
List<String> tagList = new ArrayList<String>();
if (srcText == null) {
return tagList;
}
// 先不管是什么标记,按照 xml 的标记,把文本段中的节点提取出来。
int index = srcText.indexOf("<");
Map<Integer, TagBean> tagMap = new LinkedHashMap<Integer, TagBean>();
int tagType = -1;
while (index != -1) {
int endIndex = srcText.length();
int end_1 = srcText.indexOf(">", index + 1);
int end_2 = srcText.indexOf("\\>", index + 1);
endIndex = end_1 != -1 ? (endIndex < end_1 ? endIndex : end_1) : endIndex;
endIndex = end_2 != -1 ? (endIndex < end_2 ? endIndex : end_2) : endIndex;
String tagText = srcText.substring(index, endIndex + 1);
if (tagText.indexOf("/>") != -1) {
tagType = DocxConstant.NOTPAIR;
} else if (tagText.indexOf("</") != -1) {
tagType = DocxConstant.PAIREND;
} else {
tagType = DocxConstant.PAIRSTAR;
}
tagMap.put(index, new TagBean(index, endIndex, tagType, tagText));
index = srcText.indexOf("<", index + 1);
}
// 开始处理 <ph> 标记的特殊情况
TagBean bean = null;
Integer[] keyArray = tagMap.keySet().toArray(new Integer[] {});
int key = -1;
for (int i = 0; i < keyArray.length; i++) {
key = keyArray[i];
bean = tagMap.get(key);
if (bean.getText().indexOf("<ph") != -1 && bean.getTagType() == DocxConstant.PAIRSTAR) {
int start = bean.getStartIndex();
int end = bean.getEndIndex();
int nextPhEndTagIdx = i + 1;
while (nextPhEndTagIdx <= keyArray.length) {
TagBean nextBean = tagMap.get(keyArray[nextPhEndTagIdx]);
tagMap.remove(keyArray[nextPhEndTagIdx]);
if (nextBean.getText().indexOf("</ph") != -1) {
int nextEnd = nextBean.getEndIndex();
end = nextEnd;
String newText = srcText.substring(start, end + 1);
bean.setTagType(DocxConstant.NOTPAIR);
bean.setEndIndex(end);
bean.setText(newText);
i = nextPhEndTagIdx;
break;
}
nextPhEndTagIdx++;
}
}
}
// 开始将所有标记装入结果集合中
bean = null;
for (Entry<Integer, TagBean> entry : tagMap.entrySet()) {
tagList.add(entry.getValue().getText());
}
return tagList;
}
use of net.heartsome.cat.ts.ui.docx.common.TagBean in project translationstudio8 by heartsome.
the class ImportDocxDialog method importDocxToXliffList.
/**
* 将数据从 docx 中导入 xliff 文件。
*/
private void importDocxToXliffList(List<RowBean> rowList) throws Exception {
String rowId = "";
StringBuffer contentSB = new StringBuffer();
for (RowBean bean : rowList) {
rowId = bean.getRowId();
if (bean.isLocked()) {
continue;
}
if (rowId == null || rowId.length() <= 0) {
continue;
}
ap.selectXPath(RowIdUtil.parseRowIdToXPath(rowId));
if (ap.evalXPath() != -1) {
// 开始处理源文本
contentSB = new StringBuffer();
vn.push();
ap.selectXPath("./source");
if (ap.evalXPath() != -1) {
String header = vu.getElementHead();
contentSB.append(header);
for (Object object : bean.getSrcElement()) {
if (object instanceof TagBean) {
contentSB.append(((TagBean) object).getText());
} else {
contentSB.append(object);
}
}
contentSB.append("</source>");
xm.remove();
}
vn.pop();
// 开始处理目标文本段
ap.selectXPath("./target");
if (ap.evalXPath() != -1) {
String header = vu.getElementHead();
contentSB.append(header);
for (Object object : bean.getTgtElement()) {
if (object instanceof TagBean) {
contentSB.append(((TagBean) object).getText());
} else {
contentSB.append(object);
}
}
contentSB.append("</target>");
xm.remove();
xm.insertAfterElement(contentSB.toString());
} else {
contentSB.append("<target>");
for (Object object : bean.getTgtElement()) {
if (object instanceof TagBean) {
contentSB.append(((TagBean) object).getText());
} else {
contentSB.append(object);
}
}
contentSB.append("</target>");
xm.insertBeforeTail(contentSB.toString());
}
}
}
tempXlfHandler.saveAndReparse(xm, xliffFullPath);
parseXliff(tempXlfHandler);
// 再处理状态与批注的问题
ananysisStatusAndComment(rowList);
tempXlfHandler.saveAndReparse(xm, xliffFullPath);
}
Aggregations