00001 package edu.ksu.cis.bandera.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.util.*;
00036 import java.io.*;
00037 import java.awt.*;
00038 import java.net.*;
00039
00040 public class Preferences {
00041 private static Properties prop;
00042 private static final String reportHTMLBackgroundColorKey = "report.html.background.color";
00043 private static final String reportHTMLBackgroundImageKey = "report.html.background.image";
00044 private static final String reportHTMLBackImageKey = "report.html.back.image";
00045 private static String reportHTMLBackgroundColor = "#FFFFFF";
00046 private static String reportHTMLBackgroundImage = "";
00047 private static String reportHTMLBackImage = "";
00048 private static final String clrAssertion = "color.assertion";
00049 private static final String clrHighlight = "color.highlight";
00050 private static final String clrPredicate = "color.predicate";
00051 private static final String clrSliced = "color.sliced";
00052 private static final String clrModified = "color.modified";
00053 private static final String clrUnreachable = "color.unreachable";
00054 private static final String clrDefault = "color.default";
00055
00056 private static final String dspinDir = "dspin.include";
00057 private static final String absPath = "abstraction.path";
00058 private static final String absPackage = "abstraction.package";
00059 private static final String banderaHome = "bandera.home";
00060 private static final String jpfHome = "jpf.home";
00061 private static final String javacupHome = "javacup.home";
00062 private static final String regexpHome = "regexp.home";
00063 private static final String javaclassHome = "javaclass.home";
00064 private static String banderaHomeDir = ".";
00065 private static String jpfHomeDir = ".";
00066 private static String javacupHomeDir = ".";
00067 private static String javaclassHomeDir = ".";
00068 private static String regexpHomeDir = ".";
00069 public static final String userBanderaDir = System.getProperty("user.home") + File.separator + ".bandera";
00070 public static final String userConfigFile = userBanderaDir + File.separator + ".banderarc";
00071 public static final String banderaHeader = "Bandera Configuration File";
00072 private static final Color cdefHighlight = new Color(0, 255, 0);
00073 private static final Color cdefAssertion = new Color(128, 64, 64);
00074 private static final Color cdefPredicate = new Color(64, 128, 64);
00075 private static final Color cdefSliced = new Color(255,0,0);
00076 private static final Color cdefModified = new Color(0,0,255);
00077 private static final Color cdefUnreachable = new Color(0,255,255);
00078 private static final Color cdefDefault = new Color(0,0,0);
00079
00080 static {
00081 try {
00082 banderaHomeDir = new File(Class.forName("Bandera").getResource("Bandera.class").getFile()).getParent();
00083 } catch (Exception e) {
00084 System.out.println("Could not find Bandera");
00085 System.exit(1);
00086 }
00087 try {
00088 jpfHomeDir = new File(Class.forName("gov.nasa.arc.ase.jpf.jvm.Main").getResource("Main.class").getFile()).getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParent();
00089 } catch (Exception e) {
00090 System.out.println("Could not find JPF");
00091 }
00092 try {
00093 javacupHomeDir = new File(Class.forName("java_cup.runtime.Symbol").getResource("Symbol.class").getFile()).getParentFile().getParentFile().getParent();
00094 } catch (Exception e) {
00095 System.out.println("Could not find Java Cup runtime library");
00096 }
00097 try {
00098 regexpHomeDir = new File(Class.forName("gnu.regexp.RE").getResource("RE.class").getFile()).getParentFile().getParentFile().getParent();
00099 } catch (Exception e) {
00100 System.out.println("Could not find GNU regexp runtime library (needed only for JPF)");
00101 }
00102 try {
00103 javaclassHomeDir = new File(Class.forName("de.fub.bytecode.ClassPath").getResource("ClassPath.class").getFile()).getParentFile().getParentFile().getParentFile().getParent();
00104 } catch (Exception e) {
00105 System.out.println("Could not find low level Java bytecode classes for JPF runtime library");
00106 }
00107 }
00108 private static String clr2str(Color c)
00109 {
00110 int r, g, b;
00111 if (c == null) return null;
00112 r = c.getRed(); g = c.getGreen(); b = c.getBlue();
00113 return r + "," + g + "," + b;
00114 }
00115 public static String getAbstractionPackage() { return prop.getProperty(absPackage); }
00116 public static String getAbstractionPath() { return prop.getProperty(absPath); }
00117 public static Color getAssertionColor()
00118 {
00119 Color c = str2clr(prop.getProperty(clrAssertion));
00120
00121 if (c == null)
00122 {
00123 c = cdefAssertion;
00124 setAssertionColor(c);
00125 }
00126 return c;
00127 }
00128 public static String getBanderaHomeDir()
00129 {
00130 return banderaHomeDir;
00131 }
00132 public static Color getDefaultColor()
00133 {
00134 Color c = str2clr(prop.getProperty(clrDefault));
00135
00136 if (c == null)
00137 {
00138 c = cdefDefault;
00139 setDefaultColor(c);
00140 }
00141 return c;
00142 }
00143 public static String getDSpinDir() { return prop.getProperty(dspinDir); }
00144 public static Color getHighlightColor()
00145 {
00146 Color c = str2clr(prop.getProperty(clrHighlight));
00147
00148 if (c == null)
00149 {
00150 c = cdefHighlight;
00151 setHighlightColor(c);
00152 }
00153 return c;
00154 }
00155 public static String getJavaClassHomeDir() { return javaclassHomeDir; }
00156 public static String getJavaCupHomeDir() { return javacupHomeDir; }
00157 public static String getJPFHomeDir() { return jpfHomeDir; }
00158 public static Color getModifiedColor()
00159 {
00160 Color c = str2clr(prop.getProperty(clrModified));
00161
00162 if (c == null)
00163 {
00164 c = cdefModified;
00165 setModifiedColor(c);
00166 }
00167 return c;
00168 }
00169 public static Color getPredicateColor()
00170 {
00171 Color c = str2clr(prop.getProperty(clrPredicate));
00172
00173 if (c == null)
00174 {
00175 c = cdefPredicate;
00176 setPredicateColor(c);
00177 }
00178 return c;
00179 }
00180 public static Properties getProperties() { return prop; }
00181 public static String getRegExpHomeDir() { return regexpHomeDir; }
00182
00183
00184
00185
00186 public static java.lang.String getReportHTMLBackgroundColor() {
00187 return prop.getProperty(reportHTMLBackgroundColorKey);
00188 }
00189
00190
00191
00192
00193 public static java.lang.String getReportHTMLBackgroundImage() {
00194 return prop.getProperty(reportHTMLBackgroundImageKey);
00195 }
00196
00197
00198
00199
00200 public static java.lang.String getReportHTMLBackImage() {
00201 return prop.getProperty(reportHTMLBackImageKey);
00202 }
00203 public static Color getSlicedColor()
00204 {
00205 Color c = str2clr(prop.getProperty(clrSliced));
00206
00207 if (c == null)
00208 {
00209 c = cdefSliced;
00210 setSlicedColor(c);
00211 }
00212 return c;
00213 }
00214 public static Color getUnreachableColor()
00215 {
00216 Color c = str2clr(prop.getProperty(clrUnreachable));
00217
00218 if (c == null)
00219 {
00220 c = cdefUnreachable;
00221 setUnreachableColor(c);
00222 }
00223 return c;
00224 }
00225 public static String getUserPrefDir() { return userBanderaDir; }
00226 private static void init()
00227 {
00228 System.out.println("Setting up Bandera for the first time...");
00229 reset();
00230
00231 File f = new File(userBanderaDir);
00232 System.out.println("Creating '"+f.getAbsolutePath() + "'");
00233 if (!f.exists() && !f.mkdir()) {
00234 System.out.println("Fatal error: Cannot create " + f.getAbsolutePath()+ " to setup Bandera!");
00235 System.exit(0);
00236 }
00237
00238 String[] dirs = { "integral", "real" };
00239
00240 for (int i = 0; i < dirs.length; i++)
00241 {
00242 f = new File(userBanderaDir + File.separator + dirs[i]);
00243 System.out.println("Creating '"+f.getAbsolutePath() + "'");
00244 if (!f.exists() && !f.mkdir()) {
00245 System.out.println("Fatal error: Cannot create " + f.getAbsolutePath()+ " to setup Bandera!");
00246 System.exit(0);
00247 }
00248 }
00249 try {
00250 f = new File(userBanderaDir+ File.separator + "Abstractions");
00251 f.createNewFile();
00252 System.out.println("Creating '"+f.getAbsolutePath() + "'");
00253 } catch (Exception e)
00254 {
00255 System.out.println("Fatal error: Cannot setup Bandera for the first time!");
00256 System.exit(0);
00257 }
00258 try {
00259 f = new File(userBanderaDir + File.separator + "log");
00260 System.out.println("Creating '"+f.getAbsolutePath() + "'");
00261 if (!f.exists() && !f.mkdir()) throw new Exception();
00262 } catch (Exception e)
00263 {
00264 System.out.println("Fatal error: Cannot setup Bandera logging system!");
00265 System.exit(0);
00266 }
00267 save();
00268 System.out.println("First time setup done!");
00269 }
00270 public static void load()
00271 {
00272 String userHomeDir = System.getProperty("user.home");
00273 prop = new Properties();
00274
00275 try {
00276 prop.load(new FileInputStream(userConfigFile));
00277 System.out.println("User preferences loaded.");
00278 } catch (Exception e) {
00279
00280
00281 init();
00282 }
00283 }
00284 public static void reset()
00285 {
00286 System.out.println("Resetting preferences to default values");
00287 prop.setProperty(clrAssertion,clr2str(cdefAssertion));
00288 prop.setProperty(clrHighlight,clr2str(cdefHighlight));
00289 prop.setProperty(clrPredicate,clr2str(cdefPredicate));
00290 prop.setProperty(clrSliced,clr2str(cdefSliced));
00291 prop.setProperty(clrModified,clr2str(cdefModified));
00292 prop.setProperty(clrUnreachable,clr2str(cdefUnreachable));
00293 prop.setProperty(clrDefault,clr2str(cdefDefault));
00294 prop.setProperty(dspinDir,userBanderaDir);
00295 prop.setProperty(absPath,userBanderaDir);
00296 prop.setProperty(banderaHome,banderaHomeDir);
00297 prop.setProperty(jpfHome,jpfHomeDir);
00298 prop.setProperty(javacupHome,javacupHomeDir);
00299 prop.setProperty(regexpHome,regexpHomeDir);
00300 prop.setProperty(javaclassHome,javaclassHomeDir);
00301 prop.setProperty(absPackage,"");
00302 prop.setProperty(reportHTMLBackgroundColorKey, reportHTMLBackgroundColor);
00303 prop.setProperty(reportHTMLBackgroundImageKey, reportHTMLBackgroundImage);
00304 prop.setProperty(reportHTMLBackImageKey, reportHTMLBackImage);
00305 }
00306 public static void save()
00307 {
00308 try {
00309 prop.store(new FileOutputStream(userConfigFile),banderaHeader);
00310 System.out.println("Preferences saved.");
00311 } catch (Exception e)
00312 {
00313 System.out.println("Cannot save Bandera config file!");
00314 }
00315 }
00316 public static void setAbstractionPackage(String p) { prop.setProperty(absPackage, p); }
00317 public static void setAbstractionPath(String p) { prop.setProperty(absPath, p); }
00318 public static void setAssertionColor(Color c)
00319 {
00320 prop.setProperty(clrAssertion,clr2str(c));
00321 }
00322 public static void setDefaultColor(Color c)
00323 {
00324 prop.setProperty(clrDefault,clr2str(c));
00325 }
00326 public static void setDSpinDir(String p) { prop.setProperty(dspinDir, p); }
00327 public static void setHighlightColor(Color c)
00328 {
00329 prop.setProperty(clrHighlight,clr2str(c));
00330 }
00331 public static void setJPFHomeDir(String dir) { prop.setProperty(jpfHome,dir); }
00332 public static void setModifiedColor(Color c)
00333 {
00334 prop.setProperty(clrModified,clr2str(c));
00335 }
00336 public static void setPredicateColor(Color c)
00337 {
00338 prop.setProperty(clrPredicate,clr2str(c));
00339 }
00340
00341
00342
00343
00344 public static void setReportHTMLBackgroundColor(java.lang.String newReportHTMLBackgroundColor) {
00345 prop.setProperty(reportHTMLBackgroundColorKey, newReportHTMLBackgroundColor);
00346 }
00347
00348
00349
00350
00351 public static void setReportHTMLBackgroundImage(java.lang.String newReportHTMLBackgroundImage) {
00352 prop.setProperty(reportHTMLBackgroundImageKey, newReportHTMLBackgroundImage);
00353 }
00354
00355
00356
00357
00358 public static void setReportHTMLBackImage(java.lang.String newReportHTMLBackImage) {
00359 prop.setProperty(reportHTMLBackImageKey, newReportHTMLBackImage);
00360 }
00361 public static void setSlicedColor(Color c)
00362 {
00363 prop.setProperty(clrSliced,clr2str(c));
00364 }
00365 public static void setUnreachableColor(Color c)
00366 {
00367 prop.setProperty(clrUnreachable,clr2str(c));
00368 }
00369 private static Color str2clr(String s)
00370 {
00371 Color c = null;
00372 if (s == null) return null;
00373
00374
00375 if (s.indexOf(",") == -1)
00376 {
00377 try {
00378 c = new Color(Integer.parseInt(s));
00379 } catch (Exception e) {}
00380 return c;
00381 }
00382
00383 StringTokenizer tok = new StringTokenizer(s, ",");
00384 int r, g, b;
00385 try
00386 {
00387 r = Integer.parseInt(tok.nextToken());
00388 g = Integer.parseInt(tok.nextToken());
00389 b = Integer.parseInt(tok.nextToken());
00390 c = new Color(r,g,b);
00391 } catch (Exception e)
00392 {
00393 }
00394 return c;
00395 }
00396 }