use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.
the class ConcordanceSearchDialog method createItem.
/**
* 向表格中添加记录
* @param metaData
* @param map
* ;
*/
private void createItem(MetaData metaData, ConcordanceBean bean, int rowNum, HashMap<IRow, ArrayList<int[]>> mapStyle) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", String.valueOf(bean.getId()));
map.put("changeDate", bean.getChangeDate());
map.put("changeId", bean.getChangeId());
map.put("creationDate", bean.getCreationDate());
map.put("creationId", bean.getCreationId());
map.put("flag", bean.isBlnIsFlag() ? "1" : "0");
for (LanguageTMX lang : bean.getLanguageList()) {
map.put(lang.getLanguageCode(), lang.getText());
}
map.put("dbName", metaData.getDatabaseName());
map.put("dbType", metaData.getDbType());
map.put("severName", metaData.getServerName());
List<TmxProp> lstAttr = bean.getAttributeList();
StringBuffer sbAttr = new StringBuffer();
ArrayList<int[]> lstAttrIndex = new ArrayList<int[]>();
for (TmxProp attr : lstAttr) {
if (attr.getName() != null && !attr.getName().trim().equals("") && !attr.getName().equals("x-flag")) {
String attrName = TextUtil.xmlToString(attr.getName());
int index = attrName.indexOf("::");
if (index != -1) {
attrName = attrName.substring(index + 2, attrName.length());
}
sbAttr.append(" ").append(attrName).append(Messages.getString("dialog.ConcordanceSearchDialog.colon"));
int startIndex = sbAttr.length();
sbAttr.append(TextUtil.xmlToString(attr.getValue()));
int endIndex = sbAttr.length();
lstAttrIndex.add(new int[] { startIndex, endIndex });
}
}
String creationDate = checkString(bean.getCreationDate());
String creationId = checkString(bean.getCreationId());
String changeDate = checkString(bean.getChangeDate());
String changeId = checkString(bean.getChangeId());
String attribute = MessageFormat.format(strMsg, creationDate, creationId, changeDate, changeId, sbAttr.toString());
ArrayList<int[]> lstFieldIndex = new ArrayList<int[]>();
int creationStart = attribute.indexOf(creationDate);
lstFieldIndex.add(new int[] { creationStart, creationStart + creationDate.length() });
int creationIdStart = attribute.indexOf(creationId, creationStart);
lstFieldIndex.add(new int[] { creationIdStart, creationIdStart + creationId.length() });
int changeDateStart = attribute.indexOf(changeDate, creationIdStart);
lstFieldIndex.add(new int[] { changeDateStart, changeDateStart + changeDate.length() });
int changeIdStart = attribute.indexOf(changeId, changeDateStart);
lstFieldIndex.add(new int[] { changeIdStart, changeIdStart + changeId.length() });
if (lstAttrIndex.size() > 0) {
int index = attribute.indexOf(sbAttr.toString());
for (int[] attrIndex : lstAttrIndex) {
lstFieldIndex.add(new int[] { attrIndex[0] + index, attrIndex[1] + index });
}
}
String source = map.get(strSrcLang) == null ? "" : map.get(strSrcLang);
StringBuffer sourceBf = new StringBuffer(source);
TreeMap<String, InnerTagBean> sourceTags = InnerTagUtil.parseXmlToDisplayValue(sourceBf, TagStyle.getDefault(false));
source = InnerTagUtil.resolveTag(sourceBf.toString());
source = InnerTagUtil.parseDisplayToXmlValue(sourceTags, source);
String target = map.get(strTgtLang) == null ? "" : map.get(strTgtLang);
StringBuffer tgtBf = new StringBuffer(target);
TreeMap<String, InnerTagBean> tgtTags = InnerTagUtil.parseXmlToDisplayValue(tgtBf, TagStyle.getDefault(false));
target = InnerTagUtil.resolveTag(tgtBf.toString());
target = InnerTagUtil.parseDisplayToXmlValue(tgtTags, target);
ArrayList<String> lstTarget = new ArrayList<String>();
for (String lang : lstLangs) {
String _tgt = map.get(lang);
_tgt = _tgt == null ? "" : _tgt;
StringBuffer _tgtBf = new StringBuffer(_tgt);
TreeMap<String, InnerTagBean> _tgtTags = InnerTagUtil.parseXmlToDisplayValue(_tgtBf, TagStyle.getDefault(false));
_tgt = InnerTagUtil.resolveTag(_tgtBf.toString());
_tgt = InnerTagUtil.parseDisplayToXmlValue(_tgtTags, _tgt);
lstTarget.add(_tgt);
}
try {
if (tableModel.getRow(rowNum) == null) {
XPropRow row = new XPropRow(bean.isBlnIsFlag(), source, lstTarget, target, attribute);
row.setDataMap(map);
if (metaData != null) {
row.setData("metaData", metaData);
}
tableModel.addRow(row);
mapStyle.put(row, lstFieldIndex);
} else {
tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(0), bean.isBlnIsFlag());
tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(1), source);
tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(2), target);
int i = 0;
for (; i < lstTarget.size(); i++) {
tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(3 + i), lstTarget.get(i));
}
tableModel.setValue(tableModel.getRow(rowNum), tableModel.getColumn(3 + i), attribute);
XPropRow row = (XPropRow) tableModel.getRow(rowNum);
row.setDataMap(map);
if (metaData != null) {
row.setData("metaData", metaData);
}
mapStyle.put(row, lstFieldIndex);
}
} catch (Exception e) {
XPropRow row = new XPropRow(bean.isBlnIsFlag(), source, lstTarget, target, attribute);
row.setDataMap(map);
if (metaData != null) {
row.setData("metaData", metaData);
}
tableModel.addRow(row);
mapStyle.put(row, lstFieldIndex);
}
}
use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.
the class TextPainterWithPadding method getCellTextLayout.
private TextLayout getCellTextLayout(LayerCell cell) {
int orientation = editor.getTable().getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
TextLayout layout = new TextLayout(editor.getTable().getDisplay());
layout.setOrientation(orientation);
layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
layout.setFont(font);
layout.setAscent(ascent);
// 和 StyledTextEditor 同步
layout.setDescent(descent);
layout.setTabs(new int[] { tabWidth });
Rectangle rectangle = cell.getBounds();
int width = rectangle.width - leftPadding - rightPadding;
width -= 1;
if (wrapText && width > 0) {
layout.setWidth(width);
}
String displayText = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag((String) cell.getDataValue()));
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
displayText = displayText.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
displayText = displayText.replaceAll("\\t", Constants.TAB_CHARACTER + "");
displayText = displayText.replaceAll(" ", Constants.SPACE_CHARACTER + "");
}
layout.setText(displayText);
List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
for (InnerTagBean innerTagBean : innerTagBeans) {
String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean));
int start = displayText.indexOf(placeHolder);
if (start == -1) {
continue;
}
TextStyle style = new TextStyle();
Point rect = tagRender.calculateTagSize(innerTagBean);
style.metrics = new GlyphMetrics(rect.y, 0, rect.x + SEGMENT_LINE_SPACING * 2);
layout.setStyle(style, start, start + placeHolder.length() - 1);
}
return layout;
}
use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.
the class IntelligentTagPrcessor method intelligentAppendTag.
public static String intelligentAppendTag(String srcFullText, String targetFullText) {
PlaceHolderEditModeBuilder plb = new PlaceHolderEditModeBuilder();
XliffInnerTagFactory innerTagFactory = new XliffInnerTagFactory(plb);
String targetParsedText = innerTagFactory.parseInnerTag(targetFullText);
List<InnerTagBean> targetTags = innerTagFactory.getInnerTagBeans();
String targetPureText = PATTERN.matcher(targetParsedText).replaceAll("");
innerTagFactory.reset();
innerTagFactory.parseInnerTag(srcFullText);
List<InnerTagBean> srcTags = innerTagFactory.getInnerTagBeans();
innerTagFactory.reset();
if (targetTags.size() == srcTags.size()) {
Matcher matcher = PlaceHolderEditModeBuilder.PATTERN.matcher(targetParsedText);
int offset = 0;
StringBuffer sb = new StringBuffer(targetParsedText);
while (matcher.find()) {
String placeHolder = matcher.group();
int index = plb.getIndex(null, placeHolder);
if (index > -1 && index < srcTags.size()) {
InnerTagBean b = srcTags.get(index);
String tagContent = b.getContent();
int start = matcher.start() + offset;
int end = matcher.end() + offset;
sb.replace(start, end, tagContent);
offset += tagContent.length() - 1;
} else {
sb.delete(0, sb.length());
sb.append(targetFullText);
break;
}
}
targetPureText = sb.toString();
} else if (srcTags.size() > 0) {
if (srcTags.size() == 1) {
// 只有一个标记
InnerTagBean tagFirst = srcTags.get(0);
String tagContent = tagFirst.getContent();
if (srcFullText.indexOf(tagContent) == 0) {
// header
targetPureText = tagContent + targetPureText;
} else if (srcFullText.indexOf(tagContent) + tagContent.length() == srcFullText.length()) {
// tail
targetPureText = targetPureText + tagContent;
}
} else {
InnerTagBean tagFirst = srcTags.get(0);
InnerTagBean tagLast = srcTags.get(srcTags.size() - 1);
String firstTagCnt = tagFirst.getContent();
String lastTagCnt = tagLast.getContent();
if (srcFullText.indexOf(firstTagCnt) == 0) {
// 句首标记是一个独立标记
if (tagFirst.getType() == TagType.STANDALONE) {
StringBuilder sb = processNextStandloneTag(srcFullText, srcTags, 0);
sb.insert(0, firstTagCnt);
targetPureText = sb.append(targetPureText).toString();
if (tagLast.getType() == TagType.STANDALONE && targetPureText.indexOf(lastTagCnt) == -1 && srcFullText.indexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
// 最后标记是一个独立标记,且在句末
sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
sb.insert(0, targetPureText).append(lastTagCnt);
targetPureText = sb.toString();
}
} else if (tagFirst.getType() == TagType.START && tagLast.getType() == TagType.END && tagFirst.getIndex() == tagLast.getIndex()) {
// 句首是一个开始 且最后一个标记是结束
if (srcFullText.lastIndexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
// 最后一个标记在句尾,满足环绕条件
if (srcTags.size() > 2) {
// 有其他标记
// header process
StringBuilder sb = processNextStandloneTag(srcFullText, srcTags, 0);
sb.insert(0, firstTagCnt);
targetPureText = sb.append(targetPureText).toString();
sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
sb.insert(0, targetPureText).append(lastTagCnt);
targetPureText = sb.toString();
} else {
targetPureText = firstTagCnt + targetPureText + lastTagCnt;
}
}
} else if (tagLast.getType() == TagType.STANDALONE && srcFullText.indexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
// 最后标记是一个独立标记,且在句末
StringBuilder sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
sb.insert(0, targetPureText).append(lastTagCnt);
targetPureText = sb.toString();
}
} else if (tagLast.getType() == TagType.STANDALONE && srcFullText.indexOf(lastTagCnt) + lastTagCnt.length() == srcFullText.length()) {
// 最后一个标记独立标记且在句末
StringBuilder sb = processPreStandloneTag(targetPureText, srcFullText, srcTags, srcTags.size() - 1);
sb.insert(0, targetPureText).append(lastTagCnt);
targetPureText = sb.toString();
}
}
}
return targetPureText;
}
use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.
the class InnerTagUtil method parseXmlToDisplayValueFromSource.
/**
* 根据 source 的内容显示内部标记
* @param originalValue
* 原始值
* @param srcOriginalValue
* Source 值
* @return ;
*/
public static Map<String, InnerTagBean> parseXmlToDisplayValueFromSource(String source, StringBuffer originalValue, TagStyle style) {
// 得到标签映射map(key: 内部标记;value: 内部标记实体)
TreeMap<String, InnerTagBean> tags = new TreeMap<String, InnerTagBean>(new Comparator<String>() {
public int compare(String str1, String str2) {
int num1 = InnerTagUtil.getStyledTagNum(str1);
int num2 = InnerTagUtil.getStyledTagNum(str2);
if (num1 == num2) {
return str1.indexOf(String.valueOf(num1)) - str2.indexOf(String.valueOf(num1));
}
return num1 - num2;
}
});
if (originalValue == null || originalValue.length() == 0) {
return tags;
}
placeHolderCreater.setStyle(style);
IInnerTagFactory innerTagFactory = new XliffInnerTagFactory(source, placeHolderCreater);
List<InnerTagBean> sourceInnerTagBeans = innerTagFactory.getInnerTagBeans();
if (sourceInnerTagBeans != null && sourceInnerTagBeans.size() > 0) {
int index = -1;
for (int i = 0; i < sourceInnerTagBeans.size(); i++) {
InnerTagBean innerTagBean = sourceInnerTagBeans.get(i);
String placeHolder = placeHolderCreater.getPlaceHolder(sourceInnerTagBeans, i);
tags.put(placeHolder, innerTagBean);
// String xml1 = FindReplaceDocumentAdapter.escapeForRegExPattern(entry.getValue());
String xml = innerTagBean.getContent();
if ((index = originalValue.indexOf(xml, index)) != -1) {
// 替换 Source 中存在的标记
originalValue.replace(index, index + xml.length(), placeHolder);
index += placeHolder.length();
}
}
}
// 替换目标文本中的错误标记
String target = innerTagFactory.parseInnerTag(originalValue.toString());
originalValue.replace(0, originalValue.length(), target);
return tags;
}
use of net.heartsome.cat.common.innertag.InnerTagBean in project translationstudio8 by heartsome.
the class ConcordanceSearchDialog method search.
/**
* 搜索记忆库 ;
*/
private boolean search() {
String searchText = cmbSearch.getText();
if (searchText == null || searchText.trim().equals("")) {
txtPage.setText("");
MessageDialog.openInformation(getShell(), Messages.getString("dialog.ConcordanceSearchDialog.msgTitle"), Messages.getString("dialog.ConcordanceSearchDialog.msg2"));
return false;
}
StringBuffer searchTextBf = new StringBuffer(searchText);
TreeMap<String, InnerTagBean> tags = InnerTagUtil.parseXmlToDisplayValue(searchTextBf, TagStyle.getDefault(false));
searchText = InnerTagUtil.resolveTag(searchTextBf.toString());
searchText = InnerTagUtil.escapeTag(searchText);
searchText = InnerTagUtil.parseDisplayToXmlValue(tags, searchText);
// 此处的判断已加在 ConcordanceSearchHandler 中
// if (lstDatabase.size() == 0) {
// txtPage.setText("");
// MessageDialog.openInformation(getShell(), "提示", "请为项目指定翻译记忆库");
// return false;
// }
ArrayList<String> lstSelLangs = new ArrayList<String>();
lstSelLangs.add(strSrcLang);
for (MenuItem item : menu.getItems()) {
if (item.getSelection()) {
lstSelLangs.add(item.getText());
}
}
LinkedHashMap<MetaData, List<Integer>> mapSub = getCurPageMap();
LinkedHashMap<MetaData, List<ConcordanceBean>> mapResult = new LinkedHashMap<MetaData, List<ConcordanceBean>>();
Iterator<Entry<MetaData, List<Integer>>> iterator = mapSub.entrySet().iterator();
while (iterator.hasNext()) {
Entry<MetaData, List<Integer>> entry = (Entry<MetaData, List<Integer>>) iterator.next();
MetaData metaData = entry.getKey();
DBOperator dbop = DatabaseService.getDBOperator(metaData);
try {
dbop.start();
List<ConcordanceBean> lstConcordance = dbop.getConcordanceSearchResult(cmbSearch.getText(), btnIsCaseSensitive.getSelection(), btnApplyRegularExpression.getSelection(), btnIsIgnoreMark.getSelection(), strSrcLang, lstSelLangs, entry.getValue());
if (lstConcordance != null && lstConcordance.size() > 0) {
mapResult.put(metaData, lstConcordance);
}
} catch (SQLException e1) {
LOGGER.error(Messages.getString("dialog.ConcordanceSearchDialog.logger2"), e1);
} catch (ClassNotFoundException e1) {
LOGGER.error(Messages.getString("dialog.ConcordanceSearchDialog.logger3"), e1);
} finally {
try {
if (dbop != null) {
dbop.end();
}
} catch (SQLException e) {
LOGGER.error("", e);
}
}
}
if (mapResult.size() == 0) {
clearTable();
MessageDialog.openInformation(getShell(), Messages.getString("dialog.ConcordanceSearchDialog.msgTitle"), Messages.getString("dialog.ConcordanceSearchDialog.msg1"));
txtPage.setText("");
return false;
}
// 当有多个数据库的数据时要对所有数据按 changedate 重新排序,然后取前 spiResultCount.getSelection() 数量的记录
int rowNum = 0;
HashMap<IRow, ArrayList<int[]>> mapStyle = new HashMap<IRow, ArrayList<int[]>>();
if (mapResult.size() > 1) {
LinkedHashMap<String, Object[]> map = sortMap(mapResult);
Iterator<Entry<String, Object[]>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Object[]> entry = it.next();
Object[] arrObj = entry.getValue();
createItem((MetaData) arrObj[0], (ConcordanceBean) arrObj[1], rowNum++, mapStyle);
}
} else {
Iterator<Entry<MetaData, List<ConcordanceBean>>> it = mapResult.entrySet().iterator();
while (it.hasNext()) {
Entry<MetaData, List<ConcordanceBean>> entry = it.next();
MetaData metaData2 = entry.getKey();
List<ConcordanceBean> lstBean = entry.getValue();
for (ConcordanceBean bean : lstBean) {
createItem(metaData2, bean, rowNum++, mapStyle);
}
if (lstBean.size() < rowNumPerPage) {
for (int row = lstBean.size(); row < rowNumPerPage; row++) {
if (rowNum < tableModel.getRowCount() && tableModel.getRow(rowNum) != null) {
tableModel.remRow(tableModel.getRow(rowNum));
}
}
}
}
}
jTable.registerCellRenderer(tableModel.getColumn(1), new StyleTextCellRenderer(searchText, btnIsCaseSensitive.getSelection(), btnApplyRegularExpression.getSelection(), createResultsStyle()));
jTable.registerCellRenderer(tableModel.getColumn(tableModel.getColumnCount() - 1), new AttributeTextCellRenderer(mapStyle, style));
jTable.setTableModel(tableModel);
return true;
}
Aggregations