#副標=AJAX開發工具(3)
#大標=使用Google Web Toolkit開發RSS閱讀器(下)
#眉標=AJAX
#作者=文/沈炳宏
========BOX 程式1=========
import com.runpc.ajax.RssReader.client.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.xml.parsers.*;
public class RssFeedParser extends DefaultHandler {
private URL url;
private List result;
private Entry target;
private StringBuffer text;
private boolean inContent = false;
private SimpleDateFormat sdf =
new SimpleDateFormat (
"EEE, d MMM yyyy HH:mm:ss Z",
new Locale("en"));
public RssFeedParser(String urlstring)
throws Exception{}
public List getPostings() throws Exception {}
public void startElement(String uri,
String localName,
String qName,Attributes attributes) {}
public void characters(char[] ch, int start, int length){}
public void endElement(String uri, String localName,
String qName) {}
==========END===========
=========BOX 程式2========
public RssFeedParser(String urlstring)
throws Exception {
this.url = new URL(urlstring);
}
=========END============
======BOX 程式3========
public List getPostings() throws Exception {
HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
InputStream is = conn.getInputStream();
this.result = new LinkedList();
SAXParser parser =
SAXParserFactory.newInstance().newSAXParser();
parser.parse(is, this);
return result;
}
==========END==========
=======BOX程式4=========
public void startElement(String uri, String localName,
String qName,
Attributes attributes) {
if (!inContent) {
this.text = new StringBuffer();
} else {
this.text.append("<").append(qName);
int l = attributes.getLength();
for (int i = 0; i < l; i++) {
this.text.append(" ").
append(attributes.getQName(i))
.append("=\"").append(attributes.getValue(i))
.append("\"");
}
this.text.append(">");
}
if (("content".equals(qName)) ||
("description".equals(qName))) {
inContent = true;
}
if (("item".equals(qName)) ||
("entry".equals(qName))) {
this.target = new Entry();
this.result.add(target);
}
}
=========End=========
=========box程式5===========
public void characters(char[] ch, int start, int length)
throws SAXException {
this.text.append(ch, start, length);
}
=========end===========
=======box程式6=========
public void endElement(String uri,
String localName, String qName) {
if (inContent) {
this.text.append("").
append(qName).append(">");
}
if (("item".equals(qName)) ||
("entry".equals(qName))) {
this.target = null;
}
if (this.target != null) {
if ("title".equals(qName)) {
this.target.setTitle(this.text.toString());
}
if ("id".equals(qName)) {
this.target.setExternalId(this.text.toString());
}
if ("link".equals(qName)) {
this.target.setExternalId(this.text.toString());
}
if (("description".equals(qName)) ||
("content".equals(qName))) {
this.target.setSummary(this.text.toString());
this.target.setContent(this.text.toString());
this.inContent = false;
}
if (("media:category".equals(qName)) ||
("dc:subject".equals(qName) ||
("category".equals(qName)))) {
this.target.setKeywords(this.text.toString());
}
if ("author".equals(qName) ) {
this.target.setAuthor(this.text.toString());
}
if (("pubDate".equals(qName)) ||
("created".equals(qName))) {
try {
Date date = sdf.parse(text.toString());
this.target.setCreationDate(date.getTime());
SimpleDateFormat sdf2 =
new SimpleDateFormat("MMM d");
this.target.setFormattedDate(sdf2.format(date));
} catch (Exception e) {
e.printStackTrace();
this.target.setCreationDate(System.currentTimeMillis());
}
}
}
}
===========end==========
========box程式7=======