From 9cefe60a4b158a4465a02a42f23743250bb5feeb Mon Sep 17 00:00:00 2001 From: Steven Rowe Date: Thu, 31 Mar 2011 22:34:46 +0000 Subject: [PATCH] Removed special case for looking up KeywordAnalyzer, which is *not* alone among analyzers occupying package o.a.l.analysis.core. Instead, now attempting to instantiate no-package analyzers as core analyzers, then falling back to the previous default package ("org.apache.lucene.analysis.") if that fails. Also, made the same changes in NewShingleAnalyzerTask. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1087468 13f79535-47bb-0310-9956-ffa450edef68 --- .../byTask/tasks/NewAnalyzerTask.java | 31 ++++++++++----- .../byTask/tasks/NewShingleAnalyzerTask.java | 38 +++++++++---------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewAnalyzerTask.java b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewAnalyzerTask.java index 7084125fa98..8ce123e160f 100644 --- a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewAnalyzerTask.java +++ b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewAnalyzerTask.java @@ -20,9 +20,7 @@ import org.apache.lucene.benchmark.byTask.PerfRunData; import org.apache.lucene.util.Version; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.StringTokenizer; +import java.util.*; import java.lang.reflect.Constructor; /** @@ -58,15 +56,28 @@ public class NewAnalyzerTask extends PerfTask { current = 0; } className = analyzerClassNames.get(current++); - if (className == null || className.equals("")) { + Analyzer analyzer = null; + if (null == className || 0 == className.length()) { className = "org.apache.lucene.analysis.standard.StandardAnalyzer"; - } else if (className.equals("KeywordAnalyzer")) { - className = "org.apache.lucene.analysis.core.KeywordAnalyzer"; - } else if (className.indexOf(".") == -1 || className.startsWith("standard.")) { - //there is no package name, assume o.a.l.analysis - className = "org.apache.lucene.analysis." + className; } - getRunData().setAnalyzer(createAnalyzer(className)); + if (-1 == className.indexOf(".")) { + try { + // If no package, first attempt to instantiate a core analyzer + String coreClassName = "org.apache.lucene.analysis.core." + className; + analyzer = createAnalyzer(coreClassName); + className = coreClassName; + } catch (ClassNotFoundException e) { + // If not a core analyzer, try the base analysis package + className = "org.apache.lucene.analysis." + className; + analyzer = createAnalyzer(className); + } + } else { + if (className.startsWith("standard.")) { + className = "org.apache.lucene.analysis." + className; + } + analyzer = createAnalyzer(className); + } + getRunData().setAnalyzer(analyzer); System.out.println("Changed Analyzer to: " + className); } catch (Exception e) { throw new RuntimeException("Error creating Analyzer: " + className, e); diff --git a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewShingleAnalyzerTask.java b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewShingleAnalyzerTask.java index 27b805c41a7..a4af36ae12d 100644 --- a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewShingleAnalyzerTask.java +++ b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/NewShingleAnalyzerTask.java @@ -17,13 +17,11 @@ package org.apache.lucene.benchmark.byTask.tasks; * limitations under the License. */ -import java.lang.reflect.Constructor; import java.util.StringTokenizer; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.shingle.ShingleAnalyzerWrapper; import org.apache.lucene.benchmark.byTask.PerfRunData; -import org.apache.lucene.util.Version; /** * Task to support benchmarking ShingleFilter / ShingleAnalyzerWrapper @@ -45,26 +43,26 @@ public class NewShingleAnalyzerTask extends PerfTask { } private void setAnalyzer() throws Exception { - Class clazz = null; - Analyzer wrappedAnalyzer; - try { - if (analyzerClassName == null || analyzerClassName.equals("")) { - analyzerClassName - = "org.apache.lucene.analysis.standard.StandardAnalyzer"; + Analyzer wrappedAnalyzer = null; + if (null == analyzerClassName || 0 == analyzerClassName.length()) { + analyzerClassName = "org.apache.lucene.analysis.standard.StandardAnalyzer"; + } + if (-1 == analyzerClassName.indexOf(".")) { + String coreClassName = "org.apache.lucene.analysis.core." + analyzerClassName; + try { + // If there is no package, first attempt to instantiate a core analyzer + wrappedAnalyzer = NewAnalyzerTask.createAnalyzer(coreClassName); + analyzerClassName = coreClassName; + } catch (ClassNotFoundException e) { + // If this is not a core analyzer, try the base analysis package + analyzerClassName = "org.apache.lucene.analysis." + analyzerClassName; + wrappedAnalyzer = NewAnalyzerTask.createAnalyzer(analyzerClassName); } - if (analyzerClassName.indexOf(".") == -1 - || analyzerClassName.startsWith("standard.")) { - //there is no package name, assume o.a.l.analysis + } else { + if (analyzerClassName.startsWith("standard.")) { analyzerClassName = "org.apache.lucene.analysis." + analyzerClassName; } - clazz = Class.forName(analyzerClassName).asSubclass(Analyzer.class); - // first try to use a ctor with version parameter (needed for many new - // Analyzers that have no default one anymore) - Constructor ctor = clazz.getConstructor(Version.class); - wrappedAnalyzer = ctor.newInstance(Version.LUCENE_CURRENT); - } catch (NoSuchMethodException e) { - // otherwise use default ctor - wrappedAnalyzer = clazz.newInstance(); + wrappedAnalyzer = NewAnalyzerTask.createAnalyzer(analyzerClassName); } ShingleAnalyzerWrapper analyzer = new ShingleAnalyzerWrapper(wrappedAnalyzer, maxShingleSize); @@ -77,7 +75,7 @@ public class NewShingleAnalyzerTask extends PerfTask { try { setAnalyzer(); System.out.println - ("Changed Analyzer to: ShingleAnalyzerWrapper, wrapping ShingleFilter over" + ("Changed Analyzer to: ShingleAnalyzerWrapper, wrapping ShingleFilter over " + analyzerClassName); } catch (Exception e) { throw new RuntimeException("Error creating Analyzer", e);