Search in sources :

Example 1 with Tag

use of org.omegat.util.TagUtil.Tag in project omegat by omegat-org.

the class TagRepair method fixMissing.

protected static void fixMissing(List<Tag> tags, StringBuilder text, Tag tag) {
    // Insert missing tag.
    int index = getTagIndex(tags, tag);
    Tag prev = index > 0 ? tags.get(index - 1) : null;
    Tag next = index + 1 < tags.size() ? tags.get(index + 1) : null;
    if (prev != null && text.indexOf(prev.tag) > -1) {
        // Insert after a preceding tag.
        text.insert(text.indexOf(prev.tag) + prev.tag.length(), tag.tag);
    } else if (next != null && text.indexOf(next.tag) > -1) {
        // Insert before a proceeding tag.
        text.insert(text.indexOf(next.tag), tag.tag);
    } else {
        // Nothing before or after; append to end.
        text.append(tag.tag);
    }
}
Also used : Tag(org.omegat.util.TagUtil.Tag)

Example 2 with Tag

use of org.omegat.util.TagUtil.Tag in project omegat by omegat-org.

the class TagValidation method inspectJavaMessageFormat.

public static void inspectJavaMessageFormat(ErrorReport report) {
    Pattern pattern = PatternConsts.SIMPLE_JAVA_MESSAGEFORMAT_PATTERN_VARS;
    List<Tag> srcTags = new ArrayList<Tag>();
    List<Tag> locTags = new ArrayList<Tag>();
    Matcher javaMessageFormatMatcher = pattern.matcher(report.source);
    while (javaMessageFormatMatcher.find()) {
        srcTags.add(new Tag(javaMessageFormatMatcher.start(), javaMessageFormatMatcher.group(0)));
    }
    javaMessageFormatMatcher = pattern.matcher(report.translation);
    while (javaMessageFormatMatcher.find()) {
        locTags.add(new Tag(javaMessageFormatMatcher.start(), javaMessageFormatMatcher.group(0)));
    }
    inspectUnorderedTags(srcTags, locTags, report);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Tag(org.omegat.util.TagUtil.Tag)

Example 3 with Tag

use of org.omegat.util.TagUtil.Tag in project omegat by omegat-org.

the class TagValidation method extractPrintfVars.

public static Map<String, Tag> extractPrintfVars(Pattern printfPattern, String translation) {
    Matcher printfMatcher = printfPattern.matcher(translation);
    Map<String, Tag> nameMapping = new HashMap<String, Tag>();
    int index = 1;
    while (printfMatcher.find()) {
        String printfVariable = printfMatcher.group(0);
        String argumentswapspecifier = printfMatcher.group(1);
        if (argumentswapspecifier != null && argumentswapspecifier.endsWith("$")) {
            String normalized = "" + argumentswapspecifier.substring(0, argumentswapspecifier.length() - 1) + printfVariable.substring(printfVariable.length() - 1, printfVariable.length());
            nameMapping.put(normalized, new Tag(printfMatcher.start(), printfVariable));
        } else {
            String normalized = "" + index + printfVariable.substring(printfVariable.length() - 1, printfVariable.length());
            nameMapping.put(normalized, new Tag(printfMatcher.start(), printfVariable));
            index++;
        }
    }
    return nameMapping;
}
Also used : Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Tag(org.omegat.util.TagUtil.Tag)

Example 4 with Tag

use of org.omegat.util.TagUtil.Tag in project omegat by omegat-org.

the class TagValidation method inspectRemovePattern.

public static void inspectRemovePattern(ErrorReport report) {
    Pattern removePattern = PatternConsts.getRemovePattern();
    if (removePattern == null) {
        return;
    }
    Matcher removeMatcher = removePattern.matcher(report.translation);
    while (removeMatcher.find()) {
        report.transErrors.put(new Tag(removeMatcher.start(), removeMatcher.group()), TagError.EXTRANEOUS);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Tag(org.omegat.util.TagUtil.Tag)

Example 5 with Tag

use of org.omegat.util.TagUtil.Tag in project omegat by omegat-org.

the class TagValidation method inspectPrintfVariables.

public static void inspectPrintfVariables(boolean simpleCheckOnly, ErrorReport report) {
    Pattern printfPattern = simpleCheckOnly ? PatternConsts.SIMPLE_PRINTF_VARS : PatternConsts.PRINTF_VARS;
    // printf variables should be equal in number,
    // but order can change
    // (and with that also notation: e.g. from '%s' to '%1$s')
    // We check this by adding the string "index+type specifier"
    // of every found variable to a set.
    // (Actually a map, so we can keep track of the original
    // variable for display purposes.)
    // If the sets (map keys) of the source and target are not equal, then
    // there is a problem: either missing or extra variables,
    // or the type specifier has changed for the variable at the
    // given index.
    Map<String, Tag> srcTags = extractPrintfVars(printfPattern, report.source);
    Map<String, Tag> locTags = extractPrintfVars(printfPattern, report.translation);
    if (!srcTags.keySet().equals(locTags.keySet())) {
        for (Map.Entry<String, Tag> e : srcTags.entrySet()) {
            report.srcErrors.put(e.getValue(), TagError.UNSPECIFIED);
        }
        for (Map.Entry<String, Tag> e : locTags.entrySet()) {
            report.transErrors.put(e.getValue(), TagError.UNSPECIFIED);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Tag(org.omegat.util.TagUtil.Tag) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Tag (org.omegat.util.TagUtil.Tag)16 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 Map (java.util.Map)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 HashMap (java.util.HashMap)2 TagError (org.omegat.core.tagvalidation.ErrorReport.TagError)2 List (java.util.List)1 Stack (java.util.Stack)1 ProtectedPart (org.omegat.core.data.ProtectedPart)1 SourceTextEntry (org.omegat.core.data.SourceTextEntry)1