001: /*
002: *====================================================================
003: *The JRefactory License, Version 1.0
004: *
005: *Copyright (c) 2001 JRefactory. All rights reserved.
006: *
007: *Redistribution and use in source and binary forms, with or without
008: *modification, are permitted provided that the following conditions
009: *are met:
010: *
011: *1. Redistributions of source code must retain the above copyright
012: *notice, this list of conditions and the following disclaimer.
013: *
014: *2. Redistributions in binary form must reproduce the above copyright
015: *notice, this list of conditions and the following disclaimer in
016: *the documentation and/or other materials provided with the
017: *distribution.
018: *
019: *3. The end-user documentation included with the redistribution,
020: *if any, must include the following acknowledgment:
021: *"This product includes software developed by the
022: *JRefactory (http://www.sourceforge.org/projects/jrefactory)."
023: *Alternately, this acknowledgment may appear in the software itself,
024: *if and wherever such third-party acknowledgments normally appear.
025: *
026: *4. The names "JRefactory" must not be used to endorse or promote
027: *products derived from this software without prior written
028: *permission. For written permission, please contact seguin@acm.org.
029: *
030: *5. Products derived from this software may not be called "JRefactory",
031: *nor may "JRefactory" appear in their name, without prior written
032: *permission of Chris Seguin.
033: *
034: *THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: *OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: *DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
038: *ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: *SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: *LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: *USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: *ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: *OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: *OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: *SUCH DAMAGE.
046: *====================================================================
047: *
048: *This software consists of voluntary contributions made by many
049: *individuals on behalf of JRefactory. For more information on
050: *JRefactory, please see
051: *<http://www.sourceforge.org/projects/jrefactory>.
052: */
053: package org.acm.seguin.tools.install;
054:
055: import java.io.*;
056: import java.util.*;
057:
058: import org.acm.seguin.tools.RefactoryInstaller;
059:
060: /**
061: * Parses the pretty.settings file to generate the blocks
062: *
063: * @author Chris Seguin
064: * @created September 12, 2001
065: */
066: public class PrettySettingsParser {
067: private String filename;
068: private LinkedList list;
069:
070: /** Constructor for the PrettySettingsParser object */
071: public PrettySettingsParser() {
072: list = new LinkedList();
073: }
074:
075: /**
076: * Sets the Filename attribute of the PrettySettingsParser object
077: *
078: * @paramvalue The new Filename value
079: */
080: public void setFilename(String value) {
081: filename = value;
082: }
083:
084: /**
085: * Main processing method for the PrettySettingsParser object
086: */
087: public void run() {
088: try {
089: BufferedReader input = new BufferedReader(new FileReader(
090: filename));
091:
092: String line = input.readLine();
093:
094: while (line != null) {
095: if (line.length() == 0)
096: list.clear();
097: else if (line.charAt(0) == '#')
098: addToDescription(line);
099: else
100: processKey(line);
101:
102: line = input.readLine();
103: }
104:
105: input.close();
106: } catch (IOException ioe) {
107: ioe.printStackTrace(System.out);
108: }
109: }
110:
111: /**
112: * Gets the IndexedOption attribute of the PrettySettingsParser object
113: *
114: * @paramvalue Description of Parameter
115: * @return The IndexedOption value
116: */
117: private boolean isIndexedOption(String value) {
118: try {
119: Integer.parseInt(value);
120: } catch (Exception exc) {
121: return false;
122: }
123:
124: return true;
125: }
126:
127: /**
128: * Description of the Method
129: *
130: * @paramline Description of Parameter
131: * @exceptionIOException Description of Exception
132: */
133: private void processKey(String line) throws IOException {
134: int equalsLocation = line.indexOf('=');
135: String key = line.substring(0, equalsLocation);
136: String value = line.substring(equalsLocation + 1).trim();
137:
138: if (key.equals("version"))
139: return;
140:
141: StringTokenizer tok = new StringTokenizer(key, ".");
142: StringBuffer buffer = new StringBuffer();
143:
144: while (tok.hasMoreTokens()) {
145: String next = tok.nextToken();
146:
147: buffer.append(Character.toUpperCase(next.charAt(0)));
148: buffer.append(next.substring(1));
149: }
150: buffer.append("Panel");
151:
152: String name = buffer.toString();
153: String classExtension = "TextPanel";
154: String currentValue = value.trim().toLowerCase();
155:
156: if (currentValue.equals("true") || currentValue.equals("false"))
157: classExtension = "TogglePanel";
158: else if (containsOption())
159: classExtension = "OptionPanel";
160: else if (isIndexedOption(currentValue))
161: classExtension = "IndexedPanel";
162:
163: PrintWriter output = new PrintWriter(new FileWriter(name
164: + ".java"));
165:
166: output.println("package org.acm.seguin.tools.install;");
167: output.println("public class " + name + " extends "
168: + classExtension + "{");
169: output.println("\tpublic " + name + "() {");
170: output.println("\t\tsuper();");
171: printDescription(output);
172: output.println("\t\taddControl();");
173: output.println("\t}");
174: output.println("\tpublic String getKey() { return \"" + key
175: + "\"; }");
176: output
177: .println("\tprotected String getInitialValue() { return \""
178: + value + "\"; }");
179: output.println("}");
180: output.flush();
181: output.close();
182: }
183:
184: /**
185: * Prints the description
186: *
187: * @paramoutput Description of Parameter
188: */
189: private void printDescription(PrintWriter output) {
190: Iterator iter = list.iterator();
191:
192: while (iter.hasNext()) {
193: String next = (String) iter.next();
194:
195: next = next.trim();
196: if (next.charAt(0) == '*') {
197: int indexMinus = next.indexOf('-');
198: String key = next.substring(1, indexMinus).trim();
199: String descr = next.substring(indexMinus + 1).trim();
200:
201: output.println("\t\taddOption(\"" + key + "\", \""
202: + descr + "\");");
203: } else
204: output.println("\t\taddDescription(\"" + next + "\");");
205:
206: }
207: }
208:
209: /**
210: * Determines if this is an option
211: *
212: * @return Description of the Returned Value
213: */
214: private boolean containsOption() {
215: Iterator iter = list.iterator();
216:
217: while (iter.hasNext()) {
218: String next = (String) iter.next();
219:
220: if (next.indexOf("*") >= 0)
221: return true;
222: }
223: return false;
224: }
225:
226: /**
227: * Adds a feature to the ToDescription attribute of the
228: * PrettySettingsParser object
229: *
230: * @paramline The feature to be added to the ToDescription attribute
231: */
232: private void addToDescription(String line) {
233: String message = line.substring(1);
234:
235: if (!((message == null) || (message.trim().length() == 0)))
236: list.add(message);
237:
238: }
239:
240: /**
241: * The main program for the PrettySettingsParser class
242: *
243: * @paramargs The command line arguments
244: */
245: public static void main(String[] args) {
246: (new RefactoryInstaller(false)).run();
247:
248: PrettySettingsParser psp = new PrettySettingsParser();
249:
250: psp.setFilename(args[0]);
251: psp.run();
252: }
253: }
254:
255: // This is the end of the file
|