• Como Publicar RSS Con Java Rome

    Really Simple Sindication (RSS) es un excelente standard para distribuir noticias en tu website. Hoy en dia cualquiera tiene RSS desde Clarin.com hasta YO :D.



    Buscando encontre una API oficial de SUN para generar RSS 0.90, RSS 0.91 Netscape, RSS 0.91 Userland, RSS 0.92, RSS 0.93, RSS 0.94, RSS 1.0, RSS 2.0, Atom 0.3 y Atom 1.0.



    En este articulo vamos a ver como generar un output RSS utilizando ROME.

    Para empezar hay que bajarse la API ROME desde aca y ademas hay que bajar JDOM el cual es una API de DOM exclusiva para Java desde aca.

    Una vez que tenemos el jar rome-1.0RC1.jar y jdom.jar en nuestro CLASSPATH podemos empezar a codificar una sencilla clase RSSWriter.

    import com.sun.syndication.feed.synd.SyndContentImpl;
    import com.sun.syndication.feed.synd.SyndEntryImpl;
    import com.sun.syndication.feed.synd.SyndFeed;
    import com.sun.syndication.feed.synd.SyndFeedImpl;
    import com.sun.syndication.io.SyndFeedOutput;
    
    public class RSSWriter {
    
    SyndFeed feed = new SyndFeedImpl();
    
    public RSSWriter() {
    	this.feed.setFeedType("rss_2.0"); // si es que queremos este formato
    }
    
    public void setTitle(String title) {
    	this.feed.setTitle(title);
    }
    
    public void setType(String type) {
    	this.feed.setFeedType(type);
    }
    
    public void setLink(String link) {
    	this.feed.setLink(link);
    }
    
    public void setDescription(String description) {
    	this.feed.setDescription(description);
    }
    
    public void addEntry(String title, String link, Date date, String text) {
    	SyndEntryImpl entry = new SyndEntryImpl();
    	entry.setTitle(title);
    	entry.setLink(link);
    	entry.setPublishedDate(date);
    	SyndContentImpl description = new SyndContentImpl();
    	description.setType("text/plain");
    	description.setValue(text);
    	entry.setDescription(description);
    	List entries = this.feed.getEntries();
    	if (entries == null) {
    		entries = new ArrayList();
    		this.feed.setEntries(entries);
    	}
    	entries.add(entry);
    }
    
    public void serialize(Writer writer) throws Exception {
    	SyndFeedOutput output = new SyndFeedOutput();
    	output.output(this.feed, writer);
    }
    
    public void serialize(File file) throws Exception {
    	SyndFeedOutput output = new SyndFeedOutput();
    	output.output(this.feed, file);
    }
    
    public String serialize() throws Exception {
    	SyndFeedOutput output = new SyndFeedOutput();
    	return output.outputString(this.feed);
    }
    
    }
    

    Y mas que facil el utilizarla.

    RSSWriter writer = new RSSWriter();
    writer.setTitle("Algun titulito");
    writer.setDescription("Mis ultimas noticias");
    writer.setLink("http://www.rodrigoasensio.com");
    writer.addEntry("Nuevo post!", "http://rodrigoasensio.com/permalink/xx",
    new Date(), "Este es el texto de contenido que puede ser HTML");
    writer.serialize(new FileOutputStream("/usr/share/tomcat/webapps/ROOT/feed.xml"));
    

    El delivery de noticias con este estandard tan sencillo revoluciono la manera de obtener informacion con los readers y demas. Lo que mostre aca es solo una pequenia parte de toda la API. Rome ademas tiene subprojects entre los cuales figuran ROME Fetcher que provee cache feed-fetcher, ROME Propono que provee Atom protocol y MetaWeblog API.

    Tags: ,

Leave a comment