Skip to content

Commit

Permalink
rename addValidationError -> addLocalizedError to clarify that the me…
Browse files Browse the repository at this point in the history
…thod will add localized error message
  • Loading branch information
yusuke committed Sep 1, 2015
1 parent 30e45b0 commit ed69f12
Show file tree
Hide file tree
Showing 40 changed files with 201 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import cc.redpen.model.Document;
import cc.redpen.model.Section;
import cc.redpen.model.Sentence;
import cc.redpen.parser.LineOffset;
import cc.redpen.tokenizer.TokenElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -171,34 +170,34 @@ public void addError(String message, Sentence sentenceWithError) {

@Override
public void addErrorWithPosition(String message, Sentence sentenceWithError,
Optional<LineOffset> start, Optional<LineOffset> end) {
super.addValidationErrorWithPosition(String.format("[%s] %s", currentJS.name, message),
int start, int end) {
super.addLocalizedErrorWithPosition(String.format("[%s] %s", currentJS.name, message),
sentenceWithError, start, end);
}

@Override
public void addValidationError(Sentence sentenceWithError, Object... args) {
super.addValidationError(sentenceWithError, args);
public void addLocalizedError(Sentence sentenceWithError, Object... args) {
super.addLocalizedError(sentenceWithError, args);
}

@Override
public void addValidationError(String messageKey, Sentence sentenceWithError, Object... args) {
super.addValidationError(messageKey, sentenceWithError, args);
public void addLocalizedError(String messageKey, Sentence sentenceWithError, Object... args) {
super.addLocalizedError(messageKey, sentenceWithError, args);
}

@Override
public void addValidationErrorFromToken(Sentence sentenceWithError, TokenElement token) {
super.addValidationErrorFromToken(sentenceWithError, token);
public void addLocalizedErrorFromToken(Sentence sentenceWithError, TokenElement token) {
super.addLocalizedErrorFromToken(sentenceWithError, token);
}

@Override
public void addValidationErrorWithPosition(Sentence sentenceWithError,
Optional<LineOffset> start, Optional<LineOffset> end, Object... args) {
super.addValidationErrorWithPosition(sentenceWithError, start, end, args);
public void addLocalizedErrorWithPosition(Sentence sentenceWithError,
int start, int end, Object... args) {
super.addLocalizedErrorWithPosition(sentenceWithError, start, end, args);
}

@Override
protected String getLocalizedErrorMessage(Optional<String> key, Object... args) {
protected String getLocalizedErrorMessage(String key, Object... args) {
String formatted;
if (currentJS.message != null) {
formatted = MessageFormat.format(currentJS.message, args);
Expand All @@ -221,9 +220,9 @@ class Script {
engine.put("redpenToBeBound", validator);
engine.eval("var addError = Function.prototype.bind.call(redpenToBeBound.addError, redpenToBeBound);" +
"var addErrorWithPosition = Function.prototype.bind.call(redpenToBeBound.addErrorWithPosition, redpenToBeBound);" +
"var addValidationError = Function.prototype.bind.call(redpenToBeBound.addValidationError, redpenToBeBound);" +
"var addValidationErrorFromToken = Function.prototype.bind.call(redpenToBeBound.addValidationErrorFromToken, redpenToBeBound);" +
"var addValidationErrorWithPosition = Function.prototype.bind.call(redpenToBeBound.addValidationErrorWithPosition, redpenToBeBound);");
"var addLocalizedError = Function.prototype.bind.call(redpenToBeBound.addLocalizedError, redpenToBeBound);" +
"var addLocalizedErrorFromToken = Function.prototype.bind.call(redpenToBeBound.addLocalizedErrorFromToken, redpenToBeBound);" +
"var addLocalizedErrorWithPosition = Function.prototype.bind.call(redpenToBeBound.addLocalizedErrorWithPosition, redpenToBeBound);");

CompiledScript compiledScript = ((Compilable) engine).compile(script);
compiledScript.eval();
Expand Down
32 changes: 25 additions & 7 deletions redpen-core/src/main/java/cc/redpen/validator/ValidationError.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public final class ValidationError implements java.io.Serializable {
private final String message;
private final String validatorName;
private final Sentence sentence;
private final Optional<LineOffset> startPosition;
private final Optional<LineOffset> endPosition;
private final LineOffset startPosition;
private final LineOffset endPosition;

/**
* Constructor.
Expand All @@ -47,8 +47,8 @@ public final class ValidationError implements java.io.Serializable {
this.message = errorMessage;
this.validatorName = validatorClass.getSimpleName();
this.sentence = sentenceWithError;
this.startPosition = Optional.empty();
this.endPosition = Optional.empty();
this.startPosition = null;
this.endPosition = null;
}

/**
Expand All @@ -61,7 +61,25 @@ public final class ValidationError implements java.io.Serializable {
* @param endPosition position where error ends
*/
ValidationError(Class validatorClass, String errorMessage, Sentence sentenceWithError,
Optional<LineOffset> startPosition, Optional<LineOffset> endPosition) {
int startPosition, int endPosition) {
this.message = errorMessage;
this.validatorName = validatorClass.getSimpleName();
this.sentence = sentenceWithError;
this.startPosition = sentenceWithError.getOffset(startPosition).get();
this.endPosition = sentenceWithError.getOffset(endPosition).get();
}

/**
*
* @param validatorClass validator class
* @param errorMessage error message
* @param sentenceWithError sentence containing validation error
* @param startPosition position where error starts
* @param endPosition position where error ends
* @deprecated
*/
ValidationError(Class validatorClass, String errorMessage, Sentence sentenceWithError,
LineOffset startPosition, LineOffset endPosition) {
this.message = errorMessage;
this.validatorName = validatorClass.getSimpleName();
this.sentence = sentenceWithError;
Expand Down Expand Up @@ -125,7 +143,7 @@ public String getValidatorName() {
* @return error start position (Note: some validation error does not specify the error position)
*/
public Optional<LineOffset> getStartPosition() {
return startPosition;
return Optional.ofNullable(startPosition);
}

/**
Expand All @@ -134,6 +152,6 @@ public Optional<LineOffset> getStartPosition() {
* @return error end position (Note: some validation error does not specify the error position)
*/
public Optional<LineOffset> getEndPosition() {
return endPosition;
return Optional.ofNullable(endPosition);
}
}
114 changes: 89 additions & 25 deletions redpen-core/src/main/java/cc/redpen/validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,73 +219,73 @@ protected void addError(String message, Sentence sentenceWithError) {
* @param end end position
*/
protected void addErrorWithPosition(String message, Sentence sentenceWithError,
Optional<LineOffset> start, Optional<LineOffset> end) {
int start, int end) {
errors.add(new ValidationError(this.getClass(), message, sentenceWithError, start, end));
}



/**
* create a ValidationError for the specified position with default error message
* create a ValidationError for the specified position with localized default error message
*
* @param sentenceWithError sentence
* @param args objects to format
*/
protected void addValidationError(Sentence sentenceWithError, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(Optional.empty(), args), sentenceWithError));
protected void addLocalizedError(Sentence sentenceWithError, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(null, args), sentenceWithError));
}

/**
* create a ValidationError for the specified position with specified message key
* create a ValidationError for the specified position with localized message with specified message key
*
* @param messageKey messageKey
* @param sentenceWithError sentence
* @param args objects to format
*/
protected void addValidationError(String messageKey, Sentence sentenceWithError, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(Optional.of(messageKey), args), sentenceWithError));
protected void addLocalizedError(String messageKey, Sentence sentenceWithError, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(messageKey, args), sentenceWithError));
}

/**
* create a ValidationError using the details within the given token
* create a ValidationError using the details within the given token &amp; localized message
*
* @param sentenceWithError sentence
* @param token the TokenElement that has the error
*/
protected void addValidationErrorFromToken(Sentence sentenceWithError, TokenElement token) {
addValidationErrorWithPosition(
protected void addLocalizedErrorFromToken(Sentence sentenceWithError, TokenElement token) {
addLocalizedErrorWithPosition(
sentenceWithError,
sentenceWithError.getOffset(token.getOffset()),
sentenceWithError.getOffset(token.getOffset() + token.getSurface().length()),
token.getOffset(),
token.getOffset() + token.getSurface().length(),
token.getSurface()
);
}

/**
* create a ValidationError for the specified position with default error message
* create a ValidationError for the specified position with default localized error message
*
* @param sentenceWithError sentence
* @param start start position
* @param end end position
* @param start start position in parsed sentence
* @param end end position in parsed sentence
* @param args objects to format
*/
protected void addValidationErrorWithPosition(Sentence sentenceWithError,
Optional<LineOffset> start, Optional<LineOffset> end, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(Optional.empty(), args), sentenceWithError, start, end));
protected void addLocalizedErrorWithPosition(Sentence sentenceWithError,
int start, int end, Object... args) {
addLocalizedErrorWithPosition(null, sentenceWithError, start, end, args);
}

/**
* create a ValidationError for the specified position with specified message key
*
* @param messageKey messageKey
* @param sentenceWithError sentence
* @param start start position
* @param end end position
* @param start start position in parsed sentence
* @param end end position in parsed sentence
* @param args objects to format
*/
protected void addValidationErrorWithPosition(String messageKey, Sentence sentenceWithError,
Optional<LineOffset> start, Optional<LineOffset> end, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(Optional.of(messageKey), args), sentenceWithError, start, end));
protected void addLocalizedErrorWithPosition(String messageKey, Sentence sentenceWithError,
int start, int end, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(messageKey, args), sentenceWithError, start, end));
}

/**
Expand All @@ -295,15 +295,79 @@ protected void addValidationErrorWithPosition(String messageKey, Sentence senten
* @param args objects to format
* @return localized error message
*/
protected String getLocalizedErrorMessage(Optional<String> key, Object... args) {
protected String getLocalizedErrorMessage(String key, Object... args) {
if (errorMessages != null) {
String suffix = key.isPresent() ? "." + key.get() : "";
String suffix = key != null ? "." + key : "";
return MessageFormat.format(errorMessages.getString(this.getClass().getSimpleName() + suffix), args);
} else {
throw new AssertionError("message resource not found.");
}
}



/**
* create a ValidationError for the specified position with default error message
*
* @param sentenceWithError sentence
* @param args objects to format
* @deprecated use {@link #addLocalizedError(Sentence, Object...)} instead
*/
protected void addValidationError(Sentence sentenceWithError, Object... args) {
addLocalizedError(sentenceWithError, args);
}

/**
* create a ValidationError for the specified position with specified message key
*
* @param messageKey messageKey
* @param sentenceWithError sentence
* @param args objects to format
* @deprecated use {@link #addLocalizedError(String, Sentence, Object...)} instead
*/
protected void addValidationError(String messageKey, Sentence sentenceWithError, Object... args) {
addLocalizedError(messageKey, sentenceWithError, args);
}

/**
* create a ValidationError using the details within the given token
*
* @param sentenceWithError sentence
* @param token the TokenElement that has the error
* @deprecated use {@link #addLocalizedErrorFromToken(Sentence, TokenElement)} instead
*/
protected void addValidationErrorFromToken(Sentence sentenceWithError, TokenElement token) {
addLocalizedError(sentenceWithError, token);
}

/**
* create a ValidationError for the specified position with default error message
*
* @param sentenceWithError sentence
* @param start start position
* @param end end position
* @param args objects to format
* @deprecated use {@link #addLocalizedErrorWithPosition(Sentence, int, int, Object...)} instead
*/
protected void addValidationErrorWithPosition(Sentence sentenceWithError,
Optional<LineOffset> start, Optional<LineOffset> end, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(null, args), sentenceWithError, start.get(), end.get()));
}

/**
* create a ValidationError for the specified position with specified message key
*
* @param messageKey messageKey
* @param sentenceWithError sentence
* @param start start position
* @param end end position
* @param args objects to format
* @deprecated use {@link #addLocalizedErrorWithPosition(String, Sentence, int, int, Object...)} instead
*/
protected void addValidationErrorWithPosition(String messageKey, Sentence sentenceWithError,
Optional<LineOffset> start, Optional<LineOffset> end, Object... args) {
errors.add(new ValidationError(this.getClass(), getLocalizedErrorMessage(messageKey, args), sentenceWithError, start.get(), end.get()));
}
/**
* Resource Extractor loads key-value dictionary
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void validate(Section section) {
if (sectionVector.header != section.getHeaderContent(0) &&
calcCosine(targetVector, candidateVector) > threhold) {
Optional<Sentence> header = Optional.ofNullable(section.getHeaderContent(0));
addValidationError(header.orElse(section.getParagraph(0).getSentence(0)),
addLocalizedError(header.orElse(section.getParagraph(0).getSentence(0)),
sectionVector.header.getLineNumber());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final public class ParagraphNumberValidator extends Validator {
public void validate(Section section) {
int paragraphNumber = section.getNumberOfParagraphs();
if (maxParagraphs < paragraphNumber) {
addValidationError(section.getJoinedHeaderContents(), paragraphNumber);
addLocalizedError(section.getJoinedHeaderContents(), paragraphNumber);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void validate(Section section) {
}
Sentence firstSentence = currentParagraph.getSentence(0);
if (firstSentence.getContent().indexOf(this.beginningOfParagraph) != 0) {
addValidationError(section.getJoinedHeaderContents(),
addLocalizedError(section.getJoinedHeaderContents(),
firstSentence.getContent().charAt(0));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void validate(Section section) {
}

if (sectionCharNumber > maxSectionCharNumber) {
addValidationError(section.getJoinedHeaderContents(), sectionCharNumber);
addLocalizedError(section.getJoinedHeaderContents(), sectionCharNumber);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void validate(Sentence sentence) {
content = content.substring(position + 1, content.length());
}
if (maxCommaNum < commaCount) {
addValidationError(sentence, commaCount, maxCommaNum);
addLocalizedError(sentence, commaCount, maxCommaNum);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void validate(Sentence sentence) {
String surface = token.getSurface().toLowerCase();
if (foundNonContractionNum >= foundContractionNum
&& contractions.contains(surface)) {
addValidationErrorFromToken(sentence, token);
addLocalizedErrorFromToken(sentence, token);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void validate(Sentence sentence) {
// validate with expressions (phrase)
for (ExpressionRule rule : invalidExpressions) {
if (rule.match(sentence.getTokens())) {
addValidationError(sentence, rule.toString());
addLocalizedError(sentence, rule.toString());
return;
}
}
Expand All @@ -62,7 +62,7 @@ public void validate(Sentence sentence) {
count++;
}
if (count >= 2) {
addValidationErrorFromToken(sentence, token);
addLocalizedErrorFromToken(sentence, token);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void validate(Sentence sentence) {
}
counts.entrySet().stream()
.filter(e -> e.getValue() >= 2 && !skipList.contains(e.getKey()))
.forEach(e -> addValidationError(sentence, e.getKey()));
.forEach(e -> addLocalizedError(sentence, e.getKey()));
}

@Override
Expand Down

0 comments on commit ed69f12

Please sign in to comment.