View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.tools;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.NoSuchElementException;
24  
25  import net.sf.magicproject.token.IdConst;
26  
27  import org.apache.commons.configuration.ConfigurationException;
28  import org.apache.commons.configuration.XMLConfiguration;
29  import org.apache.commons.io.FileUtils;
30  import org.apache.commons.lang.ArrayUtils;
31  
32  /***
33   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34   * @since 0.93
35   */
36  public final class Configuration {
37  
38  	/***
39  	 * The maximum amount of recent properties.
40  	 */
41  	private static final int MAX_RECENT_SIZE = 10;
42  
43  	/***
44  	 * Create a new instance of this class.
45  	 */
46  	private Configuration() {
47  		super();
48  	}
49  
50  	/***
51  	 * @param property
52  	 * @return the string value if it exists, empty string otherwise.
53  	 */
54  	public static String getString(String property) {
55  		return getConfiguration().getString(property, "");
56  	}
57  
58  	/***
59  	 * @param property
60  	 * @return the string value if it exists, empty string otherwise.
61  	 */
62  	public static String[] getArray(String property) {
63  		int maxIndex = Configuration.getConfiguration().getMaxIndex(property);
64  		String[] res = new String[maxIndex + 1];
65  		for (int i = 0; i < maxIndex + 1; i++) {
66  			String item = Configuration.getConfiguration().getString(
67  					property + "(" + i + ")");
68  			res[i] = item;
69  		}
70  		return res;
71  	}
72  
73  	/***
74  	 * Add a property to the list.
75  	 * 
76  	 * @param property
77  	 * @param pItem
78  	 */
79  	public static void addRecentProperty(String property, String pItem) {
80  		String item = pItem.replace(",", "//,");
81  		String[] previous = getArray(property);
82  		getConfiguration().clearProperty(property);
83  		getConfiguration().setProperty(property + "(0)", item);
84  		int index = ArrayUtils.indexOf(previous, item);
85  		if (index >= 0) {
86  			for (int i = 0; i < index; i++) {
87  				getConfiguration().setProperty(property + "(" + i + 1 + ")",
88  						previous[i]);
89  			}
90  			for (int i = index + 1; i < previous.length; i++) {
91  				getConfiguration().setProperty(property + "(" + i + ")", previous[i]);
92  			}
93  		} else {
94  			final int maxSize;
95  			if (previous.length > MAX_RECENT_SIZE)
96  				maxSize = MAX_RECENT_SIZE;
97  			else
98  				maxSize = previous.length;
99  			for (int i = 0; i < maxSize; i++) {
100 				getConfiguration().setProperty(property + "(" + i + 1 + ")",
101 						previous[i]);
102 			}
103 		}
104 	}
105 
106 	/***
107 	 * @param property
108 	 * @param defaultValue
109 	 * @return the string value if it exists, defaultValue otherwise.
110 	 */
111 	public static String getString(String property, String defaultValue) {
112 		return getConfiguration().getString(property, defaultValue);
113 	}
114 
115 	/***
116 	 * @param property
117 	 * @param defaultValue
118 	 * @return the boolean value if it exists, defaultValue otherwise.
119 	 */
120 	public static Boolean getBoolean(String property, Boolean defaultValue) {
121 		return getConfiguration().getBoolean(property, defaultValue);
122 	}
123 
124 	/***
125 	 * @param property
126 	 * @return the boolean value if it exists, null otherwise.
127 	 */
128 	public static Boolean getBoolean(String property) {
129 		try {
130 			return getConfiguration().getBoolean(property);
131 		} catch (NoSuchElementException e) {
132 			return null;
133 		}
134 	}
135 
136 	/***
137 	 * @param property
138 	 * @param defaultValue
139 	 * @return the integer value if it exists, defaultValue otherwise.
140 	 */
141 	public static Integer getInt(String property, Integer defaultValue) {
142 		return getConfiguration().getInt(property, defaultValue);
143 	}
144 
145 	/***
146 	 * @param property
147 	 * @return the integer value if it exists, 0 otherwise.
148 	 */
149 	public static Integer getInt(String property) {
150 		return getConfiguration().getInt(property, 0);
151 	}
152 
153 	/***
154 	 * @param property
155 	 * @param value
156 	 */
157 	public static void setProperty(String property, Object value) {
158 		getConfiguration().setProperty(property, value);
159 	}
160 
161 	/***
162 	 * @param property
163 	 * @param defaultValue
164 	 * @return the float value if it exists, defaultValue otherwise.
165 	 */
166 	public static float getFloat(String property, Float defaultValue) {
167 		return getConfiguration().getFloat(property, defaultValue);
168 	}
169 
170 	/***
171 	 * The configuration file.
172 	 */
173 	private static XMLConfiguration configuration;
174 
175 	/***
176 	 * Return the configuration node.
177 	 * 
178 	 * @return the configuration node.
179 	 */
180 	public static XMLConfiguration getConfiguration() {
181 		if (configuration == null)
182 			try {
183 				loadTemplateFile(IdConst.FILE_SETTINGS);
184 				configuration = new XMLConfiguration(IdConst.FILE_SETTINGS);
185 			} catch (ConfigurationException e) {
186 				throw new RuntimeException("Could not load the configuration :"
187 						+ IdConst.FILE_SETTINGS, e);
188 			}
189 		return configuration;
190 	}
191 
192 	/***
193 	 * Load the given <param>userFile</param> file. If is not found, try to load
194 	 * the default file.
195 	 * 
196 	 * @param userFile
197 	 *          the configuration file.
198 	 * @return the user configuration.
199 	 */
200 	public static File loadTemplateFile(String userFile) {
201 		if (MToolKit.getFile(userFile) == null) {
202 			try {
203 				// Load the default tbs file
204 				File configurationFile = MToolKit
205 						.getFile("tbs/"+IdConst.TBS_DEFAULT+"/settings/profiles/default-" + userFile);
206 				if (configurationFile == null) {
207 					throw new RuntimeException(
208 							"No configuration file found, if you are running "
209 									+ "from this from Eclipse, be sure the working "
210 									+ "directory is set to "
211 									+ "${workspace_loc:magic-project/src/main/resources}");
212 				}
213 				FileUtils.copyFile(configurationFile, new File(userFile));
214 			} catch (IOException io) {
215 				io.printStackTrace();
216 			}
217 		}
218 		return MToolKit.getFile(userFile);
219 	}
220 
221 	/***
222 	 * Load the given <param>userFile</param> file. If is not found, try to load
223 	 * the default file.
224 	 * 
225 	 * @param userFile
226 	 *          the configuration file.
227 	 * @return the user configuration.
228 	 */
229 	public static File loadTemplateTbsFile(String userFile) {
230 		String tbsFile = "tbs/" + MToolKit.tbsName + "/" + userFile;
231 		if (MToolKit.getFile(tbsFile) == null) {
232 			try {
233 				// Load the default tbs file
234 				File configurationFile = MToolKit.getFile("tbs/" + MToolKit.tbsName
235 						+ "/settings/profiles/default-" + userFile);
236 				if (configurationFile == null) {
237 					throw new RuntimeException(
238 							"No configuration file found, if you are running "
239 									+ "from this from Eclipse, be sure the working "
240 									+ "directory is set to "
241 									+ "${workspace_loc:magic-project/src/main/resources}");
242 				}
243 				FileUtils.copyFile(configurationFile, new File(tbsFile));
244 			} catch (IOException io) {
245 				io.printStackTrace();
246 			}
247 		}
248 		return MToolKit.getFile(tbsFile);
249 	}
250 }