Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / appletviewer / Main.java
blob4eff8f53ebbfbee22a60a00fd422b73d6dc54209
1 /* Main.java -- a standalone viewer for Java applets
2 Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.classpath.tools.appletviewer;
40 import gnu.classpath.tools.common.ClasspathToolParser;
41 import gnu.classpath.tools.getopt.Option;
42 import gnu.classpath.tools.getopt.OptionException;
43 import gnu.classpath.tools.getopt.OptionGroup;
44 import gnu.classpath.tools.getopt.Parser;
45 import java.applet.Applet;
46 import java.awt.Dimension;
47 import java.io.BufferedReader;
48 import java.io.FileInputStream;
49 import java.io.FileOutputStream;
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.io.InputStreamReader;
53 import java.io.OutputStream;
54 import java.net.URL;
55 import java.util.ArrayList;
56 import java.util.HashMap;
57 import java.util.List;
60 class Main
62 private static HashMap classLoaderCache = new HashMap();
64 private static ClassLoader getClassLoader(URL codebase, ArrayList archives)
66 // Should load class loader each time. It is possible that there
67 // are more than one applet to be loaded with different archives.
68 AppletClassLoader loader = new AppletClassLoader(codebase, archives);
69 classLoaderCache.put(codebase, loader);
71 return loader;
74 private static String code = null;
75 private static String codebase = null;
76 private static String archive = null;
77 private static List parameters = new ArrayList();
78 private static Dimension dimensions = new Dimension(-1, -1);
79 private static String pipeInName = null;
80 private static String pipeOutName = null;
81 private static boolean pluginMode = false;
82 private static Parser parser = null;
84 static Applet createApplet(AppletTag tag)
86 Applet applet = null;
88 try
90 ClassLoader loader = getClassLoader(tag.prependCodeBase(""),
91 tag.getArchives());
92 String code = tag.getCode();
94 if (code.endsWith(".class"))
95 code = code.substring(0, code.length() - 6).replace('/', '.');
97 Class c = loader.loadClass(code);
98 applet = (Applet) c.newInstance();
100 catch (Exception e)
102 e.printStackTrace();
105 if (applet == null)
106 applet = new ErrorApplet(Messages.getString ("Main.ErrorApplet"));
108 return applet;
111 protected static boolean verbose;
114 * The main method starting the applet viewer.
116 * @param args the arguments given on the command line.
118 * @exception IOException if an error occurs.
120 public static void main(String[] args) throws IOException
122 parser = new ClasspathToolParser("appletviewer", true);
123 parser.setHeader(Messages.getString("Main.Usage"));
125 OptionGroup attributeGroup
126 = new OptionGroup(Messages.getString("Main.AppletTagOptions"));
128 attributeGroup.add(new Option("code",
129 Messages.getString("Main.CodeDescription"),
130 Messages.getString("Main.CodeArgument"))
132 public void parsed(String argument) throws OptionException
134 code = argument;
137 attributeGroup.add
138 (new Option("codebase",
139 Messages.getString("Main.CodebaseDescription"),
140 Messages.getString("Main.CodebaseArgument"))
142 public void parsed(String argument) throws OptionException
144 codebase = argument;
147 attributeGroup.add
148 (new Option("archive",
149 Messages.getString("Main.ArchiveDescription"),
150 Messages.getString("Main.ArchiveArgument"))
152 public void parsed(String argument) throws OptionException
154 archive = argument;
157 attributeGroup.add(new Option("width",
158 Messages.getString("Main.WidthDescription"),
159 Messages.getString("Main.WidthArgument"))
161 public void parsed(String argument) throws OptionException
163 dimensions.width = Integer.parseInt(argument);
166 attributeGroup.add(new Option("height",
167 Messages.getString("Main.HeightDescription"),
168 Messages.getString("Main.HeightArgument"))
170 public void parsed(String argument) throws OptionException
172 dimensions.height = Integer.parseInt(argument);
175 attributeGroup.add(new Option("param",
176 Messages.getString("Main.ParamDescription"),
177 Messages.getString("Main.ParamArgument"))
179 public void parsed(String argument) throws OptionException
181 parameters.add(argument);
184 OptionGroup pluginGroup
185 = new OptionGroup(Messages.getString("Main.PluginOption"));
186 pluginGroup.add(new Option("plugin",
187 Messages.getString("Main.PluginDescription"),
188 Messages.getString("Main.PluginArgument"))
190 public void parsed(String argument) throws OptionException
192 pluginMode = true;
193 int comma = argument.indexOf(',');
194 pipeInName = argument.substring(0, comma);
195 pipeOutName = argument.substring(comma + 1);
198 OptionGroup debuggingGroup
199 = new OptionGroup(Messages.getString("Main.DebuggingOption"));
200 debuggingGroup.add
201 (new Option("verbose",
202 Messages.getString("Main.VerboseDescription"),
203 (String) null)
205 public void parsed(String argument) throws OptionException
207 verbose = true;
210 OptionGroup compatibilityGroup
211 = new OptionGroup(Messages.getString("Main.CompatibilityOptions"));
212 compatibilityGroup.add
213 (new Option("debug",
214 Messages.getString("Main.DebugDescription"),
215 (String) null)
217 public void parsed(String argument) throws OptionException
219 // Currently ignored.
222 compatibilityGroup.add
223 (new Option("encoding",
224 Messages.getString("Main.EncodingDescription"),
225 Messages.getString("Main.EncodingArgument"))
227 public void parsed(String argument) throws OptionException
229 // FIXME: We should probably be using
230 // java.nio.charset.CharsetDecoder to handle the encoding. What
231 // is the status of Classpath's implementation?
234 parser.add(attributeGroup);
235 parser.add(pluginGroup);
236 parser.add(debuggingGroup);
237 parser.add(compatibilityGroup);
239 String[] urls = parser.parse(args);
241 // Print arguments.
242 printArguments(args);
244 args = urls;
246 if (dimensions.height < 0)
247 dimensions.height = 200;
249 if (dimensions.width < 0)
250 dimensions.width = (int) (1.6 * dimensions.height);
252 //System.setSecurityManager(new AppletSecurityManager(pluginMode));
254 if (pluginMode)
256 // Plugin will warn user about missing security manager.
257 InputStream in;
258 OutputStream out;
260 in = new FileInputStream(pipeInName);
261 out = new FileOutputStream(pipeOutName);
263 PluginAppletViewer.start(in, out);
265 else
267 // Warn user about missing security manager.
268 System.err.println(Messages.getString("Main.SecurityWarning") + "\n");
270 System.err.println(Messages.getString("Main.ContinuationPrompt"));
272 BufferedReader stdin
273 = new BufferedReader(new InputStreamReader(System.in));
274 String response = null;
278 response = stdin.readLine();
280 catch (IOException e)
282 throw new RuntimeException("Failed to read response"
283 + " to continuation prompt.", e);
286 if (!(response.equals("c") || response.equals("C")))
288 System.exit(0);
291 if (code == null)
293 // The --code option wasn't given and there are no URL
294 // arguments so we have nothing to work with.
295 if (args.length == 0)
297 System.err.println(Messages.getString("Main.NoInputFiles"));
298 System.exit(1);
300 // Create a standalone appletviewer from a list of URLs.
301 new StandaloneAppletViewer(args);
303 else
305 // Create a standalone appletviewer from the --code
306 // option.
307 new StandaloneAppletViewer(code, codebase, archive,
308 parameters, dimensions);
313 static void printArguments(String[] args)
315 if (verbose)
317 System.out.println(Messages.getString("Main.RawArguments"));
319 for (int i = 0; i < args.length; i++)
320 System.out.println(" " + args[i]);