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;
public class TextNewsHandler 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;
VideoLinkParser vlp = new VideoLinkParser();
private ArrayList<TextNews> newslist = new ArrayList<TextNews>();
private TextNews textnews;
/**
* @return the newslist
*/
public ArrayList<TextNews> getNewslist() {
return newslist;
}
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")){
textnews = new TextNews();
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;
}
}
public void endElement(String URI, String localName, String qName) throws SAXException {
if(localName.equals("item")){
if(textnews.getImg() != null && textnews.getContent() != null){
newslist.add(textnews);
}
in_item = false;
}
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;
String content = textnews.getContent();
String[] t = content.split("alt=\"\" />", 2);
if(t.length >= 2){
t[1].replaceAll("<.*.>", "");
textnews.setNewContent(t[1]/*.replaceAll("]]>", "")*/);
String[] temp2 =t[0].split("<img src=\"");
if(temp2.length == 2){
String[] temp3 = temp2[1].split("\".*");
temp3[0].replaceAll("<.*.>", "");
textnews.setImg_url(temp3[0]);
textnews.setImg(getBitMapFromUrl(temp3[0]));
}
}
else{
textnews.setNewContent(t[0]);
}
}
}
public void characters(char[] charsequence, int start, int length){
if(this.in_item && this.in_title){
textnews.setTitle(new String(charsequence, start, length));
}
if(this.in_item && this.in_link && !this.in_content){
//textnews.setLink(new String(charsequence, start, length));
textnews.setVideoLink(vlp.getVideoLink(new String(charsequence, start, length)));
}
if(this.in_item && this.in_description){
textnews.setDescription(new String(charsequence, start, length));
}
if(this.in_item && this.in_content && !this.in_link){
textnews.setContent(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) {
return null;
} catch (IOException e) {
return null;
}
}
}