View Javadoc
1   package de.fhdw.wtf.persistence.utils;
2   
3   import java.util.Enumeration;
4   import java.util.HashMap;
5   import java.util.MissingResourceException;
6   import java.util.ResourceBundle;
7   
8   /**
9    * This PropertiesReader tries to read properties from a ressource file in a given directory. The content of the
10   * ressource file should be seperated by "key1=value1".
11   * 
12   * @author HFW413hy
13   *
14   */
15  public class PropertiesReaderRessource implements PropertiesReader {
16  	
17  	/**
18  	 * Stores the internal key-value-pairs.
19  	 */
20  	private final HashMap<String, String> propValues = new HashMap<>();
21  	
22  	/**
23  	 * Deletes the internal instance. You should initialize it again with the method "getInstance(String propFile)".
24  	 */
25  	public void reset() {
26  		this.propValues.clear();
27  	}
28  	
29  	@Override
30  	public void initialize(final String propFile) throws MissingResourceException {
31  		this.reset();
32  		final ResourceBundle rb = ResourceBundle.getBundle(propFile);
33  		final Enumeration<String> keys = rb.getKeys();
34  		while (keys.hasMoreElements()) {
35  			final String key = keys.nextElement();
36  			final String value = rb.getString(key);
37  			this.propValues.put(key, value);
38  		}
39  	}
40  	
41  	@Override
42  	public String getProperty(final String key) {
43  		if (!this.propValues.containsKey(key)) {
44  			return "";
45  		}
46  		return this.propValues.get(key);
47  	}
48  }