import java.awt.*;import java.util.*; /** * An applet to parse text containing e-mail addresses * into a list of addresses * * * @author http://www.whatrain.com/ * @version beta 0.01 * @author Timothy M. Radonich * @author WhatRain Internet Services * @see ListNode.class * @see LinkedList.class * @see EAddressParser.class */ public class wrEmailParser extends java.applet.Applet { TextArea emailTextArea; Button buttonParse; Label whatRainLabel; Button buttonSelect; Label parserLabel; /** * set up GUI with lables, textarea, * Select All button and Find Addresses button * * * @param no parameters * @return no return value * @exception exceptions no exceptions thrown */ public void init() { setLayout( null ); // no layout manager setForeground( new Color( 0, 0, 0 )); // black setBackground( new Color( 255, 255, 255 )); // white addNotify(); resize( (insets().left + insets().right + 450), (insets().top + insets().bottom + 300) ); emailTextArea = new TextArea( "Use right mouse click or keyboard to \n paste text with e-mail addresses here" ); emailTextArea.setEditable( true ); // want user to be able paste and copy emailTextArea.setBackground( new Color( 255, 255, 255 )); // white add( emailTextArea ); emailTextArea.reshape( (insets().left + 35), (insets().top + 35), 380, 217 ); buttonParse = new Button( "Find Addresses" ); add( buttonParse ); buttonParse.reshape( (insets().left + 296), (insets().top + 263), 137, 25 ); buttonSelect = new Button( "Select All" ); add( buttonSelect ); buttonSelect.reshape( (insets().left + 17), (insets().top + 264), 137, 25 ); whatRainLabel = new Label( "WhatRain", Label.LEFT ); whatRainLabel.setFont( new Font( "Courier", Font.BOLD, 20 )); add( whatRainLabel ); whatRainLabel.reshape( (insets().left + 90), (insets().top + 0), 107, 24 ); parserLabel = new Label( "E-mail Address Parser", Label.LEFT ); parserLabel.setFont( new Font( "Dialog", Font.PLAIN, 10 )); add( parserLabel ); parserLabel.reshape( (insets().left + 197), (insets().top + 0), 162, 24 ); emailTextArea.selectAll(); emailTextArea.requestFocus(); } /** * find addresses in text * * * * @param Event event * @return no return value * @exception exceptions no exceptions thrown * @see ListNode.class * @see LinkedList.class * @see EAddressParser.class */ protected void ButtonParseClicked( Event event ) { String data = emailTextArea.getText(); // get text from textArea LinkedList tempList = new LinkedList(); // prepare linked list for address list EAddressParser parser = new EAddressParser(); // prepare eEAddress parser tempList = parser.parseLine(data); // parse data to list if(tempList != null) { tempList = tempList.cleanList(); // clean list tempList.removeDuplicates(); emailTextArea.setText(tempList.toStringLines() + " \n Address Count: " + tempList.getNumberOfNodes()); emailTextArea.requestFocus(); } else { emailTextArea.setText("No e-mail addresses found \n Use right mouse click or keyboard to \n paste text with e-mail addresses here" ); } } /** * * * * * @param Event event * @return no return value * @exception exceptions no exceptions thrown */ protected void ButtonSelectClicked( Event event ) { emailTextArea.selectAll(); emailTextArea.requestFocus(); } /** * * * * * @param Event event * @return boolean true if event handled * @exception exceptions no exceptions thrown */ public boolean handleEvent( Event event ) { if ((event.target == buttonParse) && (event.id == Event.ACTION_EVENT)) { ButtonParseClicked( event ); return true; } if ((event.target == buttonSelect) && (event.id == Event.ACTION_EVENT)) { ButtonSelectClicked( event ); return true; } return super.handleEvent( event ); } public void start() { } public void stop() { } } import java.io.*; /** * * * * @see ListNode.class * @author http://www.whatrain.com/ * @version beta 0.01 * @author Timothy M. Radonich * @author WhatRain Internet Services * @exception IOException thrown from printToFile () */ class LinkedList { private ListNode first, last; // link to the first and last node private int numberOfNodes; LinkedList() { first = last = null; numberOfNodes = 0; } /** * * * * * @param no parameters * @return String numberOfNodes * @exception exceptions no exceptions thrown */ String getNumberOfNodes() { Integer non = new Integer(numberOfNodes); return non.toString() ; } /** * * * * * @see ListNode.class * @param ListNode First * @return no return value * @exception exceptions no exceptions thrown */ void setFirst(ListNode First) { first = First ; } /** * * * * * @see ListNode.class * @param ListNode Last * @return no return value * @exception exceptions no exceptions thrown */ void setLast(ListNode Last) { last = Last ; } /** * * * * @see ListNode.class * @param no parameters * @return ListNode first * @exception exceptions no exceptions thrown */ ListNode getFirst() { return first ; } /** * * * * @see ListNode.class * @param no parameters * @return ListNode last * @exception exceptions no exceptions thrown */ ListNode getLast() { return last ; } /** * * * * * @see ListNode.class * @param ListNode newNode * @return no return value * @exception exceptions no exceptions thrown */ void append(ListNode newNode) { newNode.setNextNull(); if (first == null) // This is the first entry first = newNode; if (last != null) last.setNextNode(newNode); setLast(newNode); numberOfNodes++; } /** * * * * * @see ListNode.class * @param LinkedList list * @return no return value * @exception exceptions no exceptions thrown */ void append(LinkedList list) { if (first == null) // This is the first entry first = list.first; if (last != null) last.setNextNode ( list.first); setLast ( list.last); numberOfNodes = numberOfNodes + list.numberOfNodes; } /** * * * * * @see ListNode.class * @param String newNodeValue * @return no return value * @exception exceptions no exceptions thrown */ void append(String newNodeValue) { ListNode newNode = new ListNode(newNodeValue); append(newNode); } /** * * * * * @see ListNode.class * @param no parameters * @return no return value * @exception exceptions no exceptions thrown */ void printList() { int i = 0; ListNode tempNode = first; while (tempNode != null) { tempNode.printNode(); i++; tempNode = tempNode.getNextNode(); // Get the next node } } /** * * * * * @see ListNode.class * @param no parameters * @return String * @exception exceptions no exceptions thrown */ String toStringLines() { int i = 0; String tempString = new String(" \n"); ListNode tempNode = first; while (tempNode != null) { tempString = tempString + tempNode.getNodeValue() + "\n"; i++; tempNode = tempNode.getNextNode(); // Get the next node } return tempString ; } /** * * * * * @see ListNode.class * @param no parameters * @return no return * @exception IOException thrown */ void printToFile() { try { PrintStream output = new PrintStream( new BufferedOutputStream( new FileOutputStream("test.tst"))); int i = 0; ListNode tempNode = first; while (tempNode != null) { output.println(tempNode.getNodeValue()); i++; tempNode = tempNode.getNextNode(); // Get the next node } output.close(); } catch (IOException e) { System.out.println("File error: " + e); } } /** * * * * * @see ListNode.class * @param no parameters * @return LinkedList list * @exception exceptions no exceptions thrown */ LinkedList cleanList() { ListNode node = first; LinkedList list = new LinkedList(); while (node != null) { LinkedList tempList = new LinkedList(); EAddressParser parser = new EAddressParser(); tempList = parser.parseLine(node.getNodeValue()); if (tempList != null)list.append(tempList); node = node.getNextNode(); // Get the next node } return list ; } /** * * * * * @see ListNode.class * @param no parameters * @return no return value * @exception exceptions no exceptions thrown */ void removeDuplicates() { ListNode lookForNode = first; LinkedList list = new LinkedList(); while (lookForNode != null) { ListNode checkThisNode = lookForNode.getNextNode(); ListNode pastNode = lookForNode; while (checkThisNode != null) { if(lookForNode.getNodeValue().equals(checkThisNode.getNodeValue())) { pastNode.setNextNode(checkThisNode.getNextNode());// removes checkThisNode numberOfNodes-- ; checkThisNode = checkThisNode.getNextNode(); // Get the next node do not advance pastNode } else { checkThisNode = checkThisNode.getNextNode(); // advance checkThisNode pastNode = pastNode.getNextNode(); //advance pastNode } } lookForNode = lookForNode.getNextNode(); // advance lookForNode } } } // place the following in a seperate file import java.util.StringTokenizer; class EAddressParser { public LinkedList parseLine(String string1) { StringTokenizer stringTokenizer = new StringTokenizer(string1); LinkedList linkedList = new LinkedList(); while (stringTokenizer.hasMoreTokens()) { String string2 = stringTokenizer.nextToken(); if (couldBeAddress(string2)) { String string3 = new String(cleanAddress(string2)); ListNode listNode = new ListNode(string3); linkedList.append(listNode); } } if (linkedList.getFirst() == null) return null; else return linkedList; } boolean couldBeAddress(String string) { if (string.indexOf(64) > 0) return true; else return false; } String cleanAddress(String string1) { String string2 = new String(string1); String string3 = new String(",<>;:[]'\"{}\\+=()!#$%^&*~`?/"); char ach1[] = string3.toCharArray(); char ach2[] = " ".toCharArray(); for (int i = 0; i < ach1.length; i++) string2 = string2.replace(ach1[i], ach2[0]); if (string2.indexOf(64) != string2.lastIndexOf(64)) string2 = string2.substring(0, string2.lastIndexOf(64)); string2 = string2.trim(); return string2; } EAddressParser() { } }