Parsing Javalobby (Part 3)

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() {

    initComponents();

    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);
        io.select();
        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) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            }

            public void mouseReleased(MouseEvent arg0) {
            }

            public void mouseEntered(MouseEvent arg0) {
            }

            public void mouseExited(MouseEvent arg0) {
            }
        });

        //Close the input stream:
        in.close();

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

}

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

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today