001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.core;
025    
026    import java.applet.Applet;
027    import java.io.InputStreamReader;
028    import java.io.LineNumberReader;
029    import java.io.PrintStream;
030    
031    import org.jdmp.core.module.Module;
032    import org.jdmp.core.module.ModuleFactory;
033    import org.jdmp.core.script.Result;
034    import org.ujmp.core.UJMP;
035    import org.ujmp.core.util.io.IntelligentFileReader;
036    
037    public class JDMP extends Applet {
038            private static final long serialVersionUID = -6039550773857594839L;
039    
040            public static final String JDMPVERSION = "0.1.1";
041    
042            public static final String UJMPVERSION = UJMP.UJMPVERSION;
043    
044            public static final String AUTHOR = "Holger Arndt, A. Naegele and M. Bundschus";
045    
046            public static final String COPYRIGHT = "2008-2009";
047    
048            protected static final PrintStream out = System.out;
049    
050            protected static String script = null;
051            protected static String file = null;
052            protected static boolean textMode = true;
053    
054            public static void main(String[] args) throws Exception {
055                    evaluateParameters(args);
056                    run();
057            }
058    
059            protected static void evaluateParameters(String[] args) {
060                    // parse command line
061    
062                    try {
063    
064                            for (int i = 0; i < args.length; i++) {
065    
066                                    if (args[i].equals("-e") || args[i].equals("--execute")) {
067                                            script = args[++i];
068                                            if (script.startsWith("\"")) {
069                                                    script = script.substring(1);
070                                            }
071                                            if (script.endsWith("\"")) {
072                                                    script = script.substring(1, script.length() - 1);
073                                            }
074                                    } else if (args[i].equals("-f") || args[i].equals("--file")) {
075                                            file = args[++i];
076                                    } else if (args[i].equals("-g") || args[i].equals("--gui")) {
077                                            textMode = false;
078                                    } else if (args[i].equals("-t") || args[i].equals("--text")) {
079                                            textMode = true;
080                                    } else if (args[i].equals("-h") || args[i].equals("--help")) {
081                                            help();
082                                            System.exit(0);
083                                    }
084    
085                            }
086    
087                    } catch (Exception e) {
088                            help();
089                            System.exit(1);
090                    }
091            }
092    
093            private static void help() {
094                    out.println();
095                    out.println("JDMP supports the following command line options:");
096                    out.println("    JDMP [-tgh] [-e command] [-f file]");
097                    out.println();
098                    out.println("    -e, --execute     executes a command at startup");
099                    out.println("    -f, --file        executes the commands in the specified file");
100                    out.println("    -t, --text        starts JDMP in textmode");
101                    out.println("    -g, --gui         starts JDMP in graphics mode");
102                    out.println("    -h, --help        displays this help text");
103                    out.println();
104                    out.println();
105            }
106    
107            public static void run() throws Exception {
108                    out.println("/*");
109                    out.println(" * Welcome to the Java Data Mining Package (JDMP) v" + JDMPVERSION);
110                    out.println(" * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus");
111                    out.println(" *");
112                    out.println(" * JDMP is free software; you can redistribute it and/or modify");
113                    out.println(" * it under the terms of the GNU Lesser General Public License as");
114                    out.println(" * published by the Free Software Foundation; either version 2");
115                    out.println(" * of the License, or (at your option) any later version.");
116                    out.println(" *");
117                    out.println(" * JDMP is distributed in the hope that it will be useful,");
118                    out.println(" * but WITHOUT ANY WARRANTY; without even the implied warranty of");
119                    out.println(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the");
120                    out.println(" * GNU Lesser General Public License for more details.");
121                    out.println(" *");
122                    out.println(" * If you would like to find out more, please take a look at the");
123                    out.println(" * homepage of JDMP at http://www.jdmp.org/");
124                    out.println(" */");
125                    out.println();
126    
127                    Module module = ModuleFactory.emptyModule();
128    
129                    // show GUI if necessary and possible
130                    if (textMode == false) {
131                            try {
132                                    Class.forName("org.jdmp.gui.JDMP");
133                                    module.showGUI();
134                            } catch (Exception e) {
135                                    out.println("Graphics mode is not available, probably because jdmp-gui is not");
136                                    out.println("in the class path.");
137                                    out.println();
138                                    textMode = true;
139                            }
140                    }
141    
142                    // execute script file if there is one
143                    if (file != null) {
144                            String script = IntelligentFileReader.load(file);
145                            module.execute(script);
146                    }
147    
148                    // execute command if there is one
149                    if (script != null) {
150                            module.execute(script);
151                    }
152    
153                    if (textMode == true) {
154    
155                            // read from standard input
156                            LineNumberReader lr = new LineNumberReader(new InputStreamReader(System.in));
157    
158                            while (true) {
159    
160                                    try {
161    
162                                            out.print(">> ");
163    
164                                            String line = lr.readLine();
165    
166                                            if (line.startsWith(">> ")) {
167                                                    line = line.substring(3);
168                                            }
169    
170                                            if (!line.endsWith(";")) {
171                                                    Result result = module.execute(line + ";");
172                                                    out.println(result);
173                                            } else {
174                                                    module.execute(line);
175                                            }
176    
177                                    } catch (Exception e) {
178                                            e.printStackTrace();
179                                    }
180    
181                            }
182                    }
183            }
184    
185    }