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.List;
36 import java.util.Vector;
39 import mir.util.XMLReader;
45 * <p>Description: </p>
46 * <p>Copyright: Copyright (c) 2003</p>
48 * @author not attributable
52 public class RSS091Reader {
53 private final static String MAPPED_CHANNEL_PROPERTIES[][] =
55 {"link", "rss:link" },
56 {"title", "rss:title" },
57 {"description", "rss:description" },
58 {"item", "rss:item" },
59 {"language", "dc:language" }
62 private final static String MAPPED_ITEM_PROPERTIES[][] =
64 {"link", "rss:link" },
65 {"title", "rss:title" },
66 {"description", "rss:description" },
67 {"author", "dc:creator" },
70 private Map mappedChannelProperties = new HashMap();
71 private Map mappedItemProperties = new HashMap();
73 public RSS091Reader() {
76 for (i=0; i<MAPPED_CHANNEL_PROPERTIES.length; i++) {
77 mappedChannelProperties.put(MAPPED_CHANNEL_PROPERTIES[i][0], MAPPED_CHANNEL_PROPERTIES[i][1]);
80 for (i=0; i<MAPPED_ITEM_PROPERTIES.length; i++) {
81 mappedItemProperties.put(MAPPED_ITEM_PROPERTIES[i][0], MAPPED_ITEM_PROPERTIES[i][1]);
85 public RSSData parseInputStream(InputStream aStream) throws RSSExc, RSSFailure {
87 XMLReader xmlReader = new XMLReader(false);
88 RSSData result = new RSSData();
89 xmlReader.parseInputStream(aStream, new RootSectionHandler(result));
94 throw new RSSFailure(t);
98 public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
100 InputStream inputStream = (InputStream) new URL(anUrl).getContent(new Class[] {InputStream.class});
102 if (inputStream==null)
103 throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
105 return parseInputStream(inputStream);
107 catch (Throwable t) {
108 throw new RSSFailure(t);
112 private class RootSectionHandler extends XMLReader.AbstractSectionHandler {
113 private RSSData data;
115 public RootSectionHandler(RSSData aData) {
119 public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
120 if (aTag.getLocalName().equals("rss")) {
121 return new RSS091SectionHandler(data);
124 throw new XMLReader.XMLReaderFailure(new RSSExc("'rss' tag expected"));
127 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
130 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
131 if (aCharacters.trim().length()>0)
132 throw new XMLReader.XMLReaderExc("No character data allowed here");
135 public void finishSection() throws XMLReader.XMLReaderExc {
139 private class RSS091SectionHandler extends XMLReader.AbstractSectionHandler {
140 private RSSData data;
143 public RSS091SectionHandler(RSSData aData) {
147 public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
148 if (aTag.getLocalName().equals("channel"))
149 return new RSS091ChannelSectionHandler(data);
151 throw new XMLReader.XMLReaderExc("channel tag expected, " + aTag.getLocalName() + " found");
154 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
155 if (aCharacters.trim().length()>0)
156 throw new XMLReader.XMLReaderExc("No character data allowed here");
159 public void finishSection() throws XMLReader.XMLReaderExc {
163 private class RSS091ChannelSectionHandler extends XMLReader.AbstractSectionHandler {
164 private String currentTag;
166 private RSSData data;
168 private RDFResource channel;
169 private Map attributes;
171 public RSS091ChannelSectionHandler(RSSData aData) {
173 items = new Vector();
174 channel = new RDFResource("rss:channel");
175 attributes = new HashMap();
178 public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
179 String tag = aTag.getLocalName();
181 if (tag.equals("item"))
182 return new RSS091ItemSectionHandler();
183 else if (mappedChannelProperties.containsKey(tag)) {
184 currentTag=(String) mappedChannelProperties.get(tag);
185 return new PCDATASectionHandler();
188 return new DiscardingSectionHandler();
191 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
192 if (aHandler instanceof PCDATASectionHandler) {
193 attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
195 else if (aHandler instanceof RSS091ItemSectionHandler) {
196 items.add((((RSS091ItemSectionHandler) aHandler).getItem()));
200 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
201 if (aCharacters.trim().length()>0)
202 throw new XMLReader.XMLReaderExc("No character data allowed here");
205 public void finishSection() throws XMLReader.XMLReaderExc {
206 Iterator i = items.iterator();
208 while (i.hasNext()) {
209 data.addResource((RDFResource) i.next());
214 private class RSS091ItemSectionHandler extends XMLReader.AbstractSectionHandler {
215 private String currentTag;
217 private RDFResource item;
218 private Map attributes;
220 public RSS091ItemSectionHandler() {
221 attributes = new HashMap();
224 public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
225 String tag = aTag.getLocalName();
226 System.out.println(tag);
228 if (mappedItemProperties.containsKey(tag)) {
229 currentTag=(String) mappedItemProperties.get(tag);
230 return new PCDATASectionHandler();
233 return new DiscardingSectionHandler();
236 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
237 if (aHandler instanceof PCDATASectionHandler) {
238 attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
242 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
243 if (aCharacters.trim().length()>0)
244 throw new XMLReader.XMLReaderExc("No character data allowed here");
247 public void finishSection() throws XMLReader.XMLReaderExc {
248 item = new RDFResource("rss:item", (String) attributes.get("rss:link"));
250 Iterator i = attributes.entrySet().iterator();
251 while (i.hasNext()) {
252 Map.Entry entry = (Map.Entry) i.next();
254 item.set((String) entry.getKey(), entry.getValue());
258 public RDFResource getItem() {
264 private class PCDATASectionHandler extends XMLReader.AbstractSectionHandler {
265 private StringBuffer data;
267 public PCDATASectionHandler() {
268 data = new StringBuffer();
271 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
272 throw new XMLReader.XMLReaderFailure(new RSSExc("No subtags allowed here"));
275 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
278 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
279 data.append(aCharacters);
282 public void finishSection() throws XMLReader.XMLReaderExc {
285 public String getData() {
286 return data.toString();
291 private class RDFSequenceSectionHandler extends XMLReader.AbstractSectionHandler {
294 public RDFSequenceSectionHandler() {
295 items = new Vector();
298 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
299 if (aTag.equals("rdf:li")) {
300 String item = (String) anAttributes.get("rdf:resource");
306 return new DiscardingSectionHandler();
309 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
312 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
315 public void finishSection() throws XMLReader.XMLReaderExc {
318 public List getItems() {
323 private class DiscardingSectionHandler extends XMLReader.AbstractSectionHandler {
324 public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
328 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
331 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
334 public void finishSection() throws XMLReader.XMLReaderExc {