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.List;
37 import java.util.Vector;
39 import mir.util.DateTimeFunctions;
40 import mir.util.xml.XMLParserEngine;
41 import mir.util.xml.XMLParserExc;
42 import mir.util.xml.XMLParserFailure;
47 * <p>Description: </p>
48 * <p>Copyright: Copyright (c) 2003</p>
50 * @author not attributable
54 public class RSSReader {
55 public static final String RDF_NAMESPACE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
56 public static final String RSS_1_0_NAMESPACE_URI = "http://purl.org/rss/1.0/";
57 public static final String RSS_0_9_NAMESPACE_URI = "http://my.netscape.com/rdf/simple/0.9/";
58 public static final String DUBLINCORE_NAMESPACE_URI = "http://purl.org/dc/elements/1.1/";
59 public static final String EVENT_NAMESPACE_URI = "http://purl.org/rss/1.0/modules/event/";
60 public static final String TAXONOMY_NAMESPACE_URI = "http://web.resource.org/rss/1.0/modules/taxonomy/";
61 public static final String DUBLINCORE_TERMS_NAMESPACE_URI = "http://purl.org/dc/terms/";
62 public static final String CONTENT_NAMESPACE_URI = "http://purl.org/rss/1.0/modules/content/";
64 // ML: to be localized:
65 public static final String V2V_NAMESPACE_URI = "http://v2v.cc/rss/";
67 private static final mir.util.xml.XMLName RDF_ABOUT_PARAMETER = new mir.util.xml.XMLName(RDF_NAMESPACE_URI, "about");
68 private static final mir.util.xml.XMLName RDF_SEQUENCE_TAG = new mir.util.xml.XMLName(RDF_NAMESPACE_URI, "Seq");
69 private static final mir.util.xml.XMLName RDF_BAG_PARAMETER = new mir.util.xml.XMLName(RDF_NAMESPACE_URI, "Bag");
71 private static final mir.util.xml.XMLName RSS_CHANNEL_TAG = new mir.util.xml.XMLName(RSS_1_0_NAMESPACE_URI, "channel");
72 private static final mir.util.xml.XMLName RSS_ITEM_TAG = new mir.util.xml.XMLName(RSS_1_0_NAMESPACE_URI, "item");
73 private static final mir.util.xml.XMLName RSS_ITEMS_TAG = new mir.util.xml.XMLName(RSS_1_0_NAMESPACE_URI, "items");
76 private Map namespaceURItoModule;
77 private Map moduleToPrefix;
80 modules = new Vector();
81 namespaceURItoModule = new HashMap();
82 moduleToPrefix = new HashMap();
84 registerModule(new RSSBasicModule(RDF_NAMESPACE_URI, "RDF module"), "rdf");
85 registerModule(new RSSBasicModule(RSS_1_0_NAMESPACE_URI, "RSS 1.0 module"), "rss");
86 registerModule(new RSSBasicModule(RSS_0_9_NAMESPACE_URI, "RSS 0.9 module"), "rss");
88 RSSBasicModule dcModule = new RSSBasicModule(DUBLINCORE_NAMESPACE_URI, "RSS Dublin Core 1.1");
89 dcModule.addProperty("date", RSSModule.W3CDTF_PROPERTY_TYPE);
90 registerModule(dcModule, "dc");
92 RSSBasicModule dcTermsModule = new RSSBasicModule(DUBLINCORE_TERMS_NAMESPACE_URI, "RSS Qualified Dublin core");
93 dcTermsModule.addProperty("created", RSSModule.W3CDTF_PROPERTY_TYPE);
94 dcTermsModule.addProperty("issued", RSSModule.W3CDTF_PROPERTY_TYPE);
95 dcTermsModule.addProperty("modified", RSSModule.W3CDTF_PROPERTY_TYPE);
96 dcTermsModule.addProperty("dateAccepted", RSSModule.W3CDTF_PROPERTY_TYPE);
97 dcTermsModule.addProperty("dateCopyrighted", RSSModule.W3CDTF_PROPERTY_TYPE);
98 dcTermsModule.addProperty("dateSubmitted", RSSModule.W3CDTF_PROPERTY_TYPE);
99 registerModule(dcTermsModule, "dcterms");
101 RSSBasicModule v2vTermsModule = new RSSBasicModule(V2V_NAMESPACE_URI, "indymedia v2v RSS module");
102 v2vTermsModule.addMultiValuedProperty("topic", RSSModule.PCDATA_PROPERTY_TYPE);
103 v2vTermsModule.addMultiValuedProperty("genre", RSSModule.PCDATA_PROPERTY_TYPE);
104 v2vTermsModule.addMultiValuedProperty("link", RSSModule.PCDATA_PROPERTY_TYPE);
105 registerModule(v2vTermsModule, "v2v");
107 registerModule(new RSSBasicModule(EVENT_NAMESPACE_URI, "Event RSS module"), "ev");
108 registerModule(new RSSBasicModule(TAXONOMY_NAMESPACE_URI, "Taxonomy RSS module"), "taxo");
109 registerModule(new RSSBasicModule(CONTENT_NAMESPACE_URI , "Content RSS module"), "content");
112 public void registerModule(RSSModule aModule, String aPrefix) {
113 modules.add(aModule);
114 namespaceURItoModule.put(aModule.getNamespaceURI(), aModule);
115 moduleToPrefix.put(aModule, aPrefix);
118 public RSSData parseInputStream(InputStream aStream) throws RSSExc, RSSFailure {
120 RSSData result = new RSSData();
121 XMLParserEngine.getInstance().parse("xml.namespaceaware", aStream, new RootSectionHandler(result));
125 catch (Throwable t) {
126 throw new RSSFailure(t);
130 public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
132 InputStream inputStream = (InputStream) new URL(anUrl).getContent(new Class[] {InputStream.class});
134 if (inputStream==null)
135 throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
137 return parseInputStream(inputStream);
139 catch (Throwable t) {
140 throw new RSSFailure(t);
144 private class RootSectionHandler extends mir.util.xml.AbstractSectionHandler {
145 private RSSData data;
147 public RootSectionHandler(RSSData aData) {
151 public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
152 if (aTag.getLocalName().equals("RDF")) {
153 return new RDFSectionHandler(data);
156 throw new XMLParserFailure(new RSSExc("'RDF' tag expected"));
159 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
162 public void characters(String aCharacters) throws XMLParserExc {
163 if (aCharacters.trim().length()>0)
164 throw new XMLParserExc("No character data allowed here");
167 public void finishSection() throws XMLParserExc {
171 private class RDFSectionHandler extends mir.util.xml.AbstractSectionHandler {
172 private RSSData data;
175 public RDFSectionHandler(RSSData aData) {
179 public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
180 String identifier = (String) anAttributes.get(RDF_ABOUT_PARAMETER);
181 String rdfClass = makeQualifiedName(aTag);
183 return new RDFResourceSectionHandler(rdfClass, identifier);
186 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
187 if (aHandler instanceof RDFResourceSectionHandler) {
188 data.addResource(((RDFResourceSectionHandler) aHandler).getResource());
192 public void characters(String aCharacters) throws XMLParserExc {
193 if (aCharacters.trim().length()>0)
194 throw new XMLParserExc("No character data allowed here");
197 public void finishSection() throws XMLParserExc {
201 private mir.util.xml.SectionHandler makePropertyValueSectionHandler(mir.util.xml.XMLName aTag, Map anAttributes) {
202 RSSModule module = (RSSModule) namespaceURItoModule.get(aTag.getNamespaceURI());
205 RSSModule.RSSModuleProperty property = module.getPropertyForName(aTag.getLocalName());
207 if (property!=null) {
208 switch (property.getType()) {
210 RSSModule.PCDATA_PROPERTY_TYPE:
211 return new PCDATASectionHandler();
213 RSSModule.RDFCOLLECTION_PROPERTY_TYPE:
214 return new RDFCollectionSectionHandler();
216 // RSSModule.RDF_PROPERTY_TYPE:
217 // return new RDFValueSectionHandler();
219 RSSModule.W3CDTF_PROPERTY_TYPE:
220 return new DateSectionHandler();
225 return new FlexiblePropertyValueSectionHandler();
228 private void usePropertyValueSectionHandler(RDFResource aResource, PropertyValueSectionHandler aHandler, mir.util.xml.XMLName aTag) {
229 RSSModule module = (RSSModule) namespaceURItoModule.get(aTag.getNamespaceURI());
232 RSSModule.RSSModuleProperty property = module.getPropertyForName(aTag.getLocalName());
234 if (property!=null && property.getIsMultiValued()) {
235 List value = (List) aResource.get(makeQualifiedName(aTag));
238 value = new Vector();
239 aResource.set(makeQualifiedName(aTag), value);
242 value.add(aHandler.getValue());
248 aResource.set(makeQualifiedName(aTag), aHandler.getValue());
251 private String makeQualifiedName(mir.util.xml.XMLName aName) {
252 String result=aName.getLocalName();
253 RSSModule module = (RSSModule) namespaceURItoModule.get(aName.getNamespaceURI());
255 String prefix = (String) moduleToPrefix.get(module);
257 if (prefix!=null && prefix.length()>0)
258 result = prefix+":"+result;
264 private class RDFResourceSectionHandler extends mir.util.xml.AbstractSectionHandler {
265 private String image;
266 private mir.util.xml.XMLName currentTag;
267 private RDFResource resource;
269 public RDFResourceSectionHandler(String anRDFClass, String anIdentifier) {
270 resource = new RDFResource(anRDFClass, anIdentifier);
273 public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
276 return makePropertyValueSectionHandler(aTag, anAttributes);
279 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
280 if (aHandler instanceof PropertyValueSectionHandler) {
281 usePropertyValueSectionHandler(resource, (PropertyValueSectionHandler) aHandler, currentTag);
282 // resource.set(makeQualifiedName(currentTag), ( (PropertyValueSectionHandler) aHandler).getValue());
286 public void characters(String aCharacters) throws XMLParserExc {
287 if (aCharacters.trim().length()>0)
288 throw new XMLParserExc("No character data allowed here");
291 public void finishSection() throws XMLParserExc {
294 public RDFResource getResource() {
295 if ((resource.getIdentifier()==null || resource.getIdentifier().length()==0) && resource.get("rss:link")!=null) {
296 resource.setIdentifier(resource.get("rss:link").toString());
303 private abstract class PropertyValueSectionHandler extends mir.util.xml.AbstractSectionHandler {
304 public abstract Object getValue();
307 private class FlexiblePropertyValueSectionHandler extends PropertyValueSectionHandler {
308 private StringBuffer stringData;
309 private Object structuredData;
311 public FlexiblePropertyValueSectionHandler() {
312 stringData = new StringBuffer();
316 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
317 if (aTag.equals(RDF_SEQUENCE_TAG))
318 return new RDFSequenceSectionHandler();
320 return new DiscardingSectionHandler();
323 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
324 if (aHandler instanceof RDFSequenceSectionHandler) {
325 structuredData= ((RDFSequenceSectionHandler) aHandler).getItems();
329 public void characters(String aCharacters) throws XMLParserExc {
330 stringData.append(aCharacters);
333 public void finishSection() throws XMLParserExc {
336 public String getData() {
337 return stringData.toString();
340 public Object getValue() {
341 if (structuredData==null)
342 return stringData.toString();
344 return structuredData;
348 private class RDFCollectionSectionHandler extends PropertyValueSectionHandler {
351 public RDFCollectionSectionHandler() {
352 items = new Vector();
355 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
356 if (aTag.equals(RDF_SEQUENCE_TAG))
357 return new RDFSequenceSectionHandler();
359 return new DiscardingSectionHandler();
362 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
363 if (aHandler instanceof RDFSequenceSectionHandler) {
364 items.addAll(((RDFSequenceSectionHandler) aHandler).getItems());
368 public void characters(String aCharacters) throws XMLParserExc {
369 if (aCharacters.trim().length()>0)
370 throw new XMLParserExc("No character data allowed here");
373 public void finishSection() throws XMLParserExc {
376 public List getItems() {
380 public Object getValue() {
385 private class PCDATASectionHandler extends PropertyValueSectionHandler {
386 private StringBuffer data;
388 public PCDATASectionHandler() {
389 data = new StringBuffer();
392 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
393 throw new XMLParserFailure(new RSSExc("No subtags allowed here"));
396 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
399 public void characters(String aCharacters) throws XMLParserExc {
400 data.append(aCharacters);
403 public void finishSection() throws XMLParserExc {
406 public String getData() {
407 return data.toString();
410 public Object getValue() {
411 return data.toString();
415 private class DateSectionHandler extends PropertyValueSectionHandler {
416 private StringBuffer data;
418 public DateSectionHandler() {
419 data = new StringBuffer();
422 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
423 throw new XMLParserFailure(new RSSExc("No subtags allowed here"));
426 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
429 public void characters(String aCharacters) throws XMLParserExc {
430 data.append(aCharacters);
433 public void finishSection() throws XMLParserExc {
436 public Object getValue() {
438 String expression = data.toString().trim();
440 return DateTimeFunctions.parseW3CDTFString(expression);
442 catch (Throwable t) {
450 private class RDFSequenceSectionHandler extends mir.util.xml.AbstractSectionHandler {
453 public RDFSequenceSectionHandler() {
454 items = new Vector();
457 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
458 if (aTag.equals("rdf:li")) {
459 String item = (String) anAttributes.get("rdf:resource");
465 return new DiscardingSectionHandler();
468 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
471 public void characters(String aCharacters) throws XMLParserExc {
474 public void finishSection() throws XMLParserExc {
477 public List getItems() {
482 private class RDFLiteralSectionHandler extends PropertyValueSectionHandler {
483 private StringBuffer data;
486 public RDFLiteralSectionHandler() {
487 data = new StringBuffer();
490 protected StringBuffer getData() {
494 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
496 data.append("<"+tag+">");
498 return new RDFLiteralSectionHandler();
501 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
502 data.append(((RDFLiteralSectionHandler) aHandler).getData());
503 data.append("</"+tag+">");
506 public void characters(String aCharacters) throws XMLParserExc {
507 data.append(aCharacters);
510 public void finishSection() throws XMLParserExc {
513 public Object getValue() {
514 return data.toString();
518 private class DiscardingSectionHandler extends mir.util.xml.AbstractSectionHandler {
519 public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
523 public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
526 public void characters(String aCharacters) throws XMLParserExc {
529 public void finishSection() throws XMLParserExc {