com.atlassian.bamboo.util
Class BambooStringUtils

java.lang.Object
  extended by com.atlassian.bamboo.util.BambooStringUtils

public class BambooStringUtils
extends java.lang.Object


Field Summary
static char CHAR_APOSTROPHE
           
static char CHAR_BACKSLASH
           
static char CHAR_DOUBLE_QUOTE
           
static char CHAR_ELLIPSIS
           
static char CHAR_NONE
           
static char[] XSS_RELATED_CHARACTERS
           
static java.lang.String XSS_RELATED_CHARACTERS_FIND_STR
           
 
Method Summary
static boolean containsDefinedXssRelatedCharacters(java.lang.String string, char[] characters)
           
static boolean containsRelaxedXssRelatedCharacters(java.lang.String string)
           
static com.google.common.base.Predicate<java.lang.String> containsString(java.lang.String string)
           
static boolean containsXssRelatedCharacters(java.lang.String string)
          Returns true if the supplied string contains characters that could be used to deface a page or trigger an XSS exploit (characters: "&'<>\).
static java.lang.String dos2unix(java.lang.String s)
          Changes Dos-style line endings to Unix style line endings
static java.lang.String encodeHtmlWithTagWhiteList(java.lang.String string)
          Escapes string but preserves few defined tags.
static com.google.common.base.Function<java.lang.String,java.lang.String> encodeHtmlWithTagWhiteListFunction()
           
static java.lang.String forceAlphaNumeric(java.lang.String str)
           
static java.lang.String forceValidHtmlId(java.lang.String id)
          According to the HTML standard, ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
static java.lang.String getFirstDefinedXssRelatedCharacter(java.lang.String string, char[] characters)
           
static java.lang.String getFirstRelaxedXssRelatedCharacter(java.lang.String string)
           
static java.lang.String getFirstXssRelatedCharacter(java.lang.String string)
          Retrieves first unsafe character from supplied string
static boolean in(java.lang.String needle, java.lang.String... haystacks)
           
static int indexOf(java.lang.String haystack, java.lang.String needle, int n)
          Returns n-th occurence of string (index is 0-based)
static int indexOfRegex(java.lang.String haystack, java.lang.String needle, int occurence)
          Returns n-th occurence of regex (index is 0-based)
static java.lang.Iterable<java.lang.String> lineIterator(java.lang.String string)
           
static java.lang.String remove(java.lang.String quotedVarValue, int index)
          Removes a character at specified index from string
static boolean startsWithAny(java.lang.String string, java.util.Collection<java.lang.String> searchStrings)
           
static com.google.common.base.Function<java.lang.String,java.lang.String> stripFullyEnclosingQuotes()
           
static java.lang.String toCamelCase(java.lang.String string, char separator)
          Returns the string in camel case.
static java.lang.String toCommandLineString(java.lang.Iterable<java.lang.String> commandArgs)
          Joins supplied argument into a valid command line, wrapping arguments in single quotes if needed.
static java.lang.String truncateAndAddEllipsis(java.lang.String string, int maxLength)
          Truncates a string to specified length.
static java.lang.String unquote(java.lang.String quotedVarValue, boolean processBackslashes)
          Removes quotes from variable using shell rules.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHAR_NONE

public static final char CHAR_NONE
See Also:
Constant Field Values

CHAR_APOSTROPHE

public static final char CHAR_APOSTROPHE
See Also:
Constant Field Values

CHAR_BACKSLASH

public static final char CHAR_BACKSLASH
See Also:
Constant Field Values

CHAR_DOUBLE_QUOTE

public static final char CHAR_DOUBLE_QUOTE
See Also:
Constant Field Values

CHAR_ELLIPSIS

public static final char CHAR_ELLIPSIS
See Also:
Constant Field Values

XSS_RELATED_CHARACTERS

public static final char[] XSS_RELATED_CHARACTERS

XSS_RELATED_CHARACTERS_FIND_STR

public static final java.lang.String XSS_RELATED_CHARACTERS_FIND_STR
See Also:
Constant Field Values
Method Detail

forceAlphaNumeric

@NotNull
public static java.lang.String forceAlphaNumeric(@Nullable
                                                         java.lang.String str)

containsXssRelatedCharacters

public static boolean containsXssRelatedCharacters(@Nullable
                                                   java.lang.String string)
Returns true if the supplied string contains characters that could be used to deface a page or trigger an XSS exploit (characters: "&'<>\). Should be used in addition to normal escaping of output, not instead of it.

Parameters:
string - string to check
Returns:
true if string contains unsafe characters, false otherwise

getFirstXssRelatedCharacter

@NotNull
public static java.lang.String getFirstXssRelatedCharacter(@Nullable
                                                                   java.lang.String string)
Retrieves first unsafe character from supplied string

Parameters:
string - the string to check
Returns:
string with the first encountered unsafe character.

containsRelaxedXssRelatedCharacters

public static boolean containsRelaxedXssRelatedCharacters(@Nullable
                                                          java.lang.String string)

getFirstRelaxedXssRelatedCharacter

@NotNull
public static java.lang.String getFirstRelaxedXssRelatedCharacter(@Nullable
                                                                          java.lang.String string)

containsDefinedXssRelatedCharacters

public static boolean containsDefinedXssRelatedCharacters(@Nullable
                                                          java.lang.String string,
                                                          @NotNull
                                                          char[] characters)

getFirstDefinedXssRelatedCharacter

@NotNull
public static java.lang.String getFirstDefinedXssRelatedCharacter(@Nullable
                                                                          java.lang.String string,
                                                                          @NotNull
                                                                          char[] characters)

forceValidHtmlId

@NotNull
public static java.lang.String forceValidHtmlId(@NotNull
                                                        java.lang.String id)
According to the HTML standard, ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). This method replaces all invalid characters in the supplied id with an underscore. It does not enforce the "must begin with a letter" rule. There are two functions that do it, one in FreeMarker functions.ftl and one in BambooStringUtils. They MUST be kept in sync.


lineIterator

public static java.lang.Iterable<java.lang.String> lineIterator(java.lang.String string)
Returns:
an iterable that makes it possible to iterate over lines with Windows or Unix endings

containsString

public static com.google.common.base.Predicate<java.lang.String> containsString(@NotNull
                                                                                java.lang.String string)

indexOf

public static int indexOf(java.lang.String haystack,
                          java.lang.String needle,
                          int n)
Returns n-th occurence of string (index is 0-based)

Parameters:
haystack - search here
needle - search for
n - n-the cocurence to return
Returns:
-1 if not found, else the index

indexOfRegex

public static int indexOfRegex(java.lang.String haystack,
                               java.lang.String needle,
                               int occurence)
Returns n-th occurence of regex (index is 0-based)

Parameters:
haystack - search here
needle - search for
occurence - n-the occurence to return
Returns:
-1 if not found, else the index

startsWithAny

public static boolean startsWithAny(java.lang.String string,
                                    java.util.Collection<java.lang.String> searchStrings)
See Also:
StringUtils.startsWithAny(String, String[])

encodeHtmlWithTagWhiteList

public static java.lang.String encodeHtmlWithTagWhiteList(java.lang.String string)
Escapes string but preserves few defined tags. Allows some html formatting inside strings while preventing XSS. Allowed tags are: <br /> <i> <b> <p> <div> <a>. Properties are not allowed except 'href' in anchor tag.

Parameters:
string -
Returns:
html escaped string with white listed tags preserved

encodeHtmlWithTagWhiteListFunction

public static com.google.common.base.Function<java.lang.String,java.lang.String> encodeHtmlWithTagWhiteListFunction()

unquote

@NotNull
public static java.lang.String unquote(@NotNull
                                               java.lang.String quotedVarValue,
                                               boolean processBackslashes)
Removes quotes from variable using shell rules.

Parameters:
quotedVarValue -
processBackslashes - whether backslash should be treated as a special character or not
Returns:

in

public static boolean in(java.lang.String needle,
                         java.lang.String... haystacks)

remove

@NotNull
public static java.lang.String remove(@NotNull
                                              java.lang.String quotedVarValue,
                                              int index)
Removes a character at specified index from string

Parameters:
quotedVarValue -
index -
Returns:

truncateAndAddEllipsis

@Nullable
public static java.lang.String truncateAndAddEllipsis(@Nullable
                                                               java.lang.String string,
                                                               int maxLength)
Truncates a string to specified length. If truncation causes actual information loss an ellipsis is appended to the end of the string.

Parameters:
string - string
maxLength - maximum length of the string
Returns:
truncated string

stripFullyEnclosingQuotes

public static com.google.common.base.Function<java.lang.String,java.lang.String> stripFullyEnclosingQuotes()

dos2unix

@NotNull
public static java.lang.String dos2unix(@NotNull
                                                java.lang.String s)
Changes Dos-style line endings to Unix style line endings


toCamelCase

public static java.lang.String toCamelCase(@NotNull
                                           java.lang.String string,
                                           char separator)
Returns the string in camel case. Com.atlassian.bamboo.somePlugin becomes comAtlassianBambooSomePlugin

Parameters:
string -
separator -
Returns:

toCommandLineString

public static java.lang.String toCommandLineString(@NotNull
                                                   java.lang.Iterable<java.lang.String> commandArgs)
Joins supplied argument into a valid command line, wrapping arguments in single quotes if needed.



Copyright © 2012 Atlassian. All Rights Reserved.