-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLLinkReader.java
More file actions
83 lines (59 loc) · 2.41 KB
/
HTMLLinkReader.java
File metadata and controls
83 lines (59 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
import javax.swing.text.*;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import javax.swing.JTable;
public class HTMLLinkReader extends JTable //behovs?
{
JTable getHTMLLinks(String webpage){
String [][] stringMatrix = new String [50][2];
String[] header = {"WEBBADRESS","BENAMNING"}; //column names
try{
//create table in the try to make sure an update will not overwrite current frame
InputStream in=new URL(webpage).openConnection().getInputStream();
InputStreamReader reader= new InputStreamReader(in);
HTMLDocument htmlDoc = new HTMLDocument();
htmlDoc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
new HTMLEditorKit().read(reader,htmlDoc,0); //puts the website html in a document
int counter = 0;
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A); //make a A-tag iterator object (to get all A-tags)
iterator.isValid(); iterator.next() ) {
AttributeSet attributes = iterator.getAttributes();
//get the A-tags HREF-attribute
String srcString = (String) attributes.getAttribute(HTML.Attribute.HREF);
//get the A-tags HREF-attributes value
int startOffset = iterator.getStartOffset();
int endOffset = iterator.getEndOffset();
int length = endOffset - startOffset;
String text = htmlDoc.getText(startOffset, length);
//add to matrix (only the first 50)
if (counter < 50){
stringMatrix[counter][0] = srcString;
stringMatrix[counter][1] = text;
counter += 1;
}
}
//if everything is OK the JTable is passed back to the actionEvent (and the scollpane).
setModel(new DefaultTableModel(stringMatrix, header)
{//override so user can not change html links
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
}//no semicolon here!
);
}
catch (IOException e) {
}
catch (BadLocationException e) {
}
return this; //returns the JTable object (which will not update unless the try is successfull)
}
}