add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / enhancements / EnhancementsDocumentBuilder.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.CategoryDocumentBuilder;
10 import org.apache.lucene.facet.index.attributes.CategoryProperty;
11 import org.apache.lucene.facet.index.streaming.CategoryAttributesStream;
12 import org.apache.lucene.facet.index.streaming.CategoryListTokenizer;
13 import org.apache.lucene.facet.index.streaming.CategoryParentsStream;
14 import org.apache.lucene.facet.index.streaming.CategoryTokenizer;
15 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
16
17 /**
18  * Licensed to the Apache Software Foundation (ASF) under one or more
19  * contributor license agreements.  See the NOTICE file distributed with
20  * this work for additional information regarding copyright ownership.
21  * The ASF licenses this file to You under the Apache License, Version 2.0
22  * (the "License"); you may not use this file except in compliance with
23  * the License.  You may obtain a copy of the License at
24  *
25  *     http://www.apache.org/licenses/LICENSE-2.0
26  *
27  * Unless required by applicable law or agreed to in writing, software
28  * distributed under the License is distributed on an "AS IS" BASIS,
29  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30  * See the License for the specific language governing permissions and
31  * limitations under the License.
32  */
33
34 /**
35  * An {@link EnhancementsDocumentBuilder} is a {@link CategoryDocumentBuilder}
36  * which adds categories to documents according to the list of
37  * {@link CategoryEnhancement}s from {@link EnhancementsIndexingParams}. The
38  * additions over {@link CategoryDocumentBuilder} could be in both category
39  * tokens, and additional category lists.
40  * 
41  * @lucene.experimental
42  */
43 public class EnhancementsDocumentBuilder extends CategoryDocumentBuilder {
44
45   /**
46    * @param taxonomyWriter
47    * @param params
48    *            Indexing params which include {@link CategoryEnhancement}s.
49    * @throws IOException
50    */
51   public EnhancementsDocumentBuilder(TaxonomyWriter taxonomyWriter,
52       EnhancementsIndexingParams params) throws IOException {
53     super(taxonomyWriter, params);
54   }
55
56   @Override
57   protected TokenStream getParentsStream(CategoryAttributesStream categoryAttributesStream) {
58     List<Class<? extends CategoryProperty>> toRetainList = ((EnhancementsIndexingParams) indexingParams)
59         .getRetainableProperties();
60     if (toRetainList != null) {
61       CategoryParentsStream categoryParentsStream = new CategoryParentsStream(
62           categoryAttributesStream, taxonomyWriter, indexingParams);
63       for (Class<? extends CategoryProperty> toRetain : toRetainList) {
64         categoryParentsStream.addRetainableProperty(toRetain);
65       }
66       return categoryParentsStream;
67     }
68     return super.getParentsStream(categoryAttributesStream);
69   }
70
71   @Override
72   protected CategoryListTokenizer getCategoryListTokenizer(TokenStream categoryStream) {
73     CategoryListTokenizer tokenizer = super.getCategoryListTokenizer(categoryStream);
74     // Add tokenizer for each enhancement that produces category list
75     for (CategoryEnhancement enhancement : ((EnhancementsIndexingParams) indexingParams)
76         .getCategoryEnhancements()) {
77       if (enhancement.generatesCategoryList()) {
78         tokenizer = enhancement.getCategoryListTokenizer(tokenizer,
79             (EnhancementsIndexingParams) indexingParams,
80             taxonomyWriter);
81       }
82     }
83     return tokenizer;
84   }
85
86   @Override
87   protected CategoryTokenizer getCategoryTokenizer(TokenStream categoryStream)
88       throws IOException {
89     return new EnhancementsCategoryTokenizer(categoryStream,
90         (EnhancementsIndexingParams) indexingParams);
91   }
92
93 }