freemarker.template.utility
Class StringUtil

java.lang.Object
  extended by freemarker.template.utility.StringUtil

public class StringUtil
extends Object

Some text related utilities.

Version:
$Id: StringUtil.java,v 1.48 2005/06/01 22:39:08 ddekany Exp $

Constructor Summary
StringUtil()
           
 
Method Summary
static String capitalize(String s)
           
static String chomp(String s)
          Removes the line-break from the end of the string.
static Locale deduceLocale(String input)
           
static String FTLStringLiteralDec(String s)
          FTL string literal decoding.
static String FTLStringLiteralEnc(String s)
           
static boolean getYesNo(String s)
           
static String HTMLEnc(String s)
          HTML encoding (does not convert line breaks).
static boolean isXMLID(String name)
           
static String javaScriptStringEnc(String s)
          Escapes a String according the JavaScript string literal escaping rules.
static String javaStringEnc(String s)
          Escapes the String with the escaping rules of Java language string literals, so it is safe to insert the value into a string literal.
static String jQuote(Object obj)
          Converts the parameter with toString (if not null)and passes it to jQuote(String).
static String jQuote(String s)
          Quotes string as Java Language string literal.
static String jQuoteNoXSS(Object obj)
          Converts the parameter with toString (if not null)and passes it to jQuoteNoXSS(String).
static String jQuoteNoXSS(String s)
          Same as jQuoteNoXSS(String) but also escapes '<' as <.
static String jsonStringEnc(String s)
          Escapes a String according the JSON string literal escaping rules.
static String leftPad(String s, int minLength)
          Pads the string at the left with spaces until it reaches the desired length.
static String leftPad(String s, int minLength, char filling)
          Pads the string at the left with the specified character until it reaches the desired length.
static String leftPad(String s, int minLength, String filling)
          Pads the string at the left with a filling pattern until it reaches the desired length.
static boolean matchesName(String qname, String nodeName, String nsURI, Environment env)
           
static Map parseNameValuePairList(String s, String defaultValue)
          Parses a name-value pair list, where the pairs are separated with comma, and the name and value is separated with colon.
static String replace(String text, String oldsub, String newsub, boolean caseInsensitive, boolean firstOnly)
          Replaces all occurrences of a sub-string in a string.
static String rightPad(String s, int minLength)
          Pads the string at the right with spaces until it reaches the desired length.
static String rightPad(String s, int minLength, char filling)
          Pads the string at the right with the specified character until it reaches the desired length.
static String rightPad(String s, int minLength, String filling)
          Pads the string at the right with a filling pattern until it reaches the desired length.
static String RTFEnc(String s)
          Rich Text Format encoding (does not replace line breaks).
static String[] split(String s, char c)
          Splits a string at the specified character.
static String[] split(String s, String sep, boolean caseInsensitive)
          Splits a string at the specified string.
static String URLEnc(String s, String charset)
          URL encoding (like%20this).
static int versionStringToInt(String version)
          Converts a version number string to an integer for easy comparison.
static String XHTMLEnc(String s)
          XHTML Encoding.
static String XMLEnc(String s)
          XML Encoding.
static String XMLEncNA(String s)
          XML encoding without replacing apostrophes.
static String XMLEncNQG(String s)
          XML encoding without replacing apostrophes and quotation marks and greater-thans (except in ]]>).
static String XMLEncQAttr(String s)
          XML encoding for attributes valies quoted with " (not with '!).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtil

public StringUtil()
Method Detail

HTMLEnc

public static String HTMLEnc(String s)
HTML encoding (does not convert line breaks). Replaces all '>' '<' '&' and '"' with entity reference


XMLEnc

public static String XMLEnc(String s)
XML Encoding. Replaces all '>' '<' '&', "'" and '"' with entity reference


XHTMLEnc

public static String XHTMLEnc(String s)
XHTML Encoding. Replaces all '>' '<' '&', "'" and '"' with entity reference suitable for XHTML decoding in common user agents (including legacy user agents, which do not decode "'" to "'", so "'" is used instead [see http://www.w3.org/TR/xhtml1/#C_16])


XMLEncNA

public static String XMLEncNA(String s)
XML encoding without replacing apostrophes.

See Also:
XMLEnc(String)

XMLEncQAttr

public static String XMLEncQAttr(String s)
XML encoding for attributes valies quoted with " (not with '!). Also can be used for HTML attributes that are quoted with ".

See Also:
XMLEnc(String)

XMLEncNQG

public static String XMLEncNQG(String s)
XML encoding without replacing apostrophes and quotation marks and greater-thans (except in ]]>).

See Also:
XMLEnc(String)

RTFEnc

public static String RTFEnc(String s)
Rich Text Format encoding (does not replace line breaks). Escapes all '\' '{' '}' and '"'


URLEnc

public static String URLEnc(String s,
                            String charset)
                     throws UnsupportedEncodingException
URL encoding (like%20this).

Throws:
UnsupportedEncodingException

FTLStringLiteralEnc

public static String FTLStringLiteralEnc(String s)

FTLStringLiteralDec

public static String FTLStringLiteralDec(String s)
                                  throws ParseException
FTL string literal decoding. \\, \", \', \n, \t, \r, \b and \f will be replaced according to Java rules. In additional, it knows \g, \l, \a and \{ which are replaced with <, >, & and { respectively. \x works as hexadecimal character code escape. The character codes are interpreted according to UCS basic plane (Unicode). "f\x006Fo", "f\x06Fo" and "f\x6Fo" will be "foo". "f\x006F123" will be "foo123" as the maximum number of digits is 4. All other \X (where X is any character not mentioned above or End-of-string) will cause a ParseException.

Parameters:
s - String literal without the surrounding quotation marks
Returns:
String with all escape sequences resolved
Throws:
ParseException - if there string contains illegal escapes

deduceLocale

public static Locale deduceLocale(String input)

capitalize

public static String capitalize(String s)

getYesNo

public static boolean getYesNo(String s)

split

public static String[] split(String s,
                             char c)
Splits a string at the specified character.


split

public static String[] split(String s,
                             String sep,
                             boolean caseInsensitive)
Splits a string at the specified string.


replace

public static String replace(String text,
                             String oldsub,
                             String newsub,
                             boolean caseInsensitive,
                             boolean firstOnly)
Replaces all occurrences of a sub-string in a string.

Parameters:
text - The string where it will replace oldsub with newsub.
Returns:
String The string after the replacements.

chomp

public static String chomp(String s)
Removes the line-break from the end of the string.


jQuote

public static String jQuote(Object obj)
Converts the parameter with toString (if not null)and passes it to jQuote(String).


jQuote

public static String jQuote(String s)
Quotes string as Java Language string literal. Returns string "null" if s is null.


jQuoteNoXSS

public static String jQuoteNoXSS(Object obj)
Converts the parameter with toString (if not null)and passes it to jQuoteNoXSS(String).


jQuoteNoXSS

public static String jQuoteNoXSS(String s)
Same as jQuoteNoXSS(String) but also escapes '<' as <. This is used for log messages to prevent XSS on poorly written Web-based log viewers.


javaStringEnc

public static String javaStringEnc(String s)
Escapes the String with the escaping rules of Java language string literals, so it is safe to insert the value into a string literal. The resulting string will not be quoted.

All characters under UCS code point 0x20 will be escaped. Where they have no dedicated escape sequence in Java, they will be replaced with hexadecimal escape (\uXXXX).

See Also:
jQuote(String)

javaScriptStringEnc

public static String javaScriptStringEnc(String s)
Escapes a String according the JavaScript string literal escaping rules. The resulting string will not be quoted.

It escapes both ' and ". In additional it escapes > as \> (to avoid </script>).

All characters under UCS code point 0x20 will be escaped. Where they have no dedicated escape sequence in JavaScript, they will be replaced with hexadecimal escape (\uXXXX).


jsonStringEnc

public static String jsonStringEnc(String s)
Escapes a String according the JSON string literal escaping rules. The resulting string will not be quoted; the caller have to ensure that they are there in the final output.

Beware, it doesn't escape ', as JSON string must be delimited with ", and JSON has no \' escape either!

It will escape / as \/ if it's after <, to avoid </script>.

It will escape > as \u003E if it's after ]], to avoid closing a CDATA section.

All characters under UCS code point 0x20 will be escaped. Where they have no dedicated escape sequence in JSON, they will be replaced with hexadecimal escape (\uXXXX).


parseNameValuePairList

public static Map parseNameValuePairList(String s,
                                         String defaultValue)
                                  throws ParseException
Parses a name-value pair list, where the pairs are separated with comma, and the name and value is separated with colon. The keys and values can contain only letters, digits and _. They can't be quoted. White-space around the keys and values are ignored. The value can be omitted if defaultValue is not null. When a value is omitted, then the colon after the key must be omitted as well. The same key can't be used for multiple times.

Parameters:
s - the string to parse. For example: "strong:100, soft:900".
defaultValue - the value used when the value is omitted in a key-value pair.
Returns:
the map that contains the name-value pairs.
Throws:
ParseException - if the string is not a valid name-value pair list.

isXMLID

public static boolean isXMLID(String name)
Returns:
whether the name is a valid XML tagname. (This routine might only be 99% accurate. Should maybe REVISIT)

matchesName

public static boolean matchesName(String qname,
                                  String nodeName,
                                  String nsURI,
                                  Environment env)
Returns:
whether the qname matches the combination of nodeName, nsURI, and environment prefix settings.

leftPad

public static String leftPad(String s,
                             int minLength)
Pads the string at the left with spaces until it reaches the desired length. If the string is longer than this length, then it returns the unchanged string.

Parameters:
s - the string that will be padded.
minLength - the length to reach.

leftPad

public static String leftPad(String s,
                             int minLength,
                             char filling)
Pads the string at the left with the specified character until it reaches the desired length. If the string is longer than this length, then it returns the unchanged string.

Parameters:
s - the string that will be padded.
minLength - the length to reach.
filling - the filling pattern.

leftPad

public static String leftPad(String s,
                             int minLength,
                             String filling)
Pads the string at the left with a filling pattern until it reaches the desired length. If the string is longer than this length, then it returns the unchanged string. For example: leftPad('ABC', 9, '1234') returns "123412ABC".

Parameters:
s - the string that will be padded.
minLength - the length to reach.
filling - the filling pattern. Must be at least 1 characters long. Can't be null.

rightPad

public static String rightPad(String s,
                              int minLength)
Pads the string at the right with spaces until it reaches the desired length. If the string is longer than this length, then it returns the unchanged string.

Parameters:
s - the string that will be padded.
minLength - the length to reach.

rightPad

public static String rightPad(String s,
                              int minLength,
                              char filling)
Pads the string at the right with the specified character until it reaches the desired length. If the string is longer than this length, then it returns the unchanged string.

Parameters:
s - the string that will be padded.
minLength - the length to reach.
filling - the filling pattern.

rightPad

public static String rightPad(String s,
                              int minLength,
                              String filling)
Pads the string at the right with a filling pattern until it reaches the desired length. If the string is longer than this length, then it returns the unchanged string. For example: rightPad('ABC', 9, '1234') returns "ABC412341". Note that the filling pattern is started as if you overlay "123412341" with the left-aligned "ABC", so it starts with "4".

Parameters:
s - the string that will be padded.
minLength - the length to reach.
filling - the filling pattern. Must be at least 1 characters long. Can't be null.

versionStringToInt

public static int versionStringToInt(String version)
Converts a version number string to an integer for easy comparison. The version number must start with numbers separated with dots. There can be any number of such dot-separated numbers, but only the first three will be considered. After the numbers arbitrary text can follow, and will be ignored. The string will be trimmed before interpretation.

Returns:
major * 1000000 + minor * 1000 + micro