add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / analysis / KeywordMarkerFilter.java
1 package org.apache.lucene.analysis;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.Set;
22
23 import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25 import org.apache.lucene.util.Version;
26
27 /**
28  * Marks terms as keywords via the {@link KeywordAttribute}. Each token
29  * contained in the provided is marked as a keyword by setting
30  * {@link KeywordAttribute#setKeyword(boolean)} to <code>true</code>.
31  * 
32  * @see KeywordAttribute
33  */
34 public final class KeywordMarkerFilter extends TokenFilter {
35
36   private final KeywordAttribute keywordAttr = addAttribute(KeywordAttribute.class);
37   private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
38   private final CharArraySet keywordSet;
39
40   /**
41    * Create a new KeywordMarkerFilter, that marks the current token as a
42    * keyword if the tokens term buffer is contained in the given set via the
43    * {@link KeywordAttribute}.
44    * 
45    * @param in
46    *          TokenStream to filter
47    * @param keywordSet
48    *          the keywords set to lookup the current termbuffer
49    */
50   public KeywordMarkerFilter(final TokenStream in,
51       final CharArraySet keywordSet) {
52     super(in);
53     this.keywordSet = keywordSet;
54   }
55
56   /**
57    * Create a new KeywordMarkerFilter, that marks the current token as a
58    * keyword if the tokens term buffer is contained in the given set via the
59    * {@link KeywordAttribute}.
60    * 
61    * @param in
62    *          TokenStream to filter
63    * @param keywordSet
64    *          the keywords set to lookup the current termbuffer
65    */
66   public KeywordMarkerFilter(final TokenStream in, final Set<?> keywordSet) {
67     this(in, keywordSet instanceof CharArraySet ? (CharArraySet) keywordSet
68         : CharArraySet.copy(Version.LUCENE_31, keywordSet));
69   }
70
71   @Override
72   public final boolean incrementToken() throws IOException {
73     if (input.incrementToken()) {
74       if (keywordSet.contains(termAtt.buffer(), 0, termAtt.length())) { 
75         keywordAttr.setKeyword(true);
76       }
77       return true;
78     } else {
79       return false;
80     }
81   }
82 }