acm.util
Class JTFTools

java.lang.Object
  extended by acm.util.JTFTools

public class JTFTools
extends Object

This class provides a collection of static utility methods that are used elsewhere in the ACM packages.


Method Summary
static void cancelMail(PrintStream out)
          Cancels the mail-sending process without sending the message.
static boolean checkIfLoaded(String className)
          Returns true if the specified class is already loaded.
static void copyBytes(InputStream in, OutputStream out, long nBytes)
          Copies the specified number bytes from the input stream to the output stream.
static void copyFile(File oldFile, File newFile)
          Copies a file from oldFile to newFile.
static Container createEmptyContainer()
          Returns an empty lightweight container.
static Color decodeColor(String name)
          Decodes a color name.
static Font decodeFont(String str)
          Decodes a font in the style of Font.decode.
static Font decodeFont(String str, Font oldFont)
          Decodes a font in the style of Font.decode.
static boolean executeExportAction(Program program, String command)
          Checks to see if the action command corresponds to one of the export actions.
static void exportJar(File jarFile, File dir, String imports, Object transformer)
          Exports a jar file consisting of all the .class files in the directory, including those embedded inside jar files.
static String findFontFamily(String str)
          Finds the first font family in the string str that exists in the current GraphicsEnvironment.
static Applet getApplet()
          Returns the current applet.
static String getCommandLine()
          Returns the command line that invoked this program, or null if no command line is available.
static Frame getEnclosingFrame(Component comp)
          Returns the frame that encloses the specified component.
static String[] getFontList()
          Returns an array of the available font family names.
static String getLocalHostName()
          Returns the canonical name of the local host.
static String getMainClass()
          Returns the name of the main class.
static Font getStandardFont(Font font)
          Returns a font that will approximate the specified font in this environment.
static boolean isAnonymous(Thread t)
          Returns true if the supplied thread is an anonymous one created automatically by the system.
static boolean matchFilenamePattern(String filename, String pattern)
          Determines whether the filename matches the specified pattern.
static OutputStream openBase64OutputStream(PrintStream printStream)
          Opens an output stream that allows the client to write data using Base64 encoding, which is the most common encoding for mail attachments.
static OutputStream openHexByteOutputStream(PrintStream printStream)
          Opens an output stream that allows the client to write data using pairs of hex characters.
static PrintStream openMailStream(String smtpServer, String from, String to)
          Opens a mail-sending process using the specified SMTP server, which will send a message from the specified sender to the recipient.
static void padBase64OutputStream(OutputStream out)
          Pads the data in the specified output stream without closing it.
static void pause(double milliseconds)
          Delays the calling thread for the specified time, which is expressed in milliseconds.
static void registerApplet(Applet applet)
          Adds this applet to a table indexed by the current thread.
static void registerApplet(Applet applet, Thread thread)
          Adds this applet to a table indexed by the specified thread.
static void sendStandardHeaders(PrintStream out, String senderName, String subject)
          Adds the standard "To", "From", "Subject", and "Date" headers to the message.
static void setDebugOptions(String options)
          Sets the debugging options in place for this run.
protected static void submitProject(Program program, JProgressBar progress)
          Submits this project as a mail message.
static void terminateAppletThreads(Applet applet)
          Terminates all of the threads that are registered as belonging to the specified applet.
static boolean testDebugOption(String option)
          Tests to see whether the current program was invoked with a debug parameter that contains the specified string as a lexical unit in a string delimited by + signs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

cancelMail

public static void cancelMail(PrintStream out)
Cancels the mail-sending process without sending the message. The output stream is closed after returning.

Parameters:
out - The output stream returned by openMailStream

checkIfLoaded

public static boolean checkIfLoaded(String className)
Returns true if the specified class is already loaded.

Parameters:
className - The name of the class
Returns:
The boolean value true if the class is already loaded, and false otherwise

copyBytes

public static void copyBytes(InputStream in,
                             OutputStream out,
                             long nBytes)
                      throws IOException
Copies the specified number bytes from the input stream to the output stream.

Parameters:
in - The input stream
out - The output stream
nBytes - The number of bytes to copy
Throws:
IOException

copyFile

public static void copyFile(File oldFile,
                            File newFile)
Copies a file from oldFile to newFile.

Parameters:
oldFile - A File object indicating the existing file
newFile - A File object indicating the new file

createEmptyContainer

public static Container createEmptyContainer()
Returns an empty lightweight container. Several packages need to create such components as placeholders. Defining it in JTFTools gives those pacakges access to a common mechanism.

Returns:
An empty lightweight container that can be used as a placeholder

decodeColor

public static Color decodeColor(String name)
Decodes a color name. This method is similar to Color.decode except in that it allows named colors and system colors.

Parameters:
name - The string name of the color
Returns:
The color corresponding to the specified name

decodeFont

public static Font decodeFont(String str)
Decodes a font in the style of Font.decode.

Parameters:
str - The string to decode
Returns:
The new font

decodeFont

public static Font decodeFont(String str,
                              Font oldFont)
Decodes a font in the style of Font.decode. The only difference is that this method takes a font parameter that gives default values for the different parts of the font. If the family, size, or style is specified as an asterisk, the corresponding value is taken from the supplied font.

Parameters:
str - The string to decode
oldFont - The font whose properties are used as defaults
Returns:
The new font

executeExportAction

public static boolean executeExportAction(Program program,
                                          String command)
Checks to see if the action command corresponds to one of the export actions. If so, it executes the method and returns true; if not, the method returns false.

Parameters:
program - The program to which the action is applied
command - The command name being executed
Returns:
A flag indicating whether the action was recognized

exportJar

public static void exportJar(File jarFile,
                             File dir,
                             String imports,
                             Object transformer)
Exports a jar file consisting of all the .class files in the directory, including those embedded inside jar files. If the transformer method is not null, its transform method is called to dump the bytes of the class instead of simply copying them.

Parameters:
jarFile - A File object indicating the destination jar file
dir - The directory being exported
imports - A semicolon-separated list of .jar files to include (or null)
transformer - An object to transform class data (or null)

findFontFamily

public static String findFontFamily(String str)
Finds the first font family in the string str that exists in the current GraphicsEnvironment. As in HTML font tags, the str consists of a set of font names separated by semicolons. The findFontFamily method returns the first family name that exists in the list of loaded fonts, or null if there are none.

Parameters:
str - The list of family names separated by semicolons
Returns:
The first family name that exists, or null if none

getApplet

public static Applet getApplet()
Returns the current applet. This implementation may fail in multithreaded programs. Such programs should therefore not rely on this facility. It is appropriate to use this facility, for example, during the init method of a program, which is the only context in which it is applied in the JTF tools.

Returns:
The currently running applet

getCommandLine

public static String getCommandLine()
Returns the command line that invoked this program, or null if no command line is available.

Returns:
The command line that invoked this program

getEnclosingFrame

public static Frame getEnclosingFrame(Component comp)
Returns the frame that encloses the specified component.

Parameters:
comp - The component at which to start the search
Returns:
The nearest enclosing Frame object

getFontList

public static String[] getFontList()
Returns an array of the available font family names.

Returns:
An array of the available font family names

getLocalHostName

public static String getLocalHostName()
Returns the canonical name of the local host.

Returns:
The fully qualified name of the local host, or null if unavailable

getMainClass

public static String getMainClass()
Returns the name of the main class.

Returns:
The name of the main class, or null if that class cannot be identified

getStandardFont

public static Font getStandardFont(Font font)
Returns a font that will approximate the specified font in this environment. This method is required because some browsers do not support the standard fonts Serif, SansSerif, and Monospaced.

Parameters:
font - The font being checked
Returns:
The new font

isAnonymous

public static boolean isAnonymous(Thread t)
Returns true if the supplied thread is an anonymous one created automatically by the system.

Parameters:
t - The thread being tested

matchFilenamePattern

public static boolean matchFilenamePattern(String filename,
                                           String pattern)
Determines whether the filename matches the specified pattern. The pattern string is interpreted in much the same way that a Unix shell expands filenames and supports the following wildcard options:
?Matches any single character
*Matches any sequence of characters
[...]Matches any of the specified characters
[^...]Matches any character except the specified ones
The last two options allow a range of characters to be specified in the form a-z.

Parameters:
filename - The filename being tested
pattern - The pattern including wildcards
Returns:
true if the filename matches the pattern

openBase64OutputStream

public static OutputStream openBase64OutputStream(PrintStream printStream)
Opens an output stream that allows the client to write data using Base64 encoding, which is the most common encoding for mail attachments. The return value is an OutputStream that the client can use to which the client can send binary data, which is then written in encoded form to the underlying PrintStream object. Closing the stream automatically pads the data to complete a Base64 unit. To pad the data without closing the stream (as one would need to do for a multi-attachment message, for example), call the padBase64OutputStream method.

Parameters:
printStream - The underlying PrintStream to which data is written
Returns:
An OutputStream for writing binary data

openHexByteOutputStream

public static OutputStream openHexByteOutputStream(PrintStream printStream)
Opens an output stream that allows the client to write data using pairs of hex characters. The return value is an OutputStream that the client can use to which the client can send binary data, which is then written in encoded form to the underlying PrintStream object.

Parameters:
printStream - The underlying PrintStream to which data is written
Returns:
An OutputStream for writing binary data

openMailStream

public static PrintStream openMailStream(String smtpServer,
                                         String from,
                                         String to)
Opens a mail-sending process using the specified SMTP server, which will send a message from the specified sender to the recipient. The return value is a PrintStream that the client can use to write the data in the message, including the headers. Closing the stream sends the message. To cancel the message without sending it, use the cancelMail method.

Parameters:
smtpServer - A string indicating the host name of the SMTP server
from - A string indicating the e-mail address of the sender
to - A string indicating the e-mail address of this recipient
Returns:
A PrintStream suitable for sending the message data

padBase64OutputStream

public static void padBase64OutputStream(OutputStream out)
Pads the data in the specified output stream without closing it. This method is automatically called when the stream is closed, but must be called explicitly if you intend to write additional data to the underlying PrintStream.

Parameters:
out - The output stream returned by openBase64OutputStream

pause

public static void pause(double milliseconds)
Delays the calling thread for the specified time, which is expressed in milliseconds. Unlike Thread.sleep, this method never throws an exception.

Parameters:
milliseconds - The sleep time in milliseconds

registerApplet

public static void registerApplet(Applet applet)
Adds this applet to a table indexed by the current thread.

Parameters:
applet - The applet being registered

registerApplet

public static void registerApplet(Applet applet,
                                  Thread thread)
Adds this applet to a table indexed by the specified thread.

Parameters:
applet - The applet being registered
thread - The thread used as the key

sendStandardHeaders

public static void sendStandardHeaders(PrintStream out,
                                       String senderName,
                                       String subject)
Adds the standard "To", "From", "Subject", and "Date" headers to the message.

Parameters:
out - The output stream returned by openMailStream
senderName - The real name of the sender
subject - The subject line

setDebugOptions

public static void setDebugOptions(String options)
Sets the debugging options in place for this run. The list of options are a set of strings combined with the + sign character.

Parameters:
options - The names of the enabled options separated by plus signs

submitProject

protected static void submitProject(Program program,
                                    JProgressBar progress)
Submits this project as a mail message.

Parameters:
program - The program being submitted
progress - An optional progress bar to keep track of the operation

terminateAppletThreads

public static void terminateAppletThreads(Applet applet)
Terminates all of the threads that are registered as belonging to the specified applet.

Parameters:
applet - The applet whose threads are being terminated

testDebugOption

public static boolean testDebugOption(String option)
Tests to see whether the current program was invoked with a debug parameter that contains the specified string as a lexical unit in a string delimited by + signs.

Parameters:
option - The name of the option
Returns:
The constant true if the specified debug option is in effect