org.alicebot.server.core.util
Class Toolkit

java.lang.Object
  extended by org.alicebot.server.core.util.Toolkit

public class Toolkit
extends java.lang.Object

Contains various utilities.

Author:
Richard Wallace, Jon Baer, Thomas Ringate, Pedro Colla, Noel Bush

Field Summary
protected static java.lang.String EQUAL_QUOTE
          A common string we search for when parsing attributes in tags.
protected static char QUOTE_MARK
          A quote mark, for convenience.
 
Constructor Summary
Toolkit()
           
 
Method Summary
static java.lang.String[] breakLines(java.lang.String input)
           Breaks a message into multiple lines at any tag, except if the tag comes at the beginning of the message.
static void checkOrCreate(java.lang.String path, java.lang.String description)
          Checks whether a file given by a path exists, and if not, creates it, along with any necessary subdirectories.
static java.lang.String convertXMLUnicodeEntities(java.lang.String input)
           Converts XML Unicode character entities into their character equivalents within a given string.
static java.lang.String escapeXMLChars(java.lang.String input)
           Replaces the following characters with their "escaped" equivalents:
static java.lang.String filterWhitespace(java.lang.String input)
           Filters all whitespace: line separators and multiple consecutive spaces are replaced with a single space, and any leading or trailing whitespace characters are removed.
static java.lang.String formatAIML(java.util.LinkedList trie, int level, boolean atStart)
          Formats AIML from a trie into a nicely indented multi-line string.
static java.lang.String formatAIML(java.lang.String content)
          Formats AIML from a single long string into a nicely indented multi-line string.
static java.lang.String getAttributeValue(java.lang.String attributeName, java.lang.String args)
           Gets value of an attribute by name.
static java.lang.String getDeclaredXMLEncoding(java.io.InputStream in)
          Returns the declared encoding string from the XML resource supposedly connected to a given InputStream, or the system default if none is found.
static java.lang.String getFileContents(java.lang.String path)
          Returns the entire contents of a file as a String.
static java.lang.Class[] getImplementorsOf(java.lang.String interfaceName, boolean showFound)
           Scans the classpath for classes that implement a given interface.
static java.lang.String[] glob(java.lang.String path, java.lang.String workingDirectory)
           Expands a localized file name that may contain wildcards to an array of file names without wildcards.
static java.lang.String removeMarkup(java.lang.String input)
          Removes all tags from a string (retains character content of tags, however).
static java.lang.String tab(int level)
          Returns a tab of the specified length.
static java.lang.String unescapeXMLChars(java.lang.String input)
           Replaces the following "escape" strings with their character equivalents:
static java.util.ArrayList wordSplit(java.lang.String input)
          Splits an input into words, breaking at spaces.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EQUAL_QUOTE

protected static final java.lang.String EQUAL_QUOTE
A common string we search for when parsing attributes in tags.

See Also:
Constant Field Values

QUOTE_MARK

protected static final char QUOTE_MARK
A quote mark, for convenience.

See Also:
Constant Field Values
Constructor Detail

Toolkit

public Toolkit()
Method Detail

filterWhitespace

public static java.lang.String filterWhitespace(java.lang.String input)
                                         throws java.lang.StringIndexOutOfBoundsException

Filters all whitespace: line separators and multiple consecutive spaces are replaced with a single space, and any leading or trailing whitespace characters are removed. Any data enclosed in <![CDATA[ ]]> sections, however, is left as-is (including the CDATA markers).

Parameters:
input - the input to filter
Returns:
the input with white space filtered.
Throws:
java.lang.StringIndexOutOfBoundsException - (from removeAll if there is malformed text in the input.

breakLines

public static java.lang.String[] breakLines(java.lang.String input)

Breaks a message into multiple lines at any tag, except if the tag comes at the beginning of the message.

Generally used to format output nicely for a console.

Parameters:
input - the string to break
Returns:
one line per array item

removeMarkup

public static java.lang.String removeMarkup(java.lang.String input)
Removes all tags from a string (retains character content of tags, however).

Parameters:
input - the string from which to remove markup
Returns:
the input without tags

unescapeXMLChars

public static java.lang.String unescapeXMLChars(java.lang.String input)

Replaces the following "escape" strings with their character equivalents:

  • &amp; with &
  • &lt; with <
  • &gt; with >
  • &apos; with '
  • &quot; with "

Parameters:
input - the string on which to perform the replacement
Returns:
the string with entities replaced

escapeXMLChars

public static java.lang.String escapeXMLChars(java.lang.String input)

Replaces the following characters with their "escaped" equivalents:

  • & with &amp;
  • < with &lt;
  • > with &gt;
  • ' with &apos;
  • " with &quot;

Parameters:
input - the string on which to perform the replacement
Returns:
the string with entities replaced

convertXMLUnicodeEntities

public static java.lang.String convertXMLUnicodeEntities(java.lang.String input)

Converts XML Unicode character entities into their character equivalents within a given string.

This will handle entities in the form &#xxxx; (decimal character code, where xxxx is a valid character code), or &#xxxxx (hexadecimal character code, where xxxx is a valid character code).

Parameters:
input - the string to process
Returns:
the input with all XML Unicode character entity codes replaced

getDeclaredXMLEncoding

public static java.lang.String getDeclaredXMLEncoding(java.io.InputStream in)
                                               throws java.io.IOException
Returns the declared encoding string from the XML resource supposedly connected to a given InputStream, or the system default if none is found.

Parameters:
in - the input stream
Throws:
java.io.IOException

getAttributeValue

public static java.lang.String getAttributeValue(java.lang.String attributeName,
                                                 java.lang.String args)

Gets value of an attribute by name.

This method looks in the supplied argument string, and parses it looking for the structure "attributeName=", returning whatever is after the "=" until the next blank or the end of the string. This is mostly useful for extracting attributes from AIML tags.

Parameters:
attribute - the attribute whose value is wanted
args - the argument string
Returns:
the value of the argument (or empty string if nothing)

formatAIML

public static java.lang.String formatAIML(java.lang.String content)
Formats AIML from a single long string into a nicely indented multi-line string.

Parameters:
content - the AIML content to format

formatAIML

public static java.lang.String formatAIML(java.util.LinkedList trie,
                                          int level,
                                          boolean atStart)
Formats AIML from a trie into a nicely indented multi-line string.

Parameters:
trie - the trie containing the AIML
level - the level (for indenting)
atStart - whether the whole XML string is at its beginning

tab

public static java.lang.String tab(int level)
Returns a tab of the specified length.

Parameters:
length - the length of the tab

wordSplit

public static java.util.ArrayList wordSplit(java.lang.String input)
Splits an input into words, breaking at spaces. This method is obviously limited in that it is not aware of other word boundaries.

Parameters:
input - the input to split
Returns:
the input split into sentences

getImplementorsOf

public static java.lang.Class[] getImplementorsOf(java.lang.String interfaceName,
                                                  boolean showFound)

Scans the classpath for classes that implement a given interface.

Note that this does not make sense for all class loaders. In cases where it doesn't make sense, the return value will be null.

This can also be a very slow method if the classpath is long.

Parameters:
interfaceName - the fully-qualified name of the interface whose implementations are wanted
showFound - whether to output a devinfo message when implementations are found
Returns:
classes that implement this interface

glob

public static java.lang.String[] glob(java.lang.String path,
                                      java.lang.String workingDirectory)
                               throws java.io.FileNotFoundException

Expands a localized file name that may contain wildcards to an array of file names without wildcards. All file separators in the file name must preceed any wildcard.

Adapted, with gratitude, from the JMK project. (Under the GNU LGPL)

Parameters:
path - localized file name that may contain wildcards
workingDirectory - the path to which relative paths should be considered relative
Returns:
array of file names without wildcards
Throws:
java.io.FileNotFoundException - if wild card is misused
See Also:
JMK

checkOrCreate

public static void checkOrCreate(java.lang.String path,
                                 java.lang.String description)
Checks whether a file given by a path exists, and if not, creates it, along with any necessary subdirectories.

Parameters:
path - denoting the file to create
description - describes what the file is for, for trace messages. Should fit into a sentence like, "created new description". May be null (which will result in less informative messages).

getFileContents

public static java.lang.String getFileContents(java.lang.String path)
Returns the entire contents of a file as a String.

Parameters:
path - the path to the file (local file or URL)
Returns:
the entire contents of a file as a String