add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / enhancements / association / AssociationListTokenizer.java
1 package org.apache.lucene.facet.enhancements.association;
2
3 import java.io.IOException;
4
5 import org.apache.lucene.analysis.TokenStream;
6
7 import org.apache.lucene.facet.enhancements.CategoryEnhancement;
8 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
9 import org.apache.lucene.facet.index.CategoryListPayloadStream;
10 import org.apache.lucene.facet.index.attributes.OrdinalProperty;
11 import org.apache.lucene.facet.index.streaming.CategoryListTokenizer;
12 import org.apache.lucene.util.encoding.SimpleIntEncoder;
13
14 /**
15  * Licensed to the Apache Software Foundation (ASF) under one or more
16  * contributor license agreements.  See the NOTICE file distributed with
17  * this work for additional information regarding copyright ownership.
18  * The ASF licenses this file to You under the Apache License, Version 2.0
19  * (the "License"); you may not use this file except in compliance with
20  * the License.  You may obtain a copy of the License at
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  */
30
31 /**
32  * Tokenizer for associations of a category
33  * 
34  * @lucene.experimental
35  */
36 public class AssociationListTokenizer extends CategoryListTokenizer {
37
38   protected CategoryListPayloadStream payloadStream;
39
40   private String categoryListTermText;
41
42   public AssociationListTokenizer(TokenStream input,
43       EnhancementsIndexingParams indexingParams, CategoryEnhancement enhancement) {
44     super(input, indexingParams);
45     categoryListTermText = enhancement.getCategoryListTermText();
46   }
47
48   @Override
49   protected void handleStartOfInput() throws IOException {
50     payloadStream = null;
51   }
52
53   @Override
54   public final boolean incrementToken() throws IOException {
55     if (input.incrementToken()) {
56       if (categoryAttribute != null) {
57         AssociationProperty associationProperty = AssociationEnhancement
58             .getAssociationProperty(categoryAttribute);
59         if (associationProperty != null
60             && associationProperty.hasBeenSet()) {
61           OrdinalProperty ordinalProperty = (OrdinalProperty) categoryAttribute
62               .getProperty(OrdinalProperty.class);
63           if (ordinalProperty == null) {
64             throw new IOException(
65                 "Error: Association without ordinal");
66           }
67
68           if (payloadStream == null) {
69             payloadStream = new CategoryListPayloadStream(
70                 new SimpleIntEncoder());
71           }
72           payloadStream.appendIntToStream(ordinalProperty
73               .getOrdinal());
74           payloadStream.appendIntToStream(associationProperty
75               .getAssociation());
76         }
77       }
78       return true;
79     }
80     if (payloadStream != null) {
81       termAttribute.setEmpty().append(categoryListTermText);
82       payload.setData(payloadStream.convertStreamToByteArray());
83       payloadAttribute.setPayload(payload);
84       payloadStream = null;
85       return true;
86     }
87     return false;
88   }
89
90 }