RSS Viewer Applet: Ready to Rumble (3/6) - exploring XML | WebReference

RSS Viewer Applet: Ready to Rumble (3/6) - exploring XML

RSS Viewer Applet: Ready to Rumble

Initialization and Parameter Handling

First we need member variables to hold all the new parameters defined:
package com.exploringxml.rss.applet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.exploringxml.rss.RSSChannel;
public class RSSViewerApplet extends Applet {
  String srcRSS; 
  RSSChannel channel;
  int currentItem = -2;
  Exception ex;

srcRSS holds the URL to the RSS file to be parsed. It is the argument to RSSChannel which loads itself from that location and populates its fields. currentItem will keep track of the currently activated channel item. The value -2 stands for no selection, -1 denotes the channel title, and everything from 0 and greater refers to the respective item in the channel. Exceptions from parsing the RSS file get stored in ex for later reporting.

  Color boxForeground; Color boxBackground; int boxBorder;
  boolean boxScroll; boolean boxScrollHorizontal; boolean boxScrollReverse;
  Color titleForeground; Color titleBackground;
  Color titleForegroundOnMouseOver; Color titleBackgroundOnMouseOver;
  String titleFontFamily; int titleFontSize; int titleFontStyle; String titleAlign;
  Color itemForeground; Color itemBackground;
  Color itemForegroundOnMouseOver; Color itemBackgroundOnMouseOver;
  String itemFontFamily; int itemFontSize; int itemFontStyle; String itemAlign;
  int itemMargin; String itemSeparator;
  Font titleFont; Font itemFont;
  int titleLineBase; int titleLineHeight;
  int itemLineBase; int itemLineHeight;

The remaining members will hold the style parameters, and the results of some typographic calculations. Next we define a couple of getParameter() functions for reading the applet's param values, or setting default values in case of absence:

  public String getParameter(String key, String defaultValue) {
    String paramValue = getParameter(key);
    return (paramValue != null ? paramValue : defaultValue);
  public int getFontStyleParameter(String key, String defaultValue) {
    String paramValue = getParameter(key);
    if (paramValue == null) paramValue = defaultValue;
    if (paramValue.equalsIgnoreCase("bold")) return Font.BOLD;
    else if (paramValue.equalsIgnoreCase("italic")) return Font.ITALIC;
    else if (paramValue.equalsIgnoreCase("bolditalic")) return Font.BOLD + Font.ITALIC;
    return Font.PLAIN;
  public int getParameter(String key, int defaultValue) {
    String paramValue = getParameter(key);
    return (paramValue != null ? Integer.parseInt(paramValue) : defaultValue);
  public boolean getParameter(String key, boolean defaultValue) {
    String paramValue = getParameter(key);
    return (paramValue != null ? paramValue.equalsIgnoreCase("true") ||
      paramValue.equalsIgnoreCase("yes") : defaultValue);
  public Color getParameter(String key, Color defaultValue) {
    String paramValue = getParameter(key);
    return (paramValue != null ? new Color(Integer.parseInt(paramValue.substring(1), 16)) : defaultValue);
All of these functions retrieve the applet's param value if present and convert it into the expected Java type. If the parameter is not present the passed in default value will be returned. The applet's init() function then sets all the parameters using the above funcions and reads in the RSS file through the RSSChannel class. Unexpected exceptions are caught and stored for later rendering.
  public void init() {
    Color darkPurple = new Color(208, 51, 255);
    Color lightPurple = new Color(224, 170, 255);
    srcRSS = getParameter("src", "http://localhost/xml/index.rss");
    boxForeground = getParameter("box.foreground", Color.white);
    boxBackground = getParameter("box.background", lightPurple);
    boxBorder = getParameter("box.border", 3);
    boxScroll = getParameter("box.scroll", false);
    boxScrollHorizontal = getParameter("box.scroll.horizontal", false);
    boxScrollReverse = getParameter("box.scroll.reverse", false);
    titleForeground = getParameter("title.foreground", Color.white);
    titleBackground = getParameter("title.background", lightPurple);
    titleForegroundOnMouseOver = getParameter("title.foreground.mouseover", darkPurple);
    titleBackgroundOnMouseOver = getParameter("title.background.mouseover", lightPurple);
    titleFontFamily = getParameter("", "sansserif");
    titleFontSize = getParameter("title.font.size", 16);
    titleFontStyle = getFontStyleParameter("", "bold");
    titleAlign = getParameter("title.align", "center");
    itemForeground = getParameter("item.foreground", Color.white);
    itemBackground = getParameter("item.background", darkPurple);
    itemForegroundOnMouseOver = getParameter("item.foreground.mouseover", lightPurple);
    itemBackgroundOnMouseOver = getParameter("item.background.mouseover", darkPurple);
    itemFontFamily = getParameter("", "sansserif");
    itemFontSize = getParameter("item.font.size", 12);
    itemFontStyle = getFontStyleParameter("", "plain");
    itemAlign = getParameter("item.align", "left");
    itemMargin = getParameter("item.margin", 1);
    itemSeparator = getParameter("item.separator", "*");
    channel = new RSSChannel();
    try {
    catch (Exception e) {
      ex = e;

Now on to the Drawing Logic

Produced by Michael Claßen
All Rights Reserved. Legal Notices.

Created: Mar. 12, 2000
Revised: Mar. 12, 2000