|
Applet to HTML Driving me bonkers
Hi, I was wondering if you guys could help me with the following problem:
I have an applet that I know runs in the Java Applet Viewer. The applet is a find and replace utility for a specific HTML file I used to have to manually update at work.
I want to have it run via browser. Currently I have to open it up in Eclipse and run it as an applet.
I cannot get it to work. I have tried loads of things:
- downloaded SDK
- tried HTMLconverter
- tried compiling it thru eclipse. This gave me a single class file
- tried compiling it thru cmd line (using javac.exe utility in SDK). This produced 5 separate class files
- tried putting it in a .jar file and updating the main-class document
- tried like a billion different ways of using the applet tag
I finally am sending out my call for help. All I keep getting is a box with a red "x" where the applet is supposed to be (in the browser). I have been able to run other people's applets so i know it's possible.
Thank you very much - any help is greatly appreciated!
Here's the HTML I used:
<html>
<body>
<applet
code="WebStatConverter.class"
width="500"
height="500">
</applet>
</body>
</html>
And here's my Java source code:
//Import Necessary Java Classes
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
//Main Clas and Init() method
public class WebStatConverter extends JApplet implements ActionListener{
//Graphic Display elements
Button okButton;
Button openBrowse;
Button saveBrowse;
TextField openBrowsePath;
TextField saveBrowsePath;
TextField monthField;
TextField yearField;
//Class variables
Convert converter = new Convert();
LinkedList<String> list;
JFileChooser browse = new JFileChooser();
//User Entry Variables
String openPath;
String savePath;
String month;
String year;
public void init(){
setLayout(null);
okButton = new Button("CONVERT!");
openBrowse = new Button("OPEN");
saveBrowse = new Button("SAVE");
openBrowsePath = new TextField(openPath, 300);
saveBrowsePath = new TextField(savePath, 300);
monthField = new TextField(null,100);
yearField = new TextField(null,100);
openBrowse.setBounds(50, 50, 100, 30);
saveBrowse.setBounds(50, 100, 100, 30);
openBrowsePath.setBounds(200, 50, 300, 30);
saveBrowsePath.setBounds(200, 100, 300, 30);
monthField.setBounds(50, 200, 100, 30);
yearField.setBounds(50, 300, 100, 30);
okButton.setBounds(50, 350, 100, 30);
add(okButton);
add(openBrowse);
add(saveBrowse);
add(openBrowsePath);
add(saveBrowsePath);
add(monthField);
add(yearField);
okButton.addActionListener(this);
openBrowse.addActionListener(this);
saveBrowse.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
//Sets path for file to open
if (event.getSource() == openBrowse){
int openVar = browse.showOpenDialog(browse);
if (openVar == JFileChooser.APPROVE_OPTION){
openPath = browse.getSelectedFile().getPath();
openBrowsePath.setText(openPath);
repaint();
}
}
//Sets path for file to save to
else if (event.getSource() == saveBrowse){
int saveVar = browse.showSaveDialog(browse);
if (saveVar == JFileChooser.APPROVE_OPTION){
savePath = browse.getSelectedFile().getPath();
saveBrowsePath.setText(savePath);
repaint();
}
}
//Sets it all in motion
else if (event.getSource() == okButton){
month = monthField.getText();
year = yearField.getText();
System.out.println(openPath + "," + savePath + "," + month + "," + year);
if(openPath != null && savePath != null && month != null && year != null){
//read in file
list = Convert.readFile(openPath);
//Updates CSS path
Convert.changeOne(list);
//Replaces Header and updates 'Analyzes Request...' line
Convert.changeTwo(list);
//Updates Month and Year
Convert.changeThree(month, year, list);
//save to file
Convert.saveFile(list, savePath);
JOptionPane.showMessageDialog(null, "Conversion Complete");
}
else{
JOptionPane.showMessageDialog
(null, "You must enter in a value for all four entries.");
}
}
}
//Graphical Elements
public void paint(Graphics g){
Font font = new Font ("Arial", Font.BOLD, 16);
g.setFont(font);
g.drawString("Web Server Statistics HTML converter", 50, 30);
font = new Font("Arial", Font.PLAIN, 12);
g.setFont(font);
g.drawString("Enter MONTH", 50, 190);
g.drawString("Enter YEAR", 50, 290);
}
public void stop(){
}
}
/******************* CONVERT CLASS ********************
************************************************** *****/
class Convert{
public Convert(){
super();
}
public static LinkedList<String> readFile(String fileName){
LinkedList<String> list = new LinkedList<String>();
try{
File file = new File(fileName);
Scanner scan = new Scanner(file);
while(scan.hasNextLine()){
list.add(scan.nextLine());
}//Try Block
}
catch (FileNotFoundException e){
JOptionPane.showMessageDialog(null,"Sorry. Cannot find file.");
System.exit(0);
}//Catch
return list;
}
public static void saveFile(LinkedList<String> newList, String fileName){
try{
FileWriter out = new FileWriter(fileName);
out.write(newList.toString());
out.close();
}
catch (Exception e){
JOptionPane.showMessageDialog
(null,"Sorry. Could not save File '" + fileName +"'");
System.exit(0);
}
}
//Adds "/.." to the css stylesheet href
public static void changeOne(LinkedList<String> oldList){
Node<String> node = oldList.head;
while(node != null){
if (node.getValue().equalsIgnoreCase
("<link href=\"../../../supportfiles/servercss.css\" rel=\"stylesheet\">")){
node.setValue("<link href=\"../../../../supportfiles/servercss.css\" rel=\"stylesheet\">");
break;
}
else
node = node.getNext();
}
}
//Provides Header information and updates "Analyzed request..." line
public static void changeTwo(LinkedList<String> oldList){
String analyzedRequest = "Analyzed Requests";
String top = "<h1><a name=\"Top\">";
String matched = "";
Node<String> node = oldList.head;
Node<String> nodeTwo = oldList.head;
int positionOne = 1;
int positionTwo = 1;
//Stores updated "Analyzed request.." line
try{
while(positionOne <= oldList.size){
try{
if (node.getValue().substring(0,17).equalsIgnoreCase( analyzedRequest)){
matched = node.getValue();
oldList.remove(positionOne + 1);
oldList.remove(positionOne);
break;
}
else
node = node.getNext();
positionOne++;
}
catch(StringIndexOutOfBoundsException e){
positionOne++;
node = node.getNext();
}
}//End While Loop
}//End Try Block
catch (BadItemCountException e){
System.out.println("No such item.");
System.exit(0);
}
try{
while(positionTwo <= oldList.size){
try{
if (nodeTwo.getValue().substring(0,18).equalsIgnoreCa se(top)){
nodeTwo.setValue("<a NAME=\"Top\"> \n" +
"<TABLE width=\"100%\" cellspacing=0 cellpadding=10 bgcolor=\"#b6da92\">\n" +
"<tr><td><IMG src=\"../../images/header.gif\" alt=\"\"></td></tr>\n" +
"<tr>\n<td>\n" + matched + "\n</td> \n</tr> \n</table>");
oldList.remove(positionTwo + 2);
break;
}
else
nodeTwo = nodeTwo.getNext();
positionTwo++;
}//End try block
catch (StringIndexOutOfBoundsException e){
positionTwo++;
nodeTwo = nodeTwo.getNext();
}
}//End while loop
}
catch (BadItemCountException e){
System.out.println("No such item.");
System.exit(0);
}//End catch block
}//End Method
//Updates the Month and Year of the Report
public static void changeThree(String month, String year, LinkedList<String> oldList){
String menu = "<div align=right><span class=breadcrumb>";
Node<String> node = oldList.head;
int positionOne = 1;
//Stores updated "Analyzed request.." line
try{
while(positionOne <= oldList.size){
try{
if (node.getValue().substring(0,40).equalsIgnoreCase( menu)){
node.setValue
("<div align=right><span class=breadcrumb><a href=\"../../../../index.htm\">" +
"UHM Main Page</a> | <a href=\"../server_"+ year + ".html\">" +
"Web Statistics 2008</a> | " + month + " " + year + "</span></div>" +
"\n<p class=month align=center>" + month + " " + year + "</p>");
oldList.remove(positionOne + 2);
break;
}
else
node = node.getNext();
positionOne++;
}
catch(StringIndexOutOfBoundsException e){
positionOne++;
node = node.getNext();
}//End Catch Block
}//End While Loop
}//End Try Block
catch (BadItemCountException e){
System.out.println("No such item.");
System.exit(0);
}
}
}
/********************Linked List and Node Classes*******************
************************************************** *****************/
class LinkedList<T> {
protected Node<T> head;
protected int size;
public LinkedList() {
head = null; // start with an empty list
size = 0;
}
/* accessor method */
public int size() {
return size;
}
public void add(T value) {
head = addAtEnd(head, value);
size++;
}
private Node<T> addAtEnd(Node<T> node, T value) {
if (node == null) { // special case
return new Node<T>(value, null);
}
else if (node.getNext() == null) { // other special case
node.setNext(new Node<T>(value, null));
}
else {
addAtEnd(node.getNext(), value);
}
return node;
}
/* @param position of item to be removed
* @throws BadItemCountException if this is not a valid position
* position is 1-based, so position = 1 removes the head
*/
public void remove(int position) throws BadItemCountException {
if ((position < 1) || (position > size)) {
throw new BadItemCountException("invalid position " + position +
", only 1.." + size + " available");
}
if (position == 1) {
head = head.getNext();
}
else {
Node<T> node = head;
for (int i = 2; i < position; i++) {
node = node.getNext();
}
// set this node's "next" pointer to refer to the
// node that is after the next
node.setNext(node.getNext().getNext());
}
size--; // one less item
}
/* convert the list to a printable string
* @return a string representing the stack
*/
public String toString() {
return toString(head);
}
private String toString(Node<T> node) {
if (node == null) {
return "";
}
else {
return node.getValue() + "\n" + toString(node.getNext());
}
}
}
class BadItemCountException extends Exception{
public BadItemCountException(String message){
super(message);
}
}
class Node<T> {
private T item;
private Node<T> nextNode;
public Node(T value, Node<T> next) {
item = value;
nextNode = next;
}
/* accessor methods */
public T getValue() {
return item;
}
public Node<T> getNext() {
return nextNode;
}
/* mutator methods */
public void setNext(Node<T> newNext) {
nextNode = newNext;
}
public void setValue(T value) {
item = value;
}
}
|