RandomFile (Java)
.
RandomFile returns the contents of a randomly-selected file in a specifed directory. Qualification by prefux and suffix is supported. I wound up calling this code from within a Borscht server-side processed page, by saying
{* setHandler randomFile=”client.someone.handler.misc.RandomFile” *}
{* randomFile root=$cgi.DOCUMENT_ROOT prefix=”item_ID_” dir=”/factoids/promo/” *}
{* randomFile root=$cgi.DOCUMENT_ROOT prefix=”item_ID_” dir=”/factoids/funfacts/” *}
The file RandomFile.java looks like:
package client.someone.handler.misc
import java.util.Hashtable;
import java.util.Vector;
import organic.servlet.broker.*;
import java.io.File ;
import java.io.FileReader ;
import java.io.BufferedReader ;
public class RandomFile implements HandlerInterface {
// Standard error messages
static final String kErrMissingDirArg =
“RandomFile: error: missing dir arg: ” +
“usage: \{* randomFile dir=\”e:\\\” *}”;
static final String kErrInvalidDirArg =
“RandomFile: error: invalid directory argument value: ” ;
public Object handleToken(String tokenName,
Hashtable args,
Request request)
throws HandlerException
{
try {
// Get the root argument passed to us
String rootArg = (String) args.get( “root” ) ;
// Get the dir argument passed to us; complain if missing.
String dirArg = (String) args.get( “dir” ) ;
if ( dirArg == null || dirArg.equals(“”) )
return kErrMissingDirArg ;
// Get filesInDir or return appropriate error message
File dir = new File( rootArg + dirArg ) ;
String files[] = dir.list() ;
int filesInDir ;
try { filesInDir = files.length ; }
catch (Exception e) { return kErrInvalidDirArg + “\”” + rootArg + dirArg + “”” ; }
// Get the prefix argument passed to us
String prefixArg = (String) args.get( “prefix” ) ;
// Get the suffix argument passed to us
String suffixArg = (String) args.get( “suffix” ) ;
Vector filesThatQualify = QualifyFiles( files, prefixArg, suffixArg ) ;
BufferedReader in = new BufferedReader(
new FileReader( rootArg + dirArg + filesThatQualify.elementAt(
(int) ( Math.random() * filesThatQualify.size() )))) ;
StringBuffer contents = new StringBuffer() ;
String oneLine ;
while ( ( oneLine = in.readLine() ) != null )
contents.append( oneLine ).append(” “) ;
in.close() ;
return contents ;
} catch (Exception e) { throw new HandlerException(e); }
}
// —————————————————–
// Qualify the filenames based on prefix and/or suffix.
// —————————————————–
public Vector QualifyFiles(String[] files, String prefixArg, String suffixArg) {
StringBuffer b = new StringBuffer() ;
String fn = new String() ;
Vector filesThatQualify = new Vector(1) ;
for ( int i = 0 ; i < files.length ; i++ ) \{ fn = files[i] ; if ( prefixArg != null && !prefixArg.equals("") ) if ( !fn.startsWith( prefixArg )) fn = "" ; if ( suffixArg != null && !suffixArg.equals("") ) if ( !fn.endsWith( suffixArg )) fn = "" ; if ( !fn.equals("") ) filesThatQualify.addElement( files[i] ) ; } return filesThatQualify ; } } This page is 1993-2006 by , via the Creative Commons License. Questions and comments? Send to the Geek Times Webmaster. (Domain and web content hosting at .)