Index: lutini18neditor/src/java/org/codelutin/i18n/editor/ManagerI18n.java diff -u /dev/null lutini18neditor/src/java/org/codelutin/i18n/editor/ManagerI18n.java:1.1 --- /dev/null Fri Jan 18 18:15:07 2008 +++ lutini18neditor/src/java/org/codelutin/i18n/editor/ManagerI18n.java Fri Jan 18 18:15:02 2008 @@ -0,0 +1,253 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin. +* +* This program 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 2 +* of the License, or (at your option) any later version. +* +* This program 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 this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ + +package org.codelutin.i18n.editor; + +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.codelutin.i18n.I18nFileReader; +import org.codelutin.util.Resource; + +/** + * Permet la gestion des bundles, c'est à dire la création de nouveau bundle et + * la modification de bundle. + * + * Permet la récupération d'information sur les bundles, c'est à dire les paquetages + * et les locales. + * + * @author julien + */ +public class ManagerI18n { + + /** + * Non de la racine de l'arbre + */ + public static final String ROOT_NAME = "i18n"; + + /** + * Séparateur des propriétés + */ + public static final String SEPARATOR = "."; + + /** + * Nom du projet sur lequelle on travail + */ + protected static String PROJECT_NAME = "isisfish"; // TODO: Récupération automatique + + /** + * Répertoire d'enregistrement des modification ou des nouveaux bundles + */ + protected static String REP_NAME = "./"; // TODO: Récupération automatique + + /** + * Pattern pour récupérer le paquetage et le bundle sur un nom de fichier + */ + protected static Pattern PATTERN_FILE_NAME = Pattern.compile(".*/([^/]*)-(.*)\\.properties"); + + /** + * Variable pour le stockage du singleton + */ + protected static ManagerI18n manager; + + /** + * Ensemble des bundles + */ + protected Map bundles; + + /** + * Ensemble des bundles modifiés ou créés par l'utilisateur + */ + protected Map userBundles; + + /** + * Singleton + */ + public static ManagerI18n getInstance() { + if(manager == null) { + try { + manager = new ManagerI18n(); + } catch (IOException e) { + throw new RuntimeException(e); //TODO: Gestion des exceptions + } + } + return manager; + } + + /** + * Constucteur pour initialiser les bundles disponibles + */ + protected ManagerI18n() throws IOException { + bundles = new HashMap(); + userBundles = new HashMap(); + + // Récupération des fichiers + List files = Resource.getURLs(".*-.?.?_.?.?\\.properties"); + + for (URL file : files) { + String path = file.getPath(); + Matcher matcher = PATTERN_FILE_NAME.matcher(path); + matcher.matches(); + // Ajout seulement des fichiers non agrégés + if(!"language".equals(matcher.group(1))) { + I18nFileReader property = new I18nFileReader(); + property.load2(file.openStream(), "ISO-8859-1"); + bundles.put(path, property); + } + } + } + + /** + * Récupération des bundles + * @return liste des bundles + */ + public List getBundles() { + Set locales = new HashSet(); + + for (String file : bundles.keySet()) { + Matcher matcher = PATTERN_FILE_NAME.matcher(file); + matcher.matches(); + locales.add(matcher.group(2)); + } + + return new ArrayList(locales); + } + + /** + * Récupération des paquetages + * @return liste des paquetages + */ + public List getPackages() { + Set names = new HashSet(); + + for (String file : bundles.keySet()) { + Matcher matcher = PATTERN_FILE_NAME.matcher(file); + matcher.matches(); + names.add(matcher.group(1)); + } + + return new ArrayList(names); + } + + /** + * Récupération des valeurs pour les bundles pour une clé + * @param locales liste des bundles + * @param key clé + * @return liste des valeurs pour les bundles pour une clé + */ + public List getValues(List locales, String key) { + List values = new ArrayList(); + + for (String locale : locales) { + + String value = null; + + for (String file : bundles.keySet()) { + Matcher matcher = PATTERN_FILE_NAME.matcher(file); + matcher.matches(); + if(locale.equals(matcher.group(2)) && value == null) { + value = bundles.get(file).getProperty(key); + } + } + + values.add(value); + } + + return values; + } + + /** + * Récupération des clés pour les paquetages + * @param paquetages liste des paquetages + * @return liste des clés pour les paquetages + */ + public List getKeys(List paquetages) { + Set keys = new HashSet(); + + for (String file : bundles.keySet()) { + Matcher matcher = PATTERN_FILE_NAME.matcher(file); + matcher.matches(); + if(paquetages.contains(matcher.group(1))) { + Set keySet = bundles.get(file).keySet(); + for (Object object : keySet) { + keys.add(object.toString()); + } + } + } + + return new ArrayList(keys); + } + + /** + * Création d'un nouveau bundle utilistateur + * @param bundleName nom du bundle (ex : fr_FR) + */ + public void createBundle(String bundleName) { + getUserBundle(bundleName); + } + + /** + * Enregistrement d'une modification de clé dans le bundle utilisateur associé + * @param bundleName nom du bundle + * @param key clé + * @param value nouvelle valeur + */ + public void store(String bundleName, String key, String value) { + I18nFileReader bundle = getUserBundle(bundleName); + bundle.put(key, value); + } + + /** + * Récupération d'un bundle utilisateur, créé si il n'existe pas + * @param bundleName nom du bundle + * @return bundle + */ + public I18nFileReader getUserBundle(String bundleName) { + String name = REP_NAME + PROJECT_NAME + "-" + bundleName + ".properties"; + I18nFileReader bundle = bundles.get(name); + if(bundle == null) { + bundle = new I18nFileReader(); + userBundles.put(name, bundle); + bundles.put(name, bundle); + } + return bundle; + } + + /** + * Enregistrement des bundles utilisateur sur le disque + */ + public void store() { + for (String name : userBundles.keySet()) { + I18nFileReader bundle = userBundles.get(name); + try { + bundle.store(new FileWriter(name), null); + } catch (IOException e) { + throw new RuntimeException(e); //TODO: Gestion des exceptions + } + } + } +}