View Javadoc

1   /*
2    * MMBase Lucene module
3    *
4    * The contents of this file are subject to the Mozilla Public License
5    * Version 1.0 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11   * License for the specific language governing rights and limitations
12   * under the License.
13   */
14  package net.sf.mmapps.modules.lucenesearch;
15  
16  import java.io.IOException;
17  
18  import org.apache.commons.digester.Digester;
19  import org.mmbase.bridge.Cloud;
20  import org.xml.sax.SAXException;
21  
22  /***
23   * Parse the config file and create the objects for the module
24   * 
25   * @author Wouter Heijke
26   * @version $Revision: 1.1 $
27   */
28  public class LuceneManager {
29      static final String LUCENEMOD_PAT = "luceneindexdefinition";
30  
31      static final String INDEX_PAT = "*/index";
32  
33      static final String TABLE_PAT = "*/table";
34  
35      static final String FIELD_PAT = "*/field";
36  
37      static final String RELATED_PAT = "*/related";
38  
39      private Digester dig = null;
40  
41      private SearchConfig config = null;
42  
43      public LuceneManager() {
44          dig = getConfigDigester();
45      }
46  
47      /***
48       * Creates the the Digester rules specific for the Lucene modeule config
49       * file
50       * 
51       * @return Digester object
52       */
53      private Digester getConfigDigester() {
54          // instantiate Digester and disable XML validation
55          Digester digester = new Digester();
56          digester.setValidating(false);
57  
58          digester.addObjectCreate(LUCENEMOD_PAT, SearchConfig.class);
59          digester.addSetProperties(LUCENEMOD_PAT, "defaultindex", "defaultIndexName");
60  
61          digester.addObjectCreate(INDEX_PAT, SearchIndex.class);
62          digester.addSetProperties(INDEX_PAT);
63          digester.addSetNext(INDEX_PAT, "addIndex");
64  
65          digester.addObjectCreate(FIELD_PAT, DataField.class);
66          digester.addSetProperties(FIELD_PAT, "name", "name");
67          digester.addSetProperties(FIELD_PAT, "type", "type");
68          digester.addCallMethod(FIELD_PAT, "setRename", 0);
69          digester.addSetNext(FIELD_PAT, "addField");
70  
71          digester.addObjectCreate(RELATED_PAT, DataRelation.class);
72          digester.addSetProperties(RELATED_PAT);
73          digester.addSetNext(RELATED_PAT, "addRelated");
74  
75          digester.addObjectCreate(TABLE_PAT, SourceTable.class);
76          digester.addSetProperties(TABLE_PAT);
77          digester.addSetNext(TABLE_PAT, "addTable");
78  
79          return digester;
80      }
81  
82      /***
83       * Reads and parses the configuration file for the Lucene module
84       * 
85       * @param configFile
86       *            The configuration xml file
87       */
88      public void readConfig(String configFile) {
89          try {
90              config = (SearchConfig) dig.parse(configFile);
91          } catch (IOException e) {
92              e.printStackTrace();
93          } catch (SAXException e) {
94              e.printStackTrace();
95          }
96      }
97  
98      public SearchConfig getConfig() {
99          return config;
100     }
101 
102     /***
103      * Starts the indexing process chain reaction
104      * @param cloud MMBase cloud to use for indexing
105      */
106     public void collectAll(Cloud cloud) {
107         config.collectAll(cloud);
108     }
109 }