use of net.heartsome.cat.ts.ui.docx.ExportDocx in project translationstudio8 by heartsome.
the class ExportDocxDialog method beginExport.
/**
* 开始导出功能
*/
private void beginExport(IProgressMonitor monitor, final String docxPath, boolean exportComment, boolean exportStatus, String expandXpath) throws Exception {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
VTDGen vg = new VTDGen();
if (!vg.parseFile(strXliffFullPath, true)) {
final String parseErrorTip = MessageFormat.format(Messages.getString("ExportDocxDialog.parseError"), ResourceUtils.fileToIFile(docxPath).getFullPath().toOSString());
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(getShell(), Messages.getString("all.dialog.warning"), parseErrorTip);
}
});
throw new Exception(parseErrorTip);
}
monitor.worked(1);
VTDNav vn = vg.getNav();
VTDUtils vu = new VTDUtils(vn);
AutoPilot ap = new AutoPilot(vn);
ap.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
otherAP = new AutoPilot(vn);
otherAP.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
AutoPilot childAP = new AutoPilot(vn);
childAP.declareXPathNameSpace("hs", "http://www.heartsome.net.cn/2008/XLFExtension");
String srcLang = "";
String tgtLang = "";
// 备注:目前只支持处理一种目标语言的情况
ap.selectXPath("/xliff//file[1]");
if (ap.evalXPath() != -1) {
srcLang = vu.getCurrentElementAttribut("source-language", "");
tgtLang = vu.getCurrentElementAttribut("target-language", "");
}
if ("".equals(srcLang)) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openWarning(getShell(), Messages.getString("all.dialog.warning"), Messages.getString("ExportDocxDialog.ok.msg1"));
}
});
LOGGER.error(Messages.getString("ExportDocxDialog.ok.msg1"));
}
if ("".equals(tgtLang)) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openWarning(getShell(), Messages.getString("all.dialog.warning"), Messages.getString("ExportDocxDialog.ok.msg2"));
}
});
LOGGER.error(Messages.getString("ExportDocxDialog.ok.msg2"));
}
String xpath = "/xliff/file[@source-language='" + srcLang + "' and @target-language='" + tgtLang + "']/body/descendant::trans-unit[(source/text()!='' or source/*)" + expandXpath + "]";
ap.selectXPath(xpath);
List<TUBean> tuDataList = new ArrayList<TUBean>();
String rowId = "";
int status = DocxConstant.STATUS_unstrans;
boolean approved = false;
boolean isLocked = false;
boolean isNotSendToTm = false;
boolean needsReview = false;
int index = -1;
while (ap.evalXPath() != -1) {
status = DocxConstant.STATUS_unstrans;
approved = false;
isLocked = false;
isNotSendToTm = false;
needsReview = false;
TUBean bean = new TUBean();
rowId = RowIdUtil.getSpecialRowId(vn);
bean.setRowId(rowId);
// 是否批准
if ((index = vn.getAttrVal("approved")) != -1) {
if ("yes".equals(vn.toString(index))) {
approved = true;
}
}
// 是否锁定
if ((index = vn.getAttrVal("translate")) != -1) {
if ("no".equals(vn.toString(index))) {
isLocked = true;
}
}
// 是否不发送数据库
if ((index = vn.getAttrVal("hs:send-to-tm")) != -1) {
if ("no".equals(vn.toString(index))) {
isNotSendToTm = true;
}
}
// 是否是疑问文本段
if ((index = vn.getAttrVal("hs:needs-review")) != -1) {
if ("yes".equals(vn.toString(index))) {
needsReview = true;
}
}
vn.push();
childAP.selectXPath("./source");
if (childAP.evalXPath() != -1) {
bean.setSrcText(vu.getElementContent());
}
vn.pop();
vn.push();
childAP.selectXPath("./target");
if (childAP.evalXPath() != -1) {
if ((index = vn.getAttrVal("state")) != -1) {
String stateStr = vn.toString(index);
if ("new".equals(stateStr)) {
// 草稿
status = DocxConstant.STATUS_draft;
} else if ("translated".equals(stateStr)) {
if (approved) {
// 批准翻译
status = DocxConstant.STATUS_approved;
} else {
// 完成翻译
status = DocxConstant.STATUS_translated;
}
} else if ("signed-off".equals(stateStr) && approved) {
// 签发
status = DocxConstant.STATUS_signedOff;
}
}
bean.setTgtText(vu.getElementContent());
} else {
status = DocxConstant.STATUS_unstrans;
}
vn.pop();
// 这里参照界面上状态的写法,分三个部份,第一个为(草稿,已翻译,完成翻译,批注,签发,锁定), 第二部份为不送至库,第三部份为疑问。
String beanStatus = "";
if (isLocked) {
beanStatus += Messages.getString("ExportDocxDialog.ok.status.locked");
} else {
switch(status) {
case DocxConstant.STATUS_unstrans:
beanStatus += Messages.getString("ExportDocxDialog.ok.status.unstrans");
break;
case DocxConstant.STATUS_draft:
beanStatus += Messages.getString("ExportDocxDialog.ok.status.draft");
break;
case DocxConstant.STATUS_translated:
beanStatus += Messages.getString("ExportDocxDialog.ok.status.translated");
break;
case DocxConstant.STATUS_approved:
beanStatus += Messages.getString("ExportDocxDialog.ok.status.approved");
break;
case DocxConstant.STATUS_signedOff:
beanStatus += Messages.getString("ExportDocxDialog.ok.status.signedOff");
break;
default:
break;
}
}
if (isNotSendToTm) {
beanStatus += "、";
beanStatus += Messages.getString("ExportDocxDialog.ok.status.NotSendToTm");
}
if (needsReview) {
beanStatus += "、";
beanStatus += Messages.getString("ExportDocxDialog.ok.status.Review");
}
bean.setStatus(beanStatus);
getComments(vn, vu, bean);
bean.setIndex("" + (tuDataList.size() + 1));
tuDataList.add(bean);
}
monitor.worked(1);
// 开始导出操作。规划出 subMonitor
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 18, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
ExportDocx wordOutput = new ExportDocx(srcLang, tgtLang, docxPath, getShell(), exportComment, exportStatus);
wordOutput.output(tuDataList, subMonitor);
subMonitor.done();
}
Aggregations