Geertjan's Blog

  • January 7, 2008

Parsing Javalobby (Part 3)

Geertjan Wielenga
Product Manager
Here's most of the code I used for the Javalobby Integrator. The only bits not included here are the Action class, with its getToolbarPresenter() method that places the panel into the toolbar, as well as the initialization of the JList, which is done in the initComponents() method generated by the GUI Builder.

By the way, if you're not familiar with how to read/write URL connections (as done at the start of the code below), see Working with URLs, in the Java Tutorial's Custom Networking Trail.

public class JavalobbyPanel extends javax.swing.JPanel {
private int counter = 0;
private DefaultListModel model = new DefaultListModel();
private String strippedUrl;
private OutputWriter writer;
private List list = new ArrayList();
private String inputLine;
public JavalobbyPanel() {
try {//The URL that we want to parse:
URL url = new URL("http://www.javalobby.org/");//Get the URL's input stream:
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));//Open the Output window:
InputOutput io = IOProvider.getDefault().getIO("HTML Parsing", false);
writer = io.getOut();//Reset the Output window so that it is empty every time:
writer.reset();//Read the input stream line by line:
while ((inputLine = in.readLine()) != null) {//On Javalobby, all article titles are on lines that start like this:
if (inputLine.startsWith("<h3><a")) {//Increment the counter:
counter = counter + 1;//Dig through the line, for the title:
String strippedTitle = inputLine.substring(inputLine.lastIndexOf(">")).replace(">", "");//Dig through the line, for the URL:
int startIndex = inputLine.indexOf("href=\\"");
int endIndex = inputLine.lastIndexOf("\\">");
strippedUrl = inputLine.substring(startIndex, endIndex).replaceAll("href=\\"", "http://www.javalobby.org");//Add the counter and the title to the JList's model:
model.addElement(counter + ". " + strippedTitle);//Add the URL to the ArrayList:
list.add(strippedUrl);//Write to the Output window:
writer.println(counter + ". URL: " + strippedUrl.trim() + ", Title: " + strippedTitle);
}//Add a mouse listener to the JList:
jList1.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
}//Show the URL that is related to the currently selected title:
public void mousePressed(MouseEvent evt) {
if (evt.getClickCount() == 2) {
try {//Get the selected index:
int selectedTitle = jList1.getSelectedIndex();//Get the URL from the ArrayList, if it matches the selectedTitle:
Object relatedUrl = list.get(selectedTitle);//Use a NetBeans API class for opening the browser:
URLDisplayer.getDefault().showURL(new URL(relatedUrl.toString()));
} catch (MalformedURLException ex) {
public void mouseReleased(MouseEvent arg0) {
public void mouseEntered(MouseEvent arg0) {
public void mouseExited(MouseEvent arg0) {
});//Close the input stream:
} catch (IOException ex) {

By the way, this is my most cosmopolitan plugin. I worked on it in Cairo, Istanbul, and Prague.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.