/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package de.danielnaber.languagetool.rules;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import de.danielnaber.languagetool.AnalyzedSentence;
import de.danielnaber.languagetool.AnalyzedToken;
import de.danielnaber.languagetool.AnalyzedTokenReadings;
import de.danielnaber.languagetool.Language;

/**
 * Checks that a sentence starts with an uppercase letter.
 * 
 * @author Daniel Naber
 */
public class UppercaseSentenceStartRule extends Rule {

  private final Language language;

  private String lastPragraphString = "";

  public UppercaseSentenceStartRule(final ResourceBundle messages,
      final Language language) {
    super(messages);
    super.setCategory(new Category(messages.getString("category_case")));
    this.language = language;
  }

  public final String getId() {
    return "UPPERCASE_SENTENCE_START";
  }

  public final String getDescription() {
    return messages.getString("desc_uppercase_sentence");
  }

  public final RuleMatch[] match(final AnalyzedSentence text) {
    final List<RuleMatch> ruleMatches = new ArrayList<RuleMatch>();
    final AnalyzedTokenReadings[] tokens = text.getTokensWithoutWhitespace();
    if (tokens.length < 2) {
      return toRuleMatchArray(ruleMatches);
    }
    // the case should be the same in all readings
    // discarding the rest of the possible lemmas and POS tags
    int matchTokenPos = 1;
    final AnalyzedToken token = tokens[matchTokenPos].getAnalyzedToken(0); // 0
    // =
    // SENT_START
    final String firstToken = token.getToken();
    String secondToken = null;
    String thirdToken = null;
    // ignore quote characters:
    if (tokens.length >= 3
        && ("'".equals(firstToken) || "\"".equals(firstToken) || "„"
            .equals(firstToken))) {
      matchTokenPos = 2;
      secondToken = tokens[matchTokenPos].getAnalyzedToken(0).getToken();
    }
    final String firstDutchToken = dutchSpecialCase(firstToken, secondToken,
        tokens);
    if (firstDutchToken != null) {
      thirdToken = firstDutchToken;
      matchTokenPos = 3;
    }

    String checkToken = firstToken;
    if (thirdToken != null) {
      checkToken = thirdToken;
    } else if (secondToken != null) {
      checkToken = secondToken;
    }

    final String chklastToken = tokens[tokens.length - 1].getAnalyzedToken(0)
    .getToken();

    boolean noException = false;

    if ((language == Language.RUSSIAN)
        && (";".equals(lastPragraphString) || ";".equals(chklastToken)
            || ",".equals(lastPragraphString) || ",".equals(chklastToken))) {
      noException = true;
    }
    if ((language == Language.POLISH)
        && (";".equals(lastPragraphString) || ";".equals(chklastToken)
            || ":".equals(lastPragraphString) || ":".equals(chklastToken)
            || ",".equals(lastPragraphString) || ",".equals(chklastToken))) {
      noException = true;
    }
    lastPragraphString = chklastToken;

    final char firstChar = checkToken.charAt(0);
    if (Character.isLowerCase(firstChar) && (noException != true)) {
      final RuleMatch ruleMatch = new RuleMatch(this, tokens[matchTokenPos]
                .getStartPos(), tokens[matchTokenPos].getStartPos()
                + tokens[matchTokenPos].getToken().length(), messages
                .getString("incorrect_case"));
      ruleMatch.setSuggestedReplacement(Character.toUpperCase(firstChar)
          + checkToken.substring(1));
      ruleMatches.add(ruleMatch);
    }
    return toRuleMatchArray(ruleMatches);
  }

  private String dutchSpecialCase(final String firstToken,
      final String secondToken, final AnalyzedTokenReadings[] tokens) {
    if (language != Language.DUTCH) {
      return null;
    }
    if (tokens.length >= 3 && firstToken.equals("'")
        && secondToken.matches("k|m|n|r|s|t")) {
      return tokens[3].getAnalyzedToken(0).getToken();
    }
    return null;
  }

  public void reset() {
  }

}

@@ -2127,240 +2127,37 @@
     
-// the case should be the same in all readings%0A    // discarding the rest of the possible lemmas and POS tags%0A    int matchTokenPos = 1;%0A    final AnalyzedToken token = tokens%5BmatchTokenPos%5D.getAnalyzedToken(0); // 0%0A    // =%0A    //
+int matchTokenPos = 1; // 0 =
  SEN
@@ -2199,16 +2199,32 @@
  = token
+s%5BmatchTokenPos%5D
 .getToke
@@ -2520,36 +2520,16 @@
 kenPos%5D.
-getAnalyzedToken(0).
 getToken
@@ -2970,33 +2970,8 @@
 - 1%5D
-.getAnalyzedToken(0)%0A    
 .get
@@ -3017,238 +3017,180 @@
 se;%0A
-%0A
     
-if ((language == Language.RUSSIAN)%0A        && (%22;%22.equals(lastPragraphString) %7C%7C %22;%22.equals(chklastToken)%0A            %7C%7C %22,%22.equals(lastPragraphString) %7C%7C %22,%22.equals(chklastToken))) %7B%0A      noException = true;%0A    %7D%0A    if ((
+//fix for lists; note - this will not always work for the last point in OOo,%0A    //as OOo might serve paragraphs in any order.%0A    if ((language == Language.RUSSIAN %7C%7C 
 lang
@@ -3289,82 +3289,8 @@
 en)%0A
-            %7C%7C %22:%22.equals(lastPragraphString) %7C%7C %22:%22.equals(chklastToken)%0A
     
@@ -3529,16 +3529,17 @@
 ar) && (
+!
 noExcept
@@ -3545,16 +3545,8 @@
 tion
- != true
 )) %7B
@@ -3628,30 +3628,24 @@
 %5D%0A          
-      
 .getStartPos
@@ -3694,22 +3694,16 @@
         
-   
-   
 + tokens
@@ -3748,22 +3748,16 @@
 essages%0A
-      
         
@@ -4310,28 +4310,8 @@
 %5B3%5D.
-getAnalyzedToken(0).
 getT

simplify code