BSF4REXX implementation of Java fragment

This is a discussion on BSF4REXX implementation of Java fragment within the REXX forums in Programming Languages category; As an excercise, I am trying to implement the Java Rome sample FeedReader in Rexx using BSF4REXX http://today.java.net/pub/a/today/20...r-of-rome.html I don't have a problem up till the for loop. for (final Iterator iter = feed.getModules().iterator(); iter.hasNext() { System.out.println("\t" + ((Module)iter.next()).getUri()); } However, here I am starting to lose it. I can create JavaCollectionList = .bsf~bsf.import("java.util.List") aModuleList = .JavaCollectionList feed~getModules() However, how am I going to iterate over this Java List collection? Any help with a bit of sample code as to how to treat Java collections and possibly Iterators would be very helpful. Thanks....

Go Back   Application Development Forum > Programming Languages > REXX

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-03-2008, 12:17 AM
regli
Guest
 
Default BSF4REXX implementation of Java fragment

As an excercise, I am trying to implement the Java Rome sample
FeedReader in Rexx using BSF4REXX

http://today.java.net/pub/a/today/20...r-of-rome.html

I don't have a problem up till the for loop.

for (final Iterator iter =
feed.getModules().iterator();
iter.hasNext()
{
System.out.println("\t" +
((Module)iter.next()).getUri());
}

However, here I am starting to lose it. I can create

JavaCollectionList = .bsf~bsf.import("java.util.List")
aModuleList = .JavaCollectionList feed~getModules()

However, how am I going to iterate over this Java List collection?


Any help with a bit of sample code as to how to treat Java collections
and possibly Iterators would be very helpful.

Thanks.
Reply With Quote
  #2  
Old 06-03-2008, 03:18 AM
rony
Guest
 
Default Re: BSF4REXX implementation of Java fragment

Hi Raetus,


regli wrote:
> As an excercise, I am trying to implement the Java Rome sample
> FeedReader in Rexx using BSF4REXX
>
> http://today.java.net/pub/a/today/20...r-of-rome.html
>
> I don't have a problem up till the for loop.
>
> for (final Iterator iter =
> feed.getModules().iterator();
> iter.hasNext()
> {
> System.out.println("\t" +
> ((Module)iter.next()).getUri());
> }
>
> However, here I am starting to lose it. I can create
>
> JavaCollectionList = .bsf~bsf.import("java.util.List")


The above statement will import the Java class "java.util.List" into ooRexx and make that (proxy)
class available via the environment symbol (note the leading dot): ".java.util.List". Also that
class object will get returned and you save it in the variable "JavaCollectionList".

From this moment on you could create new "java.util.List" objects by sending the ooRexx "new"
message to "JavaCollectionList", e.g.:

o=JavaCollectionList~new /* using the variable which points to the class object */

/* or with */
o=.java.util.List~new /* using the environment symbol which points to the class object */


Alternatively, you could also create Java objects without the need to import a Java class upfront, e.g.

o=.bsf~new("java.util.List") /* using the BSF4Rexx proxy class ".BSF" */


> aModuleList = .JavaCollectionList feed~getModules()


The above statement will yield the string representation of ".JavaCollectionList" which would be the
string ".JAVACOLLECTION" (note there is no entry in the ooRexx environment by the name
"JavaCollectionList" as that is the name of a plain Rexx variable in your example), which will get
concatenated to the string representation of "feed~getModules()", because there is a blank between
the two. Obviously, that is not what you are seeking.

> However, how am I going to iterate over this Java List collection?
>
>
> Any help with a bit of sample code as to how to treat Java collections
> and possibly Iterators would be very helpful.


You would treat them as documented.

You seem to have already a Java collection object that you name "feed" which implements a method
"getMethods()" which seems to return a collection object which possesses the method "iterator()"
which returns an Iterator object which posseses the methods "next()" (returning the next object from
the collection) and "hasNext()" returning "true", if there are still objects in the iterated
collection.

So this is how one could transcribe your Java example to ooRexx:

tab="09"x /* define the TAB character */
iter=feed~getModules~iterator /* get the modules collection and its iterator */
do while iter~hasNext /* as long as the iterator has an unprocessed object */
say tab iter~next~getUri~toString /* note the Java "toString" method! */
end


If you look at the transcription it probably looks pretty straight-forward. You just need the
Javadocs of the Java classes and Java interfaces (and actually not much knowledge about Java) to
learn about the fields and methods that the Java classes and interfaces define.

If a Java method returns a new Java object (from any Java class or possessing any Java interface)
then from the ooRexx side you can immediately address that object by sending it the messages it can
understand (i.e. the name of the methods and/or fields). Whatever the Javadoc says, will happen then...

A last word at the Java "toString"-method: this method will ask the Java object for its string
representation, sometimes yielding very verbose and interesting information for debugging, at other
times it is just a string representation which makes sense in the context of what the Java object
represents (probably the URI in your case), and some would just return the name of the class
concatenated with a hex-string-number with the "@" character.

HTH,

---rony
Reply With Quote
  #3  
Old 06-03-2008, 03:38 AM
rony
Guest
 
Default Re: BSF4REXX implementation of Java fragment

Hi Raetus,

a last remark:

> say tab iter~next~getUri~toString /* note the Java "toString" method! */


If the method "getUri" already returns a string and not an URL/URI Java object, then you won't need
to invoke the "toString" method.

HTH,

---rony
Reply With Quote
  #4  
Old 06-03-2008, 02:31 PM
regli
Guest
 
Default Re: BSF4REXX implementation of Java fragment

On Jun 3, 12:38*am, rony <Rony.Flatsc...@wu-wien.ac.at> wrote:
> Hi Raetus,
>
> a last remark:
>
> > say tab iter~next~getUri~toString /* note the Java "toString" method! */

>
> If the method "getUri" already returns a string and not an URL/URI Java object, then you won't need
> to invoke the "toString" method.
>
> HTH,
>
> ---rony


On Jun 3, 12:38 am, rony <Rony.Flatsc...@wu-wien.ac.at> wrote:
> Hi Raetus,
>
> a last remark:
>
> > say tab iter~next~getUri~toString /* note the Java "toString" method! */

>
> If the method "getUri" already returns a string and not an URL/URI Java object, then you won't need
> to invoke the "toString" method.
>
> HTH,
>
> ---rony


Thanks very much. The section of code solved the issue nicely.

I will post the full code of the FeedReader here once I have it
completed.

However, there is another issue that I haven't figured out yet. Here
is the Java code:

for (final Iterator iter =
feed.getEntries().iterator();
iter.hasNext()
{
System.out.println("\t" +

((SyndEntry)iter.next()).getTitle());
}


Notice the overtyping of Feed to SyndEntry. If I don't use the
overtype then the script fails obviously because feed has no method
hasNext:

method: [HASNEXT] not found!

How do I achieve the Java overtype effect in BSF?

Thanks again.
Reply With Quote
  #5  
Old 06-03-2008, 03:38 PM
regli
Guest
 
Default Re: BSF4REXX implementation of Java fragment


Sorry. Scratch that. I didn't even need to typecast it. I missed
the "~iterator" and it now works just fine. No issues here.
Reply With Quote
  #6  
Old 06-03-2008, 06:39 PM
regli
Guest
 
Default Re: BSF4REXX implementation of Java fragment

Here is the Rexx FeedReader code as promised. Any comments welcome.

Input is the URL of an RSS or Atom feed:


------------ cut -------------
parse source system . programname
parse version IPversion

if Arg()>0 then
parse arg
inURL /* Pick up
URL */
else do
say ""
say "FeedReader reads and prints info on any RSS/Atom feed."
say "The first parameter must be the URL of the feed to read."
say ""
exit (8)
end

/* main program */

call
SysDropFuncs /*
remove if you don't want to deregister */
/
* RexxUtil functions */

call
bsf.cls /*
get ooRexx support for BSF*/
s =
bsf.loadClass("java.lang.System") /*
get the Java class object */
say IPversion "- Java Version:"
s~getProperty("java.version") /* get and output Java
version*/
say

/* imports
*/
call bsf.import "java.net.URL",
"java.util.Date",
"java.util.Iterator",
"java.util.List",
"com.sun.syndication.feed.module.Module",
"com.sun.syndication.feed.synd.SyndEntry",
"com.sun.syndication.feed.synd.SyndFeed",
"com.sun.syndication.io.SyndFeedInput",
"import com.sun.syndication.io.XmlReader"

/* object proxy definitions
*/
URL = .bsf~bsf.import("java.net.URL")
SyndFeedInput
= .bsf~bsf.import("com.sun.syndication.io.SyndFeedIn put")
XmlReader = .bsf~bsf.import("com.sun.syndication.io.XmlReader" )

feedUrl = URL~new(inURL)
input = SyndFeedInput~new()
feed = input~build(XmlReader~new(feedUrl))

say "Title: " feed~getTitle()
say "Author: " feed~getAuthor()
say "Description: " feed~getDescription()
say "Pub date: " feed~getPublishedDate~toString
say "Copyright: " feed~getCopyright()
say "Modules used:"

tab="09"x /* define the TAB
character */
iter=feed~getModules~iterator /* get the modules
collection and its iterator */
do while iter~hasNext /* as long as the iterator
has an unprocessed object */
say tab iter~next~getUri /* may need a "toString"
method but not here! */
end

say "Titles of the "feed~getEntries~size" entries:"

iter=feed~getEntries~iterator /* get feed
items */
do while iter~hasNext /* as long as the iterator
has an unprocessed object */
say tab iter~next~getTitle
end

res = feed~getImage
if feed~getImage \= .NIL then
do
say "Feed image URL: " feed~getImage~getUrl()
end

exit 0 /* leave program */
------------ cut -------------


Reply With Quote
  #7  
Old 06-05-2008, 05:35 PM
rony
Guest
 
Default Re: BSF4REXX implementation of Java fragment

Hi Raetus,

here is a slightly edited version:

- adds comments about the jar-packages one needs to run this program
- does not import the Java classes, but rather instantiates them via the .BSF class

--------------- cut here --------------
/* needs: - ROME, <https://rome.dev.java.net/> states as of 2008-06-05:

"ROME is an set of open source Java tools for parsing, generating and
publishing RSS and Atom feeds. The core ROME library depends only on the
JDOM XML parser and supports parsing, generating and converting all of the
popular RSS and Atom formats including 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,
and Atom 1.0. You can parse to an RSS object model, an Atom object model
or an abstract SyndFeed model that can model either family of formats."

- JDOM, <http://jdom.org>
*/

parse source system . programname
parse version IPversion

if Arg()>0 then
parse arg inURL /* Pick up URL */
else do
say ""
say "FeedReader reads and prints info on any RSS/Atom feed."
say "The first parameter must be the URL of the feed to read."
say ""
exit (8)
end

/* main program */

call SysDropFuncs /* remove if you don't want to deregister */
/* RexxUtil functions */

call bsf.cls /* get ooRexx support for BSF */
s = bsf.loadClass("java.lang.System") /* get the Java class object */
/* get and output Java version */
say IPversion "- Java Version:" s~getProperty("java.version")
say

/* object proxy definitions */
feedUrl = .bsf~new("java.net.URL", inURL)
input = .bsf~new("com.sun.syndication.io.SyndFeedInput")
feed = input~build(.bsf~new("com.sun.syndication.io.XmlRe ader", feedUrl))

say "Title: " feed~title()

say "Author: " feed~author()
say "Description: " feed~description()
say "Pub date: " feed~publishedDate~toString
say "Copyright: " feed~copyright()
say "Modules used:"

tab="09"x /* define the TAB character */
iter=feed~getModules~iterator /* get the modules collection and
its iterator */
do while iter~hasNext /* as long as the iterator has an
unprocessed object */
say tab iter~next~uri
end

say "Titles of the "feed~getEntries~size" entries:"

iter=feed~getEntries~iterator /* get feed items */
do while iter~hasNext /* as long as the iterator has an
unprocessed object */
say tab iter~next~title
end

res = feed~image
if feed~image \= .NIL then
do
say "Feed image URL: " feed~image~url
end

exit 0 /* leave program */

--------------- cut here --------------

---rony
Reply With Quote
  #8  
Old 06-05-2008, 06:01 PM
regli
Guest
 
Default Re: BSF4REXX implementation of Java fragment

Thanks a lot rony.

It looks very nice after the cleanup job! BSF4REXX is simply a
great tool to take advantage of the huge array of Java class
libraries.

Hopefully, this example inspires a few more Rexx forks to enter the
RSS or Atom feed arena. Or better yet, it might entice a few feed
addicts to enter the world of Rexx.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:49 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.