Archive

Posts Tagged ‘feed’

JavaFX – Custom Feed Parsers

September 15, 2009 4 comments

In one of my previous post I have described the use of RSS and ATOM Task to parse the feeds.

Unfortunately many feeds do not strictly comply with the RSS/ATOM format. Example: In some cases the date specified for pubDate element may be in wrong format, or its an invalid date or the information specified is embedded in CDATA instead of TEXT. The parser will fail for such scenarios. Hence we need to implement custom parsers so as to handle the situation.

Below sample reads engadget rss feed..

For Applet mode, click on above image

Both RssTask and AtomTask API provide an attribute – factory – which can be used to specify custom implementation of RSS or ATOM Factory. Users may also customize various elements such as Channel, Item, Entry, Feed by overriding functions such as – fromXML, tryParseDate etc.

Example: In some cases the default parser fails to convert pubDate to valid DateTime instance. So a new attribute – pubDateText – is added so as to save the date as String.


/**
 * Customize Item so as to override fromXML
 */
class CustomItem extends Item {
    
    public-read var pubDateText : String;
    
    override function fromXML(parser: PullParser): Void {
        
        if(parser.event.qname == PUB_DATE) {
            if ((parser.event.type == PullParser.TEXTor
                (parser.event.type == PullParser.CDATA)) {
                pubDateText = stripCDATA(parser.event.text)
            }
        else if(parser.event.qname == TITLE) {
            title = stripCDATA(parser.event.text);
        else {
            super.fromXML(parser);
        }
    }
}

Now we need to implement a custom Factory which will instantiate and return the above CustomItem instance.


/**
 * Customize Factory so as to return CustomItem
 */
class CustomFactory extends Factory {
    public override function newItem(c: Channel: Item {
        CustomItem parent: c channel: c }
    }
}

Above Factory is set in factory attribute of RssTask as shown below.


var rssTask = RssTask {

    // set custom factory
    factory: CustomFactory { }

    location: "http://www.engadget.com/rss.xml"
    onChannel: function(channel) {  }
    onItem: function(item) { }
}
rssTask.start();

Update: Please refer to SpeedReaderFX by Jim Weaver for real world example.

Try it out and let me know feedback

var dzone_style = ‘2’;

Categories: javafx Tags: , , , ,

JavaFX – RSS and ATOM Task

June 18, 2009 3 comments

JavaFX 1.2 provides new APIs to handle RSS and ATOM feeds. RssTask and AtomTask parses the feeds and provide the content as JavaFX objects. It can also be configured to provide updates on regular intervals.

RssTask parses the feed and provides the content as Channel and Item objects. These objects in-turn provides other information such as image, link, title, category, date etc.

Similarly AtomTask parses feed and provides Feed and Entry objects. These objects in-turn provides information such as author, category, link, logo, contributors etc.

News ticker using Yahoo RSS feed is given below.

For Applet mode, click on above image

For standalone mode

RssTask Usage:


var rssTask = RssTask {
    
    location: "http://rss.news.yahoo.com/rss/world"
    interval: 300s

    onException: function(e) {
        e.printStackTrace();
    }

    onChannel: function(channel) {
        println("{channel.title}");
    }

    onItem: function(item) {
        println("{item.title}");
    }
}
rssTask.start();

AtomTask Usage:


var atomTask = AtomTask {
    
    location: "http://jfxstudio.wordpress.com/feed/atom"
    interval: 300s

    onException: function(e) {
        e.printStackTrace();
    }

    onFeed: function(feed) {
        println("{feed.title}");
    }

    onEntry: function(entry) {
        println("{entry.title}");        
    }
}
atomTask.start();

JavaFX Tweeter sample demonstrates use of both RssTask and HttpRequest.

Source:

var dzone_url = “http://blogs.sun.com/rakeshmenonp/entry/javafx_rss_and_atom_task”;
var dzone_style = ‘2’;

digg_skin = ‘compact’;
digg_window = ‘new’;

Categories: javafx Tags: , , , ,

JavaFX – News Widget

March 4, 2009 7 comments

This is an attempt to answer several queries in one go. Lets see if it helps..

  1. Can I embed JavaFX in JSP, ASP, PHP, JSF,…
    (Endless several other web technologies)?
  2. Can JavaFX dynamically interact with html page via JavaScript?
  3. Is it feasible to implement JavaFX JavaScript communication?
  4. Can JavaFX retrieve data from external services/resources?

Answer is YES! to all of above. Lets try to prove that..

We have a JavaFX Sample – RSS-Viewer which we will try to customize and integrate it into this blog. RSS-Viewer can retrieve data from various RSS feeds. It will parse the feed using JavaFX parser and render the HTML data in browser.

By default the application loads the feed from Yahoo! news. We will customize it so as to load the feed from Google News. The application accepts an argument rss_url which can be used to specify the RSS feed.

Now how will we incorporate this widget into this blog? We need to create a <div> in HTML which will act as placeholder for embedding the HTML data from JavaFX. We will update the content of this <div> using JavaScript.


<div id="ChannelData" style="text-align: center;"></div>

Now we will write a JavaScript method which will update the data from JavaFX.


function updateChannelData(channelData) {
    document.getElementById("ChannelData").innerHTML = channelData;
}

JavaFX will invoke updateChannelData( <data> ) with data from RSS feed and update the content.
Now we need to include the JavaFX Widget. For this we need to include the JavaScript dtfx.js and call javafx function passing the application attributes as shown below.


<script src="http://dl.javafx.com/1.2/dtfx.js&#34;></script>

<table width="40%" border="1" cellpadding="10" cellspacing="0">
  <tbody>
    <tr>
      <td align="center">

        <script>

          javafx(
            {
              archive: "http://javafx.com/samples/RSSViewer/webstart/RSSViewer.jar,&#34;,
              width: 610,
              height: 62,
              code: "rssviewer.Main",
              name: "RSSViewer"
            
              isApplet: "true"
              js_mode: "true"
              rss_url: "http://news.google.com/news?pz=1&ned=us&hl=en&topic=w&output=rss&#34;
            
          );
          
          function updateChannelData(channelData) {
            document.getElementById("ChannelData").innerHTML = channelData;
          }
          
        </script>
        
        <div id="ChannelData" style="text-align: center;"></div>
        
      </td>
    </tr>
  </tbody>
</table>

After embedding above code in your html you must be able to load and view Google News.

Now we have incorporated an existing widget into html page. It also uses JavaFX JavaScript interaction and loads the data from external service – Google News.

Refer to
LiveConnect Support in the Java™ Plug-In Technology
for more information on Java JavaScript communication.

But did I prove the first point – Can I embed JavaFX in JSP, ASP, PHP, JSF,…??. May be not? Please try to follow above steps and try to incorporate this widget into your page and let me know how things go. You must be able to provide any well formatted RSS feed as rss_url.

For standalone mode

function updateChannelData(channelData) {
document.getElementById(“ChannelData”).innerHTML = channelData;
}
javafx(
{
archive: “http://javafx.com/samples/RSSViewer/webstart/RSSViewer.jar,&#8221;,
width: 610,
height: 62,
code: “rssviewer.Main”,
name: “appl”
} , {
isApplet: “true”,
js_mode: “true”,
rss_url: “http://news.google.com/news?pz=1&ned=us&hl=en&topic=w&output=rss&#8221;
}
);

var dzone_url = “http://blogs.sun.com/rakeshmenonp/entry/javafx_news_widget&#8221;;
var dzone_style = ‘2’;

digg_skin = ‘compact’;
digg_window = ‘new’;

Categories: javafx Tags: , , , ,