From 338499e86bccbb938cb83b38e9a61fdf49e08203 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Mon, 29 Mar 2004 16:53:35 +0000 Subject: [PATCH] #27987 - add exceptions to WordlistLoader to missing file causes error instead of silently failing git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150256 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/analysis/de/GermanAnalyzer.java | 9 +++-- .../lucene/analysis/de/WordlistLoader.java | 40 ++++++++++--------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java b/src/java/org/apache/lucene/analysis/de/GermanAnalyzer.java index 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.java index 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 words + * @param words Word that where read + * @param length Amount of words that where read into words */ private static Hashtable makeWordTable(String[] words, int length) { Hashtable table = new Hashtable(length);