use of net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory in project translationstudio8 by heartsome.
the class InnerTagUtil method parseXmlToDisplayValue.
/**
* 将带内部标记的文本由XML格式转换为显示格式的文本
* @param originalValue
* 原始的带内部标记的XML格式的文本
* @return ;
*/
public static TreeMap<String, InnerTagBean> parseXmlToDisplayValue(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(originalValue.toString(), placeHolderCreater);
// 提取标记之后的文本。
originalValue.replace(0, originalValue.length(), innerTagFactory.getText());
List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
if (innerTagBeans != null && innerTagBeans.size() > 0) {
for (int i = 0; i < innerTagBeans.size(); i++) {
String placeHolder = placeHolderCreater.getPlaceHolder(innerTagBeans, i);
tags.put(placeHolder, innerTagBeans.get(i));
// originalValue.replace(innerTagBean.getOffset(), innerTagBean.getOffset(), innerTag.toString());
}
}
return tags;
}
use of net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory in project translationstudio8 by heartsome.
the class TextPainterWithPadding method getPreferredHeight.
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) {
if (innerTagFactory == null) {
innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder);
}
innerTagFactory.reset();
TextLayout layout = getCellTextLayout(cell);
int counts = layout.getLineCount();
int contentHeight = 0;
for (int i = 0; i < counts; i++) {
contentHeight += layout.getLineBounds(i).height;
}
layout.dispose();
contentHeight += Math.max(counts - 1, 0) * SEGMENT_LINE_SPACING;
// 加上编辑模式下,StyledTextCellEditor的边框
contentHeight += 4;
contentHeight += topPadding;
contentHeight += bottomPadding;
return contentHeight;
}
use of net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory 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.factory.XliffInnerTagFactory 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.factory.XliffInnerTagFactory in project translationstudio8 by heartsome.
the class TextPainterWithPadding method paintCell.
/**
* (non-Javadoc)
* @see net.sourceforge.nattable.painter.cell.BackgroundPainter#paintCell(net.sourceforge.nattable.layer.cell.LayerCell,
* org.eclipse.swt.graphics.GC, org.eclipse.swt.graphics.Rectangle,
* net.sourceforge.nattable.config.IConfigRegistry)
*/
@Override
public void paintCell(LayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry) {
super.paintCell(cell, gc, rectangle, configRegistry);
IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
setupGCFromConfig(gc, cellStyle);
if (innerTagFactory == null) {
innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder);
}
innerTagFactory.reset();
int rowIndex = cell.getLayer().getRowIndexByPosition(cell.getRowPosition());
int columnIndex = cell.getLayer().getColumnIndexByPosition(cell.getColumnPosition());
if (!editor.isHorizontalLayout()) {
// 垂直
if (rowIndex % 2 != 0) {
LayerCell srcCell = cell.getLayer().getCellByPosition(cell.getColumnPosition(), cell.getRowPosition() - 1);
if (srcCell != null) {
String sourceVal = (String) srcCell.getDataValue();
innerTagFactory.parseInnerTag(sourceVal);
}
}
} else {
// 水平
if (columnIndex == editor.getTgtColumnIndex()) {
LayerCell srcCell = cell.getLayer().getCellByPosition(1, cell.getRowPosition());
if (srcCell != null) {
String sourceVal = (String) srcCell.getDataValue();
innerTagFactory.parseInnerTag(sourceVal);
}
}
}
TextLayout layout = getCellTextLayout(cell);
int tempIndx = rowIndex;
if (!editor.isHorizontalLayout()) {
tempIndx = tempIndx / 2;
}
if (tempIndx == editor.getSelectedRows()[0] && (editor.isHorizontalLayout() ? columnIndex == editor.getSrcColumnIndex() : rowIndex % 2 == 0)) {
List<String> terms = editor.getTermsCache().get(tempIndx);
if (terms != null && terms.size() > 0) {
List<StyleRange> ranges = new ArrayList<StyleRange>();
TextStyle style = new TextStyle(getFont(), null, ColorConfigBean.getInstance().getHighlightedTermColor());
char[] source = layout.getText().toCharArray();
for (String term : terms) {
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
term = term.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
term = term.replaceAll("\\t", Constants.TAB_CHARACTER + "");
term = term.replaceAll(" ", Constants.SPACE_CHARACTER + "");
}
ranges.addAll(calculateTermsStyleRange(source, term.toCharArray(), style));
}
for (StyleRange range : ranges) {
layout.setStyle(range, range.start, range.start + range.length - 1);
}
}
}
try {
String displayText = layout.getText();
Rectangle bounds = cell.getBounds();
if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
appendNonprintingStyle(layout);
}
layout.draw(gc, bounds.x + leftPadding, bounds.y + topPadding);
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;
}
Point p = layout.getLocation(start, false);
int x = bounds.x + p.x + leftPadding;
x += SEGMENT_LINE_SPACING;
Point tagSize = tagRender.calculateTagSize(innerTagBean);
int lineIdx = layout.getLineIndex(start);
Rectangle r = layout.getLineBounds(lineIdx);
// -
int y = bounds.y + p.y + topPadding + r.height / 2 - tagSize.y / 2;
// layout.getLineMetrics(0).getDescent();
// if (y + r.height > tagSize.y) {
// FontMetrics fm = layout.getLineMetrics(lineIdx);
// y = y + r.height - tagSize.y - fm.getDescent();
// }
tagRender.draw(gc, innerTagBean, x, y);
}
} finally {
layout.dispose();
}
}
Aggregations