2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
34 import java.io.InputStream;
\r
35 import java.net.URL;
\r
36 import java.util.List;
\r
37 import java.util.Map;
\r
38 import java.util.Vector;
\r
40 import mir.util.XMLReader;
\r
41 import mir.util.XMLReaderTool;
\r
46 * <p>Description: </p>
\r
47 * <p>Copyright: Copyright (c) 2003</p>
\r
49 * @author not attributable
\r
53 public class RSSReader {
\r
54 public RSSReader() {
\r
57 public RSSData parseInputStream(InputStream aStream) throws RSSExc, RSSFailure {
\r
59 XMLReader xmlReader = new XMLReader();
\r
60 RSSData result = new RSSData();
\r
61 xmlReader.parseInputStream(aStream, new RootSectionHandler(result));
\r
65 catch (Throwable t) {
\r
66 throw new RSSFailure(t);
\r
70 public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
\r
72 InputStream inputStream = (InputStream) new URL(anUrl).getContent(new Class[] {InputStream.class});
\r
74 if (inputStream==null)
\r
75 throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
\r
77 return parseInputStream(inputStream);
\r
79 catch (Throwable t) {
\r
80 throw new RSSFailure(t);
\r
84 private static class RootSectionHandler extends XMLReader.AbstractSectionHandler {
\r
85 private RSSData data;
\r
87 public RootSectionHandler(RSSData aData) {
\r
91 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
92 if (XMLReaderTool.getLocalNameFromQualifiedName(aTag).equals("RDF")) {
\r
93 return new RDFSectionHandler(data);
\r
96 throw new XMLReader.XMLReaderFailure(new RSSExc("'RDF' tag expected"));
\r
99 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
102 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
103 if (aCharacters.trim().length()>0)
\r
104 throw new XMLReader.XMLReaderExc("No character data allowed here");
\r
107 public void finishSection() throws XMLReader.XMLReaderExc {
\r
111 private static class RDFSectionHandler extends XMLReader.AbstractSectionHandler {
\r
112 private RSSData data;
\r
114 public RDFSectionHandler(RSSData aData) {
\r
118 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
119 String identifier = (String) anAttributes.get("rdf:about");
\r
121 if (aTag.equals("channel")) {
\r
122 if (identifier==null)
\r
123 throw new XMLReader.XMLReaderFailure(new RSSExc("Missing rdf:about"));
\r
125 return new ChannelSectionHandler(identifier);
\r
127 else if (aTag.equals("item")) {
\r
128 if (identifier==null)
\r
129 throw new XMLReader.XMLReaderFailure(new RSSExc("Missing rdf:about"));
\r
131 return new ItemSectionHandler(identifier);
\r
134 return new DiscardingSectionHandler();
\r
137 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
138 if (aHandler instanceof ItemSectionHandler) {
\r
139 data.addItem(((ItemSectionHandler) aHandler).getItem());
\r
141 else if (aHandler instanceof ChannelSectionHandler) {
\r
142 data.setChannel(((ChannelSectionHandler) aHandler).getChannel());
\r
146 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
147 if (aCharacters.trim().length()>0)
\r
148 throw new XMLReader.XMLReaderExc("No character data allowed here");
\r
151 public void finishSection() throws XMLReader.XMLReaderExc {
\r
155 private static class ChannelSectionHandler extends XMLReader.AbstractSectionHandler {
\r
156 private String image;
\r
157 private String currentTag;
\r
158 private RSSChannel channel;
\r
160 public ChannelSectionHandler(String anIdentifier) {
\r
161 channel = new RSSChannel(anIdentifier);
\r
164 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
166 if (currentTag.equals("items")) {
\r
167 return new ChannelItemsSectionHandler();
\r
169 else if (currentTag.equals("description") ||
\r
170 currentTag.equals("link") ||
\r
171 currentTag.equals("title")) {
\r
172 return new PCDATASectionHandler();
\r
175 return new DiscardingSectionHandler();
\r
178 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
179 if (currentTag.equals("items")) {
\r
180 channel.setItems(((ChannelItemsSectionHandler) aHandler).getItems());
\r
182 if (currentTag.equals("description")) {
\r
183 channel.setDescription(((PCDATASectionHandler) aHandler).getData());
\r
185 else if (currentTag.equals("title")) {
\r
186 channel.setTitle(((PCDATASectionHandler) aHandler).getData());
\r
188 else if (currentTag.equals("link")) {
\r
189 channel.setLink(((PCDATASectionHandler) aHandler).getData());
\r
193 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
194 if (aCharacters.trim().length()>0)
\r
195 throw new XMLReader.XMLReaderExc("No character data allowed here");
\r
198 public void finishSection() throws XMLReader.XMLReaderExc {
\r
201 public RSSChannel getChannel () {
\r
206 private static class ChannelItemsSectionHandler extends XMLReader.AbstractSectionHandler {
\r
207 private List items;
\r
209 public ChannelItemsSectionHandler() {
\r
210 items = new Vector();
\r
213 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
214 if (aTag.equals("rdf:Seq"))
\r
215 return new RDFSequenceSectionHandler();
\r
217 return new DiscardingSectionHandler();
\r
220 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
221 if (aHandler instanceof RDFSequenceSectionHandler) {
\r
222 items.addAll(((RDFSequenceSectionHandler) aHandler).getItems());
\r
226 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
227 if (aCharacters.trim().length()>0)
\r
228 throw new XMLReader.XMLReaderExc("No character data allowed here");
\r
231 public void finishSection() throws XMLReader.XMLReaderExc {
\r
234 public List getItems() {
\r
239 private static class ItemSectionHandler extends XMLReader.AbstractSectionHandler {
\r
240 private String currentTag;
\r
241 private RSSItem item;
\r
243 public ItemSectionHandler(String anIdentifier) {
\r
244 item = new RSSItem(anIdentifier);
\r
247 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
250 if (currentTag.equals("description") ||
\r
251 currentTag.equals("link") ||
\r
252 currentTag.equals("title")) {
\r
253 return new PCDATASectionHandler();
\r
256 return new DiscardingSectionHandler();
\r
259 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
260 if (currentTag.equals("description")) {
\r
261 item.setDescription(((PCDATASectionHandler) aHandler).getData());
\r
263 else if (currentTag.equals("title")) {
\r
264 item.setTitle(((PCDATASectionHandler) aHandler).getData());
\r
266 else if (currentTag.equals("link")) {
\r
267 item.setLink(((PCDATASectionHandler) aHandler).getData());
\r
271 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
272 if (aCharacters.trim().length()>0)
\r
273 throw new XMLReader.XMLReaderExc("No character data allowed here");
\r
276 public void finishSection() throws XMLReader.XMLReaderExc {
\r
279 public RSSItem getItem() {
\r
284 private static class PCDATASectionHandler extends XMLReader.AbstractSectionHandler {
\r
285 private StringBuffer data;
\r
287 public PCDATASectionHandler() {
\r
288 data = new StringBuffer();
\r
291 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
292 throw new XMLReader.XMLReaderFailure(new RSSExc("No subtags allowed here"));
\r
295 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
298 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
299 data.append(aCharacters);
\r
302 public void finishSection() throws XMLReader.XMLReaderExc {
\r
305 public String getData() {
\r
306 return data.toString();
\r
311 private static class RDFSequenceSectionHandler extends XMLReader.AbstractSectionHandler {
\r
312 private List items;
\r
314 public RDFSequenceSectionHandler() {
\r
315 items = new Vector();
\r
318 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
319 if (aTag.equals("rdf:li")) {
\r
320 String item = (String) anAttributes.get("rdf:resource");
\r
326 return new DiscardingSectionHandler();
\r
329 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
332 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
335 public void finishSection() throws XMLReader.XMLReaderExc {
\r
338 public List getItems() {
\r
343 private static class RDFLiteralSectionHandler extends XMLReader.AbstractSectionHandler {
\r
344 private StringBuffer data;
\r
345 private String tag;
\r
347 public RDFLiteralSectionHandler() {
\r
348 data = new StringBuffer();
\r
351 protected StringBuffer getData() {
\r
355 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
357 data.append("<"+tag+">");
\r
359 return new RDFLiteralSectionHandler();
\r
362 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
363 data.append(((RDFLiteralSectionHandler) aHandler).getData());
\r
364 data.append("</"+tag+">");
\r
367 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
368 data.append(aCharacters);
\r
371 public void finishSection() throws XMLReader.XMLReaderExc {
\r
375 private static class DiscardingSectionHandler extends XMLReader.AbstractSectionHandler {
\r
376 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
380 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
383 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
386 public void finishSection() throws XMLReader.XMLReaderExc {
\r