~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/analysis/TrimFilter.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.solr.analysis;
19
 
 
20
 
import org.apache.lucene.analysis.TokenFilter;
21
 
import org.apache.lucene.analysis.TokenStream;
22
 
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
23
 
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
24
 
 
25
 
import java.io.IOException;
26
 
 
27
 
/**
28
 
 * Trims leading and trailing whitespace from Tokens in the stream.
29
 
 *
30
 
 * @version $Id:$
31
 
 */
32
 
public final class TrimFilter extends TokenFilter {
33
 
 
34
 
  final boolean updateOffsets;
35
 
  private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
36
 
  private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
37
 
 
38
 
 
39
 
  public TrimFilter(TokenStream in, boolean updateOffsets) {
40
 
    super(in);
41
 
    this.updateOffsets = updateOffsets;
42
 
  }
43
 
 
44
 
  @Override
45
 
  public boolean incrementToken() throws IOException {
46
 
    if (!input.incrementToken()) return false;
47
 
 
48
 
    char[] termBuffer = termAtt.buffer();
49
 
    int len = termAtt.length();
50
 
    //TODO: Is this the right behavior or should we return false?  Currently, "  ", returns true, so I think this should
51
 
    //also return true
52
 
    if (len == 0){
53
 
      return true;
54
 
    }
55
 
    int start = 0;
56
 
    int end = 0;
57
 
    int endOff = 0;
58
 
 
59
 
    // eat the first characters
60
 
    //QUESTION: Should we use Character.isWhitespace() instead?
61
 
    for (start = 0; start < len && termBuffer[start] <= ' '; start++) {
62
 
    }
63
 
    // eat the end characters
64
 
    for (end = len; end >= start && termBuffer[end - 1] <= ' '; end--) {
65
 
      endOff++;
66
 
    }
67
 
    if (start > 0 || end < len) {
68
 
      if (start < end) {
69
 
        termAtt.copyBuffer(termBuffer, start, (end - start));
70
 
      } else {
71
 
        termAtt.setEmpty();
72
 
      }
73
 
      if (updateOffsets) {
74
 
        int newStart = offsetAtt.startOffset()+start;
75
 
        int newEnd = offsetAtt.endOffset() - (start<end ? endOff:0);
76
 
        offsetAtt.setOffset(newStart, newEnd);
77
 
      }
78
 
    }
79
 
 
80
 
    return true;
81
 
  }
82
 
}