/*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001-2006 The Mir-coders group
*
* This file is part of Mir.
*
*
* In addition, as a special exception, The Mir-coders gives permission to link
* the code of this program with any library licensed under the Apache Software License,
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
- * (or with modified versions of the above that use the same license as the above),
* and distribute linked combinations including the two. You must obey the
* GNU General Public License in all respects for all of the code used other than
* the above mentioned libraries. If you modify this file, you may extend this
*/
package mir.rss;
+import mir.util.HTTPClientHelper;
+import mir.util.xml.XMLParserEngine;
+import mir.util.xml.XMLParserExc;
+import mir.util.xml.XMLParserFailure;
+
import java.io.InputStream;
-import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
-import java.util.*;
-import java.util.Vector;
-import java.text.*;
-
-import mir.util.XMLReader;
-import mir.util.*;
+import java.util.Map;
/**
*
public RSSData parseInputStream(InputStream aStream) throws RSSExc, RSSFailure {
try {
- XMLReader xmlReader = new XMLReader(false);
RSSData result = new RSSData();
- xmlReader.parseInputStream(aStream, new RootSectionHandler(result));
+ XMLParserEngine.getInstance().parse("html", aStream, new RootSectionHandler(result));
return result;
}
}
}
- public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
+ public RSSData parseInputStream(InputStream aStream, String anEncoding) throws RSSExc, RSSFailure {
try {
- InputStream inputStream = (InputStream) new URL(anUrl).getContent(new Class[] {InputStream.class});
+ RSSData result = new RSSData();
+ XMLParserEngine.getInstance().parse("html", aStream, anEncoding, new RootSectionHandler(result));
+
+ return result;
+ }
+ catch (Throwable t) {
+ throw new RSSFailure(t);
+ }
+ }
+ public RSSData parseUrl(String anUrl) throws RSSExc, RSSFailure {
+ try {
+ HTTPClientHelper httpClientHelper = new HTTPClientHelper();
+ InputStream inputStream = httpClientHelper.getUrl(anUrl);
+
if (inputStream==null)
throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
- return parseInputStream(inputStream);
+ RSSData theRSSData = parseInputStream(inputStream);
+ httpClientHelper.releaseHTTPConnection();
+ return theRSSData;
+
}
catch (Throwable t) {
throw new RSSFailure(t);
}
}
- private class RootSectionHandler extends XMLReader.AbstractSectionHandler {
+ public RSSData parseUrl(String anUrl, String anEncoding) throws RSSExc, RSSFailure {
+ try {
+ HTTPClientHelper httpClientHelper = new HTTPClientHelper();
+ InputStream inputStream = httpClientHelper.getUrl(anUrl);
+
+ if (inputStream==null)
+ throw new RSSExc("RSSChannel.parseUrl: Can't get url content");
+
+ RSSData theRSSData = parseInputStream(inputStream, anEncoding);
+ httpClientHelper.releaseHTTPConnection();
+ return theRSSData;
+ }
+
+ catch (Throwable t) {
+ throw new RSSFailure(t);
+ }
+ }
+
+ private class RootSectionHandler extends mir.util.xml.AbstractSectionHandler {
private RSSData data;
public RootSectionHandler(RSSData aData) {
data = aData;
}
- public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
-// if (aTag.getLocalName().equals("rss")) {
+ public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
+ if (aTag.getLocalName().equals("rss")) {
return new RSS091SectionHandler(data);
-// }
-// else
-// throw new XMLReader.XMLReaderFailure(new RSSExc("'rss' tag expected"));
- };
+ }
+ throw new XMLParserFailure(new RSSExc("'rss' tag expected"));
+ }
- public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
- };
+ public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
+ }
- public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+ public void characters(String aCharacters) throws XMLParserExc {
if (aCharacters.trim().length()>0)
- throw new XMLReader.XMLReaderExc("No character data allowed here");
- };
+ throw new XMLParserExc("No character data allowed here");
+ }
- public void finishSection() throws XMLReader.XMLReaderExc {
- };
+ public void finishSection() throws XMLParserExc {
+ }
}
- private class RSS091SectionHandler extends XMLReader.AbstractSectionHandler {
+ private class RSS091SectionHandler extends mir.util.xml.AbstractSectionHandler {
private RSSData data;
data = aData;
}
- public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+ public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
if (aTag.getLocalName().equals("channel"))
return new RSS091ChannelSectionHandler(data);
else
- throw new XMLReader.XMLReaderExc("channel tag expected, " + aTag.getLocalName() + " found");
- };
+ throw new XMLParserExc("channel tag expected, " + aTag.getLocalName() + " found");
+ }
- public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+ public void characters(String aCharacters) throws XMLParserExc {
if (aCharacters.trim().length()>0)
- throw new XMLReader.XMLReaderExc("No character data allowed here");
- };
+ throw new XMLParserExc("No character data allowed here");
+ }
- public void finishSection() throws XMLReader.XMLReaderExc {
- };
+ public void finishSection() throws XMLParserExc {
+ }
}
- private class RSS091ChannelSectionHandler extends XMLReader.AbstractSectionHandler {
+ private class RSS091ChannelSectionHandler extends mir.util.xml.AbstractSectionHandler {
private String currentTag;
private RSSData data;
public RSS091ChannelSectionHandler(RSSData aData) {
data = aData;
- items = new Vector();
+ items = new ArrayList();
channel = new RDFResource("rss:channel");
attributes = new HashMap();
}
- public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+ public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
String tag = aTag.getLocalName();
if (tag.equals("item"))
}
else
return new DiscardingSectionHandler();
- };
+ }
- public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+ public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
if (aHandler instanceof PCDATASectionHandler) {
attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
}
else if (aHandler instanceof RSS091ItemSectionHandler) {
items.add((((RSS091ItemSectionHandler) aHandler).getItem()));
}
- };
+ }
- public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+ public void characters(String aCharacters) throws XMLParserExc {
if (aCharacters.trim().length()>0)
- throw new XMLReader.XMLReaderExc("No character data allowed here");
- };
+ throw new XMLParserExc("No character data allowed here");
+ }
- public void finishSection() throws XMLReader.XMLReaderExc {
+ public void finishSection() throws XMLParserExc {
Iterator i = items.iterator();
while (i.hasNext()) {
data.addResource((RDFResource) i.next());
}
- };
+ }
}
- private class RSS091ItemSectionHandler extends XMLReader.AbstractSectionHandler {
+ private class RSS091ItemSectionHandler extends mir.util.xml.AbstractSectionHandler {
private String currentTag;
private RDFResource item;
attributes = new HashMap();
}
- public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+ public mir.util.xml.SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc {
String tag = aTag.getLocalName();
- System.out.println(tag);
if (mappedItemProperties.containsKey(tag)) {
currentTag=(String) mappedItemProperties.get(tag);
return new PCDATASectionHandler();
}
- else
- return new DiscardingSectionHandler();
- };
+ return new DiscardingSectionHandler();
+ }
- public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
+ public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
if (aHandler instanceof PCDATASectionHandler) {
attributes.put(currentTag, (((PCDATASectionHandler) aHandler).getData()));
}
- };
+ }
- public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+ public void characters(String aCharacters) throws XMLParserExc {
if (aCharacters.trim().length()>0)
- throw new XMLReader.XMLReaderExc("No character data allowed here");
- };
+ throw new XMLParserExc("No character data allowed here");
+ }
- public void finishSection() throws XMLReader.XMLReaderExc {
+ public void finishSection() throws XMLParserExc {
item = new RDFResource("rss:item", (String) attributes.get("rss:link"));
Iterator i = attributes.entrySet().iterator();
item.set((String) entry.getKey(), entry.getValue());
}
- };
+ }
public RDFResource getItem() {
return item;
}
- private class PCDATASectionHandler extends XMLReader.AbstractSectionHandler {
+ private class PCDATASectionHandler extends mir.util.xml.AbstractSectionHandler {
private StringBuffer data;
public PCDATASectionHandler() {
data = new StringBuffer();
}
- public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
- throw new XMLReader.XMLReaderFailure(new RSSExc("No subtags allowed here"));
- };
+ public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
+ throw new XMLParserFailure(new RSSExc("No subtags allowed here"));
+ }
- public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
- };
+ public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
+ }
- public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
+ public void characters(String aCharacters) throws XMLParserExc {
data.append(aCharacters);
- };
+ }
- public void finishSection() throws XMLReader.XMLReaderExc {
- };
+ public void finishSection() throws XMLParserExc {
+ }
public String getData() {
return data.toString();
}
- private class RDFSequenceSectionHandler extends XMLReader.AbstractSectionHandler {
+ private class RDFSequenceSectionHandler extends mir.util.xml.AbstractSectionHandler {
private List items;
public RDFSequenceSectionHandler() {
- items = new Vector();
+ items = new ArrayList();
}
- public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+ public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
if (aTag.equals("rdf:li")) {
String item = (String) anAttributes.get("rdf:resource");
}
return new DiscardingSectionHandler();
- };
+ }
- public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
- };
+ public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
+ }
- public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
- };
+ public void characters(String aCharacters) throws XMLParserExc {
+ }
- public void finishSection() throws XMLReader.XMLReaderExc {
- };
+ public void finishSection() throws XMLParserExc {
+ }
public List getItems() {
return items;
}
}
- private class DiscardingSectionHandler extends XMLReader.AbstractSectionHandler {
- public XMLReader.SectionHandler startElement(String aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
+ private class DiscardingSectionHandler extends mir.util.xml.AbstractSectionHandler {
+ public mir.util.xml.SectionHandler startElement(String aTag, Map anAttributes) throws XMLParserExc {
return this;
- };
+ }
- public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
- };
+ public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc {
+ }
- public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
- };
+ public void characters(String aCharacters) throws XMLParserExc {
+ }
- public void finishSection() throws XMLReader.XMLReaderExc {
- };
+ public void finishSection() throws XMLParserExc {
+ }
}
}