KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > search > analyzer > TokenWithQuoteFilter


1 package org.jahia.services.search.analyzer;
2
3 import org.apache.lucene.analysis.*;
4 import org.apache.lucene.analysis.Token;
5
6 import java.io.IOException JavaDoc;
7 import java.util.Stack JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 /**
11  * Created by IntelliJ IDEA.
12  * User: hollis
13  * Date: 25 mai 2005
14  * Time: 21:05:11
15  * To change this template use File | Settings | File Templates.
16  */

17 public class TokenWithQuoteFilter extends TokenFilter
18         implements StandardTokenizerConstants {
19
20     private static final String JavaDoc APOSTROPHE_TYPE = tokenImage[APOSTROPHE];
21
22     private Stack JavaDoc splittedWords;
23
24     public TokenWithQuoteFilter(TokenStream in) {
25         super(in);
26         splittedWords = new Stack JavaDoc();
27     }
28
29     public final Token next() throws IOException JavaDoc {
30         if ( splittedWords.size()>0 ){
31             return (Token)splittedWords.pop();
32         }
33         Token t = input.next();
34         if ( t == null ){
35             return null;
36         }
37         splitWords(t);
38         return t;
39     }
40
41     private void splitWords(Token t){
42         if (t.type() == APOSTROPHE_TYPE) {
43
44             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(t.termText(),"'");
45             Token token = null;
46             String JavaDoc text = null;
47             while ( st.hasMoreTokens() ){
48                 text = st.nextToken();
49                 if ( text.length()>1 ){
50                     token = new Token(text,t.startOffset(),t.endOffset());
51                     token.setPositionIncrement(0);
52                     splittedWords.push(token);
53                 }
54             }
55         }
56     }
57
58 }
59
Popular Tags