#副標=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(""); } 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======= =========end========= =========box程式8======== public interface MyConstants extends Constants { String helloRUNPC(); String goodbyeRUNPC(); } ==========end========== ========box程式9========== #MyConstants.properties helloRUNPC = hello, RUNPC goodbyeRUNPC = goodbye, RUNPC #MyConstants.properties.zh_TW helloRUNPC = 你好, RUNPC goodbyeRUNPC = 再見, RUNPC ==========end========== =======box程式10======= public void useMyConstants() { MyConstants myConstants = (MyConstants) GWT.create(MyConstants.class); Window.alert(myConstants.helloRUNPC()); } ========end========= ==========box程式11=========== var CurrentTheme = { highlightColor: "#FFFFFF", shadowColor: "#808080", errorColor: "#FF0000", errorIconSrc: "stopsign.gif" }; ==========end=========== =========box程式12========= public void useThemeDictionary() { Dictionary theme = Dictionary.getDictionary("CurrentTheme"); String highlightColor = theme.get("highlightColor"); String shadowColor = theme.get("shadowColor"); applyShadowStyle(highlightColor, shadowColor); String errorColor = theme.get("errorColor"); String errorIconSrc = theme.get("errorIconSrc"); Image errorImg = new Image(errorIconSrc); } =========end========== ========box程式13========== i18nCreator -eclipse RUNPC com.RUNPC.RSSReader.client.MyConstants Created directory src Created directory src\com\RUNPC\RSSReader\client Created file src\com\RUNPC\RSSReader\client\ MyConstants.properties Created file MyConstants-i18n.launch MyConstants-i18n.cmd ==========end========== =========box程式14======== =========end=========== ===========box程式15======== =========end========= ========box程式16======= public static native void alert(String msg) /*-{ $wnd.alert(msg); }-*/ ========end===========