import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class VideoNewsHandler extends DefaultHandler{
private boolean in_item = false;
private boolean in_title = false;
private boolean in_link = false;
private boolean in_description = false;
private boolean in_content = false;
private boolean in_category = false;
private boolean in_date = false;
private ArrayList<VideoNews> videoNewsList = new ArrayList<VideoNews>();
private VideoNews vn;
/**
* @return the videoNewsList
*/
public ArrayList<VideoNews> getVideoNewsList() {
return videoNewsList;
}
public void startDocument() throws SAXException{
}
public void endDocument() throws SAXException{
}
public void startElement(String URI, String localName, String qName, Attributes attributes) throws SAXException {
if(localName.equals("item")){
vn = new VideoNews();
in_item = true;
}
else if(localName.equals("title")){
in_title = true;
}
else if(localName.equals("link")){
in_link = true;
}
else if(localName.equals("description")){
in_description = true;
}
else if(localName.equalsIgnoreCase("content:encoded") || qName.equals("content:encoded")){
in_content = true;
}
else if(localName.equals("category")){
in_category = true;
}
else if(localName.equals("pubDate")){
in_date = true;
}
}
public void endElement(String URI, String localName, String qName) throws SAXException {
if(localName.equals("item")){
in_item = false;
videoNewsList.add(vn);
}
else if(localName.equals("title")){
in_title = false;
}
else if(localName.equals("link")){
in_link = false;
}
else if(localName.equals("description")){
in_description = false;
}
else if(localName.equalsIgnoreCase("content:encoded") || qName.equals("content:encoded")){
in_content = false;
}
else if(localName.equals("category")){
in_category = false;
}
else if(localName.equals("pubDate")){
in_date = false;
}
}
public void characters(char[] charsequence, int start, int length){
if(this.in_item && this.in_title){
vn.setTitle(new String(charsequence, start, length));
}
if(this.in_item && this.in_link){
vn.setLink(new String(charsequence, start, length));
}
if(this.in_item && this.in_description){
vn.setDescription(new String(charsequence, start, length));
}
if(this.in_item && this.in_content){
String content = new String(charsequence, start, length);
Log.i("VideoNewsParserImg", new String(charsequence, start, length));
String[] t = content.split("alt.*./>");
if(t.length >= 2){
t[1].replaceAll("<.*.>", "");
vn.setContent(t[1]);
String[] temp2 =t[0].split("<img src=\"");
if(temp2.length == 2){
String[] temp3 = temp2[1].split("\".*");
temp3[0].replaceAll("<.*.>", "");
vn.setImg(getBitMapFromUrl(temp3[0]));
}
}
else{
vn.setContent(t[0]);
}
}
if(this.in_item && this.in_category){
vn.setCategory(new String(charsequence, start, length));
}
if(this.in_item && this.in_date){
vn.setPubDate(new String(charsequence, start, length));
}
}
public static Bitmap getBitMapFromUrl(String src){
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
}
}