Apache Lucene open-source search software
#27987 - add exceptions to WordlistLoader to missing file causes error instead of silently failing
Erik Hatcher338499e86bcc
diff --git a/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java b/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.javaindex 7ff8bac7b1e..56a257215be 100644--- a/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java+++ b/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java@@ -62,6 +62,7 @@ import org.apache.lucene.analysis.standard.StandardTokenizer;import java.io.File;import java.io.Reader;+import java.io.IOException;import java.util.HashSet;import java.util.Hashtable;import java.util.Set;@@ -82,14 +83,14 @@ public class GermanAnalyzer extends Analyzer {*/private String[] GERMAN_STOP_WORDS = {"einer", "eine", "eines", "einem", "einen",- "der", "die", "das", "dass", "da�",+ "der", "die", "das", "dass", "da�","du", "er", "sie", "es","was", "wer", "wie", "wir","und", "oder", "ohne", "mit","am", "im", "in", "aus", "auf","ist", "sein", "war", "wird","ihr", "ihre", "ihres",- "als", "f�r", "von", "mit",+ "als", "f�r", "von", "mit","dich", "dir", "mich", "mir","mein", "sein", "kein","durch", "wegen", "wird"@@ -129,7 +130,7 @@ public class GermanAnalyzer extends Analyzer {/*** Builds an analyzer with the given stop words.*/- public GermanAnalyzer(File stopwords) {+ public GermanAnalyzer(File stopwords) throws IOException {stopSet = new HashSet(WordlistLoader.getWordtable(stopwords).keySet());}@@ -150,7 +151,7 @@ public class GermanAnalyzer extends Analyzer {/*** Builds an exclusionlist from the words contained in the given file.*/- public void setStemExclusionTable(File exclusionlist) {+ public void setStemExclusionTable(File exclusionlist) throws IOException {exclusionSet = new HashSet(WordlistLoader.getWordtable(exclusionlist).keySet());}diff --git a/src/java/org/apache/lucene/analysis/de/WordlistLoader.java b/src/java/org/apache/lucene/analysis/de/WordlistLoader.javaindex 916f604742f..0fe68dc1fa1 100644--- a/src/java/org/apache/lucene/analysis/de/WordlistLoader.java+++ b/src/java/org/apache/lucene/analysis/de/WordlistLoader.java@@ -62,20 +62,18 @@ import java.util.Hashtable;/*** Loads a text file and adds every line as an entry to a Hashtable. Every line- * should contain only one word. If the file is not found or on any error, an- * empty table is returned.- *- * @author Gerhard Schwarz- * @version $Id$+ * should contain only one word.*+ * @author Gerhard Schwarz+ * @version $Id$* @todo refactor to convert to Sets instead of Hashtable*/public class WordlistLoader {/**- * @param path Path to the wordlist- * @param wordfile Name of the wordlist+ * @param path Path to the wordlist+ * @param wordfile Name of the wordlist*/- public static Hashtable getWordtable(String path, String wordfile) {+ public static Hashtable getWordtable(String path, String wordfile) throws IOException {if (path == null || wordfile == null) {return new Hashtable();}@@ -83,9 +81,9 @@ public class WordlistLoader {}/**- * @param wordfile Complete path to the wordlist+ * @param wordfile Complete path to the wordlist*/- public static Hashtable getWordtable(String wordfile) {+ public static Hashtable getWordtable(String wordfile) throws IOException {if (wordfile == null) {return new Hashtable();}@@ -93,16 +91,19 @@ public class WordlistLoader {}/**- * @param wordfile File containing the wordlist+ * @param wordfile File containing the wordlist* @todo Create a Set version of this method*/- public static Hashtable getWordtable(File wordfile) {+ public static Hashtable getWordtable(File wordfile) throws IOException {if (wordfile == null) {return new Hashtable();}Hashtable result = null;+ FileReader freader = null;+ LineNumberReader lnr = null;try {- LineNumberReader lnr = new LineNumberReader(new FileReader(wordfile));+ freader = new FileReader(wordfile);+ lnr = new LineNumberReader(freader);String word = null;String[] stopwords = new String[100];int wordcount = 0;@@ -116,10 +117,11 @@ public class WordlistLoader {stopwords[wordcount - 1] = word;}result = makeWordTable(stopwords, wordcount);- }-// On error, use an empty table- catch (IOException e) {- result = new Hashtable();+ } finally {+ if (lnr != null)+ lnr.close();+ if (freader != null)+ freader.close();}return result;}@@ -127,8 +129,8 @@ public class WordlistLoader {/*** Builds the wordlist table.*- * @param words Word that where read- * @param length Amount of words that where read into <tt>words</tt>+ * @param words Word that where read+ * @param length Amount of words that where read into <tt>words</tt>*/private static Hashtable makeWordTable(String[] words, int length) {Hashtable table = new Hashtable(length);