com.github.asteraether.tomlib.property.PropertyManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TomLib Show documentation
Show all versions of TomLib Show documentation
A simple collection of usefull things in java.
The newest version!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.github.asteraether.tomlib.property;
import com.github.asteraether.tomlib.property.exceptions.PropertyDefaultNotFoundException;
import com.github.asteraether.tomlib.property.exceptions.PropertyNotManagedException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
/**
*
* @author Thomas
*/
public class PropertyManager {
private final String directory;
private final Map defaults;
/**
*
* @param directory
* @param defaults
* @throws FileNotFoundException
* @throws IOException
*/
public PropertyManager(String directory, Map defaults) throws FileNotFoundException, IOException {
this.directory = directory;
this.defaults = new LinkedHashMap<>();
for (Entry en : defaults.entrySet()) {
Properties prop = new Properties();
prop.load(en.getValue());
this.defaults.put(en.getKey(), prop);
}
File f = new File(directory);
if (!f.exists()) {
f.mkdirs();
}
}
/**
*
* @param directory
* @param defaults
* @param createDir
* @throws FileNotFoundException
* @throws IOException
*/
public PropertyManager(String directory, Map defaults, boolean createDir) throws FileNotFoundException, IOException {
this.directory = directory;
this.defaults = defaults;
File f = new File(directory);
if (createDir && !f.exists()) {
f.mkdirs();
}
}
/**
*
* @param name
* @return
* @throws IOException
* @throws PropertyDefaultNotFoundException
* @throws PropertyNotManagedException
*/
public Properties getProperties(String name) throws IOException, PropertyDefaultNotFoundException, PropertyNotManagedException {
if (!defaults.keySet().contains(name)) {
throw new PropertyNotManagedException("Property '" + name + "' is not managed by this ProptertyManager Object");
}
File f = new File(directory + File.separator + name + ".properties");
if (!f.exists()) {
if (defaults.get(name) == null) {
throw new PropertyDefaultNotFoundException("A default for '" + name + "' was not found");
}
OutputStream os;
defaults.get(name).store(os = new FileOutputStream(f), "Properties for " + name + " created by PropertyManager");
os.close();
}
Properties props = new Properties();
InputStream is;
props.load(is = new FileInputStream(f));
is.close();
return props;
}
/**
*
* @param name
* @param propName
* @return
* @throws IOException
* @throws PropertyDefaultNotFoundException
* @throws PropertyNotManagedException
*/
public String getProperty(String name, String propName) throws IOException, PropertyDefaultNotFoundException, PropertyNotManagedException {
Properties prop = getProperties(name);
return prop.getProperty(propName);
}
/**
*
* @param name
* @param propName
* @param value
* @throws IOException
* @throws PropertyDefaultNotFoundException
* @throws PropertyNotManagedException
*/
public void updateProperty(String name, String propName, String value) throws IOException, PropertyDefaultNotFoundException, PropertyNotManagedException {
Properties prop = getProperties(name);
prop.setProperty(propName, value);
File f = new File(directory + File.separator + name + ".properties");
OutputStream os;
prop.store(os = new FileOutputStream(f), "Properties for " + name + " created by PropertyManager");
os.close();
}
}