1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public static RSSParser getParser(String path)
throws NullPointerException, FileNotFoundException, IOException, ParsingException
{
InputStream is;
InputStream in;
RSSParser rss = null;
if(path.startsWith("http://"))
{
URL url = new URL(path);
is = url.openStream();
// Solution pas trés propre
in = url.openStream();
}
else
{
is = new FileInputStream(new File(path));
// Solution pas trés propre
in = new FileInputStream(new File(path));
}
// Get the beginning of the stream to determine the RSS version used
byte[] buffer = new byte[512];
int bytesRead = 0;
while(bytesRead < 512)
{
int bytes = in.read(buffer, bytesRead, 512 - bytesRead);
if(bytes == -1)
{
break;
}
bytesRead+= bytes;
}
String content = new String(buffer);
if(content.contains("xmlns=\"http://purl.org/rss/1.0/\""))
{
rss = new RSS_1_0(is);
}
else if(content.contains("version=\"0.90\"") || content.contains("version=\"0.91\"") || content.contains("version=\"0.92\"") || content.contains("version=\"0.93\""))
{
rss = new RSS_0_9(is);
}
else if(content.contains("rss version=\"2.0\""))
{
rss = new RSS_2_0(is);
}
return rss;
} |
Partager