@Test
  public void testLoadSkipList() throws Exception {
    List<Document> documents = new ArrayList<>();
    documents.add(
        Document.builder(new JapaneseTokenizer())
            .addSection(1)
            .addParagraph()
            .addSentence(new Sentence("私は彼は好き。", 1))
            .build());

    Configuration config =
        Configuration.builder("ja")
            .addValidatorConfig(new ValidatorConfiguration("DoubledJoshi").addProperty("list", "は"))
            .build();

    RedPen redPen = new RedPen(config);
    Map<Document, List<ValidationError>> errors = redPen.validate(documents);
    assertEquals(0, errors.get(documents.get(0)).size());
  }
Beispiel #2
0
  @Override
  public String formatError(cc.redpen.model.Document document, ValidationError error)
      throws RedPenException {
    // create dom
    Document doc = db.newDocument();
    Element errorElement = doc.createElement("error");
    doc.appendChild(errorElement);

    Element validatorElement = doc.createElement("validator");
    errorElement.appendChild(validatorElement);
    Text validator = doc.createTextNode(error.getValidatorName());
    validatorElement.appendChild(validator);

    Element contentElement = doc.createElement("message");
    errorElement.appendChild(contentElement);
    Text content = doc.createTextNode(error.getMessage());
    contentElement.appendChild(content);

    document
        .getFileName()
        .ifPresent(
            e -> {
              Element fileNameElement = doc.createElement("file");
              errorElement.appendChild(fileNameElement);
              Text fileName = doc.createTextNode(e);
              fileNameElement.appendChild(fileName);
            });

    Element lineNumberElement = doc.createElement("lineNum");
    errorElement.appendChild(lineNumberElement);
    Text lineNum = doc.createTextNode(Integer.toString(error.getLineNumber()));
    lineNumberElement.appendChild(lineNum);

    Element sentenceStartOffset = doc.createElement("sentenceStartColumnNum");
    errorElement.appendChild(sentenceStartOffset);
    Text offset = doc.createTextNode(Integer.toString(error.getStartColumnNumber()));
    sentenceStartOffset.appendChild(offset);

    Element sentenceElement = doc.createElement("sentence");
    errorElement.appendChild(sentenceElement);
    sentenceElement.appendChild(doc.createTextNode(error.getSentence().getContent()));

    error
        .getStartPosition()
        .ifPresent(
            e -> {
              Element startPositionElement = doc.createElement("errorStartPosition");
              errorElement.appendChild(startPositionElement);
              LineOffset startOffset = error.getStartPosition().get();
              startPositionElement.appendChild(
                  doc.createTextNode(
                      Integer.toString(startOffset.lineNum)
                          + ","
                          + Integer.toString(startOffset.offset)));
            });

    error
        .getStartPosition()
        .ifPresent(
            e -> {
              Element endPositionElement = doc.createElement("errorEndPosition");
              errorElement.appendChild(endPositionElement);
              LineOffset endOffset = error.getEndPosition().get();
              endPositionElement.appendChild(
                  doc.createTextNode(
                      Integer.toString(endOffset.lineNum)
                          + ","
                          + Integer.toString(endOffset.offset)));
            });

    // convert the result dom into a string
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    DOMSource source = new DOMSource(doc);
    try {
      transformer.transform(source, result);
    } catch (TransformerException e) {
      throw new RedPenException(e);
    }
    return writer.toString() + "\n";
  }