com.arsdigita.acs
Class Utilities

java.lang.Object
  |
  +--com.arsdigita.acs.Utilities

public class Utilities
extends Object

Utility procedures for ACS.


Constructor Summary
Utilities()
           
 
Method Summary
static void copyFile(File src, File dest)
           
static boolean emailValidP(String email)
           
static boolean emptyString(String s)
           
static String exportFormValue(String val)
          Returns "value="form_value"", URL-encoded appropriately for inclusion in an HTML form.
static String exportFormVar(String key, String value)
          Exports a single variable for use in a form, as input type=hidden.
static String exportFormVars(AdSet vars)
          Exports variables for use in a form, as input type=hidden.
static String exportFormVars(AdSet vars, String exclusionList)
          Exports variables for use in a form, as input type=hidden.
static String exportFormVars(DataSource ds, String vars)
          Exports variables for use in a form, as input type=hidden
static String exportFormVars(HttpServletRequest req, String vars)
           
static String exportFormVars(ResultSet rs, String vars)
          Exports variables for use in a form, as hidden input fields.
static String exportURLVars(AdSet vars)
          Exports variables for use in a URL, as name=value&name=value...
static String exportURLVars(AdSet vars, String exclusionList)
          Exports variables for use in a URL, as name=value&name=value...
static String exportURLVars(DataSource ds, String vars)
          Exports variables for use in a URL, as name=value&name=value...,
static String exportURLVars(HttpServletRequest req, String vars)
          Exports variables for use in a URL, as name=value&name=value...,
static String exportURLVars(ResultSet rs, String vars)
          Exports variables for use in a URL, as name=value&name=value...
static String exportVars(DataSource ds, String vars, boolean form)
          Exports variables for use in a URL, as name=value&name=value..., or in a form (input type=hidden name=...
static String exportVars(HttpServletRequest req, String vars, boolean form)
          Exports variables for use in a URL, as name=value&name=value..., or in a form (input type=hidden name=...
static String generateRandomString()
           
static String generateRandomString(int u)
           
static String getCookieValue(HttpServletRequest request, String withName)
          Returns a cookie value as a String, given a cookie name.
static String htmlToText(String s)
          Removes tags and substitutes P tags with newlines.
static boolean isNumber(String s)
          Return true if the input string looks like a number.
static String join(Object[] o, String s)
          Converts an array to a String joined on another String.
static String join(Vector v, String s)
          Converts a java.util.Vector to a String joined on another String.
static String maybeHtmlToText(String s, String html_p)
           
static String prettyBoolean(Object in)
           
static String quoteHtml(String s)
          Quotes "special" characters in HTML text (ampersand, angle brackets, quote).
static void removeDups(Vector v)
          Removes duplicate entries from a Vector.
static String secDigestString(String str)
          Digests a string into eight ASCII characters (horribly insecurely).
static char secRandomChar()
          Generates a single random character.
static String secRandomToken()
          Generates a random token, using the TokenLength as the token length.
static void sendMail(String to, String from, String subject, String body)
          Sends a single e-mail message; analogous to ns_sendmail.
static void sendRedirect(HttpServletResponse response, String location)
          Analogous to HttpServletResponse.sendRedirect(), but necessary for redirecting if there have been any headers set (such as cookie headers), since (in at least the Tomcat implementation) sendRedirect() ignores any header objects attached to the response.
static String textToHtml(String s)
          Takes a plaintext string, and returns an HTML string that, when rendered by a web browser, will appear as the original input string
static String URLDecode(String str)
          This method decodes the given urlencoded string.
static String URLEncode(String s)
          URL-encodes a string.
static boolean URLValidP(String queryURL)
           
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utilities

public Utilities()
Method Detail

quoteHtml

public static final String quoteHtml(String s)
Quotes "special" characters in HTML text (ampersand, angle brackets, quote).
Parameters:
s - The string to quote
Returns:
The quoted string

textToHtml

public static final String textToHtml(String s)
Takes a plaintext string, and returns an HTML string that, when rendered by a web browser, will appear as the original input string
Parameters:
s - The input plaintext string
Returns:
A HTML string with blank lines coverted to

and ampersands/angle brackets escaped.

htmlToText

public static final String htmlToText(String s)
Removes tags and substitutes P tags with newlines.

maybeHtmlToText

public static final String maybeHtmlToText(String s,
                                           String html_p)

exportFormVars

public static final String exportFormVars(AdSet vars)
Exports variables for use in a form, as input type=hidden.
Parameters:
vars - An AdSet of variable names and values.
Returns:
An HTML string for inclusion within HTML <FORM> tags.

exportFormVars

public static final String exportFormVars(AdSet vars,
                                          String exclusionList)
Exports variables for use in a form, as input type=hidden.
Parameters:
vars - An AdSet of variable names and values.
exclusionList - A space-separated list of fields in the AdSet to ignore (or null to export all of them)
Returns:
An HTML string for inclusion within HTML <FORM> tags.

exportURLVars

public static final String exportURLVars(AdSet vars)
Exports variables for use in a URL, as name=value&name=value...
Parameters:
vars - An AdSet of variable names and values.
Returns:
An HTML string usable as the query string after a URL. (Does NOT include the initial "?" character.)

exportURLVars

public static final String exportURLVars(AdSet vars,
                                         String exclusionList)
Exports variables for use in a URL, as name=value&name=value...
Parameters:
vars - An AdSet of variable names and values.
exclusionList - A space-separated list of fields in the AdSet to ignore (or null to export all of them)
Returns:
An HTML string usable as the query string after a URL. (Does NOT include the initial "?" character.)

exportURLVars

public static final String exportURLVars(ResultSet rs,
                                         String vars)
Exports variables for use in a URL, as name=value&name=value...
Parameters:
rs - A ResultSet.
vars - A space-separated list of fields from the result set to export. If null, all fields are exported.

exportFormVars

public static final String exportFormVars(ResultSet rs,
                                          String vars)
Exports variables for use in a form, as hidden input fields.
Parameters:
rs - A ResultSet.
vars - A space-separated list of fields from the result set to export. If null, all fields are exported.

exportVars

public static final String exportVars(DataSource ds,
                                      String vars,
                                      boolean form)
Exports variables for use in a URL, as name=value&name=value..., or in a form (input type=hidden name=... value=...)
Parameters:
ds - A DataSource.
vars - A space-separated list of fields from the result set to export. If null, all fields are exported.
form - if true, export as form vars, else export as URL vars

exportFormVar

public static final String exportFormVar(String key,
                                         String value)
Exports a single variable for use in a form, as input type=hidden.
Parameters:
key - Variable name.
value - Variable value.

exportVars

public static final String exportVars(HttpServletRequest req,
                                      String vars,
                                      boolean form)
Exports variables for use in a URL, as name=value&name=value..., or in a form (input type=hidden name=... value=...) from a servlet requests. Pulls values from the attributes first, then from the form variables
Parameters:
req - A servlet request
vars - A space-separated list of fields from the result set to export. If null, all form variables are exported.
form - if true, export as form vars, else export as URL vars

exportURLVars

public static final String exportURLVars(DataSource ds,
                                         String vars)
Exports variables for use in a URL, as name=value&name=value...,
Parameters:
ds - A DataSource.
vars - A space-separated list of fields from the result set to export. If null, all fields are exported.

exportFormVars

public static final String exportFormVars(DataSource ds,
                                          String vars)
Exports variables for use in a form, as input type=hidden
Parameters:
ds - A DataSource.
vars - A space-separated list of fields from the result set to export. If null, all fields are exported.

exportURLVars

public static final String exportURLVars(HttpServletRequest req,
                                         String vars)
Exports variables for use in a URL, as name=value&name=value...,
Parameters:
req - A servlet request
vars - A space-separated list of fields from the result set to export. If null, all fields are exported.

exportFormVars

public static final String exportFormVars(HttpServletRequest req,
                                          String vars)

URLEncode

public static final String URLEncode(String s)
URL-encodes a string. NOTE: is there a routine for this in javax.servlet.http?
Parameters:
s - The string to encode
Returns:
The encoded string.

exportFormValue

public static final String exportFormValue(String val)
Returns "value="form_value"", URL-encoded appropriately for inclusion in an HTML form.
Parameters:
val - The value to encode.

URLDecode

public static final String URLDecode(String str)
                              throws NumberFormatException,
                                     StringIndexOutOfBoundsException
This method decodes the given urlencoded string.
Parameters:
str - the url-encoded string
Returns:
the decoded string
Throws:
IllegalArgumentException - If a '%' is not followed by a valid 2-digit hex number.

getCookieValue

public static final String getCookieValue(HttpServletRequest request,
                                          String withName)
Returns a cookie value as a String, given a cookie name.
Parameters:
request - The servlet request
withName - The cookie name
Returns:
The cookie value
See Also:
Cookie, HttpServletRequest.getCookies()

join

public static final String join(Vector v,
                                String s)
Converts a java.util.Vector to a String joined on another String. Analogous to the join procedure in Tcl or Perl.
Parameters:
v - The Vector to join.
s - The string inserted between each element of v
Returns:
The list of elements in v, separated by s

join

public static final String join(Object[] o,
                                String s)
Converts an array to a String joined on another String. Analogous to the join procedure in Tcl or Perl.
Parameters:
v - The Vector to join.
s - The string inserted between each element of v
Returns:
The list of elements in v, separated by s

secRandomChar

public static final char secRandomChar()
Generates a single random character.
Returns:
A random alphanumeric character, a period, or slash.

secDigestString

public static final String secDigestString(String str)
Digests a string into eight ASCII characters (horribly insecurely).
Parameters:
str - The string input
Returns:
The digested string

secRandomToken

public static final String secRandomToken()
Generates a random token, using the TokenLength as the token length. Reads TokenLength and TokenSecret parameters from Parameters, in no module.
Returns:
The random token

emptyString

public static final boolean emptyString(String s)
Parameters:
s - A string to test
Returns:
true if s is null or empty; otherwise false

sendRedirect

public static final void sendRedirect(HttpServletResponse response,
                                      String location)
                               throws IOException
Analogous to HttpServletResponse.sendRedirect(), but necessary for redirecting if there have been any headers set (such as cookie headers), since (in at least the Tomcat implementation) sendRedirect() ignores any header objects attached to the response.
Parameters:
response - Response object to send redirect with.
location - URL to redirect to.

removeDups

public static final void removeDups(Vector v)
Removes duplicate entries from a Vector.
Parameters:
v - The Vector to modify

prettyBoolean

public static final String prettyBoolean(Object in)

copyFile

public static final void copyFile(File src,
                                  File dest)
                           throws IOException

sendMail

public static final void sendMail(String to,
                                  String from,
                                  String subject,
                                  String body)
                           throws Exception
Sends a single e-mail message; analogous to ns_sendmail.

emailValidP

public static final boolean emailValidP(String email)
Returns:
true if the given string looks like a valid email address.

URLValidP

public static final boolean URLValidP(String queryURL)

generateRandomString

public static String generateRandomString()

generateRandomString

public static String generateRandomString(int u)

isNumber

public static final boolean isNumber(String s)
Return true if the input string looks like a number.