Search in sources :

Example 1 with InlineTagHandler

use of org.omegat.util.InlineTagHandler 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();
}
Also used : ProtectedPart(org.omegat.core.data.ProtectedPart) XMLContentBasedTag(org.omegat.filters3.xml.XMLContentBasedTag) Element(org.omegat.filters3.Element) InlineTagHandler(org.omegat.util.InlineTagHandler) Tag(org.omegat.filters3.Tag) XMLContentBasedTag(org.omegat.filters3.xml.XMLContentBasedTag)

Aggregations

ProtectedPart (org.omegat.core.data.ProtectedPart)1 Element (org.omegat.filters3.Element)1 Tag (org.omegat.filters3.Tag)1 XMLContentBasedTag (org.omegat.filters3.xml.XMLContentBasedTag)1 InlineTagHandler (org.omegat.util.InlineTagHandler)1