|
|
@ -33,7 +33,6 @@ import org.xml.sax.Attributes;
|
|
|
|
import org.xml.sax.EntityResolver;
|
|
|
|
import org.xml.sax.EntityResolver;
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
import org.xml.sax.XMLReader;
|
|
|
|
|
|
|
|
import org.xml.sax.helpers.DefaultHandler;
|
|
|
|
import org.xml.sax.helpers.DefaultHandler;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -48,17 +47,17 @@ public class RSSReader extends DefaultHandler {
|
|
|
|
|
|
|
|
|
|
|
|
public enum Type { rss, atom, rdf, none };
|
|
|
|
public enum Type { rss, atom, rdf, none };
|
|
|
|
|
|
|
|
|
|
|
|
private RSSReader(int maxsize) {
|
|
|
|
private RSSReader(final int maxsize) {
|
|
|
|
theChannel = new RSSFeed(maxsize);
|
|
|
|
this.theChannel = new RSSFeed(maxsize);
|
|
|
|
buffer = new StringBuilder(300);
|
|
|
|
this.buffer = new StringBuilder(300);
|
|
|
|
item = null;
|
|
|
|
this.item = null;
|
|
|
|
parsingChannel = false;
|
|
|
|
this.parsingChannel = false;
|
|
|
|
parsingImage = false;
|
|
|
|
this.parsingImage = false;
|
|
|
|
parsingItem = false;
|
|
|
|
this.parsingItem = false;
|
|
|
|
type = Type.none;
|
|
|
|
this.type = Type.none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RSSReader(int maxsize, final InputStream stream, Type type) throws IOException {
|
|
|
|
public RSSReader(final int maxsize, final InputStream stream, final Type type) throws IOException {
|
|
|
|
this(maxsize);
|
|
|
|
this(maxsize);
|
|
|
|
this.type = type;
|
|
|
|
this.type = type;
|
|
|
|
final SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
|
|
final SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
|
@ -73,9 +72,9 @@ public class RSSReader extends DefaultHandler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
saxParser.parse(stream, this);
|
|
|
|
saxParser.parse(stream, this);
|
|
|
|
} catch (SAXException e) {
|
|
|
|
} catch (final SAXException e) {
|
|
|
|
throw new IOException (e.getMessage());
|
|
|
|
throw new IOException (e.getMessage());
|
|
|
|
} catch (ParserConfigurationException e) {
|
|
|
|
} catch (final ParserConfigurationException e) {
|
|
|
|
throw new IOException (e.getMessage());
|
|
|
|
throw new IOException (e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -84,7 +83,7 @@ public class RSSReader extends DefaultHandler {
|
|
|
|
return this.type;
|
|
|
|
return this.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static RSSReader parse(int maxsize, final byte[] a) throws IOException {
|
|
|
|
public static RSSReader parse(final int maxsize, final byte[] a) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
// check integrity of array
|
|
|
|
// check integrity of array
|
|
|
|
if ((a == null) || (a.length == 0)) {
|
|
|
|
if ((a == null) || (a.length == 0)) {
|
|
|
@ -156,25 +155,25 @@ public class RSSReader extends DefaultHandler {
|
|
|
|
public void startElement(final String uri, final String name, final String tag, final Attributes atts) throws SAXException {
|
|
|
|
public void startElement(final String uri, final String name, final String tag, final Attributes atts) throws SAXException {
|
|
|
|
if ("channel".equals(tag)) {
|
|
|
|
if ("channel".equals(tag)) {
|
|
|
|
this.type = Type.rss;
|
|
|
|
this.type = Type.rss;
|
|
|
|
item = new RSSMessage();
|
|
|
|
this.item = new RSSMessage();
|
|
|
|
parsingChannel = true;
|
|
|
|
this.parsingChannel = true;
|
|
|
|
} else if ("feed".equals(tag)) {
|
|
|
|
} else if ("feed".equals(tag)) {
|
|
|
|
this.type = Type.atom;
|
|
|
|
this.type = Type.atom;
|
|
|
|
item = new RSSMessage();
|
|
|
|
this.item = new RSSMessage();
|
|
|
|
parsingChannel = true;
|
|
|
|
this.parsingChannel = true;
|
|
|
|
} else if ("item".equals(tag) || "entry".equals(tag)) {
|
|
|
|
} else if ("item".equals(tag) || "entry".equals(tag)) {
|
|
|
|
if (parsingChannel) {
|
|
|
|
if (this.parsingChannel) {
|
|
|
|
// the channel ends with the first item not with the channel close tag
|
|
|
|
// the channel ends with the first item not with the channel close tag
|
|
|
|
theChannel.setChannel(item);
|
|
|
|
this.theChannel.setChannel(this.item);
|
|
|
|
parsingChannel = false;
|
|
|
|
this.parsingChannel = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item = new RSSMessage();
|
|
|
|
this.item = new RSSMessage();
|
|
|
|
parsingItem = true;
|
|
|
|
this.parsingItem = true;
|
|
|
|
} else if (parsingItem && this.type == Type.atom && "link".equals(tag)) {
|
|
|
|
} else if (this.parsingItem && this.type == Type.atom && "link".equals(tag)) {
|
|
|
|
String url = atts.getValue("href");
|
|
|
|
final String url = atts.getValue("href");
|
|
|
|
if (url != null && url.length() > 0) item.setValue("link", url);
|
|
|
|
if (url != null && url.length() > 0) this.item.setValue("link", url);
|
|
|
|
} else if ("image".equals(tag)) {
|
|
|
|
} else if ("image".equals(tag)) {
|
|
|
|
parsingImage = true;
|
|
|
|
this.parsingImage = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -182,37 +181,37 @@ public class RSSReader extends DefaultHandler {
|
|
|
|
public void endElement(final String uri, final String name, final String tag) {
|
|
|
|
public void endElement(final String uri, final String name, final String tag) {
|
|
|
|
if (tag == null) return;
|
|
|
|
if (tag == null) return;
|
|
|
|
if ("channel".equals(tag) || "feed".equals(tag)) {
|
|
|
|
if ("channel".equals(tag) || "feed".equals(tag)) {
|
|
|
|
if (parsingChannel) theChannel.setChannel(item);
|
|
|
|
if (this.parsingChannel) this.theChannel.setChannel(this.item);
|
|
|
|
parsingChannel = false;
|
|
|
|
this.parsingChannel = false;
|
|
|
|
} else if ("item".equals(tag) || "entry".equals(tag)) {
|
|
|
|
} else if ("item".equals(tag) || "entry".equals(tag)) {
|
|
|
|
theChannel.addMessage(item);
|
|
|
|
this.theChannel.addMessage(this.item);
|
|
|
|
parsingItem = false;
|
|
|
|
this.parsingItem = false;
|
|
|
|
} else if ("image".equals(tag)) {
|
|
|
|
} else if ("image".equals(tag)) {
|
|
|
|
parsingImage = false;
|
|
|
|
this.parsingImage = false;
|
|
|
|
} else if ((parsingImage) && (parsingChannel)) {
|
|
|
|
} else if ((this.parsingImage) && (this.parsingChannel)) {
|
|
|
|
final String value = buffer.toString().trim();
|
|
|
|
final String value = this.buffer.toString().trim();
|
|
|
|
buffer.setLength(0);
|
|
|
|
this.buffer.setLength(0);
|
|
|
|
if ("url".equals(tag)) theChannel.setImage(value);
|
|
|
|
if ("url".equals(tag)) this.theChannel.setImage(value);
|
|
|
|
} else if (parsingItem) {
|
|
|
|
} else if (this.parsingItem) {
|
|
|
|
final String value = buffer.toString().trim();
|
|
|
|
final String value = this.buffer.toString().trim();
|
|
|
|
buffer.setLength(0);
|
|
|
|
this.buffer.setLength(0);
|
|
|
|
if (RSSMessage.tags.contains(tag) && value.length() > 0) item.setValue(tag, value);
|
|
|
|
if (RSSMessage.tags.contains(tag) && value.length() > 0) this.item.setValue(tag, value);
|
|
|
|
} else if (parsingChannel) {
|
|
|
|
} else if (this.parsingChannel) {
|
|
|
|
final String value = buffer.toString().trim();
|
|
|
|
final String value = this.buffer.toString().trim();
|
|
|
|
buffer.setLength(0);
|
|
|
|
this.buffer.setLength(0);
|
|
|
|
if (RSSMessage.tags.contains(tag)) item.setValue(tag, value);
|
|
|
|
if (RSSMessage.tags.contains(tag)) this.item.setValue(tag, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void characters(final char ch[], final int start, final int length) {
|
|
|
|
public void characters(final char ch[], final int start, final int length) {
|
|
|
|
if (parsingItem || parsingChannel) {
|
|
|
|
if (this.parsingItem || this.parsingChannel) {
|
|
|
|
buffer.append(ch, start, length);
|
|
|
|
this.buffer.append(ch, start, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RSSFeed getFeed() {
|
|
|
|
public RSSFeed getFeed() {
|
|
|
|
return theChannel;
|
|
|
|
return this.theChannel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|