org.opencms.util
Class CmsStringUtil

java.lang.Object
  extended byorg.opencms.util.CmsStringUtil

public final class CmsStringUtil
extends Object

Provides String utility functions.

Since:
6.0.0
Version:
$Revision: 1.34 $
Author:
Andreas Zahner, Alexander Kandzior, Thomas Weckert

Field Summary
static String BODY_END_REGEX
          Regular expression that matches the HTML body end tag.
static String BODY_START_REGEX
          Regular expression that matches the HTML body start tag.
static String LINE_SEPARATOR
          a convienient shorthand to the line separator constant.
static String MACRO_OPENCMS_CONTEXT
          Context macro.
static String TABULATOR
          a convienient shorthand for tabulations.
 
Method Summary
static String changeFileNameSuffixTo(String filename, String suffix)
          Changes the filename suffix.
static String escapeHtml(String source)
          Replaces occurences of special control characters in the given input with a HTML representation.
static String escapeJavaScript(String source)
          Escapes a String so it may be used in JavaScript String definitions.
static String escapePattern(String source)
          Escapes a String so it may be used as a Perl5 regular expression.
static Map extendAttribute(String text, String attribute, String defValue)
          This method takes a part of a html tag definition, an attribute to extend within the given text and a default value for this attribute; and returns a Map with 2 values: a String with key "text" with the new text without the given attribute, and another String with key "value" with the new extended value for the given attribute, this value is sourrounded by the same type of quotation marks as in the given text.
static String extractHtmlBody(String content)
          Extracts the content of a <body> tag in a HTML page.
static String extractXmlEncoding(String content)
          Extracts the xml encoding setting from an xml file that is contained in a String by parsing the xml head.
static String formatRuntime(long runtime)
          Formats a runtime in the format hh:mm:ss, to be used e.g.
static boolean isEmpty(String value)
          Returns true if the provided String is either null or the empty String "".
static boolean isEmptyOrWhitespaceOnly(String value)
          Returns true if the provided String is either null or contains only white spaces.
static boolean isNotEmpty(String value)
          Returns true if the provided String is neither null nor the empty String "".
static boolean isNotEmptyOrWhitespaceOnly(String value)
          Returns true if the provided String is neither null nor contains only white spaces.
static boolean isValidJavaClassName(String className)
          Checks if the given class name is a valid Java class name.
static String padLeft(String input, int size)
          Applies white space padding to the left of the given String.
static String padRight(String input, int size)
          Applies white space padding to the right of the given String.
static String[] splitAsArray(String source, char delimiter)
          Splits a String into substrings along the provided char delimiter and returns the result as an Array of Substrings.
static String[] splitAsArray(String source, String delimiter)
          Splits a String into substrings along the provided String delimiter and returns the result as an Array of Substrings.
static List splitAsList(String source, char delimiter)
          Splits a String into substrings along the provided char delimiter and returns the result as a List of Substrings.
static List splitAsList(String source, char delimiter, boolean trim)
          Splits a String into substrings along the provided char delimiter and returns the result as a List of Substrings.
static List splitAsList(String source, String delimiter)
          Splits a String into substrings along the provided String delimiter and returns the result as List of Substrings.
static List splitAsList(String source, String delimiter, boolean trim)
          Splits a String into substrings along the provided String delimiter and returns the result as List of Substrings.
static String substitute(String content, String searchString, String replaceItem)
          Substitutes searchString in content with replaceItem.
static String substituteContextPath(String htmlContent, String context)
          Substitutes the OpenCms context path (e.g.
static String substitutePerl(String content, String searchString, String replaceItem, String occurences)
          Substitutes searchString in content with replaceItem.
static boolean validateRegex(String value, String regex, boolean allowEmpty)
          Validates a value against a regular expression.
static boolean validateResourceName(String name)
          Checks if the provided name is a valid resource name, that is contains only valid characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BODY_END_REGEX

public static final String BODY_END_REGEX
Regular expression that matches the HTML body end tag.

See Also:
Constant Field Values

BODY_START_REGEX

public static final String BODY_START_REGEX
Regular expression that matches the HTML body start tag.

See Also:
Constant Field Values

LINE_SEPARATOR

public static final String LINE_SEPARATOR
a convienient shorthand to the line separator constant.


TABULATOR

public static final String TABULATOR
a convienient shorthand for tabulations.

See Also:
Constant Field Values

MACRO_OPENCMS_CONTEXT

public static final String MACRO_OPENCMS_CONTEXT
Context macro.

See Also:
Constant Field Values
Method Detail

changeFileNameSuffixTo

public static String changeFileNameSuffixTo(String filename,
                                            String suffix)
Changes the filename suffix.

Parameters:
filename - the filename to be changed
suffix - the new suffix of the file
Returns:
the filename with the replaced suffix

escapeHtml

public static String escapeHtml(String source)
Replaces occurences of special control characters in the given input with a HTML representation.

This method currrently replaces line breaks to <br/> and special HTML chars like < > & " with their HTML entity representation.

Parameters:
source - the String to escape
Returns:
the escaped String

escapeJavaScript

public static String escapeJavaScript(String source)
Escapes a String so it may be used in JavaScript String definitions.

This method replaces line breaks, quotationmarks and \ characters.

Parameters:
source - the String to escape
Returns:
the escaped String

escapePattern

public static String escapePattern(String source)
Escapes a String so it may be used as a Perl5 regular expression.

This method replaces the following characters in a String:
{}[]()\$^.*+/

Parameters:
source - the string to escape
Returns:
the escaped string

extendAttribute

public static Map extendAttribute(String text,
                                  String attribute,
                                  String defValue)
This method takes a part of a html tag definition, an attribute to extend within the given text and a default value for this attribute; and returns a Map with 2 values: a String with key "text" with the new text without the given attribute, and another String with key "value" with the new extended value for the given attribute, this value is sourrounded by the same type of quotation marks as in the given text.

Parameters:
text - the text to search in
attribute - the attribute to remove and extend from the text
defValue - a default value for the attribute, should not have any quotation mark
Returns:
a map with the new text and the new value for the given attribute

extractHtmlBody

public static String extractHtmlBody(String content)
Extracts the content of a <body> tag in a HTML page.

This method should be pretty robust and work even if the input HTML does not contains a valid body tag.

Parameters:
content - the content to extract the body from
Returns:
the extracted body tag content

extractXmlEncoding

public static String extractXmlEncoding(String content)
Extracts the xml encoding setting from an xml file that is contained in a String by parsing the xml head.

This is useful if you have a byte array that contains a xml String, but you do not know the xml encoding setting. Since the encoding setting in the xml head is usually encoded with standard US-ASCII, you usually just create a String of the byte array without encoding setting, and use this method to find the 'true' encoding. Then create a String of the byte array again, this time using the found encoding.

This method will return null in case no xml head or encoding information is contained in the input.

Parameters:
content - the xml content to extract the encoding from
Returns:
the extracted encoding, or null if no xml encoding setting was found in the input

formatRuntime

public static String formatRuntime(long runtime)
Formats a runtime in the format hh:mm:ss, to be used e.g. in reports.

If the runtime is greater then 24 hours, the format dd:hh:mm:ss is used.

Parameters:
runtime - the time to format
Returns:
the formatted runtime

isEmpty

public static boolean isEmpty(String value)
Returns true if the provided String is either null or the empty String "".

Parameters:
value - the value to check
Returns:
true, if the provided value is null or the empty String, false otherwise

isEmptyOrWhitespaceOnly

public static boolean isEmptyOrWhitespaceOnly(String value)
Returns true if the provided String is either null or contains only white spaces.

Parameters:
value - the value to check
Returns:
true, if the provided value is null or contains only white spaces, false otherwise

isNotEmpty

public static boolean isNotEmpty(String value)
Returns true if the provided String is neither null nor the empty String "".

Parameters:
value - the value to check
Returns:
true, if the provided value is not null and not the empty String, false otherwise

isNotEmptyOrWhitespaceOnly

public static boolean isNotEmptyOrWhitespaceOnly(String value)
Returns true if the provided String is neither null nor contains only white spaces.

Parameters:
value - the value to check
Returns:
true, if the provided value is null or contains only white spaces, false otherwise

isValidJavaClassName

public static boolean isValidJavaClassName(String className)
Checks if the given class name is a valid Java class name.

Parameters:
className - the name to check
Returns:
true if the given class name is a valid Java class name

padLeft

public static String padLeft(String input,
                             int size)
Applies white space padding to the left of the given String.

Parameters:
input - the input to pad left
size - the size of the padding
Returns:
the input padded to the left

padRight

public static String padRight(String input,
                              int size)
Applies white space padding to the right of the given String.

Parameters:
input - the input to pad right
size - the size of the padding
Returns:
the input padded to the right

splitAsArray

public static String[] splitAsArray(String source,
                                    char delimiter)
Splits a String into substrings along the provided char delimiter and returns the result as an Array of Substrings.

Parameters:
source - the String to split
delimiter - the delimiter to split at
Returns:
the Array of splitted Substrings

splitAsArray

public static String[] splitAsArray(String source,
                                    String delimiter)
Splits a String into substrings along the provided String delimiter and returns the result as an Array of Substrings.

Parameters:
source - the String to split
delimiter - the delimiter to split at
Returns:
the Array of splitted Substrings

splitAsList

public static List splitAsList(String source,
                               char delimiter)
Splits a String into substrings along the provided char delimiter and returns the result as a List of Substrings.

Parameters:
source - the String to split
delimiter - the delimiter to split at
Returns:
the List of splitted Substrings

splitAsList

public static List splitAsList(String source,
                               char delimiter,
                               boolean trim)
Splits a String into substrings along the provided char delimiter and returns the result as a List of Substrings.

Parameters:
source - the String to split
delimiter - the delimiter to split at
trim - flag to indicate if leading and trailing whitespaces should be omitted
Returns:
the List of splitted Substrings

splitAsList

public static List splitAsList(String source,
                               String delimiter)
Splits a String into substrings along the provided String delimiter and returns the result as List of Substrings.

Parameters:
source - the String to split
delimiter - the delimiter to split at
Returns:
the Array of splitted Substrings

splitAsList

public static List splitAsList(String source,
                               String delimiter,
                               boolean trim)
Splits a String into substrings along the provided String delimiter and returns the result as List of Substrings.

Parameters:
source - source the String to split
delimiter - the delimiter to split at
trim - flag to indicate if leading and trailing whitespaces should be omitted
Returns:
the Array of splitted Substrings

substitute

public static String substitute(String content,
                                String searchString,
                                String replaceItem)
Substitutes searchString in content with replaceItem.

Parameters:
content - the content which is scanned
searchString - the String which is searched in content
replaceItem - the new String which replaces searchString
Returns:
String the substituted String

substituteContextPath

public static String substituteContextPath(String htmlContent,
                                           String context)
Substitutes the OpenCms context path (e.g. /opencms/opencms/) in a HTML page with a special variable so that the content also runs if the context path of the server changes.

Parameters:
htmlContent - the HTML to replace the context path in
context - the context path of the server
Returns:
the HTML with the replaced context path

substitutePerl

public static String substitutePerl(String content,
                                    String searchString,
                                    String replaceItem,
                                    String occurences)
Substitutes searchString in content with replaceItem.

Parameters:
content - the content which is scanned
searchString - the String which is searched in content
replaceItem - the new String which replaces searchString
occurences - must be a "g" if all occurences of searchString shall be replaced
Returns:
String the substituted String

validateRegex

public static boolean validateRegex(String value,
                                    String regex,
                                    boolean allowEmpty)
Validates a value against a regular expression.

Parameters:
value - the value to test
regex - the regular expression
allowEmpty - if an empty value is allowed
Returns:
true if the value satisfies the validation

validateResourceName

public static boolean validateResourceName(String name)
Checks if the provided name is a valid resource name, that is contains only valid characters.

PLEASE NOTE: This logic is NOT yet used in the current release.

Parameters:
name - the resource name to check
Returns:
true if the resource name is vaild, false otherwise