import java.awt.*; import java.lang.*; import java.applet.*; import java.util.*; import java.net.*; import java.io.*; public class Ghtml extends Applet { static Socket socket; static InputStream in; static OutputStream out; static DataInputStream din; static DataOutputStream dout; Button button = null; String rc = null; protected Vector entries = null; protected Vector header = null; protected File file = null; protected String name = null; protected String e_mail = null; protected String entry = null; protected String date = null; public void loadbook(String name) { File f = new File(name); if(f.exists()&&f.canRead()) readFromFile(f); else System.out.println("This file doesn't exist."); } public void init() { setBackground(Color.lightGray); setForeground(Color.blue); Font font = new Font("TimesRoman",Font.PLAIN,20); setFont(font); setLayout(new BorderLayout()); Panel p1=new Panel(); p1.add( button=new Button("Genernate")); add("North",p1); } public boolean action(Event evt,Object what) { if (what.equals("Genernate")) // button pressed { loadbook("guestdata"); } else return false; return true; } public boolean fromHTML(String line) { int index = line.indexOf(""); if(index<0) return false; line = line.substring(index+4); index = line.indexOf(""); if(index<0) return false; name = line.substring(0,index).trim(); line = line.substring(index+4); index = line.indexOf(""); if(index<0) return false; e_mail = line.substring(0,index).trim(); line = line.substring(index+4); index = line.indexOf(""); if(index<0) return false; entry = line.substring(0,index).trim(); date = line.substring(index+4).trim(); return true; } public void readFromFile(File f) { file = f; header = new Vector(5); FileInputStream fin = null; FileOutputStream fout = null; try { fin = new FileInputStream(f);} catch(Exception fe) { System.out.println("caught exception "+fe.getMessage()); header = null; entries = null; return; } try { fout = new FileOutputStream("ggg.html"); } catch(IOException ioe) { System.out.println("warning: couldn't save guestbook"); return; } DataOutputStream dout = new DataOutputStream(fout); try { dout.writeBytes("GuestBook\n\n\n

Tianchu Yang's GuestBook

Name : E_mail : URL address : Date :\n"); dout.writeBytes("
Tianchu Yange96tiaya@und.ida.liu.seWeclome to mark my Guestbook20.6'97\n"); } catch(IOException ioe2) { System.out.println("error while saving guestbook :"); try {dout.close(); fout.close();} catch(Exception ce) {} return; } DataInputStream din = new DataInputStream(fin); String line = null; boolean cont = true; while(cont) { try { line = din.readLine();} catch(Exception e) { System.out.println("caught exception "+e.getMessage()); try { din.close(); fin.close();} catch(Exception ce) {} header = null; entries = null; return; } if((line!=null)&&(!line.startsWith("
"))) header.addElement(line+"\n"); else cont = false; } while(line!=null) { if(line.startsWith("
")) break; else { GuestBookEntry entry = new GuestBookEntry(); if(entry.fromHTML(line)) { System.out.println("That is OK"); } else System.err.println("error parsing line "+line); } try { line = din.readLine(); String line1 = fromhtml(line); System.out.println(line1); dout.writeBytes(line1); } catch(Exception e2) { System.err.println("caught execption "+e2.getMessage()); try{ din.close(); fin.close();} catch(Exception ce2){} header = null; entries = null; return; } } try { dout.writeBytes("





"+"Come back to my homepage\n
"); dout.writeBytes(""); dout.flush(); } catch(Exception ce3){} try{ din.close(); fin.close();} catch(Exception ce31){} try{ dout.close(); fout.close();} catch(Exception ce32){} } public String fromhtml(String line) { int index = line.indexOf(""); if(index<0) return ""; line = line.substring(index+4); index = line.indexOf(""); if(index<0) return ""; name = line.substring(0,index).trim(); line = line.substring(index+4); index = line.indexOf(""); if(index<0) return ""; e_mail = line.substring(0,index).trim(); line = line.substring(index+4); index = line.indexOf(""); if(index<0) return ""; entry = line.substring(0,index).trim(); date = line.substring(index+4).trim(); String rc = ""+name+""+e_mail+""+entry+""+date+"\n"; return rc; } }