41 |
import gnu.getopt.LongOpt; |
import gnu.getopt.LongOpt; |
42 |
import java.io.*; |
import java.io.*; |
43 |
import java.applet.Applet; |
import java.applet.Applet; |
44 |
|
import java.awt.Dimension; |
45 |
import java.awt.BorderLayout; |
import java.awt.BorderLayout; |
46 |
import java.awt.Frame; |
import java.awt.Frame; |
47 |
import java.util.Map; |
import java.util.Map; |
100 |
else |
else |
101 |
loader = new URLClassLoader(new URL [] { tag.prependCodebase ("") }); |
loader = new URLClassLoader(new URL [] { tag.prependCodebase ("") }); |
102 |
|
|
|
/* Set the contect loader of the current thread to the applet class loader, |
|
|
* to avoid problems between casting classes loaded by separate class loaders. |
|
|
*/ |
|
103 |
Thread.currentThread().setContextClassLoader(loader); |
Thread.currentThread().setContextClassLoader(loader); |
104 |
|
|
105 |
if (code.endsWith( ".class")) |
if (code.endsWith( ".class")) |
106 |
{ |
code = code.substring(0, code.length() - 6); |
|
code = code.substring(0, code.length() - 6); |
|
|
} |
|
107 |
|
|
108 |
Class c = loader.loadClass(code); |
Class c = loader.loadClass(code); |
109 |
applet = (Applet) c.newInstance(); |
applet = (Applet) c.newInstance(); |
|
|
|
110 |
} |
} |
111 |
catch (Exception e) |
catch (Exception e) |
112 |
{ |
{ |
115 |
return applet; |
return applet; |
116 |
} |
} |
117 |
|
|
118 |
|
static void printHelpMessage (int status) |
119 |
|
{ |
120 |
|
System.out.println ("Usage: gcjappletviewer [options] <filename>.class | <filename>.html... | url..."); |
121 |
|
System.out.println ("Options:"); |
122 |
|
System.out.println (" --help Display this message"); |
123 |
|
System.out.println (" --archive=<filename>.jar[,...] Add archives to applet class loader"); |
124 |
|
System.out.println (" --param=<name>,<value> Pass parameter to applet"); |
125 |
|
System.out.println (" --width=<width> Set width of applet viewer frame"); |
126 |
|
System.out.println (" --height=<height> Set height of applet viewer frame"); |
127 |
|
System.out.println (" --plugin Enable plugin mode"); |
128 |
|
System.exit (status); |
129 |
|
} |
130 |
|
|
131 |
public static void main (String[] args) |
public static void main (String[] args) |
132 |
throws IOException |
throws IOException |
133 |
{ |
{ |
134 |
|
if (args.length == 0) |
135 |
|
printHelpMessage (1); |
136 |
|
|
137 |
LongOpt[] longOptions = new LongOpt[] |
LongOpt[] longOptions = new LongOpt[] |
138 |
{ |
{ |
139 |
new LongOpt ("code", LongOpt.REQUIRED_ARGUMENT, null, 0), |
new LongOpt ("archive", LongOpt.REQUIRED_ARGUMENT, null, 0), |
140 |
new LongOpt ("codebase", LongOpt.REQUIRED_ARGUMENT, null, 0), |
new LongOpt ("param", LongOpt.REQUIRED_ARGUMENT, null, 0), |
141 |
new LongOpt ("archive", LongOpt.REQUIRED_ARGUMENT, null, 0), |
new LongOpt ("width", LongOpt.REQUIRED_ARGUMENT, null, 0), |
142 |
new LongOpt ("documentbase", LongOpt.REQUIRED_ARGUMENT, null, 0), |
new LongOpt ("height", LongOpt.REQUIRED_ARGUMENT, null, 0), |
143 |
new LongOpt ("parameters", LongOpt.REQUIRED_ARGUMENT, null, 0), |
new LongOpt ("plugin", LongOpt.NO_ARGUMENT, null, 0), |
144 |
new LongOpt ("width", LongOpt.REQUIRED_ARGUMENT, null, 0), |
new LongOpt ("help", LongOpt.NO_ARGUMENT, null, 0), |
|
new LongOpt ("height", LongOpt.REQUIRED_ARGUMENT, null, 0), |
|
145 |
}; |
}; |
146 |
|
|
147 |
Getopt opts = new Getopt("appletviewer", args, "", longOptions); |
Getopt opts = new Getopt("gcjappletviewer", args, "", longOptions); |
148 |
|
|
149 |
// Parse command line, placing all option arguments at the start |
// Parse command line, placing all option arguments at the start |
150 |
// of args, all non-options at the end, and setting optind to the |
// of args, all non-options at the end, and setting optind to the |
151 |
// index of the first non-option argument. |
// index of the first non-option argument. |
152 |
while (opts.getopt() != -1); |
int optionValue; |
153 |
|
int optionIndex; |
154 |
|
|
155 |
// optind now points to the first non-option argument. |
String archives = null; |
156 |
System.out.println ("raw arguments:"); |
List parameters = null; |
157 |
for (int i = 0; i < args.length; i++) |
Dimension dimensions = new Dimension (-1, -1); |
158 |
|
|
159 |
|
boolean widthGiven = false; |
160 |
|
boolean heightGiven = false; |
161 |
|
while ((optionValue = opts.getopt()) != -1) |
162 |
{ |
{ |
163 |
System.out.println (" " + args[i]); |
if (optionValue == 0) |
164 |
|
{ |
165 |
|
optionIndex = opts.getLongind(); |
166 |
|
if (optionIndex == 0) |
167 |
|
archives = opts.getOptarg(); |
168 |
|
else if (optionIndex == 1) |
169 |
|
{ |
170 |
|
if (parameters == null) |
171 |
|
parameters = new ArrayList (); |
172 |
|
parameters.add (opts.getOptarg()); |
173 |
|
} |
174 |
|
else if (optionIndex == 2) |
175 |
|
{ |
176 |
|
widthGiven = true; |
177 |
|
dimensions.width = |
178 |
|
Math.max (0, Integer.parseInt(opts.getOptarg())); |
179 |
|
} |
180 |
|
else if (optionIndex == 3) |
181 |
|
{ |
182 |
|
heightGiven = true; |
183 |
|
dimensions.height = |
184 |
|
Math.max (0, Integer.parseInt(opts.getOptarg())); |
185 |
|
} |
186 |
|
// FIXME: Implement 4. |
187 |
|
else if (optionIndex == 5) |
188 |
|
printHelpMessage (0); |
189 |
|
} |
190 |
} |
} |
191 |
|
|
192 |
// Handle each file specified on the command line. |
if (dimensions.height < 0) |
193 |
|
dimensions.height = 200; |
194 |
|
|
195 |
|
if (dimensions.width < 0) |
196 |
|
dimensions.width = (int) (1.6 * dimensions.height); |
197 |
|
|
198 |
|
System.out.println (dimensions); |
199 |
|
|
200 |
|
boolean classFileGiven = false; |
201 |
for (int i = opts.getOptind (); i < args.length; i++) |
for (int i = opts.getOptind (); i < args.length; i++) |
202 |
{ |
{ |
203 |
appletTags.addAll (AppletTag.parseAppletTags (args[i])); |
if (args[i].endsWith(".class")) |
204 |
|
{ |
205 |
|
classFileGiven = true; |
206 |
|
|
207 |
|
String[] paramArray = null; |
208 |
|
|
209 |
|
if (parameters != null) |
210 |
|
paramArray = |
211 |
|
(String []) parameters.toArray (new String[] { "" }); |
212 |
|
|
213 |
|
appletTags.add (new AppletTag (args[i], |
214 |
|
archives, |
215 |
|
paramArray, |
216 |
|
dimensions)); |
217 |
|
break; |
218 |
|
} |
219 |
} |
} |
220 |
|
|
221 |
|
// optind now points to the first non-option argument. |
222 |
|
System.out.println ("raw arguments:"); |
223 |
|
for (int i = 0; i < args.length; i++) |
224 |
|
System.out.println (" " + args[i]); |
225 |
|
|
226 |
|
// Handle each file specified on the command line. |
227 |
|
for (int i = opts.getOptind (); i < args.length; i++) |
228 |
|
appletTags.addAll (AppletTag.parseAppletTags (args[i])); |
229 |
|
|
230 |
System.out.println ("parsed applet tags:"); |
System.out.println ("parsed applet tags:"); |
231 |
for (int i = 0; i < appletTags.size(); i++) |
for (int i = 0; i < appletTags.size(); i++) |
232 |
{ |
{ |
234 |
System.out.println ((AppletTag) appletTags.get (i)); |
System.out.println ((AppletTag) appletTags.get (i)); |
235 |
} |
} |
236 |
|
|
237 |
/* |
for (int i = 0; i < appletTags.size(); i++) |
|
int optVal; |
|
|
String arg; |
|
|
int longIndex; |
|
|
|
|
|
Getopt opts = new Getopt("gcjappletviewer", args, "", longOptions); |
|
|
|
|
|
while ((optVal = opts.getopt()) != -1) |
|
238 |
{ |
{ |
239 |
if (optVal == 0) |
AppletTag tag = (AppletTag) appletTags.get (i); |
240 |
|
// If no class file was given on the command line then the |
241 |
|
// given width and height options override the width and |
242 |
|
// height elements of the applet tag. |
243 |
|
if (!classFileGiven) |
244 |
{ |
{ |
245 |
longIndex = opts.getLongind(); |
if (widthGiven) |
246 |
switch (longIndex) |
tag.dimensions.width = dimensions.width; |
|
{ |
|
|
case 0: |
|
|
if (!parseHTML) |
|
|
tag.code = opts.getOptarg(); |
|
|
break; |
|
|
case 1: |
|
|
if (!parseHTML) |
|
|
tag.codebase = opts.getOptarg(); |
|
|
break; |
|
|
case 2: |
|
|
if (!parseHTML) |
|
|
tag.archives = opts.getOptarg(); |
|
|
break; |
|
|
case 3: |
|
|
// FIXME: documentbase |
|
|
break; |
|
|
case 4: |
|
|
// FIXME: Handle multiple parameters. |
|
|
// tag.parameters = opts.getOptarg(); |
|
|
break; |
|
|
case 5: |
|
|
tag.dimensions.width = Integer.parseInt(opts.getOptarg()); |
|
|
break; |
|
|
case 6: |
|
|
tag.dimensions.height = Integer.parseInt(opts.getOptarg()); |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
247 |
|
|
248 |
System.err.println ("AppletViewer:\n" + |
if (heightGiven) |
249 |
"code: " + tag.code + "\n" + |
tag.dimensions.height = dimensions.height; |
250 |
"codebase: " + tag.codebase + "\n" + |
} |
|
"archive: " + tag.archives + "\n" + |
|
|
"parameters: " + tag.parameters + "\n" + |
|
|
"width: " + tag.dimensions.width + "\n" + |
|
|
"height: " + tag.dimensions.height); |
|
|
*/ |
|
251 |
|
|
252 |
for (int i = 0; i < appletTags.size(); i++) |
AppletViewer viewer = new AppletViewer (tag); |
|
{ |
|
|
AppletViewer viewer = new AppletViewer ((AppletTag) appletTags.get (i)); |
|
253 |
} |
} |
254 |
} |
} |
255 |
} |
} |