import java.applet.*; import java.awt.*; import java.net.*; import java.io.*; import java.util.*; public class HTMLAppletViewer extends Frame implements AppletContext { Applet theApplet = null; AppletTag theTag = null; ParamAppletStub theStub = null; Panel appletPanel = new Panel(); TextField thePage = new TextField(40); Button loadButton = new Button("Load"); Button removeButton = new Button("Remove"); Button reloadButton = new Button("Reload"); Button restartButton = new Button("Restart"); Vector theApplets = new Vector(); TextField status = new TextField(33); URL documentBase = null; public static void main(String[] args) { HTMLAppletViewer hav = new HTMLAppletViewer(); System.setSecurityManager(new SimpleSecurityManager()); hav.show(); } public HTMLAppletViewer() { super("HTML Applet Viewer"); init(); } public void init() { Panel p1 = new Panel(); p1.setLayout(new GridLayout(3, 1)); Panel p0 = new Panel(); p0.setLayout(new FlowLayout(FlowLayout.LEFT)); p0.add(new Label("URL: ")); p0.add(thePage); p1.add(p0); Panel p2 = new Panel(); p2.add(loadButton); p2.add(removeButton); p2.add(reloadButton); p2.add(restartButton); p1.add(p2); Panel p4 = new Panel(); p4.setLayout(new FlowLayout(FlowLayout.LEFT)); p4.add(new Label("Status: ")); status.setEditable(false); p4.add(status); p1.add(p4); add("South", p1); appletPanel.setLayout(new BorderLayout()); add("Center", appletPanel); resize(400, 250); } public void readPage(String s) { // is the name a URL? try { documentBase = new URL(s); theTag = parsePage(documentBase.openStream()); if (theTag != null) { System.out.println(theTag); loadApplet(); } } catch(MalformedURLException e) { } catch(IOException e) { } } public AppletTag parsePage(InputStream in) throws IOException { String at = null; int c; while ((c = in.read()) != -1) { if (c != '<') continue; c = in.read(); if (c == ' ') continue; // just a < sign String tag = scanTag(c, in); if (tag.toUpperCase().startsWith("') break; } return sb.toString(); } public void loadApplet() { Class c = null; removeApplet(); URLClassLoader ucl; if (theTag != null) { try { if (theTag.codebase() != null) ucl = new URLClassLoader(theTag.codebase()); else ucl = new URLClassLoader(documentBase); c = ucl.loadClass(theTag.code()); theApplet = (Applet) c.newInstance(); theStub = new ParamAppletStub(this, theTag.params(), documentBase, theTag.codebase()); theApplet.setStub(theStub); theApplet.resize(theTag.height(), theTag.width()); appletPanel.add("Center", theApplet); theApplets.addElement(theApplet); theApplet.init(); theStub.activate(); theApplet.start(); validate(); } catch (Exception e) { showStatus(e.toString()); } } } public void removeApplet() { if (theApplet != null) { theApplet.stop(); theStub.deactivate(); theApplet.destroy(); theApplets.removeElement(theApplet); appletPanel.remove(theApplet); theApplet = null; validate(); repaint(); } } public void reload() { if (theTag != null) { loadApplet(); } } public void restart() { if (theApplet != null) { theApplet.stop(); theApplet.start(); } } public boolean action(Event e, Object what) { if (e.target == restartButton) { restart(); return true; } else if (e.target == reloadButton) { reload(); return true; } else if (e.target == loadButton) { readPage(thePage.getText()); return true; } else if (e.target == removeButton) { removeApplet(); return true; } else if (e.target == thePage) { loadApplet(); return true; } return false; } public Image getImage(URL u) { return Toolkit.getDefaultToolkit().getImage(u); } public AudioClip getAudioClip(URL u) { return new NullAudioClip(); } public Applet getApplet(String s) { return null; } public Enumeration getApplets() { return (Enumeration) theApplets; } public void showDocument(URL url) {}; public void showDocument(URL url, String target) {}; public void showStatus(String s) { status.setText(s); } }