Localizing Validators

If you are familiar with more than one natural language. It’s good to consider localizing your custom validators in RedPen.

To localize validation error messages, of course you need to avoid hard-coding error messages in your Validator implementation. To achieve that, use addLocalizedError(Sentence sentenceWithError,Object… args) instead of addError(String message, Sentence sentenceWithError) as follows:
NumberOfCharactersLocalizedValidator

package cc.redpen.validator.sentence;

import cc.redpen.model.Sentence;
import cc.redpen.validator.Validator;

public class NumberOfCharactersLocalizedValidator extends Validator {
    private final int MIN_LENGTH = 100;
    private final int MAX_LENGTH = 1000;
    @Override
    public void validate(Sentence sentence) {
        if (sentence.getContent().length() < MIN_LENGTH) {
            // actual error message is in NumberOfCharactersLocalizedValidator.properties
            addLocalizedError(sentence, MIN_LENGTH);
        }
        if (sentence.getContent().length() > MAX_LENGTH) {
            // You can specify a message key when you have multiple error message variations
            addLocalizedError("toolong", sentence, MAX_LENGTH);
        }
    }
}

Then you can place error messages in YourValidatorName[_LANGUAGE].properties:
NumberOfCharactersLocalizedValidator.properties (default messages)

NumberOfCharactersLocalizedValidator=Sentence is shorter than {0} characters long.
NumberOfCharactersLocalizedValidator.toolong=Sentence is longer than {0} characters long.

NumberOfCharactersLocalizedValidator_ja.properties (Japanese messages)

NumberOfCharactersLocalizedValidator=文が{0}文字より短いです。
NumberOfCharactersLocalizedValidator.toolong=文が{0}文字より長いです。

Error messages will be rendered by java.text.MessageFormat and you can put place holders in the error message text.

With the above implementation, you’ll get Japanese validation error message from Japanese environment:

$ ./bin/redpen -c conf/redpen-conf-en.xml ~/mytext.txt
mytext.txt:1: ValidationError[NumberOfCharacters], 文が100文字より短いです。 at line: Short paragraph.

, and English validation error message from English environment:

$ ./bin/redpen -c conf/redpen-conf-en.xml ~/mytext.txt
mytext.txt:1: ValidationError[NumberOfCharacters], Sentence is shorter than 100 characters long. at line: Short paragraph.
Advertisement
Localizing Validators

Writing RedPen extension with JavaScript

Extending RedPen in JavaScript

We introduced the basics of writing custom validators in the last post. For those who are unfamiliar with Java, we introduced JavaScriptValidator in RedPen v1.3. JavaScriptValidator is a special validator loads Validator implementations written in JavaScript.

Enabling JavaScriptValidator

To enable JavaScriptValidator, simply add <validator name=”JavaScript” /> in your redpen-conf.xml as follows:

<redpen-conf lang="en">
  <validators>
       ...snip...
    <validator name="JavaScript" />
  </validators>
</redpen-conf>

Write your own validator in JavaScript

Here is a JavaScript version of NumberOfCharacterValidator:

var MIN_LENGTH = 100;
var MAX_LENGTH = 1000;

function validateSentence(sentence) {
  if (sentence.getContent().length() < MIN_LENGTH) {
    addError("Sentence is shorter than "
      + MIN_LENGTH + " characters long.", sentence);
  }
  if (sentence.getContent().length() > MAX_LENGTH) {
    addError("Sentence is longer than " + MAX_LENGTH
      + " characters long.", sentence);
  }
}

The code looks pretty much similar with the Java version. But due to the difference in the type system, the callback method “validate(Sentence sentence)” is referred as “validateSentence(sentence)” in the JavaScript version.

Run

Of course, it’s JavaScript and there is no need to compile / package your validator (Actually your JavaScript code will be compiled into Java byte-code by Nashorn, and it runs very fast than you expect). JavaScriptValidator will pick any *.js file located in $REDPEN_HOME/js directory.
You can simply run the redpen command to get your file validated by the js validator.

$ ./bin/redpen -c myredpen-conf.xml 2be-validated.txt
2be-validated.txt:1: ValidationError[JavaScript], [NumberOfCharacter.js] Sentence is shorter than 100 characters long. at line: very short sentence.
Writing RedPen extension with JavaScript

Write your own RedPen Validator in Java

RedPen is extensible

RedPen is a Java based extensible document validation framework. You can implement your own extension (i.e. validation logic) in Java language.

Write your own validator

You can write your own Validator by simply overriding Validator#validate(Sentence). This is a callback method where you can receive each sentence in a document. To report a validation error in a sentence, use Validator#addError(String message, Sentence sentenceWithError).
Make sure that the class name ends with “Validator”, and your custom validation class need to be placed in either cc.redpen.validator, cc.redpen.validator.sentence, or cc.redpen.validator.section package.

Here is a very simple Validator implementation which checks sentence length:

package cc.redpen.validator.sentence;

import cc.redpen.model.Sentence;
import cc.redpen.validator.Validator;

public class NumberOfCharacterValidator extends Validator {
  private final int MIN_LENGTH = 100;
  private final int MAX_LENGTH = 1000;

  @Override
  public void validate(Sentence sentence) {
    if (sentence.getContent().length() < MIN_LENGTH) {
      // formerly addValidationError(message,sentence)
      addError("Sentence is shorter than "
        + MIN_LENGTH + " characters long.", sentence);
    }
    if (sentence.getContent().length() > MAX_LENGTH) {
      addError("Sentence is longer than " + MAX_LENGTH
        + " characters long.", sentence);
    }
  }
}

Please also consult these existing validators for your reference.

Enable your custom Validator

Once you package the validator class in jar file format and put it in $REDPEN_HOME/lib, it’s ready to validate.
Make sure that you specify a validator section in your redpen-conf.xml as follows:

<redpen-conf lang="en">
  <validators>
    <validator name="NumberOfCharacter" />
  </validators>
</redpen-conf>

In name attribute, specify the class name without “Validator”. For instance, name=”NumberOfCharacter” loads NumberOfCharacterValidator. There is no need to specify the package name.

Write your own RedPen Validator in Java