Zu diesem Zweck wird ein kleiner JFrame erstellt, der im oberen Bereich ein JTextField zur Eingabe der URL und im unteren Bereich einen Button zum Laden der Seite enthält. Der mittlere Bereich stellt die geladene Seite in Originalgröße dar.
Die Klasse besitzt neben dem Konstruktor und main() drei Methoden:
- verifyStr()
prüft die Stimmigkeit des Pfades im Textfeld. Die Methode wird abgebrochen,
wenn der String leer ist oder wenn er nicht länger als sieben Zeichen ist,
da diese Länge derjenigen der in der URL notwendigen Protokollangabe entspricht.
Beginnt die URL mit "http://" wird sie
ungeprüft übernommen, da der
Internet-Server von sich aus Unstimmigkeiten quittiert. Sollte der String
weder mit "http://" noch mit "file://"
beginnen, wird von einer lokalen Datei
ausgegangen und "file://" vorangestellt -
eine zugegebenermaßen sehr gewagte Annahme,
die zu Demonstrationszwecken aber hier einmal stehen bleiben
soll. Ebenfalls sollte man sich darüber im Klaren sein, dass eine
URL natürlich nicht nur aus einem Pfad mit vorangestellter
Protokollangabe besteht.
In der Folge wird der String wieder seiner Protokollangabe beraubt und der Pfad auf seine Gültigkeit überprüft: Es wird ermittelt, ob die angegebene Datei vorhanden und lesbar ist. - setURL() prüft durch Aufruf der erstgenannten Methode ansatzweise die Gültigkeit des URL-Strings und bildet dann ein URL-Objekt, das in der Objektvariablen url abgelegt wird.
-
loadPage() schließlich
lädt die angeforderte Seite und gibt ihren Quelltext
auf der Konsole aus. Das Laden geschieht auf einem HTML-fähigen
JEditorPane
über dessen Methode setPage(),
der als Argument die URL übergeben wird.
Das Auslesen des Quelltextes erfolgt auf die gleiche Weise wie hier beschrieben über einen InputStream, der in einem BufferedReader gespeichert wird.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DisplayHTML extends JFrame {
private JEditorPane editorPane;
private JTextField field;
private JButton button;
private JPanel buttPanel;
private String str;
private URL url;
public DisplayHTML() {
field = new JTextField("file://");
this.add(field, BorderLayout.NORTH);
button = new JButton("laden");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setURL();
loadPage();
}
});
buttPanel = new JPanel(new FlowLayout());
buttPanel.add(button);
this.add(buttPanel, BorderLayout.SOUTH);
editorPane = new JEditorPane();
editorPane.setEditable(false);
this.add(editorPane, BorderLayout.CENTER);
setURL();
loadPage();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void loadPage() {
// stop the program if there is no URL-Object
if (url == null) {
return;
}
try {
// load the page
editorPane.setPage(url);
// get the source
InputStream in = url.openStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
String s;
// display source
while ((s = buff.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void setURL() {
// check the URL-String
if (!verifyStr())
return;
if (str.startsWith("file://") || str.startsWith("http://")) {
// create an URL-Object
try {
url = new URL(str);
} catch (MalformedURLException e) {
url = null;
System.err.println("Falsches URL-Format!");
e.printStackTrace();
}
}
}
private boolean verifyStr() {
// getting the text from the JTextField
str = field.getText();
// stop the program if the string is empty or just matches
// 'http://' or 'file://'
if (str.equals("") || str.length() < 8)
return false;
// try to load an internet site
if (str.startsWith("http://"))
return true;
// if there is just a given path add 'file://'
if (!str.startsWith("http://") && !str.startsWith("file://")) {
str = "file://" + str;
}
if (str.startsWith("file://")) {
// check if file is valid and can be read
String s = str.substring(7);
File file = new File(s);
if (file.isFile() && file.canRead()) {
return true;
}
}
return false;
}
public static void main(String[] args) {
new DisplayHTML();
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DisplayHTML extends JFrame {
private JEditorPane editorPane;
private JTextField field;
private JButton button;
private JPanel buttPanel;
private String str;
private URL url;
public DisplayHTML() {
field = new JTextField("file://");
this.add(field, BorderLayout.NORTH);
button = new JButton("laden");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setURL();
loadPage();
}
});
buttPanel = new JPanel(new FlowLayout());
buttPanel.add(button);
this.add(buttPanel, BorderLayout.SOUTH);
editorPane = new JEditorPane();
editorPane.setEditable(false);
this.add(editorPane, BorderLayout.CENTER);
setURL();
loadPage();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void loadPage() {
// stop the program if there is no URL-Object
if (url == null) {
return;
}
try {
// load the page
editorPane.setPage(url);
// get the source
InputStream in = url.openStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
String s;
// display source
while ((s = buff.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void setURL() {
// check the URL-String
if (!verifyStr())
return;
if (str.startsWith("file://") || str.startsWith("http://")) {
// create an URL-Object
try {
url = new URL(str);
} catch (MalformedURLException e) {
url = null;
System.err.println("Falsches URL-Format!");
e.printStackTrace();
}
}
}
private boolean verifyStr() {
// getting the text from the JTextField
str = field.getText();
// stop the program if the string is empty or just matches
// 'http://' or 'file://'
if (str.equals("") || str.length() < 8)
return false;
// try to load an internet site
if (str.startsWith("http://"))
return true;
// if there is just a given path add 'file://'
if (!str.startsWith("http://") && !str.startsWith("file://")) {
str = "file://" + str;
}
if (str.startsWith("file://")) {
// check if file is valid and can be read
String s = str.substring(7);
File file = new File(s);
if (file.isFile() && file.canRead()) {
return true;
}
}
return false;
}
public static void main(String[] args) {
new DisplayHTML();
}
}