KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > analysis > DateTokenFilter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.impl.lucene.analysis;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import org.alfresco.util.CachingDateFormat;
26 import org.apache.lucene.analysis.Token;
27 import org.apache.lucene.analysis.Tokenizer;
28 import org.apache.lucene.analysis.WhitespaceTokenizer;
29
30 /**
31  * @author andyh
32  */

33 public class DateTokenFilter extends Tokenizer
34 {
35     Tokenizer baseTokeniser;
36     
37     public DateTokenFilter(Reader JavaDoc in)
38     {
39         super(in);
40         baseTokeniser = new WhitespaceTokenizer(in);
41     }
42
43     public Token next() throws IOException JavaDoc
44     {
45         SimpleDateFormat JavaDoc df = CachingDateFormat.getDateFormat();
46         SimpleDateFormat JavaDoc dof = CachingDateFormat.getDateOnlyFormat();
47         Token candidate;
48         while((candidate = baseTokeniser.next()) != null)
49         {
50             Date JavaDoc date;
51             try
52             {
53                 date = df.parse(candidate.termText());
54             }
55             catch (ParseException JavaDoc e)
56             {
57                continue;
58             }
59             String JavaDoc valueString = dof.format(date);
60             Token integerToken = new Token(valueString, candidate.startOffset(), candidate.startOffset(),
61                     candidate.type());
62             return integerToken;
63         }
64         return null;
65     }
66 }
Popular Tags