add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / smartcn / src / java / org / apache / lucene / analysis / cn / smart / AnalyzerProfile.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.analysis.cn.smart;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.util.Properties;
24
25 /**
26  * Manages analysis data configuration for SmartChineseAnalyzer
27  * <p>
28  * SmartChineseAnalyzer has a built-in dictionary and stopword list out-of-box.
29  * </p>
30  * @lucene.experimental
31  */
32 public class AnalyzerProfile {
33
34   /**
35    * Global indicating the configured analysis data directory
36    */
37   public static String ANALYSIS_DATA_DIR = "";
38
39   static {
40     init();
41   }
42
43   private static void init() {
44     String dirName = "analysis-data";
45     String propName = "analysis.properties";
46
47     // Try the system property:-Danalysis.data.dir=/path/to/analysis-data
48     ANALYSIS_DATA_DIR = System.getProperty("analysis.data.dir", "");
49     if (ANALYSIS_DATA_DIR.length() != 0)
50       return;
51
52     File[] cadidateFiles = new File[] { new File("./" + dirName),
53         new File("./lib/" + dirName), new File("./" + propName),
54         new File("./lib/" + propName) };
55     for (int i = 0; i < cadidateFiles.length; i++) {
56       File file = cadidateFiles[i];
57       if (file.exists()) {
58         if (file.isDirectory()) {
59           ANALYSIS_DATA_DIR = file.getAbsolutePath();
60         } else if (file.isFile() && getAnalysisDataDir(file).length() != 0) {
61           ANALYSIS_DATA_DIR = getAnalysisDataDir(file);
62         }
63         break;
64       }
65     }
66
67     if (ANALYSIS_DATA_DIR.length() == 0) {
68       // Dictionary directory cannot be found.
69       System.err
70           .println("WARNING: Can not find lexical dictionary directory!");
71       System.err
72           .println("WARNING: This will cause unpredictable exceptions in your application!");
73       System.err
74           .println("WARNING: Please refer to the manual to download the dictionaries.");
75     }
76
77   }
78
79   private static String getAnalysisDataDir(File propFile) {
80     Properties prop = new Properties();
81     try {
82       FileInputStream input = new FileInputStream(propFile);
83       prop.load(input);
84       String dir = prop.getProperty("analysis.data.dir", "");
85       input.close();
86       return dir;
87     } catch (IOException e) {
88     }
89     return "";
90   }
91
92 }