KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > welcome > content > CombinationRSSFeed


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.welcome.content;
21
22 import java.io.IOException JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Locale JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38 import org.xml.sax.XMLReader JavaDoc;
39
40 /**
41  *
42  * @author S. Aubrecht
43  */

44 public class CombinationRSSFeed extends RSSFeed {
45
46     private String JavaDoc url1;
47     private String JavaDoc url2;
48
49     /** Creates a new instance of CombinationRSSFeed */
50     public CombinationRSSFeed( String JavaDoc url1, String JavaDoc url2, boolean showProxyButton ) {
51         super( showProxyButton );
52         this.url1 = url1;
53         this.url2 = url2;
54     }
55
56     protected ArrayList JavaDoc<FeedItem> buildItemList() throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
57         XMLReader JavaDoc 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 JavaDoc(url1) );
63
64         ArrayList JavaDoc<FeedItem> res = new ArrayList JavaDoc<FeedItem>( 2*NEWS_COUNT );
65         res.addAll( handler.getItemList() );
66
67         handler = new FeedHandler();
68         reader.setContentHandler( handler );
69         reader.parse( new InputSource JavaDoc(url2) );
70
71         res.addAll( handler.getItemList() );
72
73         return sortNodes( res );
74     }
75
76     private ArrayList JavaDoc<FeedItem> sortNodes( ArrayList JavaDoc<FeedItem> res ) {
77         Collections.sort( res, new DateFeedItemComparator() );
78         return res;
79     }
80
81     private static class DateFeedItemComparator implements Comparator JavaDoc<FeedItem> {
82     private static DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc( "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH ); // NOI18N
83
private static DateFormat JavaDoc dateFormatShort = new SimpleDateFormat JavaDoc( "EEE, dd MMM yyyy", Locale.ENGLISH ); // NOI18N
84

85     public int compare(FeedItem item1, FeedItem item2) {
86             Date JavaDoc date1 = extractDate( item1 );
87             Date JavaDoc 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 JavaDoc extractDate( FeedItem item ) {
104             try {
105                 if( null != item.dateTime )
106                     return dateFormat.parse( item.dateTime );
107             } catch( ParseException JavaDoc pE ) {
108                 try {
109                     return dateFormatShort.parse( item.dateTime );
110                 } catch( ParseException JavaDoc otherPE ) {
111                     //ignore
112
}
113             }
114             return null;
115         }
116     }
117 }
118
Popular Tags