001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.welcome.content;
043:
044: import java.io.IOException;
045: import java.text.DateFormat;
046: import java.text.ParseException;
047: import java.text.SimpleDateFormat;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.Comparator;
051: import java.util.Date;
052: import java.util.List;
053: import java.util.Locale;
054: import javax.xml.parsers.ParserConfigurationException;
055: import org.netbeans.modules.welcome.content.RSSFeed.ErrorCatcher;
056: import org.netbeans.modules.welcome.content.RSSFeed.FeedHandler;
057: import org.netbeans.modules.welcome.content.RSSFeed.FeedItem;
058: import org.openide.xml.XMLUtil;
059: import org.xml.sax.InputSource;
060: import org.xml.sax.SAXException;
061: import org.xml.sax.XMLReader;
062:
063: /**
064: * Combines two RSS feeds into one.
065: *
066: * @author S. Aubrecht
067: */
068: public class CombinationRSSFeed extends RSSFeed {
069:
070: private String url1;
071: private String url2;
072: private int maxItemCount;
073:
074: /** Creates a new instance of CombinationRSSFeed */
075: public CombinationRSSFeed(String url1, String url2,
076: boolean showProxyButton, int maxItemCount) {
077: super (showProxyButton);
078: this .maxItemCount = maxItemCount;
079: this .url1 = url1;
080: this .url2 = url2;
081: }
082:
083: @Override
084: protected List<FeedItem> buildItemList() throws SAXException,
085: ParserConfigurationException, IOException {
086: XMLReader reader = XMLUtil.createXMLReader(false, true);
087: FeedHandler handler = new FeedHandler(getMaxItemCount());
088: reader.setContentHandler(handler);
089: reader.setEntityResolver(org.openide.xml.EntityCatalog
090: .getDefault());
091: reader.setErrorHandler(new ErrorCatcher());
092: reader.parse(new InputSource(url1));
093:
094: ArrayList<FeedItem> res = new ArrayList<FeedItem>(
095: 2 * getMaxItemCount());
096: res.addAll(handler.getItemList());
097:
098: handler = new FeedHandler(getMaxItemCount());
099: reader.setContentHandler(handler);
100: reader.parse(new InputSource(url2));
101:
102: res.addAll(handler.getItemList());
103:
104: List<FeedItem> items = sortNodes(res);
105: if (items.size() > getMaxItemCount()) {
106: items = items.subList(0, getMaxItemCount());
107: }
108: return items;
109: }
110:
111: private ArrayList<FeedItem> sortNodes(ArrayList<FeedItem> res) {
112: Collections.sort(res, new DateFeedItemComparator());
113: return res;
114: }
115:
116: @Override
117: protected int getMaxItemCount() {
118: return this .maxItemCount;
119: }
120:
121: private static class DateFeedItemComparator implements
122: Comparator<FeedItem> {
123: private static DateFormat dateFormat = new SimpleDateFormat(
124: "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH); // NOI18N
125: private static DateFormat dateFormatShort = new SimpleDateFormat(
126: "EEE, dd MMM yyyy", Locale.ENGLISH); // NOI18N
127:
128: public int compare(FeedItem item1, FeedItem item2) {
129: Date date1 = extractDate(item1);
130: Date date2 = extractDate(item2);
131:
132: if (null == date1 && null == date2)
133: return 0;
134: else if (null == date1)
135: return 1;
136: else if (null == date2)
137: return -1;
138: if (date1.after(date2)) {
139: return -1;
140: } else if (date1.before(date2)) {
141: return 1;
142: }
143: return 0;
144: }
145:
146: private Date extractDate(FeedItem item) {
147: try {
148: if (null != item.dateTime)
149: return dateFormat.parse(item.dateTime);
150: } catch (ParseException pE) {
151: try {
152: return dateFormatShort.parse(item.dateTime);
153: } catch (ParseException otherPE) {
154: //ignore
155: }
156: }
157: return null;
158: }
159: }
160: }
|