Index: lutinutil/src/java/org/codelutin/util/RecursiveProperties.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/RecursiveProperties.java:1.1 --- /dev/null Fri Jul 29 13:08:55 2005 +++ lutinutil/src/java/org/codelutin/util/RecursiveProperties.java Fri Jul 29 13:08:50 2005 @@ -0,0 +1,95 @@ +/* *##% + * Copyright (C) 2002, 2003 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. + *##%*/ + +/* * + * RecursiveProperties.java + * + * Created: 28 juil. 2005 + * + * @author Arnaud Thimel + * Copyright Code Lutin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2005/07/29 13:08:50 $ + * par : $Author: thimel $ + */ + +package org.codelutin.util; + +import java.util.Properties; + +/** + * Surcharge java.util.Properties de manière à aller chercher dans les + * properties la valeur de la propriété si celle ci est encadrée par "${...}". + * + * Exemple : + * myFirstName=Arnaud + * myName=Thimel + * org.codelutin.topia.userInfo.fullName=${fullName} + * fullName=${myFirstName} ${myName} + * namePhrase=My name is ${myName}. + * instruction=Placez votre texte comme ceci : ${monTexte} + * + * Dans ce cas, + * getProperty("org.codelutin.topia.userInfo.fullName") renverra "Arnaud Thimel" + * getProperty("namePhrase") renverra "My name is Thimel." + * getProperty("instruction") renverra "Placez votre texte comme ceci : ${monTexte}" + * + * @author thimel + * + */ +public class RecursiveProperties extends Properties { + + public RecursiveProperties() { + super(); + } + + public RecursiveProperties(Properties defaults) { + super(defaults); + } + + @Override + public String getProperty(String key) { + String result = super.getProperty(key); + if (result == null) + return null; + //Ex : result="My name is ${myName}." + int pos = result.indexOf("${", 0); + //Ex : pos=11 + while (pos != -1) { + int posEnd = result.indexOf("}", pos +1); + //Ex : posEnd=19 + if (posEnd != -1) { + String value = getProperty(result.substring(pos + 2 , posEnd)); + // Ex : getProperty("myName"); + if (value != null) { + // Ex : value="Thimel" + result = result.substring(0, pos) + value + result.substring(posEnd +1); + // Ex : result="My name is " + "Thimel" + "." + pos = result.indexOf("${", pos + value.length()); + // Ex : pos=-1 + } else + // Ex : value=null + pos = result.indexOf("${", posEnd + 1); + // Ex : pos=-1 + } + } + return result; + } + +} //RecursiveProperties