All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ultreia.java4all.util.RecursiveProperties Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package io.ultreia.java4all.util;

/*-
 * #%L
 * Java Util extends by Ultreia.io
 * %%
 * Copyright (C) 2018 Ultreia.io
 * %%
 * This program 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 (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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Properties;

/**
 * Overrides {@link Properties} in order to check if the expected value
 * contains another property key like {@code ${...}}. It that case, the key
 * will be replaced by its value if possible.
 * 

* Example : *

 * myFirstName=John
 * myName=Doe
 * org.nuiton.topia.userInfo.fullName=${fullName}
 * fullName=${myFirstName} ${myName}
 * namePhrase=My name is ${myName}.
 * instruction=Put your text like this : ${myText}
 * 
*

* Then: *

    *
  • {@code getProperty("org.nuiton.topia.userInfo.fullName")} → {@code "John Doe"} *
  • {@code getProperty("namePhrase")} → {@code "My name is Doe."} *
  • {@code getProperty("instruction")} → {@code "Put your text like this : ${myText}"} *
*

* Created by tchemit on 29/12/2017. * * @author Tony Chemit - [email protected] * @author Arnaud Thimel - [email protected] */ public class RecursiveProperties extends Properties { private static final long serialVersionUID = 1L; private static final String VARIABLE_START = "${"; public RecursiveProperties() { } 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(VARIABLE_START, 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="Doe" result = result.substring(0, pos) + value + result.substring(posEnd + 1); // Ex : result="My name is " + "Doe" + "." pos = result.indexOf(VARIABLE_START, pos + value.length()); // Ex : pos=-1 } else { // Ex : value=null pos = result.indexOf(VARIABLE_START, posEnd + 1); // Ex : pos=-1 } // Ex : pos=-1 } } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy