add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / enhancements / EnhancementsCategoryTokenizer.java
1 package org.apache.lucene.facet.enhancements;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import org.apache.lucene.analysis.TokenStream;
7
8 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
9 import org.apache.lucene.facet.index.streaming.CategoryTokenizer;
10 import org.apache.lucene.util.Vint8;
11
12 /**
13  * Licensed to the Apache Software Foundation (ASF) under one or more
14  * contributor license agreements.  See the NOTICE file distributed with
15  * this work for additional information regarding copyright ownership.
16  * The ASF licenses this file to You under the Apache License, Version 2.0
17  * (the "License"); you may not use this file except in compliance with
18  * the License.  You may obtain a copy of the License at
19  *
20  *     http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS,
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28
29 /**
30  * A tokenizer which adds to each category token payload according to the
31  * {@link CategoryEnhancement}s defined in the given
32  * {@link EnhancementsIndexingParams}.
33  * 
34  * @lucene.experimental
35  */
36 public class EnhancementsCategoryTokenizer extends CategoryTokenizer {
37
38   /**
39    * The data buffer used for payload instance.
40    */
41   protected byte[] payloadBytes;
42
43   /**
44    * The category enhancements to handle
45    */
46   protected List<CategoryEnhancement> enhancements;
47
48   /**
49    * Buffers for enhancement payload bytes
50    */
51   protected byte[][] enhancementBytes;
52
53   private int nStart;
54
55   /**
56    * Constructor.
57    * 
58    * @param input
59    *            The stream of category tokens.
60    * @param indexingParams
61    *            The indexing params to use.
62    * @throws IOException
63    */
64   public EnhancementsCategoryTokenizer(TokenStream input,
65       EnhancementsIndexingParams indexingParams) throws IOException {
66     super(input, indexingParams);
67     payloadBytes = new byte[Vint8.MAXIMUM_BYTES_NEEDED
68         * (indexingParams.getCategoryEnhancements().size() + 1)];
69     enhancements = indexingParams.getCategoryEnhancements();
70     if (enhancements != null) {
71       // create array of bytes per enhancement
72       enhancementBytes = new byte[enhancements.size()][];
73       // write once the number of enhancements in the payload bytes
74       nStart = Vint8.encode(enhancements.size(), payloadBytes, 0);
75     }
76   }
77
78   @Override
79   protected void setPayload() {
80     this.payloadAttribute.setPayload(null);
81     if (enhancements == null) {
82       return;
83     }
84     // clear previous payload content
85     int nBytes = nStart;
86     int i = 0;
87     int nEnhancementBytes = 0;
88     for (CategoryEnhancement enhancement : enhancements) {
89       // get payload bytes from each enhancement
90       enhancementBytes[i] = enhancement
91           .getCategoryTokenBytes(categoryAttribute);
92       // write the number of bytes in the payload
93       if (enhancementBytes[i] == null) {
94         nBytes += Vint8.encode(0, payloadBytes, nBytes);
95       } else {
96         nBytes += Vint8.encode(enhancementBytes[i].length,
97             payloadBytes, nBytes);
98         nEnhancementBytes += enhancementBytes[i].length;
99       }
100       i++;
101     }
102     if (nEnhancementBytes > 0) {
103       // make sure we have space for all bytes
104       if (payloadBytes.length < nBytes + nEnhancementBytes) {
105         byte[] temp = new byte[(nBytes + nEnhancementBytes) * 2];
106         System.arraycopy(payloadBytes, 0, temp, 0, nBytes);
107         payloadBytes = temp;
108       }
109       for (i = 0; i < enhancementBytes.length; i++) {
110         // add the enhancement payload bytes after the existing bytes
111         if (enhancementBytes[i] != null) {
112           System.arraycopy(enhancementBytes[i], 0, payloadBytes,
113               nBytes, enhancementBytes[i].length);
114           nBytes += enhancementBytes[i].length;
115         }
116       }
117       payload.setData(payloadBytes, 0, nBytes);
118       payloadAttribute.setPayload(payload);
119     }
120   }
121 }