mirror of https://github.com/apache/lucene.git
LUCENE-4268: Rename ResourceAsStreamReasourceLoader to ClasspathResourceLoader, provide FilesystemResourceLoader, bug fixing
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1366748 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
138a1d762a
commit
92af111ca3
|
@ -0,0 +1,80 @@
|
||||||
|
package org.apache.lucene.analysis.util;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple {@link ResourceLoader} that uses {@link ClassLoader#getResourceAsStream(String)}
|
||||||
|
* and {@link Class#forName(String,boolean,ClassLoader)} to open resources and
|
||||||
|
* classes, respectively.
|
||||||
|
*/
|
||||||
|
public final class ClasspathResourceLoader implements ResourceLoader {
|
||||||
|
private final Class<?> clazz;
|
||||||
|
private final ClassLoader loader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance using the context classloader to load Resources and classes.
|
||||||
|
* Resource paths must be absolute.
|
||||||
|
*/
|
||||||
|
public ClasspathResourceLoader() {
|
||||||
|
this(Thread.currentThread().getContextClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance using the given classloader to load Resources and classes.
|
||||||
|
* Resource paths must be absolute.
|
||||||
|
*/
|
||||||
|
public ClasspathResourceLoader(ClassLoader loader) {
|
||||||
|
this(null, loader);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance using the context classloader to load Resources and classes
|
||||||
|
* Resources are resolved relative to the given class, if path is not absolute.
|
||||||
|
*/
|
||||||
|
public ClasspathResourceLoader(Class<?> clazz) {
|
||||||
|
this(clazz, clazz.getClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClasspathResourceLoader(Class<?> clazz, ClassLoader loader) {
|
||||||
|
this.clazz = clazz;
|
||||||
|
this.loader = loader;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openResource(String resource) throws IOException {
|
||||||
|
final InputStream stream = (clazz != null) ?
|
||||||
|
clazz.getResourceAsStream(resource) :
|
||||||
|
loader.getResourceAsStream(resource);
|
||||||
|
if (stream == null)
|
||||||
|
throw new IOException("Resource not found: " + resource);
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T newInstance(String cname, Class<T> expectedType) {
|
||||||
|
try {
|
||||||
|
final Class<? extends T> clazz = Class.forName(cname, true, loader).asSubclass(expectedType);
|
||||||
|
return clazz.newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Cannot instantiate class: " + cname, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
package org.apache.lucene.analysis.util;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple {@link ResourceLoader} that opens resource files
|
||||||
|
* from the local file system, optionally resolving against
|
||||||
|
* a base directory.
|
||||||
|
*
|
||||||
|
* <p>This loader wraps a delegate {@link ResourceLoader}
|
||||||
|
* that is used to resolve all files, the current base directory
|
||||||
|
* does not contain. {@link #newInstance} is always resolved
|
||||||
|
* against the delegate, as a {@link ClassLoader} is needed.
|
||||||
|
*
|
||||||
|
* <p>You can chain several {@code FilesystemResourceLoader}s
|
||||||
|
* to allow lookup of files in more than one base directory.
|
||||||
|
*/
|
||||||
|
public final class FilesystemResourceLoader implements ResourceLoader {
|
||||||
|
private final File baseDirectory;
|
||||||
|
private final ResourceLoader delegate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a resource loader that requires absolute filenames or relative to CWD
|
||||||
|
* to resolve resources. Files not found in file system and class lookups
|
||||||
|
* are delegated to context classloader.
|
||||||
|
*/
|
||||||
|
public FilesystemResourceLoader() {
|
||||||
|
this((File) null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a resource loader that resolves resources against the given
|
||||||
|
* base directory (may be {@code null} to refer to CWD).
|
||||||
|
* Files not found in file system and class lookups are delegated to context
|
||||||
|
* classloader.
|
||||||
|
*/
|
||||||
|
public FilesystemResourceLoader(File baseDirectory) {
|
||||||
|
this(baseDirectory, new ClasspathResourceLoader());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a resource loader that resolves resources against the given
|
||||||
|
* base directory (may be {@code null} to refer to CWD).
|
||||||
|
* Files not found in file system and class lookups are delegated
|
||||||
|
* to the given delegate {@link ResourceLoader}.
|
||||||
|
*/
|
||||||
|
public FilesystemResourceLoader(File baseDirectory, ResourceLoader delegate) {
|
||||||
|
if (baseDirectory != null && !baseDirectory.isDirectory())
|
||||||
|
throw new IllegalArgumentException("baseDirectory is not a directory or null");
|
||||||
|
if (delegate == null)
|
||||||
|
throw new IllegalArgumentException("delegate ResourceLoader may not be null");
|
||||||
|
this.baseDirectory = baseDirectory;
|
||||||
|
this.delegate = delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream openResource(String resource) throws IOException {
|
||||||
|
try {
|
||||||
|
File file = new File (resource);
|
||||||
|
if (baseDirectory != null && !file.isAbsolute()) {
|
||||||
|
file = new File(baseDirectory, resource);
|
||||||
|
}
|
||||||
|
return new FileInputStream(file);
|
||||||
|
} catch (FileNotFoundException fnfe) {
|
||||||
|
return delegate.openResource(resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T newInstance(String cname, Class<T> expectedType) {
|
||||||
|
return delegate.newInstance(cname, expectedType);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,48 +0,0 @@
|
||||||
package org.apache.lucene.analysis.util;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
|
||||||
* this work for additional information regarding copyright ownership.
|
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
||||||
* (the "License"); you may not use this file except in compliance with
|
|
||||||
* the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Simple ResourceLoader that uses Class.getResourceAsStream
|
|
||||||
* and Class.forName to open resources and classes, respectively.
|
|
||||||
*/
|
|
||||||
public class ResourceAsStreamResourceLoader implements ResourceLoader {
|
|
||||||
private final Class<?> clazz;
|
|
||||||
|
|
||||||
public ResourceAsStreamResourceLoader(Class<?> clazz) {
|
|
||||||
this.clazz = clazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream openResource(String resource) throws IOException {
|
|
||||||
return clazz.getResourceAsStream(resource);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T newInstance(String cname, Class<T> expectedType) {
|
|
||||||
try {
|
|
||||||
Class<? extends T> clazz = Class.forName(cname).asSubclass(expectedType);
|
|
||||||
return clazz.newInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,7 +23,7 @@ import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.Tokenizer;
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
import org.apache.lucene.analysis.core.TestStopFilter;
|
import org.apache.lucene.analysis.core.TestStopFilter;
|
||||||
import org.apache.lucene.analysis.util.CharArraySet;
|
import org.apache.lucene.analysis.util.CharArraySet;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
|
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
@ -39,7 +39,7 @@ import java.util.HashMap;
|
||||||
public class TestCommonGramsFilterFactory extends BaseTokenStreamTestCase {
|
public class TestCommonGramsFilterFactory extends BaseTokenStreamTestCase {
|
||||||
|
|
||||||
public void testInform() throws Exception {
|
public void testInform() throws Exception {
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class);
|
ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class);
|
||||||
assertTrue("loader is null and it shouldn't be", loader != null);
|
assertTrue("loader is null and it shouldn't be", loader != null);
|
||||||
CommonGramsFilterFactory factory = new CommonGramsFilterFactory();
|
CommonGramsFilterFactory factory = new CommonGramsFilterFactory();
|
||||||
Map<String, String> args = new HashMap<String, String>();
|
Map<String, String> args = new HashMap<String, String>();
|
||||||
|
@ -89,7 +89,7 @@ public class TestCommonGramsFilterFactory extends BaseTokenStreamTestCase {
|
||||||
* If no words are provided, then a set of english default stopwords is used.
|
* If no words are provided, then a set of english default stopwords is used.
|
||||||
*/
|
*/
|
||||||
public void testDefaults() throws Exception {
|
public void testDefaults() throws Exception {
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class);
|
ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class);
|
||||||
assertTrue("loader is null and it shouldn't be", loader != null);
|
assertTrue("loader is null and it shouldn't be", loader != null);
|
||||||
CommonGramsFilterFactory factory = new CommonGramsFilterFactory();
|
CommonGramsFilterFactory factory = new CommonGramsFilterFactory();
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
|
|
|
@ -23,7 +23,7 @@ import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.Tokenizer;
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
import org.apache.lucene.analysis.core.TestStopFilter;
|
import org.apache.lucene.analysis.core.TestStopFilter;
|
||||||
import org.apache.lucene.analysis.util.CharArraySet;
|
import org.apache.lucene.analysis.util.CharArraySet;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
|
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
@ -39,7 +39,7 @@ import java.util.HashMap;
|
||||||
public class TestCommonGramsQueryFilterFactory extends BaseTokenStreamTestCase {
|
public class TestCommonGramsQueryFilterFactory extends BaseTokenStreamTestCase {
|
||||||
|
|
||||||
public void testInform() throws Exception {
|
public void testInform() throws Exception {
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class);
|
ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class);
|
||||||
assertTrue("loader is null and it shouldn't be", loader != null);
|
assertTrue("loader is null and it shouldn't be", loader != null);
|
||||||
CommonGramsQueryFilterFactory factory = new CommonGramsQueryFilterFactory();
|
CommonGramsQueryFilterFactory factory = new CommonGramsQueryFilterFactory();
|
||||||
Map<String, String> args = new HashMap<String, String>();
|
Map<String, String> args = new HashMap<String, String>();
|
||||||
|
@ -89,7 +89,7 @@ public class TestCommonGramsQueryFilterFactory extends BaseTokenStreamTestCase {
|
||||||
* If no words are provided, then a set of english default stopwords is used.
|
* If no words are provided, then a set of english default stopwords is used.
|
||||||
*/
|
*/
|
||||||
public void testDefaults() throws Exception {
|
public void testDefaults() throws Exception {
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(TestStopFilter.class);
|
ResourceLoader loader = new ClasspathResourceLoader(TestStopFilter.class);
|
||||||
assertTrue("loader is null and it shouldn't be", loader != null);
|
assertTrue("loader is null and it shouldn't be", loader != null);
|
||||||
CommonGramsQueryFilterFactory factory = new CommonGramsQueryFilterFactory();
|
CommonGramsQueryFilterFactory factory = new CommonGramsQueryFilterFactory();
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
|
|
|
@ -26,7 +26,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.MockTokenizer;
|
import org.apache.lucene.analysis.MockTokenizer;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.Tokenizer;
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,7 +40,7 @@ public class TestDictionaryCompoundWordTokenFilterFactory extends BaseTokenStrea
|
||||||
Reader reader = new StringReader("I like to play softball");
|
Reader reader = new StringReader("I like to play softball");
|
||||||
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
||||||
DictionaryCompoundWordTokenFilterFactory factory = new DictionaryCompoundWordTokenFilterFactory();
|
DictionaryCompoundWordTokenFilterFactory factory = new DictionaryCompoundWordTokenFilterFactory();
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
Map<String,String> args = new HashMap<String,String>();
|
Map<String,String> args = new HashMap<String,String>();
|
||||||
args.put("dictionary", "compoundDictionary.txt");
|
args.put("dictionary", "compoundDictionary.txt");
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
|
|
|
@ -26,7 +26,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.MockTokenizer;
|
import org.apache.lucene.analysis.MockTokenizer;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.Tokenizer;
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,7 +40,7 @@ public class TestHyphenationCompoundWordTokenFilterFactory extends BaseTokenStre
|
||||||
Reader reader = new StringReader("min veninde som er lidt af en læsehest");
|
Reader reader = new StringReader("min veninde som er lidt af en læsehest");
|
||||||
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
||||||
HyphenationCompoundWordTokenFilterFactory factory = new HyphenationCompoundWordTokenFilterFactory();
|
HyphenationCompoundWordTokenFilterFactory factory = new HyphenationCompoundWordTokenFilterFactory();
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
Map<String,String> args = new HashMap<String,String>();
|
Map<String,String> args = new HashMap<String,String>();
|
||||||
args.put("hyphenator", "da_UTF8.xml");
|
args.put("hyphenator", "da_UTF8.xml");
|
||||||
args.put("dictionary", "da_compoundDictionary.txt");
|
args.put("dictionary", "da_compoundDictionary.txt");
|
||||||
|
@ -64,7 +64,7 @@ public class TestHyphenationCompoundWordTokenFilterFactory extends BaseTokenStre
|
||||||
Reader reader = new StringReader("basketballkurv");
|
Reader reader = new StringReader("basketballkurv");
|
||||||
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
||||||
HyphenationCompoundWordTokenFilterFactory factory = new HyphenationCompoundWordTokenFilterFactory();
|
HyphenationCompoundWordTokenFilterFactory factory = new HyphenationCompoundWordTokenFilterFactory();
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
Map<String,String> args = new HashMap<String,String>();
|
Map<String,String> args = new HashMap<String,String>();
|
||||||
args.put("hyphenator", "da_UTF8.xml");
|
args.put("hyphenator", "da_UTF8.xml");
|
||||||
args.put("minSubwordSize", "2");
|
args.put("minSubwordSize", "2");
|
||||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.lucene.analysis.core;
|
||||||
|
|
||||||
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.util.CharArraySet;
|
import org.apache.lucene.analysis.util.CharArraySet;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -32,7 +32,7 @@ import java.util.HashMap;
|
||||||
public class TestStopFilterFactory extends BaseTokenStreamTestCase {
|
public class TestStopFilterFactory extends BaseTokenStreamTestCase {
|
||||||
|
|
||||||
public void testInform() throws Exception {
|
public void testInform() throws Exception {
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
assertTrue("loader is null and it shouldn't be", loader != null);
|
assertTrue("loader is null and it shouldn't be", loader != null);
|
||||||
StopFilterFactory factory = new StopFilterFactory();
|
StopFilterFactory factory = new StopFilterFactory();
|
||||||
Map<String, String> args = new HashMap<String, String>();
|
Map<String, String> args = new HashMap<String, String>();
|
||||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.lucene.analysis.core;
|
||||||
|
|
||||||
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.NumericTokenStream;
|
import org.apache.lucene.analysis.NumericTokenStream;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ public class TestTypeTokenFilterFactory extends BaseTokenStreamTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInform() throws Exception {
|
public void testInform() throws Exception {
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
TypeTokenFilterFactory factory = new TypeTokenFilterFactory();
|
TypeTokenFilterFactory factory = new TypeTokenFilterFactory();
|
||||||
Map<String, String> args = new HashMap<String, String>();
|
Map<String, String> args = new HashMap<String, String>();
|
||||||
args.put("types", "stoptypes-1.txt");
|
args.put("types", "stoptypes-1.txt");
|
||||||
|
@ -94,7 +94,7 @@ public class TestTypeTokenFilterFactory extends BaseTokenStreamTestCase {
|
||||||
args.put("enablePositionIncrements", "false");
|
args.put("enablePositionIncrements", "false");
|
||||||
typeTokenFilterFactory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
typeTokenFilterFactory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
typeTokenFilterFactory.init(args);
|
typeTokenFilterFactory.init(args);
|
||||||
typeTokenFilterFactory.inform(new ResourceAsStreamResourceLoader(getClass()));
|
typeTokenFilterFactory.inform(new ClasspathResourceLoader(getClass()));
|
||||||
fail("not supplying 'types' parameter should cause an IllegalArgumentException");
|
fail("not supplying 'types' parameter should cause an IllegalArgumentException");
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// everything ok
|
// everything ok
|
||||||
|
|
|
@ -27,7 +27,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.MockTokenizer;
|
import org.apache.lucene.analysis.MockTokenizer;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.Tokenizer;
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,7 +42,7 @@ public class TestElisionFilterFactory extends BaseTokenStreamTestCase {
|
||||||
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
||||||
ElisionFilterFactory factory = new ElisionFilterFactory();
|
ElisionFilterFactory factory = new ElisionFilterFactory();
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
Map<String,String> args = new HashMap<String,String>();
|
Map<String,String> args = new HashMap<String,String>();
|
||||||
args.put("articles", "frenchArticles.txt");
|
args.put("articles", "frenchArticles.txt");
|
||||||
factory.init(args);
|
factory.init(args);
|
||||||
|
@ -61,7 +61,7 @@ public class TestElisionFilterFactory extends BaseTokenStreamTestCase {
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
Map<String, String> args = Collections.emptyMap();
|
Map<String, String> args = Collections.emptyMap();
|
||||||
factory.init(args);
|
factory.init(args);
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
factory.inform(loader);
|
factory.inform(loader);
|
||||||
TokenStream stream = factory.create(tokenizer);
|
TokenStream stream = factory.create(tokenizer);
|
||||||
assertTokenStreamContents(stream, new String[] { "avion" });
|
assertTokenStreamContents(stream, new String[] { "avion" });
|
||||||
|
@ -75,7 +75,7 @@ public class TestElisionFilterFactory extends BaseTokenStreamTestCase {
|
||||||
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.WHITESPACE, false);
|
||||||
ElisionFilterFactory factory = new ElisionFilterFactory();
|
ElisionFilterFactory factory = new ElisionFilterFactory();
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
Map<String,String> args = new HashMap<String,String>();
|
Map<String,String> args = new HashMap<String,String>();
|
||||||
args.put("articles", "frenchArticles.txt");
|
args.put("articles", "frenchArticles.txt");
|
||||||
args.put("ignoreCase", "true");
|
args.put("ignoreCase", "true");
|
||||||
|
|
|
@ -25,7 +25,7 @@ import java.util.Map;
|
||||||
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.MockTokenizer;
|
import org.apache.lucene.analysis.MockTokenizer;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple tests to ensure the Hunspell stemmer loads from factory
|
* Simple tests to ensure the Hunspell stemmer loads from factory
|
||||||
|
@ -38,7 +38,7 @@ public class TestHunspellStemFilterFactory extends BaseTokenStreamTestCase {
|
||||||
args.put("affix", "test.aff");
|
args.put("affix", "test.aff");
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
factory.init(args);
|
factory.init(args);
|
||||||
factory.inform(new ResourceAsStreamResourceLoader(getClass()));
|
factory.inform(new ClasspathResourceLoader(getClass()));
|
||||||
|
|
||||||
Reader reader = new StringReader("abc");
|
Reader reader = new StringReader("abc");
|
||||||
TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false));
|
TokenStream stream = factory.create(new MockTokenizer(reader, MockTokenizer.WHITESPACE, false));
|
||||||
|
|
|
@ -19,7 +19,7 @@ package org.apache.lucene.analysis.miscellaneous;
|
||||||
|
|
||||||
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.util.CharArraySet;
|
import org.apache.lucene.analysis.util.CharArraySet;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.ResourceLoader;
|
import org.apache.lucene.analysis.util.ResourceLoader;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -32,7 +32,7 @@ import java.util.HashMap;
|
||||||
public class TestKeepFilterFactory extends BaseTokenStreamTestCase {
|
public class TestKeepFilterFactory extends BaseTokenStreamTestCase {
|
||||||
|
|
||||||
public void testInform() throws Exception {
|
public void testInform() throws Exception {
|
||||||
ResourceLoader loader = new ResourceAsStreamResourceLoader(getClass());
|
ResourceLoader loader = new ClasspathResourceLoader(getClass());
|
||||||
assertTrue("loader is null and it shouldn't be", loader != null);
|
assertTrue("loader is null and it shouldn't be", loader != null);
|
||||||
KeepWordFilterFactory factory = new KeepWordFilterFactory();
|
KeepWordFilterFactory factory = new KeepWordFilterFactory();
|
||||||
Map<String, String> args = new HashMap<String, String>();
|
Map<String, String> args = new HashMap<String, String>();
|
||||||
|
|
|
@ -25,7 +25,7 @@ import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.MockTokenizer;
|
import org.apache.lucene.analysis.MockTokenizer;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.synonym.SynonymFilter;
|
import org.apache.lucene.analysis.synonym.SynonymFilter;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
import org.apache.lucene.analysis.util.StringMockResourceLoader;
|
import org.apache.lucene.analysis.util.StringMockResourceLoader;
|
||||||
|
|
||||||
public class TestSynonymFilterFactory extends BaseTokenStreamTestCase {
|
public class TestSynonymFilterFactory extends BaseTokenStreamTestCase {
|
||||||
|
@ -36,7 +36,7 @@ public class TestSynonymFilterFactory extends BaseTokenStreamTestCase {
|
||||||
args.put("synonyms", "synonyms.txt");
|
args.put("synonyms", "synonyms.txt");
|
||||||
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
|
||||||
factory.init(args);
|
factory.init(args);
|
||||||
factory.inform(new ResourceAsStreamResourceLoader(getClass()));
|
factory.inform(new ClasspathResourceLoader(getClass()));
|
||||||
TokenStream ts = factory.create(new MockTokenizer(new StringReader("GB"), MockTokenizer.WHITESPACE, false));
|
TokenStream ts = factory.create(new MockTokenizer(new StringReader("GB"), MockTokenizer.WHITESPACE, false));
|
||||||
assertTrue(ts instanceof SynonymFilter);
|
assertTrue(ts instanceof SynonymFilter);
|
||||||
assertTokenStreamContents(ts,
|
assertTokenStreamContents(ts,
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
package org.apache.lucene.analysis.util;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
|
import org.apache.lucene.util.IOUtils;
|
||||||
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
import org.apache.lucene.util._TestUtil;
|
||||||
|
|
||||||
|
public class TestFilesystemResourceLoader extends LuceneTestCase {
|
||||||
|
|
||||||
|
private void assertNotFound(ResourceLoader rl) throws Exception {
|
||||||
|
try {
|
||||||
|
IOUtils.closeWhileHandlingException(rl.openResource("/this-directory-really-really-really-should-not-exist/foo/bar.txt"));
|
||||||
|
fail("The resource does not exist, should fail!");
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
rl.newInstance("org.apache.lucene.analysis.FooBarFilterFactory", TokenFilterFactory.class);
|
||||||
|
fail("The class does not exist, should fail!");
|
||||||
|
} catch (RuntimeException iae) {
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertClasspathDelegation(ResourceLoader rl) throws Exception {
|
||||||
|
// try a stopwords file from classpath
|
||||||
|
CharArraySet set = WordlistLoader.getSnowballWordSet(
|
||||||
|
new InputStreamReader(rl.openResource("org/apache/lucene/analysis/snowball/english_stop.txt"), IOUtils.CHARSET_UTF_8),
|
||||||
|
TEST_VERSION_CURRENT
|
||||||
|
);
|
||||||
|
assertTrue(set.contains("you"));
|
||||||
|
// try to load a class; we use string comparison because classloader may be different...
|
||||||
|
assertEquals("org.apache.lucene.analysis.en.KStemFilterFactory",
|
||||||
|
rl.newInstance("org.apache.lucene.analysis.en.KStemFilterFactory", TokenFilterFactory.class).getClass().getName());
|
||||||
|
// theoretically classes should also be loadable:
|
||||||
|
IOUtils.closeWhileHandlingException(rl.openResource("java/lang/String.class"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testBaseDir() throws Exception {
|
||||||
|
final File base = _TestUtil.getTempDir("fsResourceLoaderBase");
|
||||||
|
try {
|
||||||
|
base.mkdirs();
|
||||||
|
Writer os = new OutputStreamWriter(new FileOutputStream(new File(base, "template.txt")), IOUtils.CHARSET_UTF_8);
|
||||||
|
try {
|
||||||
|
os.write("foobar\n");
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeWhileHandlingException(os);
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourceLoader rl = new FilesystemResourceLoader(base);
|
||||||
|
assertEquals("foobar", WordlistLoader.getLines(rl.openResource("template.txt"), IOUtils.CHARSET_UTF_8).get(0));
|
||||||
|
// Same with full path name:
|
||||||
|
String fullPath = new File(base, "template.txt").toString();
|
||||||
|
assertEquals("foobar",
|
||||||
|
WordlistLoader.getLines(rl.openResource(fullPath), IOUtils.CHARSET_UTF_8).get(0));
|
||||||
|
assertClasspathDelegation(rl);
|
||||||
|
assertNotFound(rl);
|
||||||
|
|
||||||
|
// now use RL without base dir:
|
||||||
|
rl = new FilesystemResourceLoader();
|
||||||
|
assertEquals("foobar",
|
||||||
|
WordlistLoader.getLines(rl.openResource(new File(base, "template.txt").toString()), IOUtils.CHARSET_UTF_8).get(0));
|
||||||
|
assertClasspathDelegation(rl);
|
||||||
|
assertNotFound(rl);
|
||||||
|
} finally {
|
||||||
|
_TestUtil.rmDir(base);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDelegation() throws Exception {
|
||||||
|
ResourceLoader rl = new FilesystemResourceLoader(null, new StringMockResourceLoader("foobar\n"));
|
||||||
|
assertEquals("foobar", WordlistLoader.getLines(rl.openResource("template.txt"), IOUtils.CHARSET_UTF_8).get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -22,7 +22,7 @@ import java.io.StringReader;
|
||||||
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
import org.apache.lucene.analysis.BaseTokenStreamTestCase;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
|
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
|
||||||
import org.apache.lucene.analysis.util.ResourceAsStreamResourceLoader;
|
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link StempelPolishStemFilterFactory}
|
* Tests for {@link StempelPolishStemFilterFactory}
|
||||||
|
@ -31,7 +31,7 @@ public class TestStempelPolishStemFilterFactory extends BaseTokenStreamTestCase
|
||||||
public void testBasics() throws Exception {
|
public void testBasics() throws Exception {
|
||||||
StringReader document = new StringReader("studenta studenci");
|
StringReader document = new StringReader("studenta studenci");
|
||||||
StempelPolishStemFilterFactory factory = new StempelPolishStemFilterFactory();
|
StempelPolishStemFilterFactory factory = new StempelPolishStemFilterFactory();
|
||||||
factory.inform(new ResourceAsStreamResourceLoader(getClass()));
|
factory.inform(new ClasspathResourceLoader(getClass()));
|
||||||
TokenStream ts = factory.create(new WhitespaceTokenizer(TEST_VERSION_CURRENT, document));
|
TokenStream ts = factory.create(new WhitespaceTokenizer(TEST_VERSION_CURRENT, document));
|
||||||
assertTokenStreamContents(ts,
|
assertTokenStreamContents(ts,
|
||||||
new String[] { "student", "student" });
|
new String[] { "student", "student" });
|
||||||
|
|
Loading…
Reference in New Issue