2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
32 import java.io.InputStream;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.Vector;
40 import mir.util.HTTPClientHelper;
41 import mir.util.xml.*;
42 import mir.util.xml.XMLParserEngine;
47 * <p>Description: </p>
48 * <p>Copyright: Copyright (c) 2003</p>
50 * @author not attributable
54 public class RSS091Reader {
55 private final static String MAPPED_CHANNEL_PROPERTIES[][] =
57 {"link", "rss:link" },
58 {"title", "rss:title" },
59 {"description", "rss:description" },
60 {"item", "rss:item" },
61 {"language", "dc:language" }
64 private final static String MAPPED_ITEM_PROPERTIES[][] =
66 {"link", "rss:link" },
67 {"title", "rss:title" },
68 {"description", "rss:description" },
69 {"author", "dc:creator" },
72 private Map mappedChannelProperties = new HashMap();
73 private Map mappedItemProperties = new HashMap();
75 public RSS091Reader() {
78 for (i=0; i<MAPPED_CHANNEL_PROPERTIES.length; i++) {
79 mappedChannelProperties.put(MAPPED_CHANNEL_PROPERTIES[i][0], MAPPED_CHANNEL_PROPERTIES[i][1]);
82 for (i=0; i<MAPPED_ITEM_PROPERTIES.length; i++) {
83 mappedItemProperties.put(MAPPED_ITEM_PROPERTIES[i][0], MAPPED_ITEM_PROPERTIES[i][1]);
87 public RSSData parseInputStream(InputStream aStream) throws RSSExc, RSSFailure {
89 RSSData result = new RSSData();
90 XMLParserEngine.getInstance().parse("html", aStream, new RootSectionHandler(result));
95 throw new RSSFailure(t);
99 public RSSData parseInputStream(InputStream aStream, String anEncoding) throws RSSExc, RSSFailure {
101 RSSData result = new RSSData();
102 XMLParserEngine.getInstance().parse("html", aStream, anEncoding, new RootSectionHandler(result));
106 catch (Throwable t) {
107 throw new RSSFailure(t);
111 public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
113 HTTPClientHelper httpClientHelper = new HTTPClientHelper();
114 InputStream inputStream = httpClientHelper.getUrl(anUrl);
116 if (inputStream==null)
117 throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
119 RSSData theRSSData = parseInputStream(inputStream);
120 httpClientHelper.releaseHTTPConnection();
124 catch (Throwable t) {
125 throw new RSSFailure(t);
129 public RSSData parseUrl(String anUrl, String anEncoding) throws RSSExc, RSSFailure {
131 HTTPClientHelper httpClientHelper = new HTTPClientHelper();
132 InputStream inputStream = httpClientHelper.getUrl(anUrl);
134 if (inputStream==null)
135 throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
137 RSSData theRSSData = parseInputStream(inputStream, anEncoding);
138 httpClientHelper.releaseHTTPConnection();
142 catch (Throwable t) {
143 throw new RSSFailure(t);
147 private class RootSectionHandler extends mir.util.xml.AbstractSectionHandler {
148 private RSSData data;
150 public RootSectionHandler(RSSData aData) {
154 public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
155 if (aTag.getLocalName().equals("rss")) {
156 return new RSS091SectionHandler(data);
159 throw new XMLParserFailure(new RSSExc("'rss' tag expected"));
162 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
165 public void characters(String aCharacters) throws XMLParserExc {
166 if (aCharacters.trim().length()>0)
167 throw new XMLParserExc("No character data allowed here");
170 public void finishSection() throws XMLParserExc {
174 private class RSS091SectionHandler extends mir.util.xml.AbstractSectionHandler {
175 private RSSData data;
178 public RSS091SectionHandler(RSSData aData) {
182 public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
183 if (aTag.getLocalName().equals("channel"))
184 return new RSS091ChannelSectionHandler(data);
186 throw new XMLParserExc("channel tag expected, " + aTag.getLocalName() + " found");
189 public void characters(String aCharacters) throws XMLParserExc {
190 if (aCharacters.trim().length()>0)
191 throw new XMLParserExc("No character data allowed here");
194 public void finishSection() throws XMLParserExc {
198 private class RSS091ChannelSectionHandler extends mir.util.xml.AbstractSectionHandler {
199 private String currentTag;
201 private RSSData data;
203 private RDFResource channel;
204 private Map attributes;
206 public RSS091ChannelSectionHandler(RSSData aData) {
208 items = new Vector();
209 channel = new RDFResource("rss:channel");
210 attributes = new HashMap();
213 public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
214 String tag = aTag.getLocalName();
216 if (tag.equals("item"))
217 return new RSS091ItemSectionHandler();
218 else if (mappedChannelProperties.containsKey(tag)) {
219 currentTag=(String) mappedChannelProperties.get(tag);
220 return new PCDATASectionHandler();
223 return new DiscardingSectionHandler();
226 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
227 if (aHandler instanceof PCDATASectionHandler) {
228 attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
230 else if (aHandler instanceof RSS091ItemSectionHandler) {
231 items.add((((RSS091ItemSectionHandler) aHandler).getItem()));
235 public void characters(String aCharacters) throws XMLParserExc {
236 if (aCharacters.trim().length()>0)
237 throw new XMLParserExc("No character data allowed here");
240 public void finishSection() throws XMLParserExc {
241 Iterator i = items.iterator();
243 while (i.hasNext()) {
244 data.addResource((RDFResource) i.next());
249 private class RSS091ItemSectionHandler extends mir.util.xml.AbstractSectionHandler {
250 private String currentTag;
252 private RDFResource item;
253 private Map attributes;
255 public RSS091ItemSectionHandler() {
256 attributes = new HashMap();
259 public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
260 String tag = aTag.getLocalName();
262 if (mappedItemProperties.containsKey(tag)) {
263 currentTag=(String) mappedItemProperties.get(tag);
264 return new PCDATASectionHandler();
267 return new DiscardingSectionHandler();
270 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
271 if (aHandler instanceof PCDATASectionHandler) {
272 attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
276 public void characters(String aCharacters) throws XMLParserExc {
277 if (aCharacters.trim().length()>0)
278 throw new XMLParserExc("No character data allowed here");
281 public void finishSection() throws XMLParserExc {
282 item = new RDFResource("rss:item", (String) attributes.get("rss:link"));
284 Iterator i = attributes.entrySet().iterator();
285 while (i.hasNext()) {
286 Map.Entry entry = (Map.Entry) i.next();
288 item.set((String) entry.getKey(), entry.getValue());
292 public RDFResource getItem() {
298 private class PCDATASectionHandler extends mir.util.xml.AbstractSectionHandler {
299 private StringBuffer data;
301 public PCDATASectionHandler() {
302 data = new StringBuffer();
305 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
306 throw new XMLParserFailure(new RSSExc("No subtags allowed here"));
309 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
312 public void characters(String aCharacters) throws XMLParserExc {
313 data.append(aCharacters);
316 public void finishSection() throws XMLParserExc {
319 public String getData() {
320 return data.toString();
325 private class RDFSequenceSectionHandler extends mir.util.xml.AbstractSectionHandler {
328 public RDFSequenceSectionHandler() {
329 items = new Vector();
332 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
333 if (aTag.equals("rdf:li")) {
334 String item = (String) anAttributes.get("rdf:resource");
340 return new DiscardingSectionHandler();
343 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
346 public void characters(String aCharacters) throws XMLParserExc {
349 public void finishSection() throws XMLParserExc {
352 public List getItems() {
357 private class DiscardingSectionHandler extends mir.util.xml.AbstractSectionHandler {
358 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
362 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
365 public void characters(String aCharacters) throws XMLParserExc {
368 public void finishSection() throws XMLParserExc {