/**   
 * License Agreement for Jaeksoft OpenSearchServer
 *
 * Copyright (C) 2009 Emmanuel Keller / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of Jaeksoft OpenSearchServer.
 *
 * Jaeksoft OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * Jaeksoft OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Jaeksoft OpenSearchServer. 
 *  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.jaeksoft.searchlib.analysis;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class SynonymMap {

	private TreeMap<String, List<String>> sourceMap;
	private volatile TreeMap<String, String[]> perfMap;

	public SynonymMap(File file) throws FileNotFoundException, IOException {
		sourceMap = new TreeMap<String, List<String>>();
		perfMap = null;
		loadFromFile(file);
		updatePerfMap();
	}

	private void loadFromFile(File file) throws FileNotFoundException,
			IOException {
		Properties props = new Properties();
		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
		props.load(isr);
		Enumeration<Object> e = props.keys();
		while (e.hasMoreElements()) {
			String key = e.nextElement().toString();
			String value = props.getProperty(key);
			String[] terms = value.split(",");
			for (String term : terms)
				addSourceMap(key, term.trim());
		}
		updatePerfMap();
		fis.close();
	}

	private void updatePerfMap() {
		synchronized (sourceMap) {
			TreeMap<String, String[]> spm = new TreeMap<String, String[]>();
			Set<String> termSet = new TreeSet<String>();
			for (String key : sourceMap.keySet()) {
				List<String> termList = sourceMap.get(key);
				if (termList == null || termList.size() == 0)
					continue;
				termSet.clear();
				for (String term : termList) {
					String[] words = term.split("\\s");
					for (String word : words)
						termSet.add(word);
				}
				String[] termArray = new String[termSet.size()];
				termSet.toArray(termArray);
				spm.put(key, termArray);
			}
			perfMap = spm;
		}

	}

	private void addSourceMap(String key, String value) {
		if (value == null || value.length() == 0)
			return;
		synchronized (sourceMap) {
			List<String> termList = sourceMap.get(key);
			if (termList == null) {
				termList = new ArrayList<String>(1);
				sourceMap.put(key, termList);
			}
			termList.add(value);
		}
	}

	public String[] getSynonyms(String term) {
		return perfMap.get(term);
	}

	public int getSize() {
		return perfMap.size();
	}

}

@@ -942,16 +942,47 @@
 lysis;%0A%0A
+import java.io.BufferedReader;%0A
 import j
@@ -1175,71 +1175,12 @@
 til.
-Enumeration;%0Aimport java.util.List;%0Aimport java.util.Properties
+List
 ;%0Aim
@@ -1660,47 +1660,8 @@
 n %7B%0A
-%09%09Properties props = new Properties();%0A
 %09%09Fi
@@ -1776,93 +1776,107 @@
 ;%0A%09%09
-props.load(isr);%0A%09%09Enumeration%3CObject%3E e = props.keys();%0A%09%09while (e.hasMoreElements()
+BufferedReader br = new BufferedReader(isr);%0A%09%09String line;%0A%09%09while ((line = br.readLine()) != null
 ) %7B%0A
@@ -1888,83 +1888,192 @@
 ring
+%5B%5D
  key
- = e.nextElement().toString();%0A%09%09%09String value = props.getProperty(key)
+Value = line.split(%22%5B:=%5D%22, 2);%0A%09%09%09if (keyValue == null)%0A%09%09%09%09continue;%0A%09%09%09if (keyValue.length != 2)%0A%09%09%09%09continue;%0A%09%09%09String key = keyValue%5B0%5D.trim();%0A%09%09%09String value = keyValue%5B1%5D
 ;%0A%09%09
@@ -2196,16 +2196,45 @@
 fMap();%0A
+%09%09br.close();%0A%09%09isr.close();%0A
 %09%09fis.cl

Fix a bug that prevent using oss on jdk 1.5 with synonyms.