Monday, July 20, 2009

Java HTML writer

import java.io.*;import java.net.*;
import java.applet.Applet;import netscape.javascript.JSObject;
public class HTMLWriter extends Writer

{
JSObject main_window;
JSObject window;
JSObject document;
static int window_num = 0;
public HTMLWriter(Applet applet, int width, int height)

{
try { Class c = Class.forName("javascript.JSObject");

}
catch (ClassNotFoundException e)
{
throw new NoClassDefFoundError("error");
}
main_window = JSObject.getWindow(applet);
window = (JSObject) main_window.eval("self.open(''," +"'HTMLWriter" + window_num++ + "'," + "'menubar,status,resizable,scrollbars," +"width=" + width + ",height=" + height + "')");
document = (JSObject) window.getMember("document");

document.call("open", null);
}
public void write(char[] buf, int offset, int length)

{
if ((window == null) (document == null)) return;
if (((Boolean)window.getMember("closed")).booleanValue()) return;
String s = new String(buf, offset, length);
document.call("write", new String[] { s });

}
public void flush() {}
public void close() { document.call("close", null); document = null;

}
public void closeWindow()

{
if (document != null) close();
if (!((Boolean)window.getMember("closed")).booleanValue()) window.call("close", null); window = null;
}
public void finalize()

{
closeWindow();
}
public static class Test extends Applet

{
HTMLWriter out;
public void init()

{
try
{
URL url = new URL(this.getDocumentBase(), this.getParameter("url"));
Reader in = new InputStreamReader(url.openStream());
out = new HTMLWriter(this, 400, 200);
char[] buffer = new char[4096];

int numchars;
while((numchars = in.read(buffer)) != -1) out.write(buffer, 0, numchars);
in.close();

out.close();
}
catch (IOException e)
{}
}
public void destroy()

{ out.closeWindow();
}
}
}

9 comments:

  1. Cannot understand heheh not an expert in computer hehehe but Im here also brother

    ReplyDelete
  2. Thanks for the added, nice blog, sheer!!!

    ReplyDelete
  3. this code is quite common and many other sites like java2s,sun java tutorial already have this.You can write on some java interview questions and solutions if you wish.

    ReplyDelete
  4. It would be better if you give the purpose of the java code in the heading. and then give codes to help bloggers use them in their blogs and sites. Also will be appreciated if at the bottom you explain a little what the code is meant for. and how/where to insert it to obtain what results.

    ReplyDelete
  5. I agree with whisper, you should add a little bit review about these java codes purposes.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. I'm jahtenan (xp problem solution blog)

    ReplyDelete