All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cogroo.tools.checker.AbstractGenericChecker Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
/**
 * Copyright (C) 2012 cogroo 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cogroo.tools.checker;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.cogroo.entities.Mistake;
import org.cogroo.entities.impl.MistakeImpl;

import org.cogroo.tools.checker.rules.model.Example;

public abstract class AbstractGenericChecker implements GenericChecker {

  protected static final Logger LOGGER = Logger
      .getLogger(AbstractGenericChecker.class);

  private Set ignored = Collections
      .synchronizedSet(new HashSet());

  private Map definitions = new HashMap();

  protected boolean isCheckRule(String ruleId) {
    return !ignored.contains(ruleId);
  }

  protected static Example createExample(String incorrect, String correct) {
    Example e = new Example();
    e.setIncorrect(incorrect);
    e.setCorrect(correct);
    return e;
  }

  protected Mistake createMistake(String ruleID, String[] suggestions,
      int start, int end, String text) {
    RuleDefinition ruleDefinition = getRuleDefinition(ruleID);

    return new MistakeImpl(ruleDefinition.getId(), getPriority(), ruleDefinition.getMessage(),
        ruleDefinition.getShortMessage(), suggestions, start, end,
        ruleDefinition.getExamples(), text);
  }
  
  protected Mistake createMistake(String ruleID, String[] longMessageArgs,
      String[] shortMessageArgs, String[] suggestions, int start, int end,
      String text) {
    RuleDefinition ruleDefinition = getRuleDefinition(ruleID);

    return new MistakeImpl(ruleDefinition.getId(), getPriority(),
        String.format(ruleDefinition.getMessage(), (Object[]) longMessageArgs),
        String.format(ruleDefinition.getShortMessage(),
            (Object[]) shortMessageArgs), suggestions, start, end,
        ruleDefinition.getExamples(), text);
  }

  public void ignore(String id) {
    ignored.add(id);
  }

  public void resetIgnored() {
    ignored.clear();
  }

  public AbstractGenericChecker add(RuleDefinition ruleDefinition) {
    this.definitions.put(ruleDefinition.getId(), ruleDefinition);
    return this;
  }

  public RuleDefinition getRuleDefinition(String ruleID) {
    RuleDefinition ruleDefinition = definitions.get(ruleID);
    if (ruleDefinition == null) {
      LOGGER.fatal("Unknow rule ID: " + ruleID);
      List empty = Collections.emptyList();
      ruleDefinition = new JavaRuleDefinition("-", "-", "-", "-", "-", "-",
          empty);
    }
    return ruleDefinition;
  }

  public Collection getRulesDefinition() {
    if (definitions.isEmpty()) {
      LOGGER
          .fatal("Rules were not defined properly! Please define the rules using the add method of AbstractChecker.");
    }
    return definitions.values();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy