PropertyTools

Die Klasse PropertyTools ermöglicht es die Konfiguration bzw. Einstellung einer Applikation in einer Text-Datei oder in eine XML-Datei zu speichern und/oder wieder zu laden. Dazu wird der Konfigurationsmechanismus von Java benutzt.

class PropertyTools


package de.snowbits.tools;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertyTools {

	private PropertyTools() {

	}

	public static Properties load(String path) throws IOException {
		return load(path, false);
	}

	public static Properties loadFromXML(String path) throws IOException {
		return load(path, true);
	}

	public static void save(Properties props, String comment, String path) throws IOException {
		save(props, comment, path, false);
	}

	public static void saveToXML(Properties props, String comment, String path) throws IOException {
		save(props, comment, path, true);
	}

	private static Properties load(String path, boolean asXMLOrINI) throws IOException {
		Properties result = null;
		FileInputStream in = null;

		try {
			result = new Properties();
			in = new FileInputStream(path);

			if (asXMLOrINI)
				result.loadFromXML(in);
			else
				result.load(in);

			in.close();
		} catch (IOException e) {
			try {
				if (in != null)
					in.close();
			} catch (IOException ex) {
			}

			throw new IOException("Error at loading Properties from path: "	+ path, e);
		}

		return result;
	}

	private static void save(Properties props, String comment, String path,	boolean asXMLOrINI) throws IOException {
		FileOutputStream out = null;

		try {
			out = new FileOutputStream(path);

			if (asXMLOrINI)
				props.storeToXML(out, comment);
			else
				props.store(out, comment);

			out.close();
		} catch (IOException e) {
			try {
				if (out != null)
					out.close();
			} catch (IOException ex) {
			}

			throw new IOException("Error at saving Properties for path: " + path, e);
		}
	}
}
	

class PropertyHandlerTest


package de.snowbits.tools;

public class PropertyHandlerTest {

	public static void main(String[] args) {
		saveProperty();
		System.out.println("save useCaseBean: " + UseCaseBean.getInstance());
		// save useCaseBean: [windowX: 20.0; windowY: 30.0; windowWidth: 600.0; windowHeight: 500.0]
		
		loadProperty();
		System.out.println("load useCaseBean: " + UseCaseBean.getInstance());
		// load useCaseBean: [windowX: 20.0; windowY: 30.0; windowWidth: 600.0; windowHeight: 500.0]
	}

	private static void saveProperty() {
		PropertyHandler propHandler = new PropertyHandler();
		UseCaseBean useCaseBean = UseCaseBean.getInstance();
		useCaseBean.setWindowX(20.0);
		useCaseBean.setWindowY(30.0);
		useCaseBean.setWindowWidth(600.0);
		useCaseBean.setWindowHeight(500.0);
		propHandler.save(useCaseBean);
	}
	
	private static void loadProperty() {
		PropertyHandler propHandler = new PropertyHandler();
		propHandler.load();
	}
	
}
	

Property File settings.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Configuration file for MyApp Version 1.0</comment>
<entry key="WindowWidth">600.0</entry>
<entry key="WindowY">30.0</entry>
<entry key="WindowHeight">500.0</entry>
<entry key="WindowX">20.0</entry>
</properties>
	

class PropertyHandler


package de.snowbits.tools;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class PropertyHandler {
	
	private final static String CONF_PATH = "settings.xml";
	
	private final static String WINDOW_X      = "WindowX";
	private final static String WINDOW_Y      = "WindowY";
	private final static String WINDOW_WIDTH  = "WindowWidth";
	private final static String WINDOW_HEIGHT = "WindowHeight";
	
	private final static double DEFAULT_WINDOW_X      = 40.0; 
	private final static double DEFAULT_WINDOW_Y      = 40.0; 
	private final static double DEFAULT_WINDOW_WIDTH  = 800.0; 
	private final static double DEFAULT_WINDOW_HEIGHT = 600.0; 	

	public void save(UseCaseBean useCaseBean) {
		String comment = "Configuration file for MyApp Version 1.0";
		Properties props = new Properties();
		
		props.put(WINDOW_X, String.valueOf(useCaseBean.getWindowX()));
		props.put(WINDOW_Y, String.valueOf(useCaseBean.getWindowY()));
		props.put(WINDOW_WIDTH, String.valueOf(useCaseBean.getWindowWidth()));
		props.put(WINDOW_HEIGHT, String.valueOf(useCaseBean.getWindowHeight()));
				
		try {
			PropertyTools.saveToXML(props, comment, CONF_PATH);
		} catch (IOException e) {
			// TODO: handle error
			e.printStackTrace();
		}
	}

	public boolean load() {
		Properties props = null;
		UseCaseBean useCaseBean = UseCaseBean.getInstance();
		
		try {
			props = PropertyTools.loadFromXML(CONF_PATH);
		} catch (IOException e) {
			// TODO: handle error
			e.printStackTrace();
			return false;
		}
		
		Enumeration<Object> enu = props.keys();
		
		while (enu.hasMoreElements()) {
			String key = (String) enu.nextElement();
			
			if (key.equals(WINDOW_X)) {
				useCaseBean.setWindowX(StringTools.parseDouble(props.getProperty(key), DEFAULT_WINDOW_X));
			} else if (key.equals(WINDOW_Y)) {
				useCaseBean.setWindowY(StringTools.parseDouble(props.getProperty(key), DEFAULT_WINDOW_Y));
			} else if (key.equals(WINDOW_WIDTH)) {
				useCaseBean.setWindowWidth(StringTools.parseDouble(props.getProperty(key), DEFAULT_WINDOW_WIDTH));
			} else if (key.equals(WINDOW_HEIGHT)) {
				useCaseBean.setWindowHeight(StringTools.parseDouble(props.getProperty(key), DEFAULT_WINDOW_HEIGHT));
			} 
		}
		
		return true;
	}
}
	

class UseCaseBean


package de.snowbits.tools;

public class UseCaseBean {
	private static UseCaseBean _useCaseBean;
	
	private Double _windowX = new Double(0.0);
	private Double _windowY = new Double(0.0);
	private Double _windowWidth = new Double(0.0);
	private Double _windowHeight = new Double(0.0);
	
	private UseCaseBean() {
	}
	
	public static UseCaseBean getInstance() {
		if (_useCaseBean == null) 
			_useCaseBean = new UseCaseBean();
	
		return _useCaseBean;
	}

	public Double getWindowX() {
		return _windowX;
	}

	public void setWindowX(Double windowX) {
		_windowX = windowX;
	}

	public Double getWindowY() {
		return _windowY;
	}

	public void setWindowY(Double windowY) {
		_windowY = windowY;
	}

	public Double getWindowWidth() {
		return _windowWidth;
	}

	public void setWindowWidth(Double windowWidth) {
		_windowWidth = windowWidth;
	}

	public Double getWindowHeight() {
		return _windowHeight;
	}

	public void setWindowHeight(Double windowHeight) {
		_windowHeight = windowHeight;
	}
	
	@Override
	public String toString() {
		return new StringBuilder().append("[")
			.append("windowX: ").append(getWindowX())
			.append("; windowY: ").append(getWindowY())
			.append("; windowWidth: ").append(getWindowWidth())
			.append("; windowHeight: ").append(getWindowHeight())
			.append("]").toString();
	}
}
	

Top


Example
Sitemap Kontakt Impressum