1 |
779 |
jeremybenn |
/* PluginAppletViewer.java -- manages embeddable applet windows
|
2 |
|
|
Copyright (C) 2003, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
package gnu.classpath.tools.appletviewer;
|
39 |
|
|
|
40 |
|
|
import java.io.BufferedReader;
|
41 |
|
|
import java.io.BufferedWriter;
|
42 |
|
|
import java.io.IOException;
|
43 |
|
|
import java.io.InputStream;
|
44 |
|
|
import java.io.InputStreamReader;
|
45 |
|
|
import java.io.OutputStream;
|
46 |
|
|
import java.io.OutputStreamWriter;
|
47 |
|
|
import java.net.MalformedURLException;
|
48 |
|
|
import java.nio.charset.Charset;
|
49 |
|
|
import java.util.HashMap;
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* PluginAppletViewer communicates through pipes with a web browser
|
54 |
|
|
* plugin. A PluginAppletViewer manages applet windows that may be
|
55 |
|
|
* embedded into web pages.
|
56 |
|
|
*/
|
57 |
|
|
class PluginAppletViewer
|
58 |
|
|
{
|
59 |
|
|
// A mapping of instance IDs to PluginAppletWindows.
|
60 |
|
|
static HashMap appletWindows = new HashMap ();
|
61 |
|
|
|
62 |
|
|
private static BufferedReader pluginInputStream;
|
63 |
|
|
private static BufferedWriter pluginOutputStream;
|
64 |
|
|
|
65 |
|
|
static void start(InputStream inputStream, OutputStream outputStream)
|
66 |
|
|
throws MalformedURLException, IOException
|
67 |
|
|
{
|
68 |
|
|
// Set up input and output pipes. Use UTF-8 encoding.
|
69 |
|
|
pluginInputStream =
|
70 |
|
|
new BufferedReader(new InputStreamReader(inputStream,
|
71 |
|
|
Charset.forName("UTF-8")));
|
72 |
|
|
pluginOutputStream =
|
73 |
|
|
new BufferedWriter(new OutputStreamWriter(outputStream,
|
74 |
|
|
Charset.forName("UTF-8")));
|
75 |
|
|
|
76 |
|
|
write("running");
|
77 |
|
|
|
78 |
|
|
// Read first message.
|
79 |
|
|
String message = read();
|
80 |
|
|
|
81 |
|
|
PluginAppletWindow currentWindow = null;
|
82 |
|
|
|
83 |
|
|
while (true)
|
84 |
|
|
{
|
85 |
|
|
if (message.startsWith("instance"))
|
86 |
|
|
{
|
87 |
|
|
// Read applet instance identifier.
|
88 |
|
|
String key = message.substring(9);
|
89 |
|
|
|
90 |
|
|
if (appletWindows.get(key) == null)
|
91 |
|
|
appletWindows.put(key, new PluginAppletWindow());
|
92 |
|
|
|
93 |
|
|
currentWindow = (PluginAppletWindow) appletWindows.get(key);
|
94 |
|
|
}
|
95 |
|
|
else if (message.startsWith("tag"))
|
96 |
|
|
{
|
97 |
|
|
int pos = message.indexOf(' ', 4);
|
98 |
|
|
String documentbase = message.substring(4, pos);
|
99 |
|
|
String tag = message.substring(pos + 1);
|
100 |
|
|
currentWindow.setParser(tag, documentbase);
|
101 |
|
|
}
|
102 |
|
|
else if (message.startsWith("handle"))
|
103 |
|
|
{
|
104 |
|
|
long handle = Long.parseLong(message.substring(7));
|
105 |
|
|
|
106 |
|
|
currentWindow.setHandle(handle);
|
107 |
|
|
}
|
108 |
|
|
else if (message.startsWith("width"))
|
109 |
|
|
{
|
110 |
|
|
int width = Integer.parseInt(message.substring(6));
|
111 |
|
|
|
112 |
|
|
currentWindow.setSize(width, currentWindow.getHeight());
|
113 |
|
|
}
|
114 |
|
|
else if (message.startsWith("height"))
|
115 |
|
|
{
|
116 |
|
|
int height = Integer.parseInt(message.substring(7));
|
117 |
|
|
|
118 |
|
|
currentWindow.setSize(currentWindow.getWidth(), height);
|
119 |
|
|
}
|
120 |
|
|
else if (message.startsWith("destroy"))
|
121 |
|
|
{
|
122 |
|
|
appletWindows.remove(currentWindow);
|
123 |
|
|
currentWindow.dispose();
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
// Read next message.
|
127 |
|
|
message = read();
|
128 |
|
|
}
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
/**
|
132 |
|
|
* Write string to plugin.
|
133 |
|
|
*
|
134 |
|
|
* @param message the message to write
|
135 |
|
|
*
|
136 |
|
|
* @exception IOException if an error occurs
|
137 |
|
|
*/
|
138 |
|
|
static void write(String message) throws IOException
|
139 |
|
|
{
|
140 |
|
|
pluginOutputStream.write(message, 0, message.length());
|
141 |
|
|
pluginOutputStream.newLine();
|
142 |
|
|
pluginOutputStream.flush();
|
143 |
|
|
|
144 |
|
|
System.err.println
|
145 |
|
|
(" " + Messages.getString("PluginAppletViewer.AppletViewerWrote")
|
146 |
|
|
+ message);
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/**
|
150 |
|
|
* Read string from plugin.
|
151 |
|
|
*
|
152 |
|
|
* @return the read string
|
153 |
|
|
*
|
154 |
|
|
* @exception IOException if an error occurs
|
155 |
|
|
*/
|
156 |
|
|
static String read() throws IOException
|
157 |
|
|
{
|
158 |
|
|
String message = pluginInputStream.readLine();
|
159 |
|
|
|
160 |
|
|
System.err.println
|
161 |
|
|
(" " + Messages.getString("PluginAppletViewer.AppletViewerRead")
|
162 |
|
|
+ message);
|
163 |
|
|
|
164 |
|
|
if (message == null || message.equals("shutdown"))
|
165 |
|
|
{
|
166 |
|
|
// Close input/output channels to plugin.
|
167 |
|
|
pluginInputStream.close();
|
168 |
|
|
pluginOutputStream.close();
|
169 |
|
|
|
170 |
|
|
System.err.println
|
171 |
|
|
(Messages.getString("PluginAppletViewer.AppletViewerExiting"));
|
172 |
|
|
|
173 |
|
|
System.exit(0);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
return message;
|
177 |
|
|
}
|
178 |
|
|
}
|