87 public boolean isValid() { |
87 public boolean isValid() { |
88 return title != null && link != null && description != null; |
88 return title != null && link != null && description != null; |
89 } |
89 } |
90 |
90 |
91 protected void setField(String element, String value) { |
91 protected void setField(String element, String value) { |
92 if (RSSHandler.TITLE.equals(element)) |
92 if (RSSHandler.TITLE.equalsIgnoreCase(element)) |
93 title = value; |
93 title = value; |
94 else if (RSSHandler.LINK.equals(element)) { |
94 else if (RSSHandler.LINK.equalsIgnoreCase(element)) { |
95 try { |
95 try { |
96 link = new URL(value); |
96 link = new URL(value); |
97 } catch (MalformedURLException e) { |
97 } catch (MalformedURLException e) { |
98 // don't store malformed URLs |
98 // don't store malformed URLs |
99 Activator.logError("Bad URL", e); |
99 Activator.logError("Bad URL", e); |
100 } |
100 } |
101 } |
101 } |
102 else if (RSSHandler.DESCRIPTION.equals(element) || RSSHandler.SUMMARY.equals(element)) |
102 else if (RSSHandler.DESCRIPTION.equalsIgnoreCase(element) || RSSHandler.SUMMARY.equalsIgnoreCase(element)) |
103 description = value; |
103 description = value; |
104 else if (RSSHandler.PUBDATE.equals(element)) { |
104 else if (RSSHandler.PUBDATE.equalsIgnoreCase(element)) { |
105 pubDate = parseRFC822Date(value); |
105 pubDate = parseRFC822Date(value); |
106 } |
106 } |
107 else if (RSSHandler.CATEGORY.equals(element)) |
107 else if (RSSHandler.CATEGORY.equalsIgnoreCase(element)) |
108 categories.add(value); |
108 categories.add(value); |
109 } |
109 } |
110 |
110 |
111 private Date parseRFC822Date(String value) { |
111 private Date parseRFC822Date(String value) { |
112 SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); //$NON-NLS-1$ |
112 SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); //$NON-NLS-1$ |
156 private static final String ITEM = "item"; //$NON-NLS-1$ |
156 private static final String ITEM = "item"; //$NON-NLS-1$ |
157 private static final String PUBDATE = "pubDate"; //$NON-NLS-1$ |
157 private static final String PUBDATE = "pubDate"; //$NON-NLS-1$ |
158 private static final String CATEGORY = "category"; //$NON-NLS-1$ |
158 private static final String CATEGORY = "category"; //$NON-NLS-1$ |
159 private static final Set<String> charsElements = new HashSet<String>(); |
159 private static final Set<String> charsElements = new HashSet<String>(); |
160 static { |
160 static { |
161 charsElements.add(TITLE); |
161 charsElements.add(TITLE.toLowerCase()); |
162 charsElements.add(LINK); |
162 charsElements.add(LINK.toLowerCase()); |
163 charsElements.add(DESCRIPTION); |
163 charsElements.add(DESCRIPTION.toLowerCase()); |
164 charsElements.add(SUMMARY); |
164 charsElements.add(SUMMARY.toLowerCase()); |
165 charsElements.add(PUBDATE); |
165 charsElements.add(PUBDATE.toLowerCase()); |
166 charsElements.add(CATEGORY); |
166 charsElements.add(CATEGORY.toLowerCase()); |
167 } |
167 } |
168 |
168 |
169 private Channel curChannel; |
169 private Channel curChannel; |
170 private Item curItem; |
170 private Item curItem; |
171 private StringBuffer charsBuf; |
171 private StringBuffer charsBuf; |
175 this.rss = rss; |
175 this.rss = rss; |
176 } |
176 } |
177 |
177 |
178 @Override |
178 @Override |
179 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
179 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
180 if (charsElements.contains(qName)) { |
180 if (charsElements.contains(qName.toLowerCase())) { |
181 charsBuf = new StringBuffer(); |
181 charsBuf = new StringBuffer(); |
182 } |
182 } |
183 if (CHANNEL.equals(qName)) { |
183 if (CHANNEL.equalsIgnoreCase(qName)) { |
184 curChannel = new Channel(); |
184 curChannel = new Channel(); |
185 } |
185 } |
186 else if (ITEM.equals(qName) && curChannel != null) { |
186 else if (ITEM.equalsIgnoreCase(qName) && curChannel != null) { |
187 curItem = new Item(curChannel); |
187 curItem = new Item(curChannel); |
188 } |
188 } |
189 } |
189 } |
190 |
190 |
191 @Override |
191 @Override |
192 public void endElement(String uri, String localName, String qName) throws SAXException { |
192 public void endElement(String uri, String localName, String qName) throws SAXException { |
193 if (CHANNEL.equals(qName)) { |
193 if (CHANNEL.equalsIgnoreCase(qName)) { |
194 if (curChannel != null && curChannel.isValid() && curChannel.hasItems()) |
194 if (curChannel != null && curChannel.isValid() && curChannel.hasItems()) |
195 rss.addChannel(curChannel); |
195 rss.addChannel(curChannel); |
196 curChannel = null; |
196 curChannel = null; |
197 curItem = null; |
197 curItem = null; |
198 } |
198 } |
199 else if (ITEM.equals(qName)) { |
199 else if (ITEM.equalsIgnoreCase(qName)) { |
200 if (curChannel != null && curItem != null && curItem.isValid()) |
200 if (curChannel != null && curItem != null && curItem.isValid()) |
201 curChannel.addItem(curItem); |
201 curChannel.addItem(curItem); |
202 curItem = null; |
202 curItem = null; |
203 } |
203 } |
204 else if (charsBuf != null && curChannel != null) { |
204 else if (charsBuf != null && curChannel != null) { |