import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import RssChannel.RssItem;
import RssChannel.RssImage;
import RssChannel.RssTextInput;
/**
* Unmarshaling SAX Events into RSS Data
*/
public class RssConsumer extends DefaultHandler {
private RssChannel channel;
private RssItem item;
private RssImage image;
private RssTextInput input;
private Stack stack;
private Locator locator;
public RssChannel getChannel ()
{ return channel; }
private String getCurrentElementName ()
{ return (String) stack.peek (); }
// only need a handful of ContentHandler methods
public void setDocumentLocator (Locator l)
{ locator = l; }
public void startDocument () throws SAXException
{
channel = new RssChannel ();
if (locator != null)
channel.sourceUri = locator.getSystemId ();
stack = new Stack ();
}
public void startElement (
String namespace,
String local,
String name,
Attributes attrs
) throws SAXException
{
stack.push (name);
if ("item".equals (name))
item = new RssItem ();
else if ("image".equals (name))
image = new RssImage ();
else if ("textinput".equals (name))
input = new RssTextInput ();
// parser misconfigured?
else if (name.length () == 0)
throw new SAXParseException ("XML names not available", locator);
}
public void characters (char buf [], int off, int len)
throws SAXException
{
String top = getCurrentElementName ();
String value = new String (buf, off, len);
if ("title".equals (top)) {
if (item != null)
item.title += value;
else if (image != null)
image.title += value;
else if (input != null)
input.title += value;
else
channel.title += value;
} else if ("description".equals (top)) {
if (item != null)
item.description += value;
else if (image != null)
image.description += value;
else if (input != null)
input.description += value;
else
channel.description += value;
} else if ("link".equals (top)) {
if (item != null)
item.link += value;
else if (image != null)
image.link += value;
else if (input != null)
input.link += value;
else
channel.link += value;
} else if ("url".equals (top)) {
image.url += value;
} else if ("name".equals (top)) {
input.name += value;
} else if ("language".equals (top)) {
channel.language += value;
} else if ("managingEditor".equals (top)) {
channel.managingEditor += value;
} else if ("webMaster".equals (top)) {
channel.webMaster += value;
} else if ("copyright".equals (top)) {
channel.copyright += value;
} else if ("lastBuildDate".equals (top)) {
channel.lastBuildDate += value;
} else if ("pubDate".equals (top)) {
channel.pubDate += value;
} else if ("docs".equals (top)) {
channel.docs += value;
} else if ("rating".equals (top)) {
channel.rating += value;
} // else ignore ... skipDays and so on.
}
public void endElement (
String namespace,
String local,
String name
) throws SAXException
{
if ("item".equals (name)) {
// patch item.link
channel.items.addElement (item);
item = null;
} else if ("image".equals (name)) {
// patch image.link
// (patch image.url)
channel.image = image;
image = null;
} else if ("textinput".equals (name)) {
// patch input.link
channel.textinput = input;
input = null;
} else if ("channel".equals (name)) {
// patch channel.link
}
}
}
|