Search in sources :

Example 11 with Tag

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

the class TagValidationTest method testPrintfTagValidation.

@Test
public void testPrintfTagValidation() {
    // No error
    ErrorReport report = new ErrorReport("Foo %s bar %d", "Foo %s bar %d");
    TagValidation.inspectPrintfVariables(true, report);
    assertTrue(report.srcErrors.isEmpty());
    assertTrue(report.transErrors.isEmpty());
    // Missing %d
    report = new ErrorReport("Foo %s bar %d", "Foo %s bar");
    TagValidation.inspectPrintfVariables(true, report);
    assertTrue(report.srcErrors.get(new Tag(4, "%s")) == TagError.UNSPECIFIED);
    assertTrue(report.srcErrors.get(new Tag(11, "%d")) == TagError.UNSPECIFIED);
    assertTrue(report.transErrors.get(new Tag(4, "%s")) == TagError.UNSPECIFIED);
    // Extraneous %d
    report = new ErrorReport("Foo %s bar %d", "Foo %s bar %d baz %d");
    TagValidation.inspectPrintfVariables(true, report);
    assertTrue(report.srcErrors.get(new Tag(4, "%s")) == TagError.UNSPECIFIED);
    assertTrue(report.srcErrors.get(new Tag(11, "%d")) == TagError.UNSPECIFIED);
    assertTrue(report.transErrors.get(new Tag(4, "%s")) == TagError.UNSPECIFIED);
    assertTrue(report.transErrors.get(new Tag(11, "%d")) == TagError.UNSPECIFIED);
    assertTrue(report.transErrors.get(new Tag(18, "%d")) == TagError.UNSPECIFIED);
}
Also used : Tag(org.omegat.util.TagUtil.Tag) Test(org.junit.Test)

Example 12 with Tag

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

the class TagValidation method getCommonTags.

private static List<Tag> getCommonTags(List<Tag> orig, List<Tag> compare) {
    List<Tag> result = new ArrayList<Tag>();
    List<Tag> uninspected = new ArrayList<Tag>(compare);
    for (Tag oTag : orig) {
        for (Tag cTag : uninspected) {
            if (oTag.tag.equals(cTag.tag)) {
                result.add(oTag);
                uninspected.remove(cTag);
                break;
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Tag(org.omegat.util.TagUtil.Tag)

Example 13 with Tag

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

the class TagValidation method inspectPOWhitespace.

public static void inspectPOWhitespace(ErrorReport report) {
    // check PO line start:
    boolean srcStartsWith = report.source.startsWith("\n");
    boolean trgStartsWith = report.translation.startsWith("\n");
    if (srcStartsWith && !trgStartsWith) {
        report.srcErrors.put(new Tag(0, "\n"), TagError.WHITESPACE);
    }
    if (!srcStartsWith && trgStartsWith) {
        report.transErrors.put(new Tag(0, "\n"), TagError.WHITESPACE);
    }
    // check PO line ending:
    boolean srcEndsWith = report.source.endsWith("\n");
    boolean trgEndsWith = report.translation.endsWith("\n");
    if (srcEndsWith && !trgEndsWith) {
        report.srcErrors.put(new Tag(report.source.length() - 1, "\n"), TagError.WHITESPACE);
    }
    if (!srcEndsWith && trgEndsWith) {
        report.transErrors.put(new Tag(report.translation.length() - 1, "\n"), TagError.WHITESPACE);
    }
}
Also used : Tag(org.omegat.util.TagUtil.Tag)

Example 14 with Tag

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

the class TagValidation method inspectOrderedTags.

/**
 * Check that translated tags are well-formed.
 * In order to accommodate tags orphaned by segmenting,
 * unmatched tags are allowed, but only if they don't interfere with
 * non-orphaned tags.
 * @param srcTags A list of tags in the source text
 * @param locTags A list of tags in the translated text
 * @param report The report to append errors to
 */
protected static void inspectOrderedTags(List<Tag> srcTags, List<Tag> locTags, boolean looseOrdering, ErrorReport report) {
    // about out-of-order tags.
    if (!looseOrdering) {
        List<Tag> commonTagsSrc = getCommonTags(srcTags, locTags);
        List<Tag> commonTagsLoc = getCommonTags(locTags, srcTags);
        for (int i = 0; i < commonTagsSrc.size(); i++) {
            Tag tag = commonTagsLoc.get(i);
            if (!tag.tag.equals(commonTagsSrc.get(i).tag)) {
                report.transErrors.put(tag, TagError.ORDER);
                commonTagsSrc.remove(i);
                commonTagsLoc.remove(i);
                i--;
            }
        }
    }
    // Check translation tags.
    List<Tag> expectedTags = new ArrayList<Tag>(srcTags);
    Stack<Tag> tagStack = new Stack<Tag>();
    for (Tag tag : locTags) {
        // Make sure tag exists in source.
        if (!TagUtil.containsTag(srcTags, tag.tag)) {
            report.transErrors.put(tag, TagError.EXTRANEOUS);
            continue;
        }
        // Reduce count. If we're below zero, there's extra in the translation.
        Tag expected = removeTag(expectedTags, tag.tag);
        if (expected == null) {
            report.transErrors.put(tag, TagError.DUPLICATE);
            continue;
        }
        // Build stack of tags to check well-formedness.
        switch(tag.getType()) {
            case START:
                tagStack.push(tag);
                break;
            case END:
                if (!tagStack.isEmpty() && tagStack.peek().getName().equals(tag.getName())) {
                    // Closing a tag normally.
                    tagStack.pop();
                } else {
                    while (!tagStack.isEmpty()) {
                        // Closing the wrong opening tag.
                        // Rewind stack until we find its pair. Report everything along
                        // the way as malformed.
                        Tag last = tagStack.pop();
                        report.transErrors.put(last, TagError.MALFORMED);
                        if (last.getName().equals(tag.getName())) {
                            break;
                        }
                    }
                    // report the tag, but only if it's not a valid orphan.
                    if (tagStack.isEmpty()) {
                        String pair = tag.getPairedTag();
                        if (TagUtil.containsTag(srcTags, pair)) {
                            report.transErrors.put(tag, TagUtil.containsTag(locTags, pair) ? TagError.MALFORMED : TagError.ORPHANED);
                        }
                    }
                }
                break;
            case SINGLE:
        }
    }
    // Check expected tags for anything left.
    for (Tag tag : expectedTags) {
        report.srcErrors.put(tag, TagError.MISSING);
    }
    // Check the stack to see if there are straggling open tags.
    while (!tagStack.isEmpty()) {
        // Allow stragglers only if they're orphans.
        Tag tag = tagStack.pop();
        String pair = tag.getPairedTag();
        if (TagUtil.containsTag(srcTags, pair)) {
            report.transErrors.put(tag, TagUtil.containsTag(locTags, pair) ? TagError.MALFORMED : TagError.ORPHANED);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Tag(org.omegat.util.TagUtil.Tag) Stack(java.util.Stack)

Example 15 with Tag

use of org.omegat.util.TagUtil.Tag 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)

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