/* LanguageTool, a natural language style checker
 * Copyright (C) 2015 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.de;

import org.languagetool.JLanguageTool;

import java.io.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * @since 3.0
 */
final class CaseRuleExceptions {

  private static final Set<String> exceptions = loadExceptions("/de/case_rule_exceptions.txt");

  private CaseRuleExceptions() {
  }

  public static Set<String> getExceptions() {
    return exceptions;
  }

  public static Set<Pattern[]> getExceptionPatterns() {
    HashSet<Pattern[]> exceptionPatterns = new HashSet<>(250);
    for (String phrase : exceptions) {
      String[] parts = phrase.split(" ");
      Pattern[] patterns = new Pattern[parts.length];
      for (int j = 0; j < parts.length; j++) {
        patterns[j] = Pattern.compile(parts[j]);
      }
      exceptionPatterns.add(patterns);
    }
    return Collections.unmodifiableSet(exceptionPatterns);
  }

  private static Set<String> loadExceptions(String path) {
    Set<String> result = new HashSet<>();
    try (
      InputStream stream = JLanguageTool.getDataBroker().getFromResourceDirAsStream(path);
      InputStreamReader reader = new InputStreamReader(stream, "utf-8");
      BufferedReader br = new BufferedReader(reader)
    ) {
      String line;
      while ((line = br.readLine()) != null) {
        if (line.isEmpty() || line.startsWith("#")) {
          continue;
        }
        if (line.matches("^\\s.*") || line.matches(".*\\s$")) {
          throw new RuntimeException("Invalid line in " + path + ", starts or ends with whitespace: '" + line + "'");
        }
        result.add(line);
      }
    } catch (Exception e) {
      throw new RuntimeException("Could not load case rule exceptions from " + path, e);
    }
    return Collections.unmodifiableSet(result);
  }

}

@@ -946,16 +946,58 @@
 a.io.*;%0A
+import java.nio.charset.StandardCharsets;%0A
 import j
@@ -1218,16 +1218,21 @@
 eptions(
+%0A    
 %22/de/cas
@@ -1253,16 +1253,19 @@
 ons.txt%22
+%0A  
 );%0A%0A  pr
@@ -1889,21 +1889,25 @@
 s(String
+...
  path
+s
 ) %7B%0A    
@@ -1944,16 +1944,50 @@
 et%3C%3E();%0A
+    for (String path : paths) %7B%0A  
     try 
@@ -1988,16 +1988,18 @@
   try (%0A
+  
       In
@@ -2083,24 +2083,26 @@
 ath);%0A      
+  
 InputStreamR
@@ -2150,18 +2150,35 @@
 am, 
-%22utf-8%22
+StandardCharsets.UTF_8
 );%0A
+  
     
@@ -2230,16 +2230,18 @@
 er)%0A    
+  
 ) %7B%0A    
@@ -2238,24 +2238,26 @@
   ) %7B%0A      
+ 
+ 
 String line;
@@ -2257,16 +2257,18 @@
 g line;%0A
+  
       wh
@@ -2306,32 +2306,34 @@
 null) %7B%0A        
+  
 if (line.isEmpty
@@ -2372,16 +2372,18 @@
         
+ 
+ 
 continue
@@ -2384,34 +2384,38 @@
 ntinue;%0A        
-%7D%0A
+  %7D%0A  
         if (line
@@ -2458,24 +2458,26 @@
 .*%5C%5Cs$%22)) %7B%0A
+  
           th
@@ -2582,34 +2582,38 @@
 + %22'%22);%0A        
-%7D%0A
+  %7D%0A  
         result.a
@@ -2618,24 +2618,26 @@
 .add(line);%0A
+  
       %7D%0A    
@@ -2636,16 +2636,18 @@
   %7D%0A    
+  
 %7D catch 
@@ -2658,24 +2658,26 @@
 eption e) %7B%0A
+  
       throw 
@@ -2749,24 +2749,32 @@
 + path, e);%0A
+      %7D%0A
     %7D%0A    re

support more than one file with exceptions