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);
}
}
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);
}
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;
}
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);
}
}
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);
}
}
}
Aggregations