Friday, November 19, 2010

How would I write code in Java to read a html page from the internet and execute javascript within it?

I need to connect to a web site, download the home page, then search for a particular link within.



I have achieved this, however the link I require is dynamically generated when the page loads using javascript, so my HTML parser in Java cannot see it.



If anyone knows how to load a HTML file and execute the javascript in Java, please let me knowHow would I write code in Java to read a html page from the internet and execute javascript within it?
Try to analyze the javascript which loads the link, maybe you can find a way of getting the link without running the actual script.



(I do not know how to run javascript in Java nor if its even possible)How would I write code in Java to read a html page from the internet and execute javascript within it?
Hi,



Take a look at the following code. Basic as it is, I think it may help you resolve your issues.



import java.awt.*;

import java.awt.event.*;

import java.io.*;



import javax.swing.*;

import javax.swing.text.*;

import javax.swing.text.html.*;

import javax.swing.undo.*;

import java.util.*;

import java.net.*;

import javax.swing.event.DocumentEvent;

import javax.swing.event.DocumentListener;

import javax.swing.event.UndoableEditEvent;

import javax.swing.event.UndoableEditListener;



public class HTMLEditor extends JFrame

聽implements ActionListener,

聽DocumentListener, UndoableEditListener {

聽聽BorderLayout borderLayout1 =

聽聽聽new BorderLayout();

聽聽JPanel jPanel1 =

聽聽聽new JPanel();

聽聽JButton getFileBtn =

聽聽聽new JButton();

聽聽JButton closeBtn =

聽聽聽new JButton();

聽聽JTabbedPane jTabbedPane1 =

聽聽聽new JTabbedPane();

聽聽JScrollPane jScrollPane1 =

聽聽聽new JScrollPane();

聽聽JScrollPane jScrollPane2 =

聽聽聽new JScrollPane();

聽聽JEditorPane edText =

聽聽聽new JEditorPane();

聽聽JEditorPane edHTML =

聽聽聽new JEditorPane();

聽聽Document textDoc = null;

聽聽ArrayList undoList = new ArrayList();

聽聽JButton undoBtn = new JButton();



聽聽public HTMLEditor() {

聽聽聽try {

聽聽聽聽jbInit();

聽聽聽聽getFileBtn.addActionListener(

聽聽聽聽聽this);

聽聽聽聽undoBtn.addActionListener(this);

聽聽聽聽closeBtn.addActionListener(this);

聽聽聽}

聽聽聽catch(Exception e) {

聽聽聽聽e.printStackTrace();

聽聽聽}

聽聽}



聽聽public void loadFile(String fp) throws

聽聽聽Exception {



聽聽聽if (fp==null) return;

聽聽聽String filePath=fp;

聽聽聽File f=new File(fp);

聽聽聽long av=f.length();

聽聽聽if (av==0) {

聽聽聽聽System.out.println(';no data';);

聽聽聽聽return;

聽聽聽}

聽聽聽URL url=new URL(';file:';+fp);

聽聽聽edHTML.setPage(url);

聽聽聽FileReader fr=new FileReader(f);

聽聽聽edText.read(fr,null);



聽聽聽textDoc=this.edText.getDocument();

聽聽聽textDoc.addDocumentListener(this);

聽聽聽textDoc.addUndoableEditListener(

聽聽聽聽this);



聽聽聽this.validate();

聽聽}



聽聽public static void main(String[] args) {

聽聽聽HTMLEditor ed = new HTMLEditor();

聽聽聽ed.setBounds(20,20,500,400);

聽聽聽ed.setVisible(true);

聽聽}

聽聽private void jbInit() throws Exception {

聽聽聽this.getContentPane().setLayout(

聽聽聽聽borderLayout1);

聽聽聽getFileBtn.setText(';Get FIle';);

聽聽聽closeBtn.setText(';Close';);

聽聽聽edText.setText(';jEditorPane1';);

聽聽聽edHTML.setEditable(false);

聽聽聽edHTML.setText(';jEditorPane2';);

聽聽聽undoBtn.setEnabled(false);

聽聽聽undoBtn.setText(';Undo';);

聽聽聽this.getContentPane().add(

聽聽聽聽jPanel1, BorderLayout.SOUTH);

聽聽聽jPanel1.add(getFileBtn, null);

聽聽聽jPanel1.add(undoBtn, null);

聽聽聽jPanel1.add(closeBtn, null);

聽聽聽this.getContentPane().add(

聽聽聽聽jTabbedPane1, BorderLayout.CENTER);

聽聽聽jTabbedPane1.add(jScrollPane1,

聽聽聽聽 ';HTML';);

聽聽聽jScrollPane1.getViewport().add(

聽聽聽聽edHTML, null);

聽聽聽jTabbedPane1.add(

聽聽聽聽jScrollPane2, ';Text';);

聽聽聽jScrollPane2.getViewport().add(

聽聽聽聽edText, null);

聽聽聽addWindowListener(new WindowAdapter() {

聽聽聽聽public void windowClosing(

聽聽聽聽聽WindowEvent e) {

聽聽聽聽聽System.exit(0);

聽聽聽聽}

聽聽聽});

聽聽}

聽聽聽聽public void actionPerformed(

聽聽聽ActionEvent e) {

聽聽聽if (e.getSource() == this.closeBtn)

聽聽聽System.exit(0);

聽聽聽if (e.getSource() == this.getFileBtn) {

聽聽聽聽FileDialog fd=new FileDialog(

聽聽聽聽聽this,';Select File';,FileDialog.LOAD);

聽聽聽聽fd.setVisible(true);

聽聽聽聽String fName=fd.getFile();

聽聽聽聽if (fName==null) return;

聽聽聽聽String fPath=fd.getDirectory()+fName;

聽聽聽聽try {

聽聽聽聽聽loadFile(fPath);

聽聽聽聽}

聽聽聽聽catch (Exception ex) {

聽聽聽聽聽System.out.println(

聽聽聽聽聽聽ex.getMessage());

聽聽聽聽聽ex.printStackTrace();

聽聽聽聽}

聽聽聽} else if (

聽聽聽聽e.getSource() == this.undoBtn) {

聽聽聽聽undoLast();

聽聽聽}

聽聽}

聽聽public void undoLast() {

聽聽聽int ix=undoList.size()-1;

聽聽聽if (ix%26gt;=0) {

聽聽聽聽UndoableEdit ue =

聽聽聽聽聽(UndoableEdit

聽聽聽聽聽聽)this.undoList.get(ix);

聽聽聽聽ue.undo();;

聽聽聽聽undoList.remove(ix);

聽聽聽聽if (undoList.size()==0) 聽聽聽聽聽this.undoBtn.setEnabled(false);

聽聽聽聽}

聽聽聽}



聽聽public boolean isFileChanged() {

聽聽聽return this.undoList.size()%26gt;0;

聽聽}

聽聽public void insertUpdate(DocumentEvent e) {

聽聽聽System.out.println(';insertUpdate';);

聽聽}

聽聽public void removeUpdate(DocumentEvent e) {

聽聽聽System.out.println(';removeUpdate';);

聽聽}

聽聽public void changedUpdate(DocumentEvent e) {

聽聽聽System.out.println(

聽聽聽聽';changedUpdate';);

聽聽}

聽聽public void undoableEditHappened(

聽聽聽UndoableEditEvent e) {

聽聽聽this.undoList.add(e.getEdit());

聽聽聽this.undoBtn.setEnabled(true);

聽聽}

}



Regards,



TgTips

No comments:

Post a Comment