001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: /** this class parses in a config file that has to use the following grammar:
012: * S -> (IDENTIFIER ':' (alles ausser ';')* ';')*
013: * then it creates files that can refer to the first IDENTIFIER by using
014: * @IDENTIFIER@ that will be replaced by the expression after the ':'
015: * and before ';'
016: */package de.uka.ilkd.key.util.make;
017:
018: import java.io.*;
019: import java.net.URL;
020: import java.util.HashMap;
021: import java.util.Properties;
022:
023: public class Config {
024:
025: private Config() {
026: }
027:
028: private static final Config unique = new Config();
029:
030: //these two can't be made local to, say, GFinterface because of
031: //static loading
032: public static File GF_PATH_FILE = new File(
033: de.uka.ilkd.key.gui.configuration.PathConfig.KEY_CONFIG_DIR
034: + File.separator + "gf-path.props");
035:
036: public static final String GF_PATH_KEY = "[GFPath]";
037:
038: public static File SIMPLIFY_PATH_FILE = new File(
039: de.uka.ilkd.key.gui.configuration.PathConfig.KEY_CONFIG_DIR
040: + File.separator + "simplify-path.props");
041:
042: public static final String SIMPLIFY_PATH_KEY = "[SimplifyPath]";
043:
044: static HashMap map = new HashMap();
045:
046: /** loads a resource and returns its URL
047: * @param cl the Class used to determine the resource
048: * @param resourcename the String that contains the name of the resource
049: * @return the URL of the resource
050: */
051: public static URL getResourceFile(Class cl, String resourcename) {
052: URL resourceURL = cl.getResource(resourcename);
053: if (resourceURL == null && cl.getSuperclass() != null) {
054: return getResourceFile(cl.getSuperclass(), resourcename);
055: } else if (resourceURL == null && cl.getSuperclass() == null) {
056: return null;
057: }
058: return resourceURL;
059: }
060:
061: /** loads a resource and returns its URL
062: * @param o the Object used to determine the resource
063: * @param resourcename the String that contains the name of the resource
064: * @return the URL of the resource
065: */
066: private static URL getResourceFile(Object o, String resourcename) {
067: return getResourceFile(o.getClass(), resourcename);
068: }
069:
070: /** reads until character char is found and return alls read signs without
071: * character last */
072: private static StringBuffer readUntil(InputStreamReader fr,
073: char last) {
074: StringBuffer result = new StringBuffer();
075: try {
076: int chr = fr.read();
077: while (chr != -1 && ((char) chr) != last) {
078: if (((char) chr) != '\n' && ((char) chr) != '\r'
079: && ((char) chr) != '\t') { //&& ((char)chr)!=' '
080: result.append((char) chr);
081: }
082: chr = fr.read();
083: }
084: } catch (IOException io) {
085: System.err
086: .println("Error occured while reading config file.");
087: System.err.println(io);
088: System.exit(-1);
089: }
090: return result;
091: }
092:
093: private static int readIdentifier(FileReader fr) {
094: StringBuffer identifier = new StringBuffer();
095: identifier = readUntil(fr, '[');
096: if (identifier.length() == 0)
097: return -1;
098: if (identifier.length() == 0) {
099: throw new RuntimeException("IDENTIFIER EXPECTED.");
100: }
101: StringBuffer path = new StringBuffer();
102: path = readUntil(fr, ']');
103:
104: StringBuffer result = (StringBuffer) map.get(identifier);
105: if (map.get(identifier.toString()) == null) {
106: map.put(identifier.toString(), path);
107: } else {
108: result.append(File.pathSeparator + path);
109: }
110: return 0;
111: }
112:
113: private static void createFile(String filename, String template) {
114: InputStreamReader fr = null;
115: FileWriter fw = null;
116: try {
117: fr = new InputStreamReader(
118: getResourceFile(unique, template).openStream());
119: fw = new FileWriter(filename);
120: int chr = fr.read();
121: while (chr != -1) {
122: if (((char) chr) != '@') {
123: fw.write(chr);
124: } else {
125: StringBuffer res = readUntil(fr, '@');
126: if (map.get(res.toString()) == null) {
127: System.err.println("Identifier " + res
128: + " not defined");
129: System.exit(-1);
130: }
131: String repl = map.get(res.toString()).toString();
132: fw.write(repl, 0, repl.length());
133: }
134: chr = fr.read();
135: }
136: fr.close();
137: fw.close();
138: } catch (IOException io) {
139: System.err.println("File " + filename
140: + " can not be written.\n" + io);
141: System.exit(-1);
142: }
143: }
144:
145: private static void writeToKeYConfig(File file, String header,
146: String key, String prop) {
147: if (!file.exists()) {
148: new File(
149: de.uka.ilkd.key.gui.configuration.PathConfig.KEY_CONFIG_DIR
150: + File.separator).mkdir();
151: }
152: try {
153: FileOutputStream out = new FileOutputStream(file);
154: Properties props = new Properties();
155: props.setProperty(key, prop);
156: props.store(out, header);
157: } catch (Exception e) {
158: System.err
159: .println("Could not write property to config file "
160: + file);
161: }
162: }
163:
164: /** args[0] contains the file with the config infromation
165: * args[1] if dev use development templates else dist templates
166: */
167: public static void main(String args[]) {
168: FileReader fr = null;
169: try {
170: fr = new FileReader(args[0]);
171: map = new HashMap();
172: while (readIdentifier(fr) != -1) {
173: }
174: fr.close();
175: } catch (IOException io) {
176: System.err.println("File " + args[0] + " not found");
177: System.exit(-1);
178: }
179: if ("dev".equals(args[1])) {
180: createFile("install_dev.key", "install_dev.template");
181: createFile("Makefile.mk", "config.template");
182: createFile("runTests", "runTests.template");
183: createFile("runProver", "runProver.template");
184: createFile("runJava", "runJava.template");
185: createFile("miscConfigExtension",
186: "miscConfigExtension_dev.template");
187: if ("50".equals(args[2])) {
188: createFile("startkey", "startkey_dev_50.template");
189: } else if ("55".equals(args[2])) {
190: createFile("startkey", "startkey_dev_55.template");
191: } else {
192: createFile("startkey", "startkey_dev_60.template");
193: createFile("startkey.bat", "startkey_60_bat.template");
194: }
195: } else if ("dist".equals(args[1])) {
196: createFile("install.key", "install.template");
197: createFile("miscConfigExtension",
198: "miscConfigExtension.template");
199: if ("50".equals(args[2])) {
200: createFile("startkey", "startkey_50.template");
201: } else if ("55".equals(args[2])) {
202: createFile("startkey", "startkey_55.template");
203: } else {
204: createFile("startkey", "startkey_60.template");
205: createFile("startkey.bat", "startkey_60_bat.template");
206: }
207: }
208: createFile("configDefaultSnapShot",
209: "configDefaultSnapShot.template");
210: if (args.length > 3 && args[3] != null && !("".equals(args[3]))) {
211: writeToKeYConfig(GF_PATH_FILE, "GF-Path-File", GF_PATH_KEY,
212: args[3]);
213: }
214: if (args.length > 4 && args[4] != null && !("".equals(args[4]))) {
215: writeToKeYConfig(SIMPLIFY_PATH_FILE, "Simplify-Path-File",
216: SIMPLIFY_PATH_KEY, args[4]);
217: }
218: }
219: }
|