Skip to content

Commit a624ffe

Browse files
author
pborissow
committed
- Updated Date parser to use the javaxt.utils.Date class if it is available. Falls back to the native Date parser if the javaxt-core is not found.
- Added ability to create/output an RSS Document. See Feed.toXML() method. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-rss@933 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 62799b7 commit a624ffe

File tree

3 files changed

+166
-31
lines changed

3 files changed

+166
-31
lines changed

src/javaxt/rss/Feed.java

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
package javaxt.rss;
2+
import java.io.ByteArrayInputStream;
3+
import java.io.InputStream;
4+
import javax.xml.parsers.DocumentBuilder;
5+
import javax.xml.parsers.DocumentBuilderFactory;
26
import org.w3c.dom.*;
37

48
//******************************************************************************
@@ -26,7 +30,7 @@ public class Feed {
2630
//**************************************************************************
2731
//** Constructor
2832
//**************************************************************************
29-
protected Feed(){}
33+
public Feed(){}
3034

3135

3236
//**************************************************************************
@@ -123,9 +127,8 @@ public Location getLocation(){
123127
//**************************************************************************
124128
//** getLastUpdate
125129
//**************************************************************************
126-
public java.util.Date getLastUpdate(){
127-
return lastUpdate;
128-
}
130+
public java.util.Date getLastUpdate(){ return lastUpdate; }
131+
public void setLastUpdate(java.util.Date date){ this.lastUpdate = date; }
129132

130133

131134
//**************************************************************************
@@ -138,6 +141,15 @@ public Item[] getItems(){
138141
}
139142

140143

144+
//**************************************************************************
145+
//** addItem
146+
//**************************************************************************
147+
/** Used to add an RSS item to the Feed.
148+
*/
149+
public void addItem(Item item){
150+
items.add(item);
151+
}
152+
141153

142154
//**************************************************************************
143155
//** getRefreshInterval
@@ -149,7 +161,11 @@ public Item[] getItems(){
149161
public Integer getRefreshInterval(){
150162
return interval;
151163
}
152-
164+
165+
public void setRefreshInterval(Integer interval){
166+
this.interval = interval;
167+
}
168+
153169

154170
//**************************************************************************
155171
//** toString
@@ -166,5 +182,56 @@ public String toString(){
166182
}
167183
return out.toString();
168184
}
185+
186+
187+
//**************************************************************************
188+
//** toXML
189+
//**************************************************************************
190+
/** Returns an RSS/XML document for this Feed.
191+
*/
192+
public org.w3c.dom.Document toXML(){
193+
StringBuffer str = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
194+
str.append("<rss xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss/\">\n");
195+
str.append(" <channel>\n");
196+
str.append(" <title>" + getTitle() + "</title>\n");
197+
str.append(" <link>" + getLink() + "</link>\n");
198+
str.append(" <lastBuildDate>" + formatDate(lastUpdate==null ? new java.util.Date() : lastUpdate) + "</lastBuildDate>\n");
199+
str.append(" <generator>JavaXT RSS</generator>\n");
200+
str.append(" <ttl>" + (interval==null ? 15 : interval) + "</ttl>\n");
201+
for (Item item : items){
202+
str.append(item.toXML());
203+
}
204+
str.append(" </channel>\n");
205+
str.append("</rss>");
206+
207+
208+
return createDocument(str.toString());
209+
}
169210

211+
212+
//**************************************************************************
213+
//** createDocument
214+
//**************************************************************************
215+
private Document createDocument(String xml){
216+
try{
217+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
218+
DocumentBuilder builder = builderFactory.newDocumentBuilder();
219+
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
220+
return builder.parse(is);
221+
}
222+
catch(Exception e){
223+
//e.printStackTrace();
224+
return null;
225+
}
226+
}
227+
228+
229+
//**************************************************************************
230+
//** formatDate
231+
//**************************************************************************
232+
protected static String formatDate(java.util.Date date){
233+
java.text.DateFormat formatter = new java.text.SimpleDateFormat(
234+
"EEE, dd MMM yyyy HH:mm:ss zzz", java.util.Locale.US);
235+
return formatter.format(date);
236+
}
170237
}

src/javaxt/rss/Item.java

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
public class Item {
1313

14-
1514
private String title;
1615
private String description;
1716
private String author = null;
@@ -28,8 +27,9 @@ public class Item {
2827
//**************************************************************************
2928
//** Constructor
3029
//**************************************************************************
31-
protected Item(){}
32-
30+
public Item(){}
31+
32+
3333
//**************************************************************************
3434
//** Constructor
3535
//**************************************************************************
@@ -179,10 +179,7 @@ else if (nodeName.equals("long") || nodeName.equals(geoNS + ":long")){
179179
}
180180
}
181181

182-
183182

184-
185-
186183
//**************************************************************************
187184
//** getTitle
188185
//**************************************************************************
@@ -256,8 +253,8 @@ public void setLink(java.net.URL url){
256253
//**************************************************************************
257254
//** getDate
258255
//**************************************************************************
259-
/** Return the date/time stamp associated with the current entry. Uses the
260-
* pubDate if it exists. Otherwise, returns dc:date
256+
/** Return the date/time stamp associated with the current entry. Uses the
257+
* pubDate if it exists. Otherwise, returns dc:date
261258
*/
262259
public java.util.Date getDate(){
263260
return date;
@@ -300,14 +297,21 @@ public Location getLocation(){
300297
return location;
301298
}
302299

303-
304300

301+
//**************************************************************************
302+
//** getNodeList
303+
//**************************************************************************
304+
/** Returns the NodeList used to instantiate this class via the RSS Parser.
305+
* @deprecated This method will be removed in future releases.
306+
*/
305307
public NodeList getNodeList(){
306308
return nodeList;
307309
}
308310

309311

310-
312+
//**************************************************************************
313+
//** toString
314+
//**************************************************************************
311315
public String toString(){
312316
StringBuffer out = new StringBuffer();
313317
String br = "\r\n";
@@ -330,13 +334,58 @@ public String toString(){
330334
return out.toString();
331335
}
332336

333-
334-
// public String toXML(){
335-
//
336-
// java.text.DateFormat formatter = new java.text.SimpleDateFormat(
337-
// "EEE, dd MMM yyyy HH:mm:ss Z", java.util.Locale.ENGLISH);
338-
// String d = formatter.format(date);
339-
//
340-
// }
341337

338+
//**************************************************************************
339+
//** toXML
340+
//**************************************************************************
341+
/** Returns an XML fragment used by the Feed class to generate an RSS/XML
342+
* document.
343+
*/
344+
protected String toXML(){
345+
StringBuffer str = new StringBuffer();
346+
347+
java.util.HashMap<String, Object> info = new java.util.HashMap<String, Object>();
348+
349+
String title = getTitle();
350+
if (title!=null){
351+
title = title.trim();
352+
if (title.length()>0) info.put("title", title);
353+
}
354+
355+
String desc = getDescription();
356+
if (desc!=null){
357+
desc = desc.trim();
358+
if (desc.length()>0) info.put("description", desc);
359+
}
360+
361+
if (link!=null) info.put("link", link);
362+
if (date!=null) info.put("pubDate", date);
363+
364+
365+
if (!info.isEmpty()){
366+
str.append(" <item>\n");
367+
java.util.Iterator<String> it = info.keySet().iterator();
368+
while (it.hasNext()){
369+
String key = it.next();
370+
Object val = info.get(key);
371+
str.append(" <" + key + ">");
372+
if (val instanceof String){
373+
str.append("<![CDATA[");
374+
str.append(val);
375+
str.append("]]>");
376+
}
377+
else if (val instanceof java.util.Date){
378+
String d = Feed.formatDate((java.util.Date) val);
379+
str.append(d);
380+
}
381+
else{
382+
str.append(val);
383+
}
384+
str.append("</" + key + ">\n");
385+
}
386+
str.append(" </item>\n");
387+
}
388+
389+
return str.toString();
390+
}
342391
}

src/javaxt/rss/Parser.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,24 @@ private static boolean hasChildren(Node node){
209209

210210
private static String[] SupportedFormats = new String[] {
211211

212-
"EEE, d MMM yy HH:mm:ss z", // Mon, 07 Jun 76 13:02:09 EST
213212

214213
"EEE, d MMM yyyy HH:mm:ss z", // Mon, 7 Jun 1976 13:02:09 EST
215214
"EEE, dd MMM yyyy HH:mm:ss z", // Mon, 07 Jun 1976 13:02:09 EST
215+
"EEE, dd MMM yyyy HH:mm:ss", // Mon, 07 Jun 1976 13:02:09 EST
216216

217217
"EEE MMM dd HH:mm:ss z yyyy", // Mon Jun 07 13:02:09 EST 1976
218218
"EEE MMM d HH:mm:ss z yyyy", // Mon Jun 7 13:02:09 EST 1976
219219

220220
"EEE MMM dd HH:mm:ss yyyy", // Mon Jun 07 13:02:09 1976
221221
"EEE MMM d HH:mm:ss yyyy", // Mon Jun 7 13:02:09 1976
222222

223+
"EEE MMM dd yyyy HH:mm:ss z", //"Mon Jun 07 2013 00:00:00 GMT-0500 (Eastern Standard Time)"
224+
225+
"yyyy-MM-dd HH:mm:ss.SSS Z", // 1976-06-07 13:02:36.000 America/New_York
223226
"yyyy-MM-dd HH:mm:ss.SSSZ", // 1976-06-07 01:02:09.000-0500
224227
"yyyy-MM-dd HH:mm:ss.SSS", // 1976-06-07 01:02:09.000
225228

229+
"yyyy-MM-dd HH:mm:ss Z", // 1976-06-07 13:02:36 America/New_York
226230
"yyyy-MM-dd HH:mm:ssZ", // 1976-06-07 13:02:36-0500
227231
"yyyy-MM-dd HH:mm:ss", // 1976-06-07 01:02:09
228232

@@ -239,7 +243,7 @@ private static boolean hasChildren(Node node){
239243
"dd-MMM-yy h:mm:ss a", // 07-Jun-76 1:02:09 PM
240244
//"d-MMM-yy h:mm:ss a", // 7-Jun-76 1:02:09 PM
241245

242-
246+
"yyyy-MM-dd HH:mm Z", // 1976-06-07 13:02 America/New_York"
243247
"yyyy-MM-dd HH:mmZ", // 1976-06-07T13:02-0500
244248
"yyyy-MM-dd HH:mm", // 1976-06-07T13:02
245249
"yyyy-MM-dd", // 1976-06-07
@@ -253,7 +257,14 @@ private static boolean hasChildren(Node node){
253257
"M/d/yy h:mm:ss a", // 6/7/1976 1:02:09 PM
254258
"M/d/yy h:mm a", // 6/7/1976 1:02 PM
255259

260+
"MM/dd/yy HH:mm:ss Z", // 06/07/1976 13:02:09 America/New_York
261+
"MM/dd/yy HH:mm:ss", // 06/07/1976 13:02:09
262+
"MM/dd/yy HH:mm Z", // 06/07/1976 13:02 America/New_York
263+
"MM/dd/yy HH:mm", // 06/07/1976 13:02
264+
265+
"MM/dd/yyyy HH:mm:ss Z", // 06/07/1976 13:02:09 America/New_York
256266
"MM/dd/yyyy HH:mm:ss", // 06/07/1976 13:02:09
267+
"MM/dd/yyyy HH:mm Z", // 06/07/1976 13:02 America/New_York
257268
"MM/dd/yyyy HH:mm", // 06/07/1976 13:02
258269

259270
"M/d/yy", // 6/7/76
@@ -274,6 +285,15 @@ private static boolean hasChildren(Node node){
274285
protected static java.util.Date getDate(String date) throws java.text.ParseException {
275286

276287
try{
288+
//Try to parse the date using the javaxt-core library
289+
Class Date = new ClassLoader("javaxt.utils.Date", "javaxt-core.jar").load();
290+
java.lang.reflect.Constructor constructor = Date.getDeclaredConstructor(new Class[] {String.class});
291+
java.lang.reflect.Method method = Date.getDeclaredMethod("getDate");
292+
Object instance = constructor.newInstance(new Object[] { date });
293+
return (java.util.Date) method.invoke(instance);
294+
}
295+
catch(Exception e){
296+
277297

278298
//Special Case: Java fails to parse the "T" in strings like
279299
//"1976-06-07T01:02:09.000" and "1976-06-07T13:02-0500"
@@ -285,23 +305,22 @@ protected static java.util.Date getDate(String date) throws java.text.ParseExcep
285305

286306

287307
//Loop through all known date formats and try to convert the string to a date
288-
for (int i=0; i<SupportedFormats.length; i++){
308+
for (String format : SupportedFormats){
289309

290310
//Special Case: Java fails to parse the "Z" in "1976-06-07 00:00:00Z"
291-
if (date.endsWith("Z") && SupportedFormats[i].endsWith("Z")){
311+
if (date.endsWith("Z") && format.endsWith("Z")){
292312
date = date.substring(0, date.length()-1) + "UTC";
293313
}
294314

295315
try{
296-
return parseDate(date, SupportedFormats[i]);
316+
return parseDate(date, format);
297317
}
298-
catch(java.text.ParseException e){
318+
catch(java.text.ParseException ex){
299319
}
300320
}
301321

302322
}
303-
catch(Exception e){
304-
}
323+
305324

306325
//If we're still here, throw an exception
307326
throw new java.text.ParseException("Failed to parse date: " + date, 0);

0 commit comments

Comments
 (0)