View Javadoc
1   /*
2    * #%L
3    * Nuiton Utils
4    * %%
5    * Copyright (C) 2004 - 2011 CodeLutin, Chatellier Eric
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.util;
24  
25  import java.util.Properties;
26  
27  /**
28   * Overrides {@link Properties} in order to check if the expected value
29   * contains another property key like "${...}". It that case, the key
30   * will be replaced by its value if possible.
31   *
32   * Example :
33   * <pre>
34   * myFirstName=Arnaud
35   * myName=Thimel
36   * org.nuiton.topia.userInfo.fullName=${fullName}
37   * fullName=${myFirstName} ${myName}
38   * namePhrase=My name is ${myName}.
39   * instruction=Put your text like this : ${myText}
40   * </pre>
41   *
42   * Dans ce cas,
43   * <ul>
44   * <li>getProperty("org.nuiton.topia.userInfo.fullName") renverra "Arnaud Thimel"
45   * <li>getProperty("namePhrase") renverra "My name is Thimel."
46   * <li>getProperty("instruction") renverra "Put your text like this : ${myText}"
47   * </ul>
48   *
49   * @author Arnaud Thimel - thimel@codelutin.com
50   */
51  public class RecursiveProperties extends Properties {
52  
53      private static final long serialVersionUID = -5012939272780929116L;
54  
55      public RecursiveProperties() {
56      }
57  
58      public RecursiveProperties(Properties defaults) {
59          super(defaults);
60      }
61  
62      @Override
63      public String getProperty(String key) {
64          String result = super.getProperty(key);
65          if (result == null) {
66              return null;
67          }
68          //Ex : result="My name is ${myName}."
69          int pos = result.indexOf("${", 0);
70          //Ex : pos=11
71          while (pos != -1) {
72              int posEnd = result.indexOf("}", pos + 1);
73              //Ex : posEnd=19
74              if (posEnd != -1) {
75                  String value = getProperty(result.substring(pos + 2, posEnd));
76                  // Ex : getProperty("myName");
77                  if (value != null) {
78                      // Ex : value="Thimel"
79                      result = result.substring(0, pos) + value + result.substring(posEnd + 1);
80                      // Ex : result="My name is " + "Thimel" + "."
81                      pos = result.indexOf("${", pos + value.length());
82                      // Ex : pos=-1
83                  } else {
84                      // Ex : value=null
85                      pos = result.indexOf("${", posEnd + 1);
86                      // Ex : pos=-1
87                  }
88                  // Ex : pos=-1
89              }
90          }
91          return result;
92      }
93  
94  } //RecursiveProperties