|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.opencms.util.CmsStringUtil
public final class CmsStringUtil
Provides String utility functions.
Nested Class Summary | |
---|---|
static class |
CmsStringUtil.CmsSlashComparator
Compares two Strings according to the count of containing slashes. |
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 |
FALSE
Constant for "false" . |
static String |
LINE_SEPARATOR
a convenient shorthand to the line separator constant. |
static String |
MACRO_OPENCMS_CONTEXT
Context macro. |
static String |
PLACEHOLDER_END
The place holder end sign in the pattern. |
static String |
PLACEHOLDER_START
The place holder start sign in the pattern. |
static char[] |
SENTENCE_ENDING_CHARS
Contains all chars that end a sentence in the trimToSize(String, int, int, String) method. |
static String |
TABULATOR
a convenient shorthand for tabulations. |
static String |
TRUE
Constant for "true" . |
Method Summary | ||
---|---|---|
static String |
arrayAsString(String[] arg,
String separator)
Returns a string representation for the given array using the given separator. |
|
static String |
changeFileNameSuffixTo(String filename,
String suffix)
Changes the filename suffix. |
|
static void |
checkName(String name,
String contraints,
String key,
I_CmsMessageBundle bundle)
Checks if a given name is composed only of the characters a...z,A...Z,0...9
and the provided constraints . |
|
static String |
collectionAsString(Collection<?> collection,
String separator)
Returns a string representation for the given collection using the given separator. |
|
static int |
countChar(String s,
char c)
Counts the occurrence of a given char in a given String. |
|
static String |
escapeHtml(String source)
Replaces occurrences 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<String,String> |
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
with 2 values: a with key "text" with the new text
without the given attribute, and another with key "value"
with the new extended value for the given attribute, this value is surrounded 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 |
formatResourceName(String name,
int maxLength)
Formats a resource name that it is displayed with the maximum length and path information is adjusted. |
|
static String |
formatRuntime(long runtime)
Formats a runtime in the format hh:mm:ss, to be used e.g. |
|
static Color |
getColorValue(String value,
Color defaultValue,
String key)
Returns the color value ( ) for the given String value. |
|
static String |
getCommonPrefixPath(String first,
String second)
Gets the common prefix path of two paths. |
|
static int |
getIntValue(String value,
int defaultValue,
String key)
Returns the Integer (int) value for the given String value. |
|
static int |
getIntValueRounded(String value,
int defaultValue,
String key)
Returns the closest Integer (int) value for the given String value. |
|
static long |
getLongValue(String value,
long defaultValue,
String key)
Returns the Long (long) value for the given String value. |
|
static List<String> |
getPathComponents(String path)
Splits a path into its non-empty path components. |
|
static String |
getRelativeSubPath(String base,
String path)
Converts a given path to a path relative to a base folder, but only if it actually is a sub-path of the latter, otherwise null is returned. |
|
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 |
isEqual(Object value1,
Object value2)
Returns true if the provided Objects are either both null
or equal according to Object.equals(Object) . |
|
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 |
isPrefixPath(String firstPath,
String secondPath)
Checks whether one path is a prefix path of another, i.e. |
|
static boolean |
isValidJavaClassName(String className)
Checks if the given class name is a valid Java class name. |
|
static String |
joinPaths(List<String> paths)
Concatenates multiple paths and separates them with '/'. |
|
static String |
joinPaths(String... paths)
Concatenates multiple paths and separates them with '/'. |
|
static int |
lastIndexOf(String source,
char[] chars)
Returns the last index of any of the given chars in the given source. |
|
static int |
lastWhitespaceIn(String source)
Returns the last index a whitespace char the given source. |
|
static String |
listAsString(List<?> list,
String separator)
Returns a string representation for the given list using the given separator. |
|
static
|
mapAsString(Map<K,V> map,
String sepItem,
String sepKeyval)
Returns a string representation for the given map using the given separators. |
|
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<String> |
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<String> |
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<String> |
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<String> |
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 Map<String,String> |
splitAsMap(String source,
String paramDelim,
String keyValDelim)
Splits a String into substrings along the provided paramDelim delimiter,
then each substring is treat as a key-value pair delimited by keyValDelim . |
|
static String |
substitute(Pattern pattern,
String text,
I_CmsRegexSubstitution sub)
Substitutes a pattern in a string using a I_CmsRegexSubstitution . |
|
static String |
substitute(String source,
Map<String,String> substitions)
Replaces a set of searchString and replaceString pairs,
given by the substitutions Map parameter. |
|
static String |
substitute(String source,
String searchString,
String replaceString)
Substitutes searchString in the given source String with replaceString . |
|
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 String |
toUnicodeLiteral(String s)
Returns the java String literal for the given String. |
|
static String |
transformValues(String oldFormat,
String newFormat,
String value)
This method transformes a string which matched a format with one or more place holders into another format. |
|
static String |
trimToSize(String source,
int length)
Returns a substring of the source, which is at most length characters long. |
|
static String |
trimToSize(String source,
int length,
int area,
String suffix)
Returns a substring of the source, which is at most length characters long, cut in the last area chars in the source at a sentence ending char or whitespace. |
|
static String |
trimToSize(String source,
int length,
String suffix)
Returns a substring of the source, which is at most length characters long. |
|
static boolean |
validateRegex(String value,
String regex,
boolean allowEmpty)
Validates a value against a regular expression. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String BODY_END_REGEX
public static final String BODY_START_REGEX
public static final String FALSE
"false"
.
public static final String LINE_SEPARATOR
public static final String MACRO_OPENCMS_CONTEXT
public static final String PLACEHOLDER_END
public static final String PLACEHOLDER_START
public static final char[] SENTENCE_ENDING_CHARS
trimToSize(String, int, int, String)
method.
public static final String TABULATOR
public static final String TRUE
"true"
.
Method Detail |
---|
public static String arrayAsString(String[] arg, String separator)
arg
- the array to transform to a Stringseparator
- the item separator
public static String changeFileNameSuffixTo(String filename, String suffix)
filename
- the filename to be changedsuffix
- the new suffix of the file
public static void checkName(String name, String contraints, String key, I_CmsMessageBundle bundle) throws CmsIllegalArgumentException
a...z,A...Z,0...9
and the provided constraints
.If the check fails, an Exception is generated. The provided bundle and key is used to generate the Exception. 4 parameters are passed to the Exception:
name
constraints
name
- the name to checkcontraints
- the additional character constraintskey
- the key to use for generating the Exception (if required)bundle
- the bundle to use for generating the Exception (if required)
CmsIllegalArgumentException
- if the check fails (generated from the given key and bundle)public static String collectionAsString(Collection<?> collection, String separator)
collection
- the collection to printseparator
- the item separator
public static int countChar(String s, char c)
s
- the stringc
- the char to count
public static String escapeHtml(String source)
This method currently replaces line breaks to <br/>
and special HTML chars
like < > & "
with their HTML entity representation.
source
- the String to escape
public static String escapeJavaScript(String source)
This method replaces line breaks, quotation marks and \ characters.
source
- the String to escape
public static String escapePattern(String source)
This method replaces the following characters in a String:
{}[]()\$^.*+/
source
- the string to escape
public static Map<String,String> extendAttribute(String text, String attribute, String defValue)
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 surrounded by the same type of
quotation marks as in the given text.
text
- the text to search inattribute
- the attribute to remove and extend from the textdefValue
- a default value for the attribute, should not have any quotation mark
public static String extractHtmlBody(String content)
<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.
content
- the content to extract the body from
public static String extractXmlEncoding(String content)
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.
content
- the xml content to extract the encoding from
public static String formatResourceName(String name, int maxLength)
In order to reduce the length of the displayed names, single folder names are removed/replaced with ... successively, starting with the second! folder. The first folder is removed as last.
Example: formatResourceName("/myfolder/subfolder/index.html", 21) returns /myfolder/.../index.html
.
name
- the resource name to formatmaxLength
- the maximum length of the resource name (without leading /...
)
public static String formatRuntime(long runtime)
If the runtime is greater then 24 hours, the format dd:hh:mm:ss is used.
runtime
- the time to format
public static Color getColorValue(String value, Color defaultValue, String key)
Color
) for the given String value.All parse errors are caught and the given default value is returned in this case.
value
- the value to parse as colordefaultValue
- the default value in case of parsing errorskey
- a key to be included in the debug output in case of parse errors
public static String getCommonPrefixPath(String first, String second)
first
- the first pathsecond
- the second path
public static int getIntValue(String value, int defaultValue, String key)
All parse errors are caught and the given default value is returned in this case.
value
- the value to parse as intdefaultValue
- the default value in case of parsing errorskey
- a key to be included in the debug output in case of parse errors
public static int getIntValueRounded(String value, int defaultValue, String key)
All parse errors are caught and the given default value is returned in this case.
value
- the value to parse as int, can also represent a float valuedefaultValue
- the default value in case of parsing errorskey
- a key to be included in the debug output in case of parse errors
public static long getLongValue(String value, long defaultValue, String key)
All parse errors are caught and the given default value is returned in this case.
value
- the value to parse as longdefaultValue
- the default value in case of parsing errorskey
- a key to be included in the debug output in case of parse errors
public static List<String> getPathComponents(String path)
If the path is the root path, an empty list will be returned.
path
- the path to split
public static String getRelativeSubPath(String base, String path)
base
- the base pathpath
- the path which should be converted to a relative path
public static boolean isEmpty(String value)
true
if the provided String is either null
or the empty String ""
.
value
- the value to check
public static boolean isEmptyOrWhitespaceOnly(String value)
true
if the provided String is either null
or contains only white spaces.
value
- the value to check
public static boolean isEqual(Object value1, Object value2)
true
if the provided Objects are either both null
or equal according to Object.equals(Object)
.
value1
- the first object to comparevalue2
- the second object to compare
true
if the provided Objects are either both null
or equal according to Object.equals(Object)
public static boolean isNotEmpty(String value)
true
if the provided String is neither null
nor the empty String ""
.
value
- the value to check
public static boolean isNotEmptyOrWhitespaceOnly(String value)
true
if the provided String is neither null
nor contains only white spaces.
value
- the value to check
true
, if the provided value is null
or contains only white spaces, false
otherwisepublic static boolean isPrefixPath(String firstPath, String secondPath)
It is not enough to just use String.startsWith(java.lang.String, int)
, because we want /foo/bar to
be a prefix path of /foo/bar/baz, but not of /foo/bar42.
firstPath
- the first pathsecondPath
- the second path
public static boolean isValidJavaClassName(String className)
className
- the name to check
public static String joinPaths(List<String> paths)
Consecutive slashes will be reduced to a single slash in the resulting string. For example, joinPaths("/foo/", "/bar", "baz") will return "/foo/bar/baz".
paths
- the list of paths
public static String joinPaths(String... paths)
Consecutive slashes will be reduced to a single slash in the resulting string. For example, joinPaths("/foo/", "/bar", "baz") will return "/foo/bar/baz".
paths
- the array of paths
public static int lastIndexOf(String source, char[] chars)
If no char is found, -1 is returned.
source
- the source to checkchars
- the chars to find
public static int lastWhitespaceIn(String source)
If no whitespace char is found, -1 is returned.
source
- the source to check
public static String listAsString(List<?> list, String separator)
list
- the list to writeseparator
- the item separator string
public static <K,V> String mapAsString(Map<K,V> map, String sepItem, String sepKeyval)
K
- type of map keysV
- type of map valuesmap
- the map to writesepItem
- the item separator stringsepKeyval
- the key-value pair separator string
public static String padLeft(String input, int size)
input
- the input to pad leftsize
- the size of the padding
public static String padRight(String input, int size)
input
- the input to pad rightsize
- the size of the padding
public static String[] splitAsArray(String source, char delimiter)
source
- the String to splitdelimiter
- the delimiter to split at
public static String[] splitAsArray(String source, String delimiter)
source
- the String to splitdelimiter
- the delimiter to split at
public static List<String> splitAsList(String source, char delimiter)
source
- the String to splitdelimiter
- the delimiter to split at
public static List<String> splitAsList(String source, char delimiter, boolean trim)
source
- the String to splitdelimiter
- the delimiter to split attrim
- flag to indicate if leading and trailing white spaces should be omitted
public static List<String> splitAsList(String source, String delimiter)
source
- the String to splitdelimiter
- the delimiter to split at
public static List<String> splitAsList(String source, String delimiter, boolean trim)
source
- the String to splitdelimiter
- the delimiter to split attrim
- flag to indicate if leading and trailing white spaces should be omitted
public static Map<String,String> splitAsMap(String source, String paramDelim, String keyValDelim)
paramDelim
delimiter,
then each substring is treat as a key-value pair delimited by keyValDelim
.
source
- the string to splitparamDelim
- the string to delimit each key-value pairkeyValDelim
- the string to delimit key and value
public static String substitute(Pattern pattern, String text, I_CmsRegexSubstitution sub)
I_CmsRegexSubstitution
.
pattern
- the pattern to substitutetext
- the text in which the pattern should be substitutedsub
- the substitution handler
public static String substitute(String source, Map<String,String> substitions)
searchString
and replaceString
pairs,
given by the substitutions
Map parameter.
source
- the string to scansubstitions
- the map of substitutions
substitute(String, String, String)
public static String substitute(String source, String searchString, String replaceString)
searchString
in the given source String with replaceString
.
This is a high-performance implementation which should be used as a replacement for
in case no
regular expression evaluation is required.String.replaceAll(java.lang.String, java.lang.String)
source
- the content which is scannedsearchString
- the String which is searched in contentreplaceString
- the String which replaces searchString
public static String substituteContextPath(String htmlContent, String context)
htmlContent
- the HTML to replace the context path incontext
- the context path of the server
public static String substitutePerl(String content, String searchString, String replaceItem, String occurences)
content
- the content which is scannedsearchString
- the String which is searched in contentreplaceItem
- the new String which replaces searchStringoccurences
- must be a "g" if all occurrences of searchString shall be replaced
public static String toUnicodeLiteral(String s)
This is the form of the String that had to be written into source code using the unicode escape sequence for special characters.
Example: "Ä" would be transformed to "\\u00C4".
s
- a string that may contain non-ascii characters
public static String transformValues(String oldFormat, String newFormat, String value)
PLACEHOLDER_START
and end with PLACEHOLDER_END
.
oldFormat
- the original formatnewFormat
- the new formatvalue
- the value which matched the original format and which shall be transformed into the new format
public static String trimToSize(String source, int length)
This is the same as calling trimToSize(String, int, String)
with the
parameters (source, length, " ...")
.
source
- the string to trimlength
- the maximum length of the string to be returned
public static String trimToSize(String source, int length, int area, String suffix)
area
chars in the source at a sentence ending char or whitespace.
If a char is cut, the given suffix
is appended to the result.
source
- the string to trimlength
- the maximum length of the string to be returnedarea
- the area at the end of the string in which to find a sentence ender or whitespacesuffix
- the suffix to append in case the String was trimmed
public static String trimToSize(String source, int length, String suffix)
If a char is cut, the given suffix
is appended to the result.
This is almost the same as calling trimToSize(String, int, int, String)
with the
parameters (source, length, length*, suffix)
. If length
if larger then 100, then length* = length / 2
,
otherwise length* = length
.
source
- the string to trimlength
- the maximum length of the string to be returnedsuffix
- the suffix to append in case the String was trimmed
public static boolean validateRegex(String value, String regex, boolean allowEmpty)
value
- the value to testregex
- the regular expressionallowEmpty
- if an empty value is allowed
true
if the value satisfies the validation
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |