- Some Google in Your Apps
- Getting Started
- A .NET Google Fight
- Viewing Cached Pages in Java
- Displaying a Cached Page
- Just That Easy
Displaying a Cached Page
Now to the most complicated part of the app: retrieving Google's cached version of a page and displaying it. The GoogleSearch object has a doGetCachedPage() method that takes a URL and returns a byte array of the contents of the page. A MouseAdapter on the JList discovers the index in the list that has been double-clicked and passes that index into a method that will retrieve the URL and display the cached version. Java's JEditorPane can render HTML, which makes the job easier, but we still need to convert that byte array into a something that we can display.
First we'll retrieve the GoogleSearchResultElement that we added to the JList via its index, and retrieve its cached version:
GoogleSearchResultElement element = (GoogleSearchResultElement)getResultsList().getModel().getElementAt(index); byte[] page = getSearch().doGetCachedPage(element.getURL());
Then we need to create the pane to view the HTML, and the various HTML-related classes to read it:
JEditorPane pane = new JEditorPane(); pane.setContentType("text/html"); //Convert the byte array into something that can be read Reader reader = new InputStreamReader(new ByteArrayInputStream(page), "UTF-8"); HTMLEditorKit kit = new HTMLEditorKit(); HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument(); //Display the content pane.setEditorKit(kit); pane.setDocument(doc); pane.getDocument().putProperty("IgnoreCharsetDirective", Boolean.TRUE);
Here we use a ByteArrayInputStream to read the byte array, and wrap that in an InputStreamReader that we can use to read in the HTML. The selected encoding of UTF-8 should be correct, and setting the IgnoreCharsetDirective property to TRUE should prevent any exceptions relating to encodings, but by catching the ChangedCharSetException we can determine the page's encoding and try to read it again:
try { kit.read(reader, doc, 0); } catch(ChangedCharSetException ccse) { //If the encoding is incorrect, get the correct one reader = new InputStreamReader(new ByteArrayInputStream(page), ccse.getCharSetSpec()); try { kit.read(reader, doc, 0); } catch(ChangedCharSetException ccse2) { error("Couldn't set correct encoding: " + ccse2); return; } }
Now, we just need to create a modal JDialog to display the content, and show it:
dialog = new JDialog(this, element.getTitle(), true); scrollPane = new JScrollPane(pane); dialog.getContentPane().add(scrollPane, BorderLayout.CENTER); dialog.setSize(new Dimension(400, 300)); dialog.show();
Unfortunately, the JEditorPane doesn't always do a stellar job of displaying the cached content, and quite often the cached versions are missing styling information anyhow, but what we wind up with is a reasonably presentable version of the page (see Figure 5).
Figure 5 Displaying the cached result of a search.