|
|
|||||||||||||||||||||||||||||
by Dave Johnson | |||||||||||||||||||||||||||||
| AnyFeedParser | Simple JDOM-based RSS/Atom feed parser (Chapter 5) |
| BlogClient | Blog client library supporting MetaWeblog API and Atom (Chapters 8 and 9) |
| PlanetTool | Planet-style blog aggregator (Chapter 11) |
| TechnoratiJ | Simple Technorati API client library (Chapter 12) |
| Cross Poster | Parses feeds, posts to blog (Chapter 13) |
| Mail Blogger | Receives mail, posts it to blog (Chapter 14) |
| Blog Digest | Parses feeds, sends daily digest by email (Chapter 15) |
| Ant Blogger | Blog and upload files from build scripts (Chapter 16) |
| Blogbot | IRC chat robot that blogs on command (Chapter 17) |
| File Caster | Simple file/podcast server with file upload (Chapter 18) |
| File Catcher | Simple command-line file/podcast downloader (Chapter 19) |
| Feed Poster | Parses feeds, posts daily digest via Blog Client (not in book) |
| Grabber | Downloads all posts from a blog and saves them each separately (not in book) |
Now that you know what's available, let's discuss how to work with the Java versions of the examples.
Before you can start working with the examples, you need to install and build them on your computer. It's a lot easier than installing the Blogapps Server, but the examples are basically the same; i.e., you need the J2SE SDK Version 1.4.2 or later.
First, you've got to download and install the Blogapps Examples. Download the latest version from the blogapps-server folder in the files area of the Blogapps project and decompress as before.
To build the Java examples, use the Ant script located in the java directory. Assuming you've got a Java development kit and you've got Ant, then all you need to do to build the examples is to open a console window, cd to the blogapps java directory, and run Ant. For example, on Windows:
c> cd \blogapps\java
c> ant
Or on UNIX:
$ cd ~/blogapps/java
$ ant
As Ant runs, it will visit all of the chapter sub-directories under the java directory, from ch02 to ch18, running the Ant script in each. After Ant finished, you'll find that each chapter directory is organized as follows:
The dist directory contains the complete and ready-to-run example, packaged for distribution; thus the directory name. If you want to run an example, go to its dist directory. For example, if you'd like to run the Chapter 2 example, which posts to a blog server via MetaWeblog API, you'd do something like this:
Start the Blogapps Server using the instructions above (because we'll need to post to a blog server and Blogapps Server supports the MetaWeblog API).
Change directories to the blogposter directory, which lives in the dist directory of the ch02 examples. For example, if you've installed Blogapps Examples under Windows in the directory c:\blogapps, here's what you'd do:
c> cd \blogapps\java\ch02\dist\blogposter
c> blogposter "this is the title" "this is the content"
In Unix, it would look like this:
$ cd ~/blogapps/ch02/dist/blogposter
$ ./blogposter.sh "this is the title" "this is the content"
You can find instructions for building and running all of the examples on the Blogapps website. There is a README for each chapter, and you can find them all on the Blogapps project page. That's all you need to know about building and running the examples; now let's discuss how to use them in your own projects.
Let's look at Blog Client, a client library for publishing to blog servers that support either the XML-RPC-based MetaWeblog API or the Atom protocol. The library is defined by a set of interfaces, shown in Figure 3.

Figure 3. The Blog Client library interfaces
Now let's take a look at an example that shows how to post a blog entry via Atom protocol and the Blog Client library.
import com.manning.blogapps.chapter10.blogclient.*;
// class and method declaration omitted
String endpointURL = // URL of your blog server
String username = // your username on the blog server
String password = // your password
String title = // title of the new blog entry
String content = // content of the new blog entry
BlogConnection con = #1
BlogConnectionFactory.getBlogConnection(
"atom", endpointURL, username, password);
Blog blog = (Blog)con.getBlogs().get(0); #2
BlogEntry entry = blog.newEntry(); #3
entry.setTitle(title); #4
entry.setContent(new BlogEntry.Content(content)); #5
entry.save(); #6
Let's review that code. First we get a connection from the factory by specifying the Atom protocol, our Blog Server's endpoint URL, and login credentials (#1). From the connection, we get the first blog that's available (#2). Next, we create a new entry in the blog's primary collection (#3), set its title (#4), set its content (#5), and post it to the blog server by calling its save() method (#5).
Before you can use the Blog Client library in your application, you need to add the required .jars to your application's classpath. You can find those .jars in the dist/blogclient/lib directory in the Chapter 10 directory. Now let's move on to the Blogapps parser.
AnyFeedParserIn RSS and Atom in Action, we recommend that you use a parser library like ROME for Java, but we also show you how to parse feeds "by hand" with the JDOM XML parser. To that end, we develop a simple parser called AnyFeedParser that can handle RSS 0.9X, RSS 1.0, RSS 2.0, and Atom 1.0 feeds.
So that we're not locked in to just one implementation of our simple parser, the parser implements an interface called IFeedParser.
package com.manning.blogapps.chapter05;
import java.io.Reader;
import java.util.Map;
public interface IFeedParser {
public Map parseFeed(Reader is) throws Exception;
public Map parseFeed(String fileName) throws Exception;
}
And to keep things simple, we don't parse to a Java object model. As you can see in the interface, we parse to a hashtable, which is keyed by RSS element names. That may seem like an oversimplification, but it works well and, in fact, it's the approach taken by the king of parsers, the Universal Feed Parser (the Python-based parser that lives at feedparser.org). Our little parser pales in comparison to the king, but it's small, relatively easy to understand, and easy hack up if you have your own unique parsing needs. We only parse a subset of RSS and Atom elements. Here are the keys you can use to extract data from the hashtable:
Using AnyFeedParser is simple too. All you have to do is copy two .jars to your application's classpath, add an import statement, and add a call to the parser. You can find the parser in the Chapter 5 examples directory and the .jars are located there too, in the dist/parsers/lib subdirectory. You'll need two .jars: ch05.jar and jdom.jar.
Once you've got the .jars in your classpath, you should be able to write code like this, which parses a feed and prints it out to the console:
IFeedParser parser = new AnyFeedParser();
Map feed = parser.parseFeed(filePath);
System.out.println("Feed title: " + feed.get("title"));
List items = (List)feed.get("items");
for (int i=0; i<items.size(); i++) {
Map item = (Map)items.get(i);
System.out.println("Item #" + i);
System.out.println(" title: " + item.get("title"));
System.out.println(" link: " + item.get("link"));
System.out.println(" pubDate: " + item.get("pubDate"));
}
There's also a version of the parser called AnyFeedParserCaching that parses feeds specified by URL and maintains a local cache via HTTP Conditional Get, which is a bandwidth-saving best-practice for fetching feeds. To use the caching parser you need to provide a cache directory, a feedURL from which to fetch the feed and some code like this to create the parser:
AnyFeedParserCaching parser = new AnyFeedParserCaching();
parser.setCacheDir(cacheDirectoryPath);
Map feed = parser.parseFeed(feedURL);
That's it for the simple feed parser, now let's talk about the future.
What's next for the Blogapps project? That depends on the branch. In the Blogapps 1.0 branch, which is supposed to remain true to the code in the book, I will only make changes to fix bugs and to update the code for the final Atom Publishing Protocol specification and for the final release of IE7.
But the Blogapps 2.0 branch can run wild and free. It can deviate from the code in the book. So I (and maybe someday, we) can release new versions of the Blogapps Examples with completely new features and new versions of the Blogapps Server that use newer versions of Roller an JSPWiki. Here are some my ideas for future development.
FileCaster podcast server to provide a password-protected administrative interface, to extract ID3 tags, and to support iTunes tags.net.java.dev.blogapps instead of com.manning.blogapps.You've learned that the Blogapps project is more than just a set of examples; it's an RSS and Atom development kit based on Java (and C#) with a set of immediately useful applications. Whether you're looking for a snippet of example RSS/Atom code or a complete blog application, I hope you'll find the project is a good starting point. And if you do find the project useful, get involved. Please help out your fellow readers by directing your bug reports, comments, patches, and suggestions for improvement to the project's issue tracker and mailing list.
Dave Johnson is a North Carolina based software developer, blogger, and the author of RSS and Atom in Action.
View all java.net Articles.
Showing messages 1 through 3 of 3.
|
|