org.sakaiproject.util
Class Validator

java.lang.Object
  extended byorg.sakaiproject.util.Validator

public class Validator
extends Object

Validator is utility class that helps to validate stuff for CHEF.

It is also a Turbine PULL tool so it can be used in a .vm file

Version:
$Revision: 5823 $
Author:
University of Michigan, CHEF Software Development Team

Field Summary
protected static String BLANK_MSG
           
protected static String ESCAPE_CHARS_IN_RESOURCE_ID
          These characters are allowed; but if escapeResourceName() is called, they are escaped (actually, removed) Certain characters cause problems with filenames in certain OSes - so get rid of these characters in filenames
protected static String ESCAPE_URL
          These characters are escaped when making a URL
protected static String ESCAPE_URL_SPECIAL
          These can't be encoded in URLs safely even using %nn notation, so encode them using our own custom URL encoding, which the ParameterParser decodes
protected static String INVALID_CHARS_IN_RESOURCE_ID
          These characters are not allowed in a resource id
protected static String INVALID_CHARS_IN_USER_ID
          These characters are not allowed in a user id
protected static String INVALID_CHARS_IN_ZIP_ENTRY
           
protected static String INVALID_MSG
           
protected static String MAP_TO_A
           
protected static String MAP_TO_B
           
protected static String MAP_TO_C
           
protected static String MAP_TO_E
           
protected static String MAP_TO_I
           
protected static String MAP_TO_L
           
protected static String MAP_TO_N
           
protected static String MAP_TO_O
           
protected static String MAP_TO_U
           
protected static String MAP_TO_X
           
protected static String MAP_TO_Y
           
protected static String VALID_EMAIL
          Valid special email local id characters (- those that are invalid resource ids)
 
Constructor Summary
Validator()
           
 
Method Summary
static boolean checkDate(int day, int month, int year)
          Validate whether the date input is valid
static boolean checkEmailLocal(String id)
          Is this a valid local part of an email id?
static void checkResourceId(String id)
          Check for a valid resource id.
static void checkResourceRef(String ref)
          Check for a valid resource reference.
static void checkUserId(String id)
          Check for a valid user id.
static String cleanInput(String value)
          Clean the user input string of strange newlines, etc.
static String escapeHtml(String value)
          Escape a plaintext string so that it can be output as part of an HTML document.
static String escapeHtmlFixCopyright(String value)
          escapeHtml(), but also fix the case where we start with © and treat it as copyright (c) Note: ResourcesAction used to (before 1.1.05) place this as the copyright symbol.
static String escapeHtmlFormattedText(String value)
          Escape HTML-formatted text in preparation to include it in an HTML document.
static String escapeHtmlFormattedTextarea(String value)
          Escapes the given HTML-formatted text for editing within the WYSIWYG editor.
static String escapeHtmlFormattedTextSupressNewlines(String value)
          Escape HTML-formatted text in preparation to include it in an HTML document, except that HTML line breaks ("<br />") will be supressed (removed).
static String escapeHtmlSupressNewlines(String value)
          Escape a plaintext string so that it can be output as part of an HTML document, except that newlines are NOT escaped and therefore are treated as whitespace instead of displaying as line-breaks.
static String escapeHtmlTextarea(String value)
          Escape plaintext for display inside a plain textarea.
static String escapeJavascript(String value)
          Return a string based on value that is safe to place into a javascript / html identifier: anything not alphanumeric change to 'x'.
static String escapeJsQuoted(String value)
          Return a string based on value that is safe to place into a javascript value that is in single quiotes.
static String escapeQuestionMark(String id)
          Return a string based on id that is fully escaped the question mark.
static String escapeResourceName(String id)
          Return a string based on id that is fully escaped using Resource name validity rules.
static String escapeSql(String value)
          Return a string based on value that is safe to place into a sql statement: sql statements use the single quote, and this must be doubled as an escape.
static String escapeUrl(String id)
          Return a string based on id that is fully escaped using URL rules, using a UTF-8 underlying encoding.
static String escapeZipEntry(String id)
          Return a string based on id that is fully escaped to create a zip entry
static String getFileExtension(String fullName)
          Isolate and return just the file extension part of a full drive and path file name.
static String getFileName(String fullName)
          Isolate and return just the file name part of a full drive and path file name.
static String getFileSizeWithDividor(String size)
          Put the dividor (comma) inside the size string, for example, 1,003 for 1003
static String getResourceTarget(ResourceProperties properties)
          Determine whether a file resource should be opened in the current window or a new window.
static boolean letBrowserInline(String type)
          Is this a mime type that the browser can handle inline, in a browser window?
static String limit(String value, int length)
          Limit the string to a certain number of characters, adding "..." if it was truncated
static String limit(String value, Integer length)
          Limit the string to a certain number of characters, adding "..." if it was truncated.
static String limitFormattedText(String value, int length)
          Limit the formatted to a certain number of DISPLAYED characters, adding "..." if it was truncated.
static String stripAllNewlines(String value)
          Clean the string parameter of all newlines (replace with space character) and trim leading and trailing spaces
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INVALID_CHARS_IN_RESOURCE_ID

protected static final String INVALID_CHARS_IN_RESOURCE_ID
These characters are not allowed in a resource id

See Also:
Constant Field Values

INVALID_CHARS_IN_USER_ID

protected static final String INVALID_CHARS_IN_USER_ID
These characters are not allowed in a user id

See Also:
Constant Field Values

MAP_TO_A

protected static final String MAP_TO_A
See Also:
Constant Field Values

MAP_TO_B

protected static final String MAP_TO_B
See Also:
Constant Field Values

MAP_TO_C

protected static final String MAP_TO_C
See Also:
Constant Field Values

MAP_TO_E

protected static final String MAP_TO_E
See Also:
Constant Field Values

MAP_TO_I

protected static final String MAP_TO_I
See Also:
Constant Field Values

MAP_TO_L

protected static final String MAP_TO_L
See Also:
Constant Field Values

MAP_TO_N

protected static final String MAP_TO_N
See Also:
Constant Field Values

MAP_TO_O

protected static final String MAP_TO_O
See Also:
Constant Field Values

MAP_TO_U

protected static final String MAP_TO_U
See Also:
Constant Field Values

MAP_TO_Y

protected static final String MAP_TO_Y
See Also:
Constant Field Values

MAP_TO_X

protected static final String MAP_TO_X
See Also:
Constant Field Values

ESCAPE_CHARS_IN_RESOURCE_ID

protected static final String ESCAPE_CHARS_IN_RESOURCE_ID
These characters are allowed; but if escapeResourceName() is called, they are escaped (actually, removed) Certain characters cause problems with filenames in certain OSes - so get rid of these characters in filenames

See Also:
Constant Field Values

INVALID_CHARS_IN_ZIP_ENTRY

protected static final String INVALID_CHARS_IN_ZIP_ENTRY
See Also:
Constant Field Values

INVALID_MSG

protected static final String INVALID_MSG
See Also:
Constant Field Values

BLANK_MSG

protected static final String BLANK_MSG
See Also:
Constant Field Values

ESCAPE_URL

protected static final String ESCAPE_URL
These characters are escaped when making a URL

See Also:
Constant Field Values

ESCAPE_URL_SPECIAL

protected static final String ESCAPE_URL_SPECIAL
These can't be encoded in URLs safely even using %nn notation, so encode them using our own custom URL encoding, which the ParameterParser decodes

See Also:
Constant Field Values

VALID_EMAIL

protected static final String VALID_EMAIL
Valid special email local id characters (- those that are invalid resource ids)

See Also:
Constant Field Values
Constructor Detail

Validator

public Validator()
Method Detail

escapeHtml

public static String escapeHtml(String value)
Escape a plaintext string so that it can be output as part of an HTML document. Amperstand, greater-than, less-than, newlines, etc, will be escaped so that they display (instead of being interpreted as formatting).

Parameters:
value - The string to escape.
Returns:
value fully escaped for HTML.

escapeHtmlSupressNewlines

public static String escapeHtmlSupressNewlines(String value)
Escape a plaintext string so that it can be output as part of an HTML document, except that newlines are NOT escaped and therefore are treated as whitespace instead of displaying as line-breaks. Amperstand, greater-than, less-than, etc, will be escaped so that they display (instead of being interpreted as formatting).

Parameters:
value - The string to escape.
Returns:
value fully escaped for HTML.

escapeHtmlTextarea

public static String escapeHtmlTextarea(String value)
Escape plaintext for display inside a plain textarea.


escapeHtmlFormattedText

public static String escapeHtmlFormattedText(String value)
Escape HTML-formatted text in preparation to include it in an HTML document.


escapeHtmlFormattedTextSupressNewlines

public static String escapeHtmlFormattedTextSupressNewlines(String value)
Escape HTML-formatted text in preparation to include it in an HTML document, except that HTML line breaks ("<br />") will be supressed (removed).


escapeHtmlFormattedTextarea

public static String escapeHtmlFormattedTextarea(String value)
Escapes the given HTML-formatted text for editing within the WYSIWYG editor. All HTML meta-characters in the string (such as amperstand, less-than, etc), will be escaped.

Parameters:
value - The formatted text to escape
Returns:
The string to use as the value of the formatted textarea widget

escapeHtmlFixCopyright

public static String escapeHtmlFixCopyright(String value)
escapeHtml(), but also fix the case where we start with © and treat it as copyright (c) Note: ResourcesAction used to (before 1.1.05) place this as the copyright symbol. -ggolden


escapeUrl

public static String escapeUrl(String id)
Return a string based on id that is fully escaped using URL rules, using a UTF-8 underlying encoding.

Parameters:
id - The string to escape.
Returns:
id fully escaped using URL rules.

escapeResourceName

public static String escapeResourceName(String id)
Return a string based on id that is fully escaped using Resource name validity rules.

Parameters:
id - The string to escape.
Returns:
id fully escaped using Resource name validity rules.

escapeQuestionMark

public static String escapeQuestionMark(String id)
Return a string based on id that is fully escaped the question mark.

Parameters:
id - The string to escape.
Returns:
id fully escaped question mark.

escapeZipEntry

public static String escapeZipEntry(String id)
Return a string based on id that is fully escaped to create a zip entry

Parameters:
id - The string to escape.
Returns:
id fully escaped to create a zip entry

escapeJsQuoted

public static String escapeJsQuoted(String value)
Return a string based on value that is safe to place into a javascript value that is in single quiotes.

Parameters:
value - The string to escape.
Returns:
value escaped.

escapeSql

public static String escapeSql(String value)
Return a string based on value that is safe to place into a sql statement: sql statements use the single quote, and this must be doubled as an escape.

Parameters:
value - The string to escape.
Returns:
value escaped.

escapeJavascript

public static String escapeJavascript(String value)
Return a string based on value that is safe to place into a javascript / html identifier: anything not alphanumeric change to 'x'. If the first character is not alphabetic, a letter 'i' is prepended.

Parameters:
value - The string to escape.
Returns:
value fully escaped using javascript / html identifier rules.

checkUserId

public static void checkUserId(String id)
                        throws IdInvalidException
Check for a valid user id.

Throws:
IdInvalidException - if the id is invalid.

checkResourceId

public static void checkResourceId(String id)
                            throws IdInvalidException
Check for a valid resource id.

Throws:
IdInvalidException - if the id is invalid.

checkResourceRef

public static void checkResourceRef(String ref)
                             throws IdInvalidException
Check for a valid resource reference.

Throws:
IdInvalidException - if the id is invalid.

checkEmailLocal

public static boolean checkEmailLocal(String id)
Is this a valid local part of an email id?


getFileName

public static String getFileName(String fullName)
Isolate and return just the file name part of a full drive and path file name.

Parameters:
fullName - The full file name from a local os file system (mac, unix, windoze)
Returns:
Just the name (and extension) of the file, without the drive or path.

getFileSizeWithDividor

public static String getFileSizeWithDividor(String size)
Put the dividor (comma) inside the size string, for example, 1,003 for 1003

Parameters:
size - The string of size number
Returns:
The size string with the dividor added

getFileExtension

public static String getFileExtension(String fullName)
Isolate and return just the file extension part of a full drive and path file name.

Parameters:
fullName - The full file name from a local os file system (mac, unix, windoze)
Returns:
Just the extension of the file, to the right of the dot, not including the dot, or blank if none.

getResourceTarget

public static String getResourceTarget(ResourceProperties properties)
Determine whether a file resource should be opened in the current window or a new window.

Parameters:
properties - The properties for a file resource
Returns:
A string identifying the window in which to open the resource: "_self" to open the resource in the current window, "_blank" for a new window, or an empty string if the resource is not a file.

letBrowserInline

public static boolean letBrowserInline(String type)
Is this a mime type that the browser can handle inline, in a browser window? If so, links to this type should be to a _blank, and content-disposition should be inline. If not, links to this type should be to _self, and content-disposition should be attachment.

Parameters:
type - The mime type to check.
Returns:
true if this type of resource the browser can handle in line, false if not.

limit

public static String limit(String value,
                           Integer length)
Limit the string to a certain number of characters, adding "..." if it was truncated.

Parameters:
value - The string to limit.
Returns:
The limited string.

limit

public static String limit(String value,
                           int length)
Limit the string to a certain number of characters, adding "..." if it was truncated

Parameters:
value - The string to limit.
Returns:
The limited string.

limitFormattedText

public static String limitFormattedText(String value,
                                        int length)
Limit the formatted to a certain number of DISPLAYED characters, adding "..." if it was truncated. For example, trim("Hello \n<b>World</b>!", 7) returns "Hello \n<b>W</b>..."

Parameters:
value - The formatted text to limit.
Returns:
The limited string.

cleanInput

public static String cleanInput(String value)
Clean the user input string of strange newlines, etc.

Parameters:
value - The user input string.
Returns:
value cleaned of string newlines, etc.

stripAllNewlines

public static String stripAllNewlines(String value)
Clean the string parameter of all newlines (replace with space character) and trim leading and trailing spaces

Parameters:
value - The user input string.
Returns:
value cleaned of newlines, etc.

checkDate

public static boolean checkDate(int day,
                                int month,
                                int year)
Validate whether the date input is valid