/**
* Copyright (C) 2008-2010, Squale Project - http://www.squale.org
*
* This file is part of Squale.
*
* Squale is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* Squale 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 Lesser General Public License
* along with Squale. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Cr�� le 3 f�vr. 06
*
* Pour changer le mod�le de ce fichier g�n�r�, allez � :
* Fen�tre>Pr�f�rences>Java>G�n�ration de code>Code et commentaires
*/
package org.squale.welcom.addons.spell.engine;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.squale.welcom.addons.spell.exception.WSpellCheckerNotFound;
import com.swabunga.spell.engine.SpellDictionaryHashMap;
import com.swabunga.spell.event.SpellChecker;
/**
* @author M327837 Pour changer le mod�le de ce commentaire de type g�n�r�, allez � :
* Fen�tre>Pr�f�rences>Java>G�n�ration de code>Code et commentaires
*/
public class WSpellChecker
{
/** logger */
private static Log logStartup = LogFactory.getLog( "Welcom" );
/** Instance du WSpellChecker */
private static WSpellChecker instance = null;
/** Map des dictionnaire en fonction de la langue */
private final HashMap spellCheckerLocale = new HashMap();
/**
* Contructeur cahc�, cf pattern singleton.
*/
private WSpellChecker()
{
}
/**
* Recherche si le spellChecker est charg� dans la map
*
* @param locale : locale
* @return : Le spell checker
* @throws WSpellCheckerNotFound Le dictionnaire n'est pas disponible
*/
private SpellChecker findSpellChecker( final String locale )
throws WSpellCheckerNotFound
{
synchronized ( spellCheckerLocale )
{
if ( !spellCheckerLocale.containsKey( locale ) )
{
spellCheckerLocale.put( locale, loadSpellChecker( locale ) );
}
return (SpellChecker) spellCheckerLocale.get( locale );
}
}
/**
* Chargement du spell checker pour une locale
*
* @param locale : locale
* @return : SpelleChecker
* @throws WSpellCheckerNotFound Le dictionnaire n'est pas disponible
*/
private SpellChecker loadSpellChecker( final String locale )
throws WSpellCheckerNotFound
{
final long startTime = System.currentTimeMillis();
logStartup.info( "Chargement du dictionnaire '" + locale + "' en cours " );
final SpellChecker spellCheck = new SpellChecker();
final ClassLoader myLoader = this.getClass().getClassLoader();
try
{
final String dictFile = "dico/" + locale + "/word.0";
final String phonetFile = "dico/" + locale + "/phonet.0";
logStartup.debug( "Dico File : " + dictFile + ", " + myLoader.getResourceAsStream( dictFile ) );
logStartup.debug( "Phonet File : " + phonetFile + ", " + myLoader.getResourceAsStream( phonetFile ) );
spellCheck.addDictionary( new SpellDictionaryHashMap(
new java.io.InputStreamReader(
myLoader.getResourceAsStream( dictFile ) ),
new java.io.InputStreamReader(
myLoader.getResourceAsStream( phonetFile ) ) ) );
}
catch ( final FileNotFoundException e )
{
throw new WSpellCheckerNotFound( e );
}
catch ( final IOException e )
{
throw new WSpellCheckerNotFound( e );
}
logStartup.info( "Chargement du dictionnaire '" + locale + "' termin� ("
+ ( System.currentTimeMillis() - startTime ) + "ms)" );
return spellCheck;
}
/**
* Retourne un dictionnaire
*
* @param locale en fonction de la locale
* @return Le dicionnaire
* @throws WSpellCheckerNotFound Le dictionnaire n'est pas disponible
*/
public static SpellChecker getSpellChecker( final String locale )
throws WSpellCheckerNotFound
{
if ( instance == null )
{
instance = new WSpellChecker();
}
return instance.findSpellChecker( locale );
}
}