/* 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 org.languagetool.rules.patterns;

import org.languagetool.JLanguageTool;
import org.languagetool.Language;
import org.languagetool.ShortDescriptionProvider;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.*;
import java.io.*;
import java.text.MessageFormat;
import java.util.*;

/**
 * Loads {@link PatternRule}s from a false friends XML file.
 * 
 * @author Daniel Naber
 */
public class FalseFriendRuleLoader extends DefaultHandler {

  private final String falseFriendHint;
  private final String falseFriendSugg;

  public FalseFriendRuleLoader(Language motherTongue) {
    ResourceBundle messages = JLanguageTool.getDataBroker().getResourceBundle(JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale());
    this.falseFriendHint =  messages.getString("false_friend_hint");
    this.falseFriendSugg =  messages.getString("false_friend_suggestion");
  }

  public FalseFriendRuleLoader(String falseFriendHint, String falseFriendSugg) {
    this.falseFriendHint = Objects.requireNonNull(falseFriendHint);
    this.falseFriendSugg = Objects.requireNonNull(falseFriendSugg);
  }

  /**
   * @param file XML file with false friend rules
   * @since 2.3
   */
  public final List<AbstractPatternRule> getRules(File file, Language language, Language motherTongue) throws IOException {
    try (InputStream inputStream = new FileInputStream(file)) {
      return getRules(inputStream, language, motherTongue);
    } catch (ParserConfigurationException | SAXException e) {
      throw new IOException("Could not load false friend rules from " + file, e);
    }
  }

  public final List<AbstractPatternRule> getRules(InputStream stream,
      Language textLanguage, Language motherTongue)
      throws ParserConfigurationException, SAXException, IOException {
    FalseFriendRuleHandler handler = new FalseFriendRuleHandler(
        textLanguage, motherTongue, falseFriendHint);
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    saxParser.getXMLReader().setFeature(
            "http://apache.org/xml/features/nonvalidating/load-external-dtd",
            false);
    saxParser.parse(stream, handler);
    List<AbstractPatternRule> rules = handler.getRules();
    // Add suggestions to each rule:
    MessageFormat msgFormat = new MessageFormat(falseFriendSugg);
    ShortDescriptionProvider descProvider = new ShortDescriptionProvider();
    for (AbstractPatternRule rule : rules) {
      List<String> suggestions = handler.getSuggestionMap().get(rule.getId());
      if (suggestions != null) {
        List<String> formattedSuggestions = new ArrayList<>();
        for (String suggestion : suggestions) {
          String desc = descProvider.getShortDescription(suggestion, textLanguage);
          if (desc != null) {
            formattedSuggestions.add("<suggestion>" + suggestion + "</suggestion> (" + desc + ")");
          } else {
            formattedSuggestions.add("<suggestion>" + suggestion + "</suggestion>");
          }
        }
        rule.setMessage(rule.getMessage() + " " + msgFormat.format(formattedSuggestions.toArray()));
      }
    }
    return rules;
  }

}

@@ -973,58 +973,8 @@
 ge;%0A
-import org.languagetool.ShortDescriptionProvider;%0A
 impo
@@ -1143,16 +1143,52 @@
 .util.*;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -1248,17 +1248,16 @@
 file.%0A *
- 
 %0A * @aut
@@ -3229,84 +3229,8 @@
 g);%0A
-    ShortDescriptionProvider descProvider = new ShortDescriptionProvider();%0A
     
@@ -3394,84 +3394,32 @@
     
-List%3C
 String
-%3E formattedSuggestions = new ArrayList%3C%3E();%0A        for (String s
+%5B%5D msg = %7B formatS
 ugge
@@ -3423,19 +3423,18 @@
 ggestion
- : 
+s(
 suggesti
@@ -3438,17 +3438,18 @@
 stions) 
-%7B
+%7D;
 %0A       
@@ -3453,81 +3453,71 @@
     
-  String desc = descProvider.getShortDescription(suggestion, textLanguage
+rule.setMessage(rule.getMessage() + %22 %22 + msgFormat.format(msg)
 );%0A 
@@ -3525,53 +3525,62 @@
     
+%7D%0A
     
-if (desc != null) %7B%0A           
+%7D%0A    return rules;%0A  %7D%0A%0A  private String
  format
-ted
 Sugg
@@ -3590,132 +3590,58 @@
 ions
-.add(%22%3Csuggestion%3E%22 + suggestion + %22%3C/suggestion%3E (%22 + desc + %22)%22);%0A          %7D else
+(List%3CString%3E l)
  %7B%0A    
- 
-       formattedSuggestions.add(
+return l.stream().map(o -%3E 
 %22%3Csu
@@ -3653,26 +3653,17 @@
 ion%3E%22 + 
-suggestion
+o
  + %22%3C/su
@@ -3677,163 +3677,42 @@
 n%3E%22)
-;%0A          %7D%0A        %7D%0A        rule.setMessage(rule.getMessage() + %22 %22 + msgFormat.format(formattedSuggestions.toArray()));%0A      %7D%0A    %7D%0A    return rules
+.collect(Collectors.joining(%22, %22))
 ;%0A  

revert for now to fix tests (FalseFriendRuleTest)