com.arsdigita.acs
Class Utilities

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

public class Utilities
extends java.lang.Object

Utility procedures for ACS.


Field Summary
static java.lang.String cvsId
           
 
Constructor Summary
Utilities()
           
 
Method Summary
static java.lang.String commifyNumber(long num)
          putting commas into numbers (thank you, Michael Bryzek)
static java.lang.String commifyNumber(java.lang.String num)
          putting commas into numbers (thank you, Michael Bryzek)
static void copyFile(java.io.File src, java.io.File dest)
           
static java.lang.String decorateTop(java.lang.String simpleHeadline, java.lang.String potentialDecoration)
          Use this for pages that might or might not have an im\age defined in ad.ini; if the second argument isn't the empty string, this will make a one-row table for the top of the page.
static boolean emailValidP(java.lang.String email)
           
static boolean emptyString(java.lang.Object o)
           
static boolean emptyString(java.lang.String s)
           
static java.lang.String exportFormValue(java.lang.String val)
          Returns "value="form_value"", URL-encoded appropriately for inclusion in an HTML form.
static java.lang.String exportFormVar(java.lang.String key, java.lang.Object value)
          Exports a single variable for use in a form, as input type=hidden.
static java.lang.String exportFormVars(AdSet vars)
          Exports variables for use in a form, as input type=hidden.
static java.lang.String exportFormVars(AdSet vars, java.util.List inclusionList, java.lang.String exclusionList)
          Exports variables for use in a form, as input type=hidden.
static java.lang.String exportFormVars(AdSet vars, java.lang.String inclusionList, java.lang.String exclusionList)
          Exports variables for use in a form, as input type=hidden.
static java.lang.String exportFormVars(DataSource ds, java.lang.String vars)
          Exports variables for use in a form, as input type=hidden
static java.lang.String exportFormVars(javax.servlet.http.HttpServletRequest req, java.lang.String vars)
           
static java.lang.String exportFormVars(java.sql.ResultSet rs, java.lang.String vars)
          Exports variables for use in a form, as hidden input fields.
static java.lang.String exportURLVar(java.lang.String key, java.lang.Object value)
          Exports a single variable for use in a URL
static java.lang.String exportURLVars(AdSet vars)
          Exports variables for use in a URL, as name=value&name=value...
static java.lang.String exportURLVars(AdSet vars, java.util.List inclusionList, java.lang.String exclusionList)
          Exports variables for use in a URL, as name=value&name=value...
static java.lang.String exportURLVars(AdSet vars, java.lang.String inclusionList, java.lang.String exclusionList)
          Exports variables for use in a URL, as name=value&name=value...
static java.lang.String exportURLVars(DataSource ds, java.lang.String vars)
          Exports variables for use in a URL, as name=value&name=value...,
static java.lang.String exportURLVars(javax.servlet.http.HttpServletRequest req, java.lang.String vars)
          Exports variables for use in a URL, as name=value&name=value...,
static java.lang.String exportURLVars(java.sql.ResultSet rs, java.lang.String vars)
          Exports variables for use in a URL, as name=value&name=value...
static java.lang.String exportURLVars(java.lang.String key, java.lang.Object[] values)
          Exports a "multiple" variable, where the same name is used for multiple values.
static java.lang.String exportVars(DataSource ds, java.lang.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 java.lang.String exportVars(javax.servlet.http.HttpServletRequest req, java.lang.String vars, boolean form)
          Deprecated. use ACSForm.exportXXX routines instead as URL vars
static java.lang.String fileToString(java.io.File f)
          Return a file's contents as a string.
static java.lang.String fileToString(java.io.File f, java.lang.String exc_message)
          Return a file's contents as a string.
static java.util.Vector findAllFiles(java.io.File path)
           
static java.util.Vector findAllFiles(java.io.File path, boolean include_dirs, boolean include_backup, int max_depth)
          Returns a list of Files under path in the directory tree (descending the tree to a depth of up to max_depth).
static java.lang.String generateRandomString()
           
static java.lang.String generateRandomString(int u)
           
static java.lang.String getCookieValue(javax.servlet.http.HttpServletRequest request, java.lang.String withName)
          Returns a cookie value as a String, given a cookie name.
static java.lang.String getFirstPackageURL(java.lang.String package_key, ACSRequest req)
          Same as above, but uses ACSRequest instead of HttpServletRequest.
static java.lang.String getFirstPackageURL(java.lang.String package_key, javax.servlet.http.HttpServletRequest req)
          Returns the URL to the first package named package_key.
static float getRandomFloat()
          Gets a random number between 0.0 and 1.0.
static java.lang.String htmlToText(java.lang.String s)
          Removes tags and substitutes P tags with newlines.
static boolean ignoreFile(java.io.File file)
          Return true if path should, in general, be ignored for package operations.
static boolean ignoreFile(java.io.File file, boolean includeBackup)
          Return true if path should, in general, be ignored for package operations.
static boolean isBackupFile(java.io.File file)
          Returns 1 if $path is a backup file, or 0 if not.
static boolean isFalse(java.lang.String s)
          False if argument equals "f", "0", "n", "false", "no", or "off".
static boolean isNumber(java.lang.String s)
          Return true if the input string looks like a number.
static boolean isTrue(java.lang.String s)
          ! isFalse.
static java.lang.String join(java.lang.Object[] o, java.lang.String s)
          Converts an array to a String joined on another String.
static java.lang.String join(java.util.Vector v, java.lang.String s)
          Converts a java.util.Vector to a String joined on another String.
static boolean linkResponding(java.lang.String url)
          Return true/false about whether a url is responding.
static boolean linkResponding(java.lang.String url, int[] bad_codes)
          Return true/false about whether a url is responding.
static java.lang.String makeRelativePath(java.io.File path)
           
static java.lang.String makeRelativePath(java.lang.String path)
           
static java.lang.String maybeHtmlToText(java.lang.String s, java.lang.String html_p)
           
static AdSet parseQuery(java.lang.String query)
          Equivalent to ns_parsequery Parses given query string and returns an AdSet of the key value pairs.
static java.lang.String quoteHtml(java.lang.String s)
          Quotes "special" characters in HTML text (ampersand, angle brackets, quote).
static void removeDups(java.util.Vector v)
          /** Removes duplicate entries from a Vector.
static char secRandomChar()
          Generates a single random character.
static void sendRedirect(javax.servlet.http.HttpServletResponse resp, java.lang.String url)
          Redirects to the response, setting the status code and location headers directly.
static void sendRedirect(javax.servlet.http.HttpServletResponse resp, Url url)
           
static boolean stringGlobMatch(java.lang.String pattern, java.lang.String s)
          This is a utility proc for performing a TCL-style glob (i.e.
static java.lang.String textToHtml(java.lang.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 java.lang.String toPrettyDate(java.util.Date date)
          Converts java.util.Date to a "pretty" date
static java.lang.String toPrettyDate(java.lang.String ansiDate)
          Converts an ANSI yyyy-mm-dd date to a "pretty" date
static java.lang.String URLDecode(java.lang.String str)
          This method decodes the given urlencoded string.
static java.lang.String URLEncode(java.lang.String s)
          URL-encodes a string.
static boolean URLValidP(java.lang.String queryURL)
           
static void writeStringToResponse(javax.servlet.http.HttpServletResponse resp, java.lang.String s)
          Write out a string as a full http response.
static void writeStringToResponse(javax.servlet.http.HttpServletResponse resp, java.lang.String s, int status, java.lang.String content_type)
          Write out a string as a full http response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cvsId

public static final java.lang.String cvsId
Constructor Detail

Utilities

public Utilities()
Method Detail

quoteHtml

public static final java.lang.String quoteHtml(java.lang.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 java.lang.String textToHtml(java.lang.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 java.lang.String htmlToText(java.lang.String s)
Removes tags and substitutes P tags with newlines.

maybeHtmlToText

public static final java.lang.String maybeHtmlToText(java.lang.String s,
                                                     java.lang.String html_p)

exportFormVars

public static final java.lang.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 java.lang.String exportFormVars(AdSet vars,
                                                    java.lang.String inclusionList,
                                                    java.lang.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)
inclusionList - A space-separated list of fields in the AdSet to include Note that the exclusionList takes precedence over the inclusionList. Leave null to indicate "all that are not excluded."
Returns:
An HTML string for inclusion within HTML <FORM> tags.

exportFormVars

public static final java.lang.String exportFormVars(AdSet vars,
                                                    java.util.List inclusionList,
                                                    java.lang.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)
inclusionList - A List containing the only keys to include. Note that the exclusionList takes precedence over the inclusionList. Leave null to include all that are not excluded.
Returns:
An HTML string for inclusion within HTML <FORM> tags.

exportURLVars

public static final java.lang.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 java.lang.String exportURLVars(java.lang.String key,
                                                   java.lang.Object[] values)
Exports a "multiple" variable, where the same name is used for multiple values.

exportURLVars

public static final java.lang.String exportURLVars(AdSet vars,
                                                   java.lang.String inclusionList,
                                                   java.lang.String exclusionList)
Exports variables for use in a URL, as name=value&name=value...
Parameters:
vars - An AdSet of variable names and values.
inclusionList - A space-separated list of fields in the AdSet to include Note that the exclusionList takes precedence over the inclusionList. Leave null to indicate "all that are not excluded."
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 java.lang.String exportURLVars(AdSet vars,
                                                   java.util.List inclusionList,
                                                   java.lang.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)
inclusionList - A List containing the only keys to include. Note that the exclusionList takes precedence over the inclusionList. Leave null to include all that are not excluded.
Returns:
An HTML string usable as the query string after a URL. (Does NOT include the initial "?" character.)

exportURLVars

public static final java.lang.String exportURLVars(java.sql.ResultSet rs,
                                                   java.lang.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 java.lang.String exportFormVars(java.sql.ResultSet rs,
                                                    java.lang.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 java.lang.String exportVars(DataSource ds,
                                                java.lang.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 java.lang.String exportFormVar(java.lang.String key,
                                                   java.lang.Object value)
Exports a single variable for use in a form, as input type=hidden.
Parameters:
key - Variable name.
value - Variable value.

exportURLVar

public static final java.lang.String exportURLVar(java.lang.String key,
                                                  java.lang.Object value)
Exports a single variable for use in a URL
Parameters:
key - Variable name.
value - Variable value.

exportURLVars

public static final java.lang.String exportURLVars(DataSource ds,
                                                   java.lang.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 java.lang.String exportFormVars(DataSource ds,
                                                    java.lang.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 java.lang.String exportURLVars(javax.servlet.http.HttpServletRequest req,
                                                   java.lang.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 java.lang.String exportFormVars(javax.servlet.http.HttpServletRequest req,
                                                    java.lang.String vars)

exportVars

public static final java.lang.String exportVars(javax.servlet.http.HttpServletRequest req,
                                                java.lang.String vars,
                                                boolean form)
Deprecated. use ACSForm.exportXXX routines instead as URL vars

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

URLEncode

public static final java.lang.String URLEncode(java.lang.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 java.lang.String exportFormValue(java.lang.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 java.lang.String URLDecode(java.lang.String str)
                                        throws java.lang.NumberFormatException,
                                               java.lang.StringIndexOutOfBoundsException
This method decodes the given urlencoded string.
Parameters:
str - the url-encoded string
Returns:
the decoded string
Throws:
java.lang.IllegalArgumentException - If a '%' is not followed by a valid 2-digit hex number.

getCookieValue

public static final java.lang.String getCookieValue(javax.servlet.http.HttpServletRequest request,
                                                    java.lang.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()

sendRedirect

public static void sendRedirect(javax.servlet.http.HttpServletResponse resp,
                                java.lang.String url)
                         throws java.io.IOException
Redirects to the response, setting the status code and location headers directly. response.sendRedirect() is broken with respect to cookies.

sendRedirect

public static void sendRedirect(javax.servlet.http.HttpServletResponse resp,
                                Url url)
                         throws java.io.IOException

join

public static final java.lang.String join(java.util.Vector v,
                                          java.lang.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 java.lang.String join(java.lang.Object[] o,
                                          java.lang.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.

getRandomFloat

public static final float getRandomFloat()
Gets a random number between 0.0 and 1.0. This is equivalent to calling ns_rand with no arguments.

emptyString

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

emptyString

public static final boolean emptyString(java.lang.Object o)
Parameters:
o - A string to test
Returns:
true if o is null or empty; otherwise false

removeDups

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

copyFile

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

emailValidP

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

URLValidP

public static final boolean URLValidP(java.lang.String queryURL)

generateRandomString

public static java.lang.String generateRandomString()

generateRandomString

public static java.lang.String generateRandomString(int u)

isNumber

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

toPrettyDate

public static final java.lang.String toPrettyDate(java.util.Date date)
Converts java.util.Date to a "pretty" date

toPrettyDate

public static final java.lang.String toPrettyDate(java.lang.String ansiDate)
Converts an ANSI yyyy-mm-dd date to a "pretty" date

stringGlobMatch

public static final boolean stringGlobMatch(java.lang.String pattern,
                                            java.lang.String s)
This is a utility proc for performing a TCL-style glob (i.e. the "string match" command) via a mostly equivalent regexp command. TCL glob syntax specifies match anything on * and match nothing or one character on ?, unless they are preceeded with a \. There are some other differences between string match and regexps, but this should be enough to account for the most common ones.

fileToString

public static final java.lang.String fileToString(java.io.File f)
                                           throws ACSException
Return a file's contents as a string.

fileToString

public static final java.lang.String fileToString(java.io.File f,
                                                  java.lang.String exc_message)
                                           throws ACSException
Return a file's contents as a string.

writeStringToResponse

public static final void writeStringToResponse(javax.servlet.http.HttpServletResponse resp,
                                               java.lang.String s)
                                        throws ACSException
Write out a string as a full http response.

writeStringToResponse

public static final void writeStringToResponse(javax.servlet.http.HttpServletResponse resp,
                                               java.lang.String s,
                                               int status,
                                               java.lang.String content_type)
                                        throws ACSException
Write out a string as a full http response.

linkResponding

public static final boolean linkResponding(java.lang.String url)
Return true/false about whether a url is responding.

linkResponding

public static final boolean linkResponding(java.lang.String url,
                                           int[] bad_codes)
Return true/false about whether a url is responding.

decorateTop

public static final java.lang.String decorateTop(java.lang.String simpleHeadline,
                                                 java.lang.String potentialDecoration)
Use this for pages that might or might not have an im\age defined in ad.ini; if the second argument isn't the empty string, this will make a one-row table for the top of the page. This should probably be changed to a template at some point.
Parameters:
simple_headline - Headline.
potential_decoration - Decoration string (usually an image reference).
Returns:
Combination of headline and decoration string in a table, or just headline if decoration string was empty.

parseQuery

public static final AdSet parseQuery(java.lang.String query)
Equivalent to ns_parsequery Parses given query string and returns an AdSet of the key value pairs.
Parameters:
query - Query string to parse (a=b&c=d).
Returns:
AdSet (a:b, c:d).

commifyNumber

public static java.lang.String commifyNumber(long num)
putting commas into numbers (thank you, Michael Bryzek)
Returns:
the number with commas inserted where appropriate. Number can be positive or negative and can have a decimal point. e.g. -1465.98 => -1,465.98

commifyNumber

public static java.lang.String commifyNumber(java.lang.String num)
putting commas into numbers (thank you, Michael Bryzek)
Returns:
the number with commas inserted where appropriate. Number can be positive or negative and can have a decimal point. e.g. -1465.98 => -1,465.98

isTrue

public static boolean isTrue(java.lang.String s)
! isFalse.

isFalse

public static boolean isFalse(java.lang.String s)
False if argument equals "f", "0", "n", "false", "no", or "off".

ignoreFile

public static boolean ignoreFile(java.io.File file)
Return true if path should, in general, be ignored for package operations. Currently, a file is ignored if it is a backup file, a CVS directory, or in a directory named "dist" (distribution).

ignoreFile

public static boolean ignoreFile(java.io.File file,
                                 boolean includeBackup)
Return true if path should, in general, be ignored for package operations. Currently, a file is ignored if it is a backup file or a CVS directory.

isBackupFile

public static boolean isBackupFile(java.io.File file)
Returns 1 if $path is a backup file, or 0 if not. We consider it a backup file if any of the following apply:

findAllFiles

public static java.util.Vector findAllFiles(java.io.File path)

findAllFiles

public static java.util.Vector findAllFiles(java.io.File path,
                                            boolean include_dirs,
                                            boolean include_backup,
                                            int max_depth)
Returns a list of Files under path in the directory tree (descending the tree to a depth of up to max_depth). Includes backup files only if include_backup is true; includes directories in the returned list only if include_dirs is true. Ignores all files for which returns true. Clients should not depend on the order of files returned.

makeRelativePath

public static java.lang.String makeRelativePath(java.lang.String path)
                                         throws ACSException

makeRelativePath

public static java.lang.String makeRelativePath(java.io.File path)
                                         throws ACSException

getFirstPackageURL

public static java.lang.String getFirstPackageURL(java.lang.String package_key,
                                                  javax.servlet.http.HttpServletRequest req)
                                           throws java.sql.SQLException
Returns the URL to the first package named package_key. useful for finding acs-admin and acs-subsite in particular.

getFirstPackageURL

public static java.lang.String getFirstPackageURL(java.lang.String package_key,
                                                  ACSRequest req)
                                           throws java.sql.SQLException
Same as above, but uses ACSRequest instead of HttpServletRequest.