1 package org.apache.lucene.analysis.payloads;
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
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;
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
35 public class TokenOffsetPayloadTokenFilter extends TokenFilter {
36 private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
37 private final PayloadAttribute payAtt = addAttribute(PayloadAttribute.class);
39 public TokenOffsetPayloadTokenFilter(TokenStream input) {
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);