Tuesday, September 15, 2009

Java and myself

In my first job, I have used VB and Java. At that time, I was new to Internet. I used to develop programs in VB6 and Java2 around Internet programming. I wrote java programs using notepad and Emacs editors. After that many years I ran behind GUI automation tools such as VisualTest, WinRunner, QTP and Silktest. Sometimes I'm using VBScript for few testing tasks.

Recently many tools are supported to use Java as test language. Again I'm going to re-look at java. I was looking into my old java code. I tried to compile few java programs with recent Java version 1.6.0.14. Few classes are deprecated. I have used this command javac ZDownloadURL.java -Xlint:deprecation to compile java program.

In Sun Developer network, one tutorial was there for Basic Java. Link :Essentials of the Java Programming Language

Below program can be used to download any web page. I tried my blog and it still works.
To Compile - javac ZDownloadURL.java -Xlint:deprecation
To run the program - java ZDownloadURL



// $Id$

/**
*
*
* Created: Tue Oct 24 18:51:26 2000
*
* @author palani selvam
* @version $Revision$
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;


/* To Run this file, put command as
java -Dhttp.proxyHost=192.168.27.2 -Dhttp.proxyPort=3128 ZDownloadURL
*/


public class ZDownloadURL extends JFrame implements WindowListener,ActionListener {
String URL; //Given URL
// String inputLine; //To read Line by line
String title="Downloading Trial"; //To give title of the URl

static JFrame frame; //Parent Level Container
JButton save; //Save Button
JButton down; //Declare Download Button
JButton exit; //exit Button
JTextField url; //To enter URl
JTextArea contents; //Contents of specific file
//JPanel panel; //Second level Container
Container panel;
//JPanel temp;
Container temp;


public ZDownloadURL() {
//frame=new JFrame();
super.setTitle(title);
super.setForeground(Color.blue);
super.setBackground(Color.yellow);

panel=getContentPane();
temp=new JPanel();
temp.setLayout(new BorderLayout());
temp.setSize(10,20);

GridBagLayout grid=new GridBagLayout(); //Set GridBagLayout
GridBagConstraints c=new GridBagConstraints();
c.fill=GridBagConstraints.HORIZONTAL;

panel.setLayout(grid);
// grid.setConstraints(c); //Add GridbagConstraints

url=new JTextField(20);
contents=new JTextArea(30,30);
contents.setLineWrap(true); //To Wrap lines in TextArea

/***
* To create ScrollBars to JTextArea
*
*/

JScrollPane pane = new JScrollPane(contents);
temp.add(pane);



/***
contents.setColumns(20);
contents.setRows(10);
contents.setLineWrap(true);
contents.setWrapStyleWord(true);
contents.setSize(10,20);

temp.add(contents);
**/



down=new JButton("DownLoad");
save=new JButton("Save");
exit=new JButton("Exit");
JLabel label=new JLabel("Enter valid URL");

c.gridx=0;
c.gridy=0;
c.insets=new Insets(5,0,0,0);
grid.setConstraints(label,c);
panel.add(label);

c.gridx=1;
c.gridy=0;
c.gridwidth=1;
//c.gridheight=2;
c.insets=new Insets(5,0,0,0);
grid.setConstraints(url,c);
panel.add(url);

c.gridx=2;
c.gridy=0;
c.insets=new Insets(5,0,0,0);
grid.setConstraints(down,c);
panel.add(down);

c.gridx=0;
c.gridy=1;
c.gridwidth=20;
c.gridheight=10;
c.insets=new Insets( 20,0,0,0);
grid.setConstraints(temp,c);
panel.add(temp);


c.gridx=0;
c.gridy=12;
c.gridwidth=1;
c.insets=new Insets(10,0,0,0);
grid.setConstraints(save,c);
panel.add(save);

c.gridx=1;
c.gridy=12;
c.gridwidth=1;
c.insets=new Insets(10,0,0,0);
grid.setConstraints(exit,c);
panel.add(exit);

exit.addActionListener(this);
save.addActionListener(this);
down.addActionListener(this);
// frame.addActionListener(this);

// frame.add(panel);


}


public void windowActivated(WindowEvent we) {

}

public void windowDeactivated(WindowEvent we) {

}

public void windowIconified(WindowEvent we) {

}

public void windowDeiconified(WindowEvent we) {

}

public void windowOpened(WindowEvent we) {

}

public void windowClosed(WindowEvent we) {

}

public void windowClosing(WindowEvent we) {

System.exit(10);

}


/***
* pass the URL as string
*/


public void Download(String _url) {
try {
URL myURL=new URL(_url);
BufferedReader in=new BufferedReader(new InputStreamReader(myURL.openStream()));

String inputLine;
contents.setText("");

while((inputLine=in.readLine()) != null) {
//contents.setText(contents.getText()+"\n"+inputLine);
contents.append("\n"+inputLine);

}
} catch(Exception ex) {

System.out.println(ex.toString());
}

}

public boolean checkURL(String _url) {
if((_url.indexOf("http"))!=0) {
return true;
}
else {
return false;
}
}


public void actionPerformed(ActionEvent ae) {
//String action=ae.getActionCommand();

URL=url.getText();
Object action=ae.getSource();

//To Download

if(action.equals(down)) {
/** if(checkURL(URL)) {
Download(URL);
}
else
JOptionPane.showMessageDialog(new JFrame(),"Enter Valid URL in TextField!","Information",JOptionPane.INFORMATION_MESSAGE);

**/

Download(URL);


}


//To save that URL's contents
//Not implemented

if(action.equals(save)) {
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(new JFrame());

if(returnVal==JFileChooser.APPROVE_OPTION) {
File file=fc.getSelectedFile();
super.setTitle(file.getName());
}
}

if(action.equals(exit)) {
System.exit(0);
}

}

public static void main(String args[]) {
frame=new ZDownloadURL();

frame.setSize(500,400);
frame.pack();
frame.show();

}

} // ZDownloadURL

/*
* $Log$
*/

No comments: