use of org.omegat.filters3.Text in project mycore by MyCoRe-Org.
the class MCRChangeData method getProcessingInstruction.
public ProcessingInstruction getProcessingInstruction() {
if (pi == null) {
String data = RAW_OUTPUTTER.outputString(new Text(text));
this.pi = new ProcessingInstruction(type, data);
}
return pi;
}
use of org.omegat.filters3.Text in project omegat by omegat-org.
the class Entry method detectTags.
/**
* Detects the first starting tag that has its ending in the paragraph
* "first translatable" and the last ending tag that has its beginning in
* the paragraph "last translatable".
*/
private void detectTags() {
// first, detecting if we have any text and where we have it
int textStart = -1;
for (int i = 0; i < size(); i++) {
Element elem = get(i);
if ((elem instanceof Text) && ((Text) elem).isMeaningful()) {
textStart = i;
break;
}
if (elem instanceof XMLContentBasedTag) {
textStart = i;
}
}
for (int i = 0; i < size(); i++) {
Element elem = get(i);
if ((elem instanceof Text) && ((Text) elem).isMeaningful()) {
textInstance = (Text) elem;
break;
}
}
if (textStart < 0) {
// we have no translatable text in the whole entry
firstGood = -1;
lastGood = -2;
textInstance = null;
return;
}
int textEnd = textStart;
for (int i = size() - 1; i >= 0; i--) {
Element elem = get(i);
if ((elem instanceof Text) && ((Text) elem).isMeaningful()) {
textEnd = i;
break;
}
}
// if content-based tag is inside text, then expand text into paired content-based tag
for (int i = textStart; i <= textEnd; i++) {
Element elem = get(i);
if (elem instanceof XMLContentBasedTag) {
XMLContentBasedTag tag = (XMLContentBasedTag) elem;
if (tag.getTag().equals("bpt") || tag.getTag().equals("ept")) {
// find id of paired tag
String id = StringUtil.nvl(tag.getAttribute("rid"), tag.getAttribute("id"), tag.getAttribute("i"));
if (id == null) {
continue;
}
// find paired tag before
for (int j = textStart - 1; j >= 0; j--) {
if (get(j) instanceof XMLContentBasedTag) {
XMLContentBasedTag tag2 = (XMLContentBasedTag) get(j);
if (tag2.getTag().equals("bpt") || tag2.getTag().equals("ept")) {
// find id of paired tag
String id2 = StringUtil.nvl(tag2.getAttribute("rid"), tag2.getAttribute("id"), tag2.getAttribute("i"));
if (id.equals(id2)) {
textStart = j;
}
}
}
}
// find paired tag after
for (int j = textEnd + 1; j < size(); j++) {
if (get(j) instanceof XMLContentBasedTag) {
XMLContentBasedTag tag2 = (XMLContentBasedTag) get(j);
if (tag2.getTag().equals("bpt") || tag2.getTag().equals("ept")) {
// find id of paired tag
String id2 = StringUtil.nvl(tag2.getAttribute("rid"), tag2.getAttribute("id"), tag2.getAttribute("i"));
if (id.equals(id2)) {
textEnd = j;
}
}
}
}
}
}
}
// //////////////////////////////////////////////////////////////////////
// "first good"
// detecting the first starting tag that has its ending in the paragraph
boolean found = false;
for (firstGood = 0; firstGood < textStart; firstGood++) {
Element goodElem = get(firstGood);
if (!(goodElem instanceof Tag)) {
continue;
}
Tag good = (Tag) goodElem;
if (Tag.Type.BEGIN != good.getType()) {
continue;
}
// trying to test
int recursion = 1;
for (int i = firstGood + 1; i < textEnd; i++) {
Element candElement = get(i);
if (candElement instanceof Tag) {
Tag cand = (Tag) candElement;
if (cand.getTag().equals(good.getTag())) {
if (Tag.Type.BEGIN == cand.getType()) {
recursion++;
} else if (Tag.Type.END == cand.getType()) {
recursion--;
if (recursion == 0) {
if (i > textStart) {
found = true;
}
break;
}
}
}
}
}
// if we could find an ending, this is a "good one"
if (found) {
break;
}
}
if (!found) {
firstGood = textStart;
}
// //////////////////////////////////////////////////////////////////////
// "last good"
// detecting the last ending tag that has its starting in the paragraph
found = false;
for (lastGood = size() - 1; lastGood > textEnd; lastGood--) {
Element goodElem = get(lastGood);
if (!(goodElem instanceof Tag)) {
continue;
}
Tag good = (Tag) goodElem;
if (Tag.Type.END != good.getType()) {
continue;
}
// trying to test
int recursion = 1;
for (int i = lastGood - 1; i > textStart; i--) {
Element candElement = get(i);
if (candElement instanceof Tag) {
Tag cand = (Tag) candElement;
if (cand.getTag().equals(good.getTag())) {
if (Tag.Type.END == cand.getType()) {
recursion++;
} else if (Tag.Type.BEGIN == cand.getType()) {
recursion--;
if (recursion == 0) {
if (i < textEnd) {
found = true;
}
break;
}
}
}
}
}
// if we coud find a starting, this is a "good one"
if (found) {
break;
}
}
if (!found) {
lastGood = textEnd;
}
boolean removeTags;
if (handler.getContext().isRemoveAllTags()) {
// If Remove Tags is on,
// Remove leading and trailing tags must be on
removeTags = true;
} else {
removeTags = Core.getFilterMaster().getConfig().isRemoveTags();
}
// tags was already removed - restore they if need
if (!removeTags) {
for (int i = firstGood - 1; i >= 0; i--) {
Element elem = get(i);
if (elem instanceof Tag) {
if (handler.isParagraphTag((Tag) elem)) {
break;
}
firstGood = i;
}
}
for (int i = lastGood + 1; i < size(); i++) {
Element elem = get(i);
if (elem instanceof Tag) {
if (handler.isParagraphTag((Tag) elem)) {
break;
}
lastGood = i;
}
}
}
boolean removeSpacesAround = Core.getFilterMaster().getConfig().isRemoveSpacesNonseg();
// spaces was already removed - restore they if need
if (!removeSpacesAround) {
for (int i = firstGood - 1; i >= 0; i--) {
Element elem = get(i);
if (elem instanceof Tag) {
if (handler.isParagraphTag((Tag) elem)) {
break;
}
}
if ((elem instanceof Text) && !((Text) elem).isMeaningful()) {
firstGood = i;
}
}
for (int i = lastGood + 1; i < size(); i++) {
Element elem = get(i);
if (elem instanceof Tag) {
if (handler.isParagraphTag((Tag) elem)) {
break;
}
}
if ((elem instanceof Text) && !((Text) elem).isMeaningful()) {
lastGood = i;
}
}
}
}
use of org.omegat.filters3.Text in project omegat by omegat-org.
the class ResXFilterTest method testLoad.
@Test
public void testLoad() throws Exception {
String f = "test/data/filters/ResX/Resources.resx";
IProject.FileInfo fi = loadSourceFiles(new ResXFilter(), f);
checkMultiStart(fi, f);
checkMulti("This is a text displayed in the UI.", "InfoExperimentStoppingMessage", null, null, null, "This is a comment. It should not be displayed to the translator.");
checkMulti("One more text", "InfoExperimentStoppingMessage2", null, null, null, "Second comment");
checkMultiEnd();
}
use of org.omegat.filters3.Text in project omegat by omegat-org.
the class XLIFFDialect method constructShortcuts.
@Override
public String constructShortcuts(List<Element> elements, List<ProtectedPart> protectedParts) {
protectedParts.clear();
// create shortcuts
InlineTagHandler tagHandler = new InlineTagHandler();
StringBuilder r = new StringBuilder();
for (Element el : elements) {
if (el instanceof XMLContentBasedTag) {
XMLContentBasedTag tag = (XMLContentBasedTag) el;
String shortcut = null;
int shortcutLetter;
int tagIndex;
boolean tagProtected;
if ("bpt".equals(tag.getTag())) {
// XLIFF specification requires 'rid' and 'id' attributes,
// but some tools uses 'i' attribute like for TMX
tagHandler.startBPT(tag.getAttribute("rid"), tag.getAttribute("id"), tag.getAttribute("i"));
shortcutLetter = calcTagShortcutLetter(tag, ignoreTypeForBptTags);
tagHandler.setTagShortcutLetter(shortcutLetter);
tagIndex = tagHandler.endBPT();
shortcut = "<" + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f') + tagIndex + '>';
tagProtected = false;
} else if ("ept".equals(tag.getTag())) {
tagHandler.startEPT(tag.getAttribute("rid"), tag.getAttribute("id"), tag.getAttribute("i"));
tagIndex = tagHandler.endEPT();
shortcutLetter = tagHandler.getTagShortcutLetter();
shortcut = "</" + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f') + tagIndex + '>';
tagProtected = false;
} else if ("it".equals(tag.getTag())) {
tagHandler.startOTHER();
tagHandler.setCurrentPos(tag.getAttribute("pos"));
tagIndex = tagHandler.endOTHER();
// XLIFF specification requires 'open/close' values,
// but some tools may use 'begin/end' values like for TMX
shortcutLetter = calcTagShortcutLetter(tag);
if ("close".equals(tagHandler.getCurrentPos()) || "end".equals(tagHandler.getCurrentPos())) {
// for better compatibility with corresponding TMX files
if (forceShortCutToF) {
shortcutLetter = 'f';
}
shortcut = "</" + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f') + tagIndex + '>';
} else {
shortcut = "<" + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f') + tagIndex + '>';
}
tagProtected = false;
} else if ("ph".equals(tag.getTag())) {
tagHandler.startOTHER();
tagIndex = tagHandler.endOTHER();
shortcutLetter = calcTagShortcutLetter(tag, ignoreTypeForPhTags);
shortcut = "<" + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f') + tagIndex + "/>";
tagProtected = false;
} else if ("mrk".equals(tag.getTag())) {
tagHandler.startOTHER();
tagIndex = tagHandler.endOTHER();
shortcutLetter = 'm';
shortcut = "<m" + tagIndex + ">" + tag.getIntactContents().sourceToOriginal() + "</m" + tagIndex + ">";
tagProtected = true;
} else {
shortcutLetter = 'f';
tagIndex = -1;
tagProtected = false;
}
tag.setShortcutLetter(shortcutLetter);
tag.setShortcutIndex(tagIndex);
tag.setShortcut(shortcut);
r.append(shortcut);
ProtectedPart pp = new ProtectedPart();
pp.setTextInSourceSegment(shortcut);
pp.setDetailsFromSourceFile(tag.toOriginal());
if (tagProtected) {
// protected text with related tags, like <m0>Acme</m0>
if (StatisticsSettings.isCountingProtectedText()) {
// Protected texts are counted, but related tags are not counted in the word count
pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT + tag.getIntactContents().sourceToOriginal() + StaticUtils.TAG_REPLACEMENT);
} else {
// All protected parts are not counted in the word count(default)
pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT);
}
pp.setReplacementUniquenessCalculation(StaticUtils.TAG_REPLACEMENT);
pp.setReplacementMatchCalculation(tag.getIntactContents().sourceToOriginal());
} else {
// simple tag, like <i0>
if (StatisticsSettings.isCountingStandardTags()) {
pp.setReplacementWordsCountCalculation(tag.toSafeCalcShortcut());
} else {
pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT);
}
pp.setReplacementUniquenessCalculation(StaticUtils.TAG_REPLACEMENT);
pp.setReplacementMatchCalculation(StaticUtils.TAG_REPLACEMENT);
}
protectedParts.add(pp);
} else if (el instanceof Tag) {
Tag tag = (Tag) el;
int tagIndex = tagHandler.paired(tag.getTag(), tag.getType());
tag.setIndex(tagIndex);
String shortcut = tag.toShortcut();
r.append(shortcut);
ProtectedPart pp = new ProtectedPart();
pp.setTextInSourceSegment(shortcut);
pp.setDetailsFromSourceFile(tag.toOriginal());
if (StatisticsSettings.isCountingStandardTags()) {
pp.setReplacementWordsCountCalculation(tag.toSafeCalcShortcut());
} else {
pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT);
}
pp.setReplacementUniquenessCalculation(StaticUtils.TAG_REPLACEMENT);
pp.setReplacementMatchCalculation(StaticUtils.TAG_REPLACEMENT);
protectedParts.add(pp);
} else {
r.append(el.toShortcut());
}
}
return r.toString();
}
use of org.omegat.filters3.Text in project jspwiki by apache.
the class XHtmlElementToWikiTranslator method print.
private void print(Object element) throws IOException, JDOMException {
if (element instanceof Text) {
Text t = (Text) element;
String s = t.getText();
if (m_preStack.isPreMode()) {
m_out.print(s);
} else {
// remove all "line terminator" characters
s = s.replaceAll("[\\r\\n\\f\\u0085\\u2028\\u2029]", "");
m_out.print(s);
}
} else if (element instanceof Element) {
Element base = (Element) element;
String n = base.getName().toLowerCase();
if ("imageplugin".equals(base.getAttributeValue("class"))) {
printImage(base);
} else if ("wikiform".equals(base.getAttributeValue("class"))) {
// only print the children if the div's class="wikiform", but not the div itself.
printChildren(base);
} else {
boolean bold = false;
boolean italic = false;
boolean monospace = false;
String cssSpecial = null;
String cssClass = base.getAttributeValue("class");
// accomodate a FCKeditor bug with Firefox: when a link is removed, it becomes <span class="wikipage">text</span>.
boolean ignoredCssClass = cssClass != null && cssClass.matches("wikipage|createpage|external|interwiki|attachment");
Map styleProps = null;
// handled as an AugmentedWikiLink instead.
if (!n.equals("a")) {
styleProps = getStylePropertiesLowerCase(base);
}
if (styleProps != null) {
String fontFamily = (String) styleProps.get("font-family");
String whiteSpace = (String) styleProps.get("white-space");
if (fontFamily != null && (fontFamily.indexOf("monospace") >= 0 && whiteSpace != null && whiteSpace.indexOf("pre") >= 0)) {
styleProps.remove("font-family");
styleProps.remove("white-space");
monospace = true;
}
String weight = (String) styleProps.remove("font-weight");
String style = (String) styleProps.remove("font-style");
if (n.equals("p")) {
// change it so we can print out the css styles for <p>
n = "div";
}
italic = "oblique".equals(style) || "italic".equals(style);
bold = "bold".equals(weight) || "bolder".equals(weight);
if (!styleProps.isEmpty()) {
cssSpecial = propsToStyleString(styleProps);
}
}
if (cssClass != null && !ignoredCssClass) {
if (n.equals("div")) {
m_out.print("\n%%" + cssClass + " \n");
} else if (n.equals("span")) {
m_out.print("%%" + cssClass + " ");
}
}
if (bold) {
m_out.print("__");
}
if (italic) {
m_out.print("''");
}
if (monospace) {
m_out.print("{{{");
m_preStack.push();
}
if (cssSpecial != null) {
if (n.equals("div")) {
m_out.print("\n%%(" + cssSpecial + " )\n");
} else {
m_out.print("%%(" + cssSpecial + " )");
}
}
printChildren(base);
if (cssSpecial != null) {
if (n.equals("div")) {
m_out.print("\n/%\n");
} else {
m_out.print("/%");
}
}
if (monospace) {
m_preStack.pop();
m_out.print("}}}");
}
if (italic) {
m_out.print("''");
}
if (bold) {
m_out.print("__");
}
if (cssClass != null && !ignoredCssClass) {
if (n.equals("div")) {
m_out.print("\n/%\n");
} else if (n.equals("span")) {
m_out.print("/%");
}
}
}
}
}
Aggregations