Showing posts with label today. Show all posts
Showing posts with label today. Show all posts

Wednesday, 22 June 2011

RSS Parser (SAX)

RSS (Really Simple Syndication)
RSS is way to publish frequently changing contents like blog posts, news updates, stock quotes & things like that. An RSS document, which is called a “feed,” “web feed,” or “channel,” contains either a summary of content from an associated web site or the full text. RSS formats are specified using XML, a generic specification for the creation of data formats.
I have attached a simple SAX parser for RSS. Please let me know if there is any flaw in the attached code. This code is provided for learning purpose with less focus on coding standards & it’s efficiency. You are free to use & modify it.

Code
RssParser.java

package com.vaani.rss.parser;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class RssParser extends DefaultHandler
{
private String urlString;
private RssFeed rssFeed;
private StringBuilder text;
private Item item;
private boolean imgStatus;

public RssParser(String url)
{
this.urlString = url;
this.text = new StringBuilder();
}

public void parse()
{
InputStream urlInputStream = null;
SAXParserFactory spf = null;
SAXParser sp = null;

try
{
URL url = new URL(this.urlString);
_setProxy(); // Set the proxy if needed
urlInputStream = url.openConnection().getInputStream();
spf = SAXParserFactory.newInstance();
if (spf != null)
{
sp = spf.newSAXParser();
sp.parse(urlInputStream, this);
}
}

/*
         * Exceptions need to be handled
         * MalformedURLException
         * ParserConfigurationException
         * IOException
         * SAXException
         */

catch (Exception e)
{
System.out.println("Exception: " + e);
e.printStackTrace();
}
finally
{
try
{
if (urlInputStream != null) urlInputStream.close();
}
catch (Exception e) {}
}
}

public RssFeed getFeed()
{
return (this.rssFeed);
}

public void startElement(String uri, String localName, String qName,
Attributes attributes)
{
if (qName.equalsIgnoreCase("channel"))
this.rssFeed = new RssFeed();
else if (qName.equalsIgnoreCase("item") && (this.rssFeed != null))
{
this.item = new Item();
this.rssFeed.addItem(this.item);
}
else if (qName.equalsIgnoreCase("image") && (this.rssFeed != null))
this.imgStatus = true;
}

public void endElement(String uri, String localName, String qName)
{
if (this.rssFeed == null)
return;

if (qName.equalsIgnoreCase("item"))
this.item = null;

else if (qName.equalsIgnoreCase("image"))
this.imgStatus = false;

else if (qName.equalsIgnoreCase("title"))
{
if (this.item != null) this.item.title = this.text.toString().trim();
else if (this.imgStatus) this.rssFeed.imageTitle = this.text.toString().trim();
else this.rssFeed.title = this.text.toString().trim();
}

else if (qName.equalsIgnoreCase("link"))
{
if (this.item != null) this.item.link = this.text.toString().trim();
else if (this.imgStatus) this.rssFeed.imageLink = this.text.toString().trim();
else this.rssFeed.link = this.text.toString().trim();
}

else if (qName.equalsIgnoreCase("description"))
{
if (this.item != null) this.item.description = this.text.toString().trim();
else this.rssFeed.description = this.text.toString().trim();
}

else if (qName.equalsIgnoreCase("url") && this.imgStatus)
this.rssFeed.imageUrl = this.text.toString().trim();

else if (qName.equalsIgnoreCase("language"))
this.rssFeed.language = this.text.toString().trim();

else if (qName.equalsIgnoreCase("generator"))
this.rssFeed.generator = this.text.toString().trim();

else if (qName.equalsIgnoreCase("copyright"))
this.rssFeed.copyright = this.text.toString().trim();

else if (qName.equalsIgnoreCase("pubDate") && (this.item != null))
this.item.pubDate = this.text.toString().trim();

else if (qName.equalsIgnoreCase("category") && (this.item != null))
this.rssFeed.addItem(this.text.toString().trim(), this.item);

this.text.setLength(0);
}

public void characters(char[] ch, int start, int length)
{
this.text.append(ch, start, length);
}

public static void _setProxy()
throws IOException
{
Properties sysProperties = System.getProperties();
sysProperties.put("proxyHost", "<Proxy IP Address>");
sysProperties.put("proxyPort", "<Proxy Port Number>");
System.setProperties(sysProperties);
}

public static class RssFeed
{
public String title;
public String description;
public String link;
public String language;
public String generator;
public String copyright;
public String imageUrl;
public String imageTitle;
public String imageLink;

public ArrayList <Item> items;
public HashMap <String, ArrayList <Item>> category;

public void addItem(Item item)
{
if (this.items == null)
this.items = new ArrayList<Item>();
this.items.add(item);
}

public void addItem(String category, Item item)
{
if (this.category == null)
this.category = new HashMap<String, ArrayList<Item>>();
if (!this.category.containsKey(category))
this.category.put(category, new ArrayList<Item>());
this.category.get(category).add(item);
}
}



}

Item.java

package com.vaani.rss.parser;

public class Item
{
public String title;
public String description;
public String link;
public String pubDate;

public String toString()
{
return (this.title + ": " +
this.pubDate + "n" + this.description);
}
}

RssParserDemo.java - Ready with demo

package com.vaani.rss.main;

import java.util.ArrayList;

import com.vaani.rss.parser.RssParser;
import com.vaani.rss.parser.RssParser.RssFeed;
import com.vaani.rss.parser.Item;



public class RssParserDemo {

public static void main(String[] args){
RssParser rp = new RssParser("<some rss feed>");
rp.parse();
RssFeed feed = rp.getFeed();

// Listing all categories & the no. of elements in each category
if (feed.category != null)
{
System.out.println("Category List: ");
for (String category : feed.category.keySet())
{
System.out.println(category
+ ": "
+ ((ArrayList<Item>)feed.category.get(category)).size());
}
}

// Listing all items in the feed
for (int i = 0; i < feed.items.size(); i++)
System.out.println(feed.items.get(i).title);
}
}

Sending Email with Attachments using JavaMail

We can send emails using JavaMail API.
Instead of directly using JavaMail API here is a small utility to send emails which shields the user from JavaMail internals.  
EmailConfiguration.java

import java.util.Properties;

public class EmailConfiguration
{
private Properties properties = new Properties();

public static final String SMTP_HOST = "mail.smtp.host";
public static final String SMTP_AUTH = "mail.smtp.auth";
public static final String SMTP_TLS_ENABLE = "mail.smtp.starttls.enable";
public static final String SMTP_AUTH_USER = "smtp.auth.user";
public static final String SMTP_AUTH_PWD = "smtp.auth.pwd";
public static final String DEBUG = "debug";

public Properties getProperties()
{
return this.properties;
}

public void setProperty(String key, String value)
{
this.properties.put(key, value);
}

public void addProperties(Properties props)
{
this.properties.putAll(props);
}

public String getProperty(String key)
{
return this.properties.getProperty(key);
}
}

Email.java
import java.util.ArrayList;
import java.util.List;

public class Email
{
private String from;
private String[] to;
private String[] cc;
private String[] bcc;
private String subject;
private String text;
private String mimeType;
private List<Attachment> attachments = new ArrayList<Attachment>();

public String getFrom()
{
return from;
}
public void setFrom(String from)
{
this.from = from;
}
public String[] getTo()
{
return to;
}
public void setTo(String... to)
{
this.to = to;
}
public String[] getCc()
{
return cc;
}
public void setCc(String... cc)
{
this.cc = cc;
}
public String[] getBcc()
{
return bcc;
}
public void setBcc(String... bcc)
{
this.bcc = bcc;
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
public String getMimeType()
{
return mimeType;
}
public void setMimeType(String mimeType)
{
this.mimeType = mimeType;
}
public List<Attachment> getAttachments()
{
return attachments;
}
public void addAttachments(List<Attachment> attachments)
{
this.attachments.addAll(attachments);
}
public void addAttachment(Attachment attachment)
{
this.attachments.add(attachment);
}
public void removeAttachment(int index)
{
this.attachments.remove(index);
}
public void removeAllAttachments()
{
this.attachments.clear();
}
}

Attachment.java

public class Attachment
{
private byte[] data;
private String filename;
private String mimeType;

public Attachment()
{
}

public Attachment(byte[] data, String filename, String mimeType)
{
super();
this.data = data;
this.filename = filename;
this.mimeType = mimeType;
}

public byte[] getData()
{
return data;
}
public void setData(byte[] data)
{
this.data = data;
}
public String getFilename()
{
return filename;
}
public void setFilename(String filename)
{
this.filename = filename;
}

public String getMimeType()
{
return mimeType;
}

public void setMimeType(String mimeType)
{
this.mimeType = mimeType;
}

}

EmailService.java

import java.util.List;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

public class EmailService
{
private EmailConfiguration configuration = null;
private Authenticator auth =null;

public EmailService(EmailConfiguration configuration)
{
this.configuration = configuration;
this.auth = this.buildSmtpAuthenticator();
}

private Authenticator buildSmtpAuthenticator()
{
String emailId = configuration.getProperty(EmailConfiguration.SMTP_AUTH_USER);
String password = configuration.getProperty(EmailConfiguration.SMTP_AUTH_PWD);
return new SMTPAuthenticator(emailId, password);
}

public void sendEmail(Email email)
{
Session session = Session.getDefaultInstance(this.configuration.getProperties(), auth);
boolean debug = Boolean.valueOf(this.configuration.getProperty(EmailConfiguration.DEBUG));
session.setDebug(debug);

try
{
Message msg = this.buildEmailMessage(session, email);
Transport.send(msg);
}
catch (MessagingException e)
{
throw new RuntimeException(e);
}
}

private Message buildEmailMessage(Session session, Email email) throws MessagingException
{
Message msg = new MimeMessage(session);
msg.setSubject(email.getSubject());
this.addRecievers(msg, email);
Multipart multipart = new MimeMultipart();
this.addMessageBodyPart(multipart, email);
this.addAttachments(multipart, email);
msg.setContent(multipart);
return msg;
}

private void addRecievers(Message msg, Email email) throws MessagingException
{
InternetAddress from = new InternetAddress(email.getFrom());
msg.setFrom(from);

InternetAddress[] to = this.getInternetAddresses(email.getTo());
msg.setRecipients(Message.RecipientType.TO, to);

InternetAddress[] cc = this.getInternetAddresses(email.getCc());
msg.setRecipients(Message.RecipientType.CC, cc);

InternetAddress[] bcc = this.getInternetAddresses(email.getBcc());
msg.setRecipients(Message.RecipientType.BCC, bcc);

}
private void addMessageBodyPart(Multipart multipart, Email email) throws MessagingException
{
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(email.getText(), email.getMimeType());
multipart.addBodyPart(messageBodyPart);
}
private void addAttachments(Multipart multipart, Email email) throws MessagingException
{
List<Attachment> attachments = email.getAttachments();
if(attachments != null && attachments.size() > 0)
{
for (Attachment attachment : attachments)
{
BodyPart attachmentBodyPart = new MimeBodyPart();
String filename = attachment.getFilename() ;
DataSource source = new ByteArrayDataSource(attachment.getData(),
attachment.getMimeType());
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(filename);
multipart.addBodyPart(attachmentBodyPart);
}
}
}
private InternetAddress[] getInternetAddresses(String... addresses)
throws AddressException
{
if(addresses == null || addresses.length == 0)
{
return null;
}
InternetAddress[] iAddresses = new InternetAddress[addresses.length];
for (int i = 0; i < addresses.length; i++)
{
iAddresses[i] = new InternetAddress(addresses[i]);
}
return iAddresses;
}
}

class SMTPAuthenticator extends javax.mail.Authenticator
{
private String username;
private String password;

public SMTPAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
}


Add the mail.jar, activation.jar to the classpath. Here is the TestClient on how to use the EmailService Utility.

Testing our classes
public class EmailClient
{
public static void main(String[] args)
{
EmailConfiguration configuration = new EmailConfiguration();
configuration.setProperty(EmailConfiguration.SMTP_HOST, "smtp.gmail.com");
configuration.setProperty(EmailConfiguration.SMTP_AUTH, "true");
configuration.setProperty(EmailConfiguration.SMTP_TLS_ENABLE, "true");
configuration.setProperty(EmailConfiguration.SMTP_AUTH_USER, "xyz@gmail.com");
configuration.setProperty(EmailConfiguration.SMTP_AUTH_PWD, "**********");

EmailService emailService = new EmailService(configuration);
Email email = new Email();
email.setFrom("kinshuk.ram@gmail.com");
email.setTo("kinshuk_ram@yahoo.co.in");
email.setCc("kinshuk_ram@rediffmail.com");

email.setSubject("Test Mail from Java Jazzle");
email.setText("Hi,
This is a test email from Java Jazzle");
email.setMimeType("text/html");

Attachment attachment1 = new Attachment("ABCDEFGH".getBytes(), "test1.txt","text/plain");
email.addAttachment(attachment1);

Attachment attachment2 = new Attachment("XYZZZZZZ".getBytes(), "test2.txt","text/plain");
email.addAttachment(attachment2);

emailService.sendEmail(email);

}

}