import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;



/**
   Automatic Email Munger.
   Mungs every character of an email address into its HTML entity.
   @author Daniele Raffo
   @version 0.3 11DEC2003

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
public class AEM extends Applet implements ActionListener {


    public final String INFO = "Automatic Email Munger v0.3 - Copyright 2000-2003 Daniele Raffo\nPlease enter your email address in the field and click on the button Mung";    
    private final static String ASCII_CODE = new String(" !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
    
    TextField query;
    Button button;
    Checkbox addlink;
    TextArea result;



    public void init() {
	
	query = new TextField();
	button = new Button("Mung");
	addlink = new Checkbox("Link", true);
	result = new TextArea(INFO);
	query.addActionListener(this);
	button.addActionListener(this);
	result.setEditable(false);
	
	int width = Integer.parseInt(getParameter("width"));
	int height = Integer.parseInt(getParameter("height"));
	int gap = 2;
	int tf = 30;
	
	query.setBounds(gap, gap, 
			(width * 5)/10, tf);
	button.setBounds((width * 5)/10 + gap * 2, gap, 
			 (width * 3)/10, tf);
	addlink.setBounds((width * 8)/10 + gap * 6, gap,
			  (width * 2)/10 - gap * 7, tf);
	result.setBounds(gap, tf + gap * 2, 
			 width - gap * 2, height - tf - gap * 3); 
	
	setLayout(null);
	add(query);
	add(button);
	add(addlink);
	add(result);
	validate();
	
    }       
    
    
    
    public void actionPerformed(ActionEvent e) {
	
	String email = query.getText();
	result.setText("");
	String munged = new String(toEntity(email));

	if (addlink.getState()) {
	    addItem("<A HREF=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;\n");
	    addItem(munged + "\">\n");
	    addItem(munged + "</A>");
	}
	else {
	    addItem(munged);
	}
	
    }


    
    private void addItem(String newWord) {
	
	String t = result.getText();
	result.setText(t + newWord);
	
    }


    
    public static String toEntity(String source) {
	
	int n, length = source.length();
	StringBuffer dest = new StringBuffer();
	
	for (int i = 0; i < length; i++) {
	    n = ASCII_CODE.indexOf(source.charAt(i));
	    if (n == -1)
		dest.append(source.charAt(i));
	    else {
		Integer entity = new Integer(n + 32);
		dest.append("&#" + entity.toString() + ";");
	    }
	}
	
	return dest.toString();
	
    }
    
}









