00001 package edu.stanford.hci.r3.config;
00002
00003 import java.io.File;
00004 import java.io.FileNotFoundException;
00005 import java.io.IOException;
00006 import java.io.InputStream;
00007 import java.net.URI;
00008 import java.net.URISyntaxException;
00009 import java.net.URL;
00010 import java.util.InvalidPropertiesFormatException;
00011 import java.util.Properties;
00012
00013 import edu.stanford.hci.r3.PaperToolkit;
00014 import edu.stanford.hci.r3.actions.remote.ActionReceiver;
00015 import edu.stanford.hci.r3.render.RegionRenderer;
00016 import edu.stanford.hci.r3.units.Pixels;
00017
00031 public class Configuration extends Properties {
00032
00033 private static final Configuration config = new Configuration();
00034
00043 private static String getPropertyValue(String propertyName) {
00044 final String property = config.getProperty(propertyName);
00045
00046
00047 return property;
00048 }
00049
00056 public static File getConfigFile(String configFileKey) {
00057 try {
00058
00059 final URL resource = Configuration.class.getResource(getPropertyValue(configFileKey));
00060
00061 final URI uri = resource.toURI();
00062 final File file = new File(uri);
00063 return file;
00064 } catch (URISyntaxException e) {
00065 e.printStackTrace();
00066 }
00067 return null;
00068 }
00069
00075 private static InputStream getConfigFileStream(String propertyName) throws IOException {
00076 final String resourceName = getPropertyValue(propertyName);
00077
00078 final URL resource = Configuration.class.getResource(resourceName);
00079 return resource.openStream();
00080 }
00081
00086 public static Properties getPropertiesFromConfigFile(String configFileKey) {
00087 final Properties props = new Properties();
00088 try {
00089
00090
00091 final InputStream configFileStream = Configuration.getConfigFileStream(configFileKey);
00092
00093
00094
00095
00096
00097
00098
00099
00100 props.loadFromXML(configFileStream);
00101 } catch (InvalidPropertiesFormatException e) {
00102 e.printStackTrace();
00103 } catch (FileNotFoundException e) {
00104 e.printStackTrace();
00105 } catch (IOException e) {
00106 e.printStackTrace();
00107 }
00108 return props;
00109 }
00110
00116 public static String getPropertyFromConfigFile(String propertyName, String configFileKey) {
00117 return getPropertiesFromConfigFile(configFileKey).getProperty(propertyName);
00118 }
00119
00123 private Configuration() {
00124 setDefaultConfig();
00125 }
00126
00130 private void setDefaultConfig() {
00131
00132
00133 setProperty(Pixels.CONFIG_FILE_KEY, Pixels.CONFIG_FILE_VALUE);
00134 setProperty(RegionRenderer.CONFIG_FILE_KEY, RegionRenderer.CONFIG_FILE_VALUE);
00135 setProperty(ActionReceiver.CONFIG_FILE_KEY, ActionReceiver.CONFIG_FILE_VALUE);
00136 setProperty(PaperToolkit.CONFIG_PATTERN_PATH_KEY, PaperToolkit.CONFIG_PATTERN_PATH_VALUE);
00137 setProperty(PaperToolkit.CONFIG_FILE_KEY, PaperToolkit.CONFIG_FILE_VALUE);
00138 }
00139
00140 }