1 19 20 package org.netbeans.modules.welcome.content; 21 22 import java.io.IOException ; 23 import java.text.DateFormat ; 24 import java.text.ParseException ; 25 import java.text.SimpleDateFormat ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Comparator ; 29 import java.util.Date ; 30 import java.util.Locale ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import org.netbeans.modules.welcome.content.RSSFeed.ErrorCatcher; 33 import org.netbeans.modules.welcome.content.RSSFeed.FeedHandler; 34 import org.netbeans.modules.welcome.content.RSSFeed.FeedItem; 35 import org.openide.xml.XMLUtil; 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.XMLReader ; 39 40 44 public class CombinationRSSFeed extends RSSFeed { 45 46 private String url1; 47 private String url2; 48 49 50 public CombinationRSSFeed( String url1, String url2, boolean showProxyButton ) { 51 super( showProxyButton ); 52 this.url1 = url1; 53 this.url2 = url2; 54 } 55 56 protected ArrayList <FeedItem> buildItemList() throws SAXException , ParserConfigurationException , IOException { 57 XMLReader reader = XMLUtil.createXMLReader( false, true ); 58 FeedHandler handler = new FeedHandler(); 59 reader.setContentHandler( handler ); 60 reader.setEntityResolver( org.openide.xml.EntityCatalog.getDefault() ); 61 reader.setErrorHandler( new ErrorCatcher() ); 62 reader.parse( new InputSource (url1) ); 63 64 ArrayList <FeedItem> res = new ArrayList <FeedItem>( 2*NEWS_COUNT ); 65 res.addAll( handler.getItemList() ); 66 67 handler = new FeedHandler(); 68 reader.setContentHandler( handler ); 69 reader.parse( new InputSource (url2) ); 70 71 res.addAll( handler.getItemList() ); 72 73 return sortNodes( res ); 74 } 75 76 private ArrayList <FeedItem> sortNodes( ArrayList <FeedItem> res ) { 77 Collections.sort( res, new DateFeedItemComparator() ); 78 return res; 79 } 80 81 private static class DateFeedItemComparator implements Comparator <FeedItem> { 82 private static DateFormat dateFormat = new SimpleDateFormat ( "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH ); private static DateFormat dateFormatShort = new SimpleDateFormat ( "EEE, dd MMM yyyy", Locale.ENGLISH ); 85 public int compare(FeedItem item1, FeedItem item2) { 86 Date date1 = extractDate( item1 ); 87 Date date2 = extractDate( item2 ); 88 89 if( null == date1 && null == date2 ) 90 return 0; 91 else if( null == date1 ) 92 return 1; 93 else if( null == date2 ) 94 return -1; 95 if( date1.after( date2 ) ) { 96 return -1; 97 } else if( date1.before( date2 ) ) { 98 return 1; 99 } 100 return 0; 101 } 102 103 private Date extractDate( FeedItem item ) { 104 try { 105 if( null != item.dateTime ) 106 return dateFormat.parse( item.dateTime ); 107 } catch( ParseException pE ) { 108 try { 109 return dateFormatShort.parse( item.dateTime ); 110 } catch( ParseException otherPE ) { 111 } 113 } 114 return null; 115 } 116 } 117 } 118 | Popular Tags |