Author: bpoussin Date: 2012-05-05 03:32:04 +0200 (Sat, 05 May 2012) New Revision: 2348 Url: http://nuiton.org/repositories/revision/nuiton-utils/2348 Log: Evolution #1993 [ApplicationConfig] new method to get sub-config from prefix value (manque le test unitaire, mais pas le courage maintenant :() Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2012-05-02 14:02:15 UTC (rev 2347) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2012-05-05 01:32:04 UTC (rev 2348) @@ -1271,6 +1271,17 @@ } /** + * Return sub config that encapsulate this ApplicationConfig + * @param prefix prefix to put automaticaly at beginning of all key + * @return + * @since 2.4.9 + */ + public SubApplicationConfig getSubConfig(String prefix) { + SubApplicationConfig result = new SubApplicationConfig(this, prefix); + return result; + } + + /** * Permet de recuperer l'ensemble des options commencant par une certaine * chaine. * @@ -2164,6 +2175,160 @@ }; /** + * Permet de masquer un prefix. Il est possible d'avoir des valeurs par + * defaut. Par exemple: + * <pre> + * monOption=toto + * monPrefix.monOption=titi + * </pre> + * + * <li>Si on cree le subApp avec le prefix "monPrefix." et qu'on demande la valeur + * de "monOption", la valeur retournee est "titi". + * <li>Si on cree le subApp avec le prefix "monAutrePrefix." et qu'on demande la valeur + * de "monOption", la valeur retournee est "toto" (valeur par defaut de monOption. + * + * Certaines methodes retournees ne sont pas + * surchargee et ne masque pas le prefix: + * <li>getOptions() + * @since 2.4.9 + */ + static public class SubApplicationConfig extends ApplicationConfig { + + protected ApplicationConfig parent; + protected String prefix; + + public SubApplicationConfig(ApplicationConfig parent, String prefix) { + this.parent = parent; + this.prefix = prefix; + } + + public ApplicationConfig getParent() { + return parent; + } + + public String getPrefix() { + return prefix; + } + + @Override + public Properties getOptions() { + return getParent().getOptions(); + } + + @Override + public void setDefaultOption(String key, String value) { + getParent().setDefaultOption(getPrefix() + key, value); + } + + @Override + public boolean hasOption(String key) { + return getParent().hasOption(getPrefix() + key); + } + + @Override + public void setOption(String key, String value) { + getParent().setOption(getPrefix() + key, value); + } + + /** + * Surcharge pour recherche la cle avec le prefix. Si on ne la retrouve + * pas, on recherche sans le prefix pour permettre d'avoir des valeurs + * par defaut + * @param key La cle de l'option + * @return + */ + @Override + public String getOption(String key) { + String result; + if (hasOption(key)) { + result = getParent().getOption(getPrefix() + key); + } else { + result = getParent().getOption(key); + } + return result; + } + + /** + * Surcharge de la methode pour que les options commencant par le prefix + * soit modifiee pour qu'elle est la meme cle sans le prefix. Le but + * est de garder les autres options et si une option avait le meme nom + * qu'elle soit effacee par celle dont on a supprime le prefix + * @param replaceInner + * @return + */ + @Override + public Properties getFlatOptions(boolean replaceInner) { + Properties result = getParent().getFlatOptions(replaceInner); + Properties tmp = new Properties(); + int lenght = getPrefix().length(); + for (Map.Entry e : result.entrySet()) { + String k = (String)e.getKey(); + if (k.startsWith(getPrefix())) { + k = k.substring(lenght); + String v = (String)e.getValue(); + tmp.setProperty(k, v); + } + } + result.putAll(tmp); + return result; + } + + /** + * Surcharge pour recupere les valeurs commencant par le prefix demande + * en plus du prefix 'sub'. Les options sont ensuite fusionnee pour + * permettre aussi les valeurs par defaut + * @param prefix + * @return + */ + @Override + public Properties getOptionStartsWith(String prefix) { + Properties result = getParent().getOptionStartsWith(prefix); + Properties tmp = getParent().getOptionStartsWith(getPrefix() + prefix); + int lenght = getPrefix().length(); + for (Map.Entry e : tmp.entrySet()) { + String k = (String)e.getKey(); + k = k.substring(lenght); + String v = (String)e.getValue(); + // on ajout/ecrase les valeurs de result + result.setProperty(k, v); + } + return result; + } + + @Override + protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + if (propertyName.startsWith(getPrefix())) { + propertyName = propertyName.substring(getPrefix().length()); + getParent().firePropertyChange(propertyName, oldValue, newValue); + } // else not fire event + } + + @Override + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + getParent().addPropertyChangeListener(getPrefix() + propertyName, listener); + } + + @Override + public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + getParent().removePropertyChangeListener(getPrefix() + propertyName, listener); + } + + @Override + public boolean hasListeners(String propertyName) { + return getParent().hasListeners(getPrefix() + propertyName); + } + + // methode interdite dans le sub + + @Override + public ApplicationConfig parse(String... args) throws ArgumentsParserException { + throw new UnsupportedOperationException("This method is not supported in SubApplicationConfig"); + } + + + } + + /** * Le contrat de marquage des options, on utilise cette interface pour * caracteriser une option de configuration. * <p/>