2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.analysis;
20
import java.io.IOException;
21
import java.util.Collection;
22
import java.util.HashMap;
25
import org.apache.lucene.analysis.Token;
26
import org.apache.lucene.analysis.TokenStream;
27
import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
28
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
29
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
30
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
31
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
32
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
37
public class TestTrimFilter extends BaseTokenTestCase {
39
public void testTrim() throws Exception {
40
char[] a = " a ".toCharArray();
41
char[] b = "b ".toCharArray();
42
char[] ccc = "cCc".toCharArray();
43
char[] whitespace = " ".toCharArray();
44
char[] empty = "".toCharArray();
45
TrimFilterFactory factory = new TrimFilterFactory();
46
Map<String,String> args = new HashMap<String,String>();
47
args.put("updateOffsets", "false");
49
TokenStream ts = factory.create(new IterTokenStream(new Token(a, 0, a.length, 1, 5),
50
new Token(b, 0, b.length, 6, 10),
51
new Token(ccc, 0, ccc.length, 11, 15),
52
new Token(whitespace, 0, whitespace.length, 16, 20),
53
new Token(empty, 0, empty.length, 21, 21)));
55
assertTokenStreamContents(ts, new String[] { "a", "b", "cCc", "", ""});
57
a = " a".toCharArray();
58
b = "b ".toCharArray();
59
ccc = " c ".toCharArray();
60
whitespace = " ".toCharArray();
61
factory = new TrimFilterFactory();
62
args = new HashMap<String,String>();
63
args.put("updateOffsets", "true");
65
ts = factory.create(new IterTokenStream(
66
new Token(a, 0, a.length, 0, 2),
67
new Token(b, 0, b.length, 0, 2),
68
new Token(ccc, 0, ccc.length, 0, 3),
69
new Token(whitespace, 0, whitespace.length, 0, 3)));
71
assertTokenStreamContents(ts,
72
new String[] { "a", "b", "c", "" },
73
new int[] { 1, 0, 1, 3 },
74
new int[] { 2, 1, 2, 3 },
75
new int[] { 1, 1, 1, 1 });
79
* @deprecated does not support custom attributes
82
private static class IterTokenStream extends TokenStream {
85
CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
86
OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
87
PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
88
FlagsAttribute flagsAtt = addAttribute(FlagsAttribute.class);
89
TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
90
PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);
92
public IterTokenStream(Token... tokens) {
97
public IterTokenStream(Collection<Token> tokens) {
98
this(tokens.toArray(new Token[tokens.size()]));
102
public boolean incrementToken() throws IOException {
103
if (index >= tokens.length)
107
Token token = tokens[index++];
108
termAtt.setEmpty().append(token);
109
offsetAtt.setOffset(token.startOffset(), token.endOffset());
110
posIncAtt.setPositionIncrement(token.getPositionIncrement());
111
flagsAtt.setFlags(token.getFlags());
112
typeAtt.setType(token.type());
113
payloadAtt.setPayload(token.getPayload());