add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / payloads / TokenOffsetPayloadTokenFilter.java
1 package org.apache.lucene.analysis.payloads;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 import java.io.IOException;
21
22 import org.apache.lucene.analysis.TokenFilter;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
25 import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
26 import org.apache.lucene.index.Payload;
27
28
29 /**
30  * Adds the {@link org.apache.lucene.analysis.Token#setStartOffset(int)}
31  * and {@link org.apache.lucene.analysis.Token#setEndOffset(int)}
32  * First 4 bytes are the start
33  *
34  **/
35 public class TokenOffsetPayloadTokenFilter extends TokenFilter {
36   private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
37   private final PayloadAttribute payAtt = addAttribute(PayloadAttribute.class);
38
39   public TokenOffsetPayloadTokenFilter(TokenStream input) {
40     super(input);
41   }
42
43   @Override
44   public final boolean incrementToken() throws IOException {
45     if (input.incrementToken()) {
46       byte[] data = new byte[8];
47       PayloadHelper.encodeInt(offsetAtt.startOffset(), data, 0);
48       PayloadHelper.encodeInt(offsetAtt.endOffset(), data, 4);
49       Payload payload = new Payload(data);
50       payAtt.setPayload(payload);
51       return true;
52     } else {
53     return false;
54     }
55   }
56 }