Search in sources :

Example 11 with ProtectedPart

use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.

the class DefaultXMLDialect method constructShortcuts.

/**
 * {@inheritDoc}
 */
@Override
public String constructShortcuts(List<Element> elements, List<ProtectedPart> protectedParts) {
    protectedParts.clear();
    StringBuilder r = new StringBuilder();
    for (Element el : elements) {
        String shortcut = el.toShortcut();
        r.append(shortcut);
        if (!(el instanceof Text)) {
            ProtectedPart pp = new ProtectedPart();
            pp.setTextInSourceSegment(shortcut);
            pp.setDetailsFromSourceFile(el.toOriginal());
            if (StatisticsSettings.isCountingStandardTags()) {
                pp.setReplacementWordsCountCalculation(el.toSafeCalcShortcut());
            } else {
                pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT);
            }
            pp.setReplacementUniquenessCalculation(StaticUtils.TAG_REPLACEMENT);
            pp.setReplacementMatchCalculation(StaticUtils.TAG_REPLACEMENT);
            protectedParts.add(pp);
        }
    }
    return r.toString();
}
Also used : ProtectedPart(org.omegat.core.data.ProtectedPart) Element(org.omegat.filters3.Element) Text(org.omegat.filters3.Text)

Example 12 with ProtectedPart

use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.

the class TagUtilTest method testBuildTagList.

/**
 * Test of buildTagList method, of class org.omegat.util.StaticUtils.
 *
 * @throws Exception
 */
@Test
public void testBuildTagList() throws Exception {
    TestPreferencesInitializer.init();
    String str = "Tag <test> case <b0>one</b0>.<b1>";
    List<ProtectedPart> pps = TagUtil.applyCustomProtectedParts(str, PatternConsts.OMEGAT_TAG, null);
    List<Tag> tagList = TagUtil.buildTagList(str, new SourceTextEntry(null, 0, null, null, pps).getProtectedParts());
    assertEquals("Wrong tags found in '" + str + "'", Arrays.asList(new Tag(16, "<b0>"), new Tag(23, "</b0>"), new Tag(29, "<b1>")), tagList);
    tagList.clear();
    ProtectedPart p;
    List<ProtectedPart> pp = new ArrayList<ProtectedPart>();
    p = new ProtectedPart();
    p.setTextInSourceSegment("<b0>");
    pp.add(p);
    p = new ProtectedPart();
    p.setTextInSourceSegment("</b0>");
    pp.add(p);
    tagList = TagUtil.buildTagList(str, new SourceTextEntry(null, 0, null, null, pp).getProtectedParts());
    assertEquals("Wrong tags found in '" + str + "'", Arrays.asList(new Tag(16, "<b0>"), new Tag(23, "</b0>")), tagList);
    str = "Tag <test>case</test>.";
    tagList.clear();
    pp.clear();
    p = new ProtectedPart();
    p.setTextInSourceSegment("<test>case</test>");
    pp.add(p);
    tagList = TagUtil.buildTagList(str, new SourceTextEntry(null, 0, null, null, pp).getProtectedParts());
    assertEquals("Wrong tags found in '" + str + "'", Arrays.asList(new Tag(4, "<test>case</test>")), tagList);
}
Also used : ProtectedPart(org.omegat.core.data.ProtectedPart) SourceTextEntry(org.omegat.core.data.SourceTextEntry) ArrayList(java.util.ArrayList) Tag(org.omegat.util.TagUtil.Tag) Test(org.junit.Test)

Example 13 with ProtectedPart

use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.

the class DocumentFilter3 method isPossible.

private boolean isPossible(Document d, int offset, int length) throws BadLocationException {
    Document3 doc = (Document3) d;
    if (doc.trustedChangesInProgress) {
        // this call created by internal changes
        return true;
    }
    if (!doc.isEditMode()) {
        // segment not active - change disabled
        return false;
    }
    if (offset < doc.getTranslationStart() || offset + length > doc.getTranslationEnd()) {
        // Is inside translation ?
        return false;
    }
    // check protected parts
    if (!Preferences.isPreference(Preferences.ALLOW_TAG_EDITING)) {
        SegmentBuilder sb = doc.controller.getCurrentSegmentBuilder();
        if (sb == null) {
            // there is no current active entry
            return false;
        }
        // check if inside tag
        String text = doc.getText(doc.getTranslationStart(), doc.getTranslationEnd() - doc.getTranslationStart());
        int off = offset - doc.getTranslationStart();
        for (ProtectedPart pp : sb.ste.getProtectedParts()) {
            int pos = -1;
            while ((pos = text.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
                int checkPos = pos;
                int checkLen = pp.getTextInSourceSegment().length();
                if (sb.hasRTL && doc.controller.targetLangIsRTL) {
                    // should be bidi-chars around tags
                    if (EditorUtils.hasBidiAroundTag(text, pp.getTextInSourceSegment(), pos)) {
                        checkPos -= 2;
                        checkLen += 4;
                    }
                }
                if (off > checkPos && off < checkPos + checkLen) {
                    return false;
                }
                if (off + length > checkPos && off + length < checkPos + checkLen) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ProtectedPart(org.omegat.core.data.ProtectedPart)

Example 14 with ProtectedPart

use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.

the class EditorTextArea3 method moveCursorOverTag.

/**
 * Move cursor over tag(possible, with selection)
 *
 * @param withShift
 *            true if selection need
 * @param checkTagStart
 *            true if check tag start, false if check tag end
 * @return true if tag processed
 */
boolean moveCursorOverTag(boolean withShift, boolean checkTagStart) {
    Document3 doc = getOmDocument();
    int caret = getCaretPosition();
    int start = doc.getTranslationStart();
    int end = doc.getTranslationEnd();
    if (caret < start || caret > end) {
        // Don't try to jump over tags.
        return false;
    }
    if ((caret == start && !checkTagStart) || (caret == end && checkTagStart)) {
        // Don't try to jump over tags.
        return false;
    }
    SourceTextEntry ste = doc.controller.getCurrentEntry();
    String text = doc.extractTranslation();
    int off = caret - start;
    // iterate by 'protected parts'
    if (ste != null) {
        for (ProtectedPart pp : ste.getProtectedParts()) {
            if (checkTagStart) {
                if (StringUtil.isSubstringAfter(text, off, pp.getTextInSourceSegment())) {
                    int pos = off + start + pp.getTextInSourceSegment().length();
                    if (withShift) {
                        getCaret().moveDot(pos);
                    } else {
                        getCaret().setDot(pos);
                    }
                    return true;
                }
            } else {
                if (StringUtil.isSubstringBefore(text, off, pp.getTextInSourceSegment())) {
                    int pos = off + start - pp.getTextInSourceSegment().length();
                    if (withShift) {
                        getCaret().moveDot(pos);
                    } else {
                        getCaret().setDot(pos);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ProtectedPart(org.omegat.core.data.ProtectedPart) SourceTextEntry(org.omegat.core.data.SourceTextEntry) Point(java.awt.Point)

Example 15 with ProtectedPart

use of org.omegat.core.data.ProtectedPart in project omegat by omegat-org.

the class EditorTextArea3 method selectTag.

/**
 * Try to select full tag on specified position, in the source and
 * translation part of segment.
 *
 * @param pos
 *            position
 * @return true if selected
 */
boolean selectTag(int pos) {
    int s = controller.getSegmentIndexAtLocation(pos);
    if (s < 0) {
        return false;
    }
    SegmentBuilder segment = controller.m_docSegList[s];
    if (pos < segment.getStartPosition() || pos >= segment.getEndPosition()) {
        return false;
    }
    SourceTextEntry ste = getOmDocument().controller.getCurrentEntry();
    if (ste != null) {
        try {
            String text = getOmDocument().getText(segment.getStartPosition(), segment.getEndPosition() - segment.getStartPosition());
            int off = pos - segment.getStartPosition();
            if (off < 0 || off >= text.length()) {
                return false;
            }
            for (ProtectedPart pp : ste.getProtectedParts()) {
                int p = -1;
                while ((p = text.indexOf(pp.getTextInSourceSegment(), p + 1)) >= 0) {
                    if (p <= off && off < p + pp.getTextInSourceSegment().length()) {
                        p += segment.getStartPosition();
                        select(p, p + pp.getTextInSourceSegment().length());
                        return true;
                    }
                }
            }
        } catch (BadLocationException ex) {
        }
    }
    return false;
}
Also used : ProtectedPart(org.omegat.core.data.ProtectedPart) SourceTextEntry(org.omegat.core.data.SourceTextEntry) Point(java.awt.Point) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

ProtectedPart (org.omegat.core.data.ProtectedPart)18 ArrayList (java.util.ArrayList)10 SourceTextEntry (org.omegat.core.data.SourceTextEntry)6 IFilter (org.omegat.filters2.IFilter)4 IParseCallback (org.omegat.filters2.IParseCallback)4 Point (java.awt.Point)3 File (java.io.File)3 Element (org.omegat.filters3.Element)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 FileInfo (org.omegat.core.data.IProject.FileInfo)2 TMXEntry (org.omegat.core.data.TMXEntry)2 Tag (org.omegat.filters3.Tag)2 XMLContentBasedTag (org.omegat.filters3.xml.XMLContentBasedTag)2 Matcher (java.util.regex.Matcher)1 AttributeSet (javax.swing.text.AttributeSet)1 BadLocationException (javax.swing.text.BadLocationException)1 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)1 Test (org.junit.Test)1