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)
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
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
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
;
55 import java
.util
.ArrayList
;
56 import java
.util
.HashMap
;
57 import java
.util
.List
;
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
);
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
)
90 ClassLoader loader
= getClassLoader(tag
.prependCodeBase(""),
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();
106 applet
= new ErrorApplet(Messages
.getString ("Main.ErrorApplet"));
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
138 (new Option("codebase",
139 Messages
.getString("Main.CodebaseDescription"),
140 Messages
.getString("Main.CodebaseArgument"))
142 public void parsed(String argument
) throws OptionException
148 (new Option("archive",
149 Messages
.getString("Main.ArchiveDescription"),
150 Messages
.getString("Main.ArchiveArgument"))
152 public void parsed(String argument
) throws OptionException
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
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"));
201 (new Option("verbose",
202 Messages
.getString("Main.VerboseDescription"),
205 public void parsed(String argument
) throws OptionException
210 OptionGroup compatibilityGroup
211 = new OptionGroup(Messages
.getString("Main.CompatibilityOptions"));
212 compatibilityGroup
.add
214 Messages
.getString("Main.DebugDescription"),
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
);
242 printArguments(args
);
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));
256 // Plugin will warn user about missing security manager.
260 in
= new FileInputStream(pipeInName
);
261 out
= new FileOutputStream(pipeOutName
);
263 PluginAppletViewer
.start(in
, out
);
267 // Warn user about missing security manager.
268 System
.err
.println(Messages
.getString("Main.SecurityWarning") + "\n");
270 System
.err
.println(Messages
.getString("Main.ContinuationPrompt"));
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")))
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"));
300 // Create a standalone appletviewer from a list of URLs.
301 new StandaloneAppletViewer(args
);
305 // Create a standalone appletviewer from the --code
307 new StandaloneAppletViewer(code
, codebase
, archive
,
308 parameters
, dimensions
);
313 static void printArguments(String
[] args
)
317 System
.out
.println(Messages
.getString("Main.RawArguments"));
319 for (int i
= 0; i
< args
.length
; i
++)
320 System
.out
.println(" " + args
[i
]);