org.clapper.curn.output.script
Class ScriptOutputHandler

java.lang.Object
  extended by org.clapper.curn.output.FileOutputHandler
      extended by org.clapper.curn.output.script.ScriptOutputHandler
All Implemented Interfaces:
OutputHandler

public class ScriptOutputHandler
extends FileOutputHandler

Provides an output handler calls a script via the Apache Jakarta Bean Scripting Framework (BSF). This handler supports any scripting language supported by BSF. In addition to the configuration parameters supported by the FileOutputHandler base class, this handler supports the following additional configuration variables, which must be specified in the handler's configuration section.

Parameter Explanation
Script Path to the script to be invoked. The script will be called as if from the command line, except that additional objects will be available via BSF.
Language

The scripting language, as recognized by BSF. This handler supports all the scripting language engines that are registered with the BSF software. (Those predefined engines are configured in a properties file within the BSF software.) Some of the scripting language engines are actually bundled with BSF. Some are not. Regardless, of course, the actual the jar files for the scripting languages themselves must be in the CLASSPATH at runtime, for those languages to be available.

If you want to use a BSF scripting language engine that isn't automatically registered with BSF, simply extend this class and override the registerAdditionalScriptingEngines() method. In that method, call BSFManager.registerScriptingEngine() for each additional language you want to support. For example, to provide a handler that supports JudoScript, you might write an output handler that looks like this:

 import org.clapper.curn.CurnException;
 import org.clapper.curn.output.script.ScriptOutputHandler;
 import org.apache.bsf.BSFManager;

 public class MyOutputHandler extends ScriptOutputHandler
 {
     public JudoScriptOutputHandler()
     {
         super();
     }

     public void registerAdditionalScriptingEngines()
         throws CurnException
     {
         BSFManager.registerScriptingEngine ("mylang",
                                             "com.example.BSFMyLangEngine",
                                             new String[] {"ml", "myl"});
     }
 }
 
Then, simply use your class instead of ScriptOutputHandler in your configuration file.

This handler's displayChannel() method does not invoke the script; instead, it buffers up all the channels so that the flush() method can invoke the script. That way, the overhead of invoking the script only occurs once. Via the BSF engine, this handler makes available an iterator of special objects that wrap both the RSSChannel and FeedInfo objects for a given channel. See below for a more complete description.

The complete list of objects bound into the BSF beanspace follows.

Bound name Java type Explanation
channels java.util.Collection An Collection of special internal objects that wrap both RSSChannel and FeedInfo objects. The wrapper objects provide two methods:
outputPath java.lang.String The path to an output file. The script should write its output to that file. Overwriting the file is fine. If the script generates no output, then it can ignore the file.
config CurnConfig The org.clapper.curn.CurnConfig object that represents the parsed configuration data. Useful in conjunction with the "configSection" object, to parse additional parameters from the configuration.
configSection java.lang.String The name of the configuration file section in which the output handler was defined. Useful if the script wants to access additional script-specific configuration data.
mimeType java.io.PrintWriter A PrintWriter object to which the script should print the MIME type that corresponds to the generated output. If the script generates no output, then it can ignore this object.
logger org.clapper.util.logging.Logger A Logger object, useful for logging messages to the curn log file.
version java.lang.String Full curn version string, in case the script wants to include it in the generated output

For example, the following Jython script can be used as a template for a Jython output handler.

 import sys

 def __init__ (self):
     """
     Initialize a new TextOutputHandler object.
     """
     self.__channels    = bsf.lookupBean ("channels")
     self.__outputPath  = bsf.lookupBean ("outputPath")
     self.__mimeTypeOut = bsf.lookupBean ("mimeType")
     self.__config      = bsf.lookupBean ("config")
     self.__sectionName = bsf.lookupBean ("configSection")
     self.__logger      = bsf.lookupBean ("logger");
     self.__version     = bsf.lookupBean ("version")
     self.__message     = None

 def processChannels (self):
     """
     Process the channels passed in through the Bean Scripting Framework.
     """

     out = open (self.__outputPath, "w")
     msg = self.__config.getOptionalStringValue (self.__sectionName,
                                                 "Message",
                                                 None)

     totalNew = 0

     # First, count the total number of new items

     iterator = self.__channels.iterator()
     while iterator.hasNext():
         channel_wrapper = iterator.next()
         channel = channel_wrapper.getChannel()
         totalNew = totalNew + channel.getItems().size()

     if totalNew > 0:
         # If the config file specifies a message for this handler,
         # display it.

         if msg != None:
             out.println (msg)
             out.println ()

         # Now, process the items

         iterator = self.__channels.iterator()
         while iterator.hasNext():
             channel_wrapper = iterator.next()
             channel = channel_wrapper.getChannel()
             feed_info = channel_wrapper.getFeedInfo()
             self.__process_channel (out, channel, feed_info, indentation)

         self.__mimeTypeBuf.print ("text/plain")

         # Output a footer

         self.__indent (out, indentation)
         out.write ("\n")
         out.write (self.__version + "\n")
         out.close ()

 def process_channel (channel, feed_info):
     item_iterator = channel.getItems().iterator()
     while item_iterator.hasNext():
         # Do output for item
         ...

 main()
 

Version:
$Revision: 6441 $
See Also:
OutputHandler, FileOutputHandler, Curn, RSSChannel

Nested Class Summary
 class ScriptOutputHandler.ChannelWrapper
          Wraps an RSSChannel object and its FeedInfo object.
 class ScriptOutputHandler.CurnScriptObjects
          Container for the objects exported to the script.
 
Field Summary
 
Fields inherited from class org.clapper.curn.output.FileOutputHandler
CFG_ENCODING, CFG_SAVE_AS, CFG_SAVE_ONLY, CFG_SAVED_BACKUPS, CFG_SHOW_CURN_INFO
 
Constructor Summary
ScriptOutputHandler()
          Construct a new ScriptOutputHandler.
 
Method Summary
 void displayChannel(RSSChannel channel, FeedInfo feedInfo)
          Display the list of RSSItem news items to whatever output is defined for the underlying class.
 void flush()
          Flush any buffered-up output.
 java.lang.String getContentType()
          Get the content (i.e., MIME) type for output produced by this output handler.
 void initOutputHandler(CurnConfig config, ConfiguredOutputHandler cfgHandler)
          Initializes the output handler for another set of RSS channels.
 void registerAdditionalScriptingEngines()
          Deprecated. as of curn 3.1.
 
Methods inherited from class org.clapper.curn.output.FileOutputHandler
displayToolInfo, getGeneratedOutput, getName, getOutputEncoding, getOutputFile, hasGeneratedOutput, init, openOutputFile, savingOutputOnly, setName, setOutputEncoding
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScriptOutputHandler

public ScriptOutputHandler()
Construct a new ScriptOutputHandler.

Method Detail

initOutputHandler

public final void initOutputHandler(CurnConfig config,
                                    ConfiguredOutputHandler cfgHandler)
                             throws org.clapper.util.config.ConfigurationException,
                                    CurnException
Initializes the output handler for another set of RSS channels.

Specified by:
initOutputHandler in class FileOutputHandler
Parameters:
config - the parsed curn configuration data
cfgHandler - the ConfiguredOutputHandler wrapper containing this object; the wrapper has some useful metadata, such as the object's configuration section name and extra variables.
Throws:
org.clapper.util.config.ConfigurationException - configuration error
CurnException - some other initialization error

displayChannel

public final void displayChannel(RSSChannel channel,
                                 FeedInfo feedInfo)
                          throws CurnException
Display the list of RSSItem news items to whatever output is defined for the underlying class. This handler simply buffers up the channel, so that flush() can pass all the channels to the script.

Specified by:
displayChannel in interface OutputHandler
Specified by:
displayChannel in class FileOutputHandler
Parameters:
channel - The channel containing the items to emit. The method should emit all the items in the channel; the caller is responsible for clearing out any items that should not be seen.
feedInfo - Information about the feed, from the configuration
Throws:
CurnException - unable to write output

flush

public final void flush()
                 throws CurnException
Flush any buffered-up output.

Specified by:
flush in interface OutputHandler
Specified by:
flush in class FileOutputHandler
Throws:
CurnException - unable to write output

getContentType

public final java.lang.String getContentType()
Get the content (i.e., MIME) type for output produced by this output handler.

Specified by:
getContentType in interface OutputHandler
Specified by:
getContentType in class FileOutputHandler
Returns:
the content type
See Also:
OutputHandler.hasGeneratedOutput(), OutputHandler.getGeneratedOutput()

registerAdditionalScriptingEngines

public void registerAdditionalScriptingEngines()
                                        throws CurnException
Deprecated. as of curn 3.1.

Register additional scripting language engines that are not supported by this class. By default, this method does nothing. Subclasses that wish to register additional BSF scripting engine bindings should override this method and use BSFManager.registerScriptingEngine() to register the engined. See the class documentation, above, for additional details.

Throws:
CurnException - on error


Copyright © 2004-2006 Brian M. Clapper. All Rights Reserved.