Velocity Tools

Subprojects

Docs

Development

Velocity News Feed

FeedTool

FeedTool is a tool providing the ability to retrieve and manipulate RSS and Atom feeds from within Velocity templates. It is built upon the Rome API.

The tool works by retrieving a feed from a specified URI and returning it in a ContextFeedWrapper class. The idea with the wrapper class is to have a convenient location for the provision of convenience methods for feed and feed entry manipulation ... although it could easily be incorporated into a single class.

Dependencies

  • Rome API

    <dependency>
        <groupId>com.rometools</groupId>
        <artifactId>rome</artifactId>
        <version>1.10.0</version>
    </dependency>
    
  • Rome Fetcher

    <dependency>
        <groupId>com.rometools</groupId>
        <artifactId>rome-fetcher</artifactId>
        <version>1.10.0</version>
    </dependency>
    
  • JDom2

    <dependency>
        <groupId>org.jdom</groupId>
        <artifactId>jdom2</artifactId>
        <version>2.0.6</version>
    </dependency>
    

Code

FeedTool.java

ContextFeedWrapper.java

package org.apache.velocity.tools.view.tools;

import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.feed.synd.SyndEntry;

/**
 * Feed wrapper for Velocity context providing
 * utility methods for accessing feed object properties
 * 
 * @author Christopher Townson
 *
 */
public class ContextFeedWrapper {

        /**
         * The feed
         */
        private SyndFeed feed;

        /**
         * An interger containing the total number of items in the retrieved feed
         */
        private int numberOfEntries;

        /**
         * A SyndEntry object for holding the most recent feed entry
         */
        private SyndEntry latest;

        /**
         * 
         * @param feed
         */
        public ContextFeedWrapper(SyndFeed feed) {
                // assign feed object
                this.setFeed(feed);

                // grab number of entries
                this.numberOfEntries = this.getFeed().getEntries().size();

                // grab most recent entry (presently: just first entry)
                // TODO make this loop through entries and compare dates
                this.latest = (SyndEntry) this.getFeed().getEntries().get(0);
        }

        /**
         * @return Returns the feed.
         */
        public SyndFeed getFeed() {
                return this.feed;
        }

        /**
         * @param feed The feed to set.
         */
        public void setFeed(SyndFeed feed) {
                this.feed = feed;
        }

        /**
         * @return Returns the numberOfEntries.
         */
        public int getNumberOfEntries() {
                return numberOfEntries;
        }

        /**
         * @param numberOfEntries The numberOfEntries to set.
         */
        public void setNumberOfEntries(int numberOfEntries) {
                this.numberOfEntries = numberOfEntries;
        }

        /**
         * @return Returns the latest feed entry.
         */
        public SyndEntry getLatest() {
                return latest;
        }

        /**
         * @param latest The entry to set as the latest
         */
        public void setLatest(SyndEntry latest) {
                this.latest = latest;
        }

}