Class Text
- java.lang.Object
-
- com.day.text.Text
-
public final class Text extends java.lang.Object
This class holds a collection of useful string operations that are not available in java.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_DATE_FORMAT_PATTERN
The default format pattern used in strftime() if no pattern parameter has been supplied.static char[]
hexTable
used for the md5static java.lang.String
NON_VALID_CHARS
Non-valid handle characters: / \ : * ? " < > | and carriage return.
(That's slash, backslash, colon, asterisk, question mark, quotation mark, less than symbol, greater than symbol, pipe, and carriage return.)static java.util.TimeZone
TIMEZONE_LOCAL
The local timezonestatic java.util.TimeZone
TIMEZONE_UTC
The UTC timezonestatic java.util.BitSet
URISave
The list of characters that are not encoded by theescape()
andunescape()
methods.static java.util.BitSet
URISaveEx
Same asURISave
but also contains the '/'
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static int
compareHandles(java.lang.String h1, java.lang.String h2)
compares to handles lexigographically with one exception: the '/' character is always considered smaller than all other chars.static java.lang.String
createValidLabel(java.lang.String labelhint)
Create a valid label out of an arbitrary string.static java.lang.String
dateToRfc1123String(java.util.Date date)
Returns a stringified date accoring to the date format specified in RTF1123.static java.lang.String
digest(java.lang.String algorithm, byte[] data)
Digest the plain string using the given algorithm.static java.lang.String
digest(java.lang.String algorithm, java.lang.String data)
Deprecated.static java.lang.String
digest(java.lang.String algorithm, java.lang.String data, java.lang.String enc)
Digest the plain string using the given algorithm.static java.lang.String
escape(java.lang.String string)
Does a URL encoding of thestring
.static java.lang.String
escape(java.lang.String string, char escape)
Does an URL encoding of thestring
using theescape
character.static java.lang.String
escape(java.lang.String string, char escape, boolean isPath)
Does an URL encoding of thestring
using theescape
character.static java.lang.String
escapePath(java.lang.String path)
Does a URL encoding of thepath
.static java.lang.String
escapeXml(java.lang.String string)
Escapes all character that have specific meaning in XMl/HTML, namely '<', '>', '"', '&' by their isolating encoding aequivalents "<", ">", """, "&"static java.lang.String[]
explode(java.lang.String str, int ch)
returns an array of strings decomposed of the original string, split at every occurance of 'ch'.static java.lang.String[]
explode(java.lang.String str, int ch, boolean respectEmpty)
returns an array of strings decomposed of the original string, split at every occurance of 'ch'.static java.lang.String
formatISO8601(java.util.Calendar cal)
Formats aCalendar
value into an ISO8601-compliant date/time string.static java.lang.String
fullFilePath(java.lang.String parent, java.lang.String path)
this method does the similar than thefullPath(String, String)
but it considers the parent as parent directory rather than a base handle.static java.lang.String
fullPath(java.lang.String base, java.lang.String path)
returns a full path.static java.lang.String
getAbsoluteParent(java.lang.String handle, int level)
Returns the nth absolute parent of the handle, where n=level.static java.lang.String
getLabel(java.lang.String handle)
Deprecated.usegetName(String)
static java.lang.String
getLabel(java.lang.String handle, char delim)
Deprecated.static java.lang.String
getLocalName(java.lang.String qname)
Returns the local name of the givenqname
.static java.lang.String
getName(java.lang.String path)
Returns the name part of the pathstatic java.lang.String
getName(java.lang.String path, boolean ignoreTrailingSlash)
Same asgetName(String)
but adding the possibility to pass paths that end with a trailing '/'static java.lang.String
getName(java.lang.String path, char delim)
Returns the name part of the path, delimited by the givendelim
static java.lang.String
getNamespacePrefix(java.lang.String qname)
Returns the namespace prefix of the givenqname
.static java.lang.String
getRelativeParent(java.lang.String handle, int level)
Returns the nth relative parent of the handle, where n=level.static java.lang.String
implode(java.lang.String[] arr, java.lang.String delim)
Concatenates all strings in the string array using the specified delimiter.static boolean
isDescendant(java.lang.String handle, java.lang.String descendant)
Determines if thedescendant
handle is hierarchical a descendant ofhandle
.static boolean
isDescendantOrEqual(java.lang.String handle, java.lang.String descendant)
Determines if thedescendant
handle is hierarchical a descendant ofhandle
or equal to it.static boolean
isSibling(java.lang.String h1, java.lang.String h2)
Determines, if two handles are sister-pages, that meens, if they represent the same hierarchic level and share the same parent page.static java.lang.String
joinFixSlash(java.lang.String first, java.lang.String second)
Deprecated.static java.lang.String
makeCanonicalPath(char[] source)
make a cannonical path.static java.lang.String
makeCanonicalPath(java.lang.String path)
Make a path canonical.static java.lang.String
makeCanonicalPath(java.lang.StringBuffer source)
Deprecated.usemakeCanonicalPath(char[])
insteadstatic java.lang.String
md5(java.lang.String data)
Deprecated.static java.lang.String
md5(java.lang.String data, java.lang.String enc)
Calculate an MD5 hash of the string given.static java.util.Date
parseDate(java.lang.String dateString)
Parses the date string based on the format pattern which is in the default formatdd.MM.yyyy HH:mm:ss
.static java.util.Date
parseDate(java.lang.String dateString, boolean isUTC)
Parses the date string based on the format pattern which is in the default formatdd.MM.yyyy HH:mm:ss
.static java.util.Date
parseDate(java.lang.String dateString, java.lang.String formatPattern)
Parses the date string based on the format pattern which is in the format used by the Java platfrom SimpleDateFormat class.static java.util.Date
parseDate(java.lang.String dateString, java.lang.String formatPattern, boolean isUTC)
Parses the date string based on the format pattern which is in the format used by the Java platfrom SimpleDateFormat class.static java.util.Calendar
parseISO8601(java.lang.String text)
Parses an ISO8601-compliant date/time string.static java.lang.String
replace(java.lang.String text, java.lang.String oldString, java.lang.String newString)
Replaces all occurences ofoldString
intext
withnewString
.static java.lang.String
replaceVariables(java.util.Properties variables, java.lang.String value, boolean ignoreMissing)
Performs variable replacement on the given string value.static java.lang.String[]
split(java.lang.String str, int ch)
Deprecated.static java.lang.String[]
split(java.lang.String str, int ch, boolean respectEmpty)
Deprecated.static java.lang.String
sprintf(java.lang.String format, java.lang.Object a0)
Formats a string according to format and argument.static java.lang.String
sprintf(java.lang.String format, java.lang.Object[] args)
This method implements the famous and ubiquituoussprintf
formatter in Java.static java.lang.String
sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1)
Formats a string according to format and argument.static java.lang.String
sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1, java.lang.Object a2)
Formats a string according to format and argument.static java.lang.String
sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1, java.lang.Object a2, java.lang.Object a3)
Formats a string according to format and argument.static java.lang.String
sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1, java.lang.Object a2, java.lang.Object a3, java.lang.Object a4)
Formats a string according to format and argument.static java.lang.String
strftime(java.util.Date date)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function.static java.lang.String
strftime(java.util.Date date, boolean asUTC)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function.static java.lang.String
strftime(java.util.Date date, java.lang.String formatPattern, boolean asUTC)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function.static java.lang.String
strftime(java.util.Date date, java.lang.String formatPattern, java.util.TimeZone zone)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function.static java.lang.String
unescape(java.lang.String string)
Does a URL decoding of thestring
.static java.lang.String
unescape(java.lang.String string, char escape)
Does a URL decoding of thestring
using theescape
character.static void
validateLabel(java.lang.String labelhint)
Checks if the label is not empty and contains all valid label chars.
-
-
-
Field Detail
-
DEFAULT_DATE_FORMAT_PATTERN
public static final java.lang.String DEFAULT_DATE_FORMAT_PATTERN
The default format pattern used in strftime() if no pattern parameter has been supplied. This is the default format used to format dates in Communiqué 2- See Also:
- Constant Field Values
-
TIMEZONE_UTC
public static final java.util.TimeZone TIMEZONE_UTC
The UTC timezone
-
TIMEZONE_LOCAL
public static final java.util.TimeZone TIMEZONE_LOCAL
The local timezone
-
hexTable
public static final char[] hexTable
used for the md5
-
URISave
public static java.util.BitSet URISave
The list of characters that are not encoded by theescape()
andunescape()
methods. They contains the characters as defined 'unreserved' in section 2.3 of the RFC 2396 'URI genric syntax':unreserved = alphanum | mark mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
-
URISaveEx
public static java.util.BitSet URISaveEx
Same asURISave
but also contains the '/'
-
NON_VALID_CHARS
public static final java.lang.String NON_VALID_CHARS
Non-valid handle characters: / \ : * ? " < > | and carriage return.
(That's slash, backslash, colon, asterisk, question mark, quotation mark, less than symbol, greater than symbol, pipe, and carriage return.)- See Also:
- Constant Field Values
-
-
Method Detail
-
split
public static java.lang.String[] split(java.lang.String str, int ch)
Deprecated.returns an array of strings decomposed of the original string, split at every occurance of 'ch'. if 2 'ch' follow each other with no intermediate characters, empty "" entries are avoided.- Parameters:
str
- the string to decomposech
- the character to use a split pattern- Returns:
- an array of strings
-
split
public static java.lang.String[] split(java.lang.String str, int ch, boolean respectEmpty)
Deprecated.returns an array of strings decomposed of the original string, split at every occurance of 'ch'.- Parameters:
str
- the string to decomposech
- the character to use a split patternrespectEmpty
- iftrue
, empty elements are generated- Returns:
- an array of strings
-
explode
public static java.lang.String[] explode(java.lang.String str, int ch)
returns an array of strings decomposed of the original string, split at every occurance of 'ch'. if 2 'ch' follow each other with no intermediate characters, empty "" entries are avoided.- Parameters:
str
- the string to decomposech
- the character to use a split pattern- Returns:
- an array of strings
-
explode
public static java.lang.String[] explode(java.lang.String str, int ch, boolean respectEmpty)
returns an array of strings decomposed of the original string, split at every occurance of 'ch'.- Parameters:
str
- the string to decomposech
- the character to use a split patternrespectEmpty
- iftrue
, empty elements are generated- Returns:
- an array of strings
-
implode
public static java.lang.String implode(java.lang.String[] arr, java.lang.String delim)
Concatenates all strings in the string array using the specified delimiter.- Parameters:
arr
-delim
-- Returns:
- the concatenated string
-
replace
public static java.lang.String replace(java.lang.String text, java.lang.String oldString, java.lang.String newString)
Replaces all occurences ofoldString
intext
withnewString
.- Parameters:
text
-oldString
- old substring to be replaced withnewString
newString
- new substring to replace occurences ofoldString
- Returns:
- a string
-
getName
public static java.lang.String getName(java.lang.String path)
Returns the name part of the path- Parameters:
path
- the path- Returns:
- the name part
-
getName
public static java.lang.String getName(java.lang.String path, char delim)
Returns the name part of the path, delimited by the givendelim
- Parameters:
path
- the pathdelim
- the delimiter- Returns:
- the name part
-
getName
public static java.lang.String getName(java.lang.String path, boolean ignoreTrailingSlash)
Same asgetName(String)
but adding the possibility to pass paths that end with a trailing '/'- Parameters:
path
- the pathignoreTrailingSlash
- iftrue
a trailing slash is ignored- Returns:
- the name part
- See Also:
getName(String)
-
getNamespacePrefix
public static java.lang.String getNamespacePrefix(java.lang.String qname)
Returns the namespace prefix of the givenqname
. If the prefix is missing, an empty string is returned. Please note, that this method does not validate the name or prefix. the qname has the format: qname := [prefix ':'] local;- Parameters:
qname
- a qualified name- Returns:
- the prefix of the name or "".
- Throws:
java.lang.NullPointerException
- ifqname
isnull
- See Also:
getLocalName(String)
-
getLocalName
public static java.lang.String getLocalName(java.lang.String qname)
Returns the local name of the givenqname
. Please note, that this method does not validate the name. the qname has the format: qname := [prefix ':'] local;- Parameters:
qname
- a qualified name- Returns:
- the localname
- Throws:
java.lang.NullPointerException
- ifqname
isnull
- See Also:
getNamespacePrefix(String)
-
compareHandles
public static int compareHandles(java.lang.String h1, java.lang.String h2)
compares to handles lexigographically with one exception: the '/' character is always considered smaller than all other chars. this results in a ordering, in which the parent pages come first (it's about 6 times slower than the string impl. of compareTo).- example (normal string compare):
- /foo
- /foo-bar
- /foo/bar
- example (this handle compare):
- /foo
- /foo/bar
- /foo-bar
- Parameters:
h1
- the first handleh2
- the second handle- Returns:
- the return is positive, if the first handle is bigger than the second; negative, if the first handle is smaller than the second; and zero, if the two handles are equal.
-
fullFilePath
public static java.lang.String fullFilePath(java.lang.String parent, java.lang.String path)
this method does the similar than thefullPath(String, String)
but it considers the parent as parent directory rather than a base handle. if further respects full qualified uri's.
examples:parent | path | result ----------+----------+------------ "" | "" | / /foo | "" | /foo "" | /foo | /foo "." | foo | foo /foo/bar | bla | /foo/bar/bla /foo/bar | /bla | /bla /foo/bar | ../bla | /foo/bla /foo/bar | ./bla | /foo/bar/bla foo | bar | foo/bar c:/bla | gurk | c:/bla/gurk /foo | c:/bar | c:/bar - Parameters:
parent
- the base handlepath
- the path
-
fullPath
public static java.lang.String fullPath(java.lang.String base, java.lang.String path)
returns a full path. if base is empty, '/' is assumed if base and path are relative, a relative path will be generated.
examples:base | path | result ----------+----------+------------ "" | "" | / /foo | "" | /foo "" | /foo | /foo "." | foo | foo /foo/bar | bla | /foo/bla /foo/bar | /bla | /bla /foo/bar | ../bla | /bla /foo/bar | ./bla | /foo/bla foo | bar | bar
- Parameters:
base
- the base handlepath
- the path
-
makeCanonicalPath
public static java.lang.String makeCanonicalPath(java.lang.String path)
Make a path canonical. This is a shortcut forText.makeCanonicalPath(new StringBuffer(path));
- Parameters:
path
- path to make canonical
-
makeCanonicalPath
public static java.lang.String makeCanonicalPath(java.lang.StringBuffer source)
Deprecated.usemakeCanonicalPath(char[])
insteadmake a cannonical path. removes all /./ and /../ and multiple slashes.- Parameters:
source
- the input source- Returns:
- a string containing the cannonical path
-
makeCanonicalPath
public static java.lang.String makeCanonicalPath(char[] source)
make a cannonical path. removes all /./ and /../ and multiple slashes.- Parameters:
source
- the input source- Returns:
- a string containing the cannonical path
-
isSibling
public static boolean isSibling(java.lang.String h1, java.lang.String h2)
Determines, if two handles are sister-pages, that meens, if they represent the same hierarchic level and share the same parent page.- Parameters:
h1
- first handleh2
- second handle- Returns:
- true if on same level, false otherwise
-
isDescendant
public static boolean isDescendant(java.lang.String handle, java.lang.String descendant)
Determines if thedescendant
handle is hierarchical a descendant ofhandle
./content/playground/en isDescendantOf /content/playground /content/playground/en isDescendantOf /content /content/playground/en isNOTDescendantOf /content/designground /content/playground/en isNOTDescendantOf /content/playground/en - Parameters:
handle
- the current handledescendant
- the potential descendant- Returns:
true
if thedescendant
is a descendant;false
otherwise.- Since:
- gumbear
-
isDescendantOrEqual
public static boolean isDescendantOrEqual(java.lang.String handle, java.lang.String descendant)
Determines if thedescendant
handle is hierarchical a descendant ofhandle
or equal to it./content/playground/en isDescendantOrEqualOf /content/playground /content/playground/en isDescendantOrEqualOf /content /content/playground/en isDescendantOrEqualOf /content/playground/en /content/playground/en isNOTDescendantOrEqualOf /content/designground - Parameters:
handle
- the current handledescendant
- the potential descendant- Returns:
true
if thedescendant
is a descendant or equal;false
otherwise.- Since:
- gumbear
-
getLabel
public static java.lang.String getLabel(java.lang.String handle)
Deprecated.usegetName(String)
Returns the label of a handle- Parameters:
handle
- the handle- Returns:
- the label
-
getLabel
public static java.lang.String getLabel(java.lang.String handle, char delim)
Deprecated.Returns the label of a string- Parameters:
handle
- the stringdelim
- the delimiter- Returns:
- the label
-
md5
public static java.lang.String md5(java.lang.String data, java.lang.String enc) throws java.io.UnsupportedEncodingException
Calculate an MD5 hash of the string given.- Parameters:
data
- the data to encodeenc
- the character encoding to use- Returns:
- a hex encoded string of the md5 digested input
- Throws:
java.io.UnsupportedEncodingException
- if the given encoding is not supported
-
md5
public static java.lang.String md5(java.lang.String data)
Deprecated.Calculate an MD5 hash of the string given.- Parameters:
data
- the data to encode- Returns:
- a hex encoded string of the md5 digested input
-
digest
public static java.lang.String digest(java.lang.String algorithm, java.lang.String data, java.lang.String enc) throws java.security.NoSuchAlgorithmException, java.io.UnsupportedEncodingException
Digest the plain string using the given algorithm.- Parameters:
algorithm
- The alogrithm for the digest. This algorithm must be supported by the MessageDigest class.data
- The plain text String to be digested.enc
- The character encoding to use- Returns:
- The digested plain text String represented as Hex digits.
- Throws:
java.security.NoSuchAlgorithmException
- if the desired algorithm is not supported by the MessageDigest class.java.io.UnsupportedEncodingException
- if the encoding is not supported
-
digest
public static java.lang.String digest(java.lang.String algorithm, java.lang.String data) throws java.security.NoSuchAlgorithmException
Deprecated.Digest the plain string using the given algorithm.- Parameters:
algorithm
- The alogrithm for the digest. This algorithm must be supported by the MessageDigest class.data
- The plain text String to be digested.- Returns:
- The digested plain text String represented as Hex digits.
- Throws:
java.security.NoSuchAlgorithmException
- if the desired algorithm is not supported by the MessageDigest class.
-
digest
public static java.lang.String digest(java.lang.String algorithm, byte[] data) throws java.security.NoSuchAlgorithmException
Digest the plain string using the given algorithm.- Parameters:
algorithm
- The alogrithm for the digest. This algorithm must be supported by the MessageDigest class.data
- the data to digest with the given algorithm- Returns:
- The digested plain text String represented as Hex digits.
- Throws:
java.security.NoSuchAlgorithmException
- if the desired algorithm is not supported by the MessageDigest class.
-
getRelativeParent
public static java.lang.String getRelativeParent(java.lang.String handle, int level)
Returns the nth relative parent of the handle, where n=level.Example:
Text.getRelativeParent("/en/home/index/about", 1) == "/en/home/index"
- Parameters:
handle
- the handle of the pagelevel
- the level of the parent
-
getAbsoluteParent
public static java.lang.String getAbsoluteParent(java.lang.String handle, int level)
Returns the nth absolute parent of the handle, where n=level.Example:
Text.getAbsoluteParent("/en/home/index/about", 1) == "/en/home"
- Parameters:
handle
- the handle of the pagelevel
- the level of the parent
-
escape
public static java.lang.String escape(java.lang.String string, char escape)
Does an URL encoding of thestring
using theescape
character. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI genric syntax' RFC 2396, but without the escape character.- Parameters:
string
- the string to encode.escape
- the escape character.- Returns:
- the escaped string
- Throws:
java.lang.NullPointerException
- ifstring
isnull
.
-
escape
public static java.lang.String escape(java.lang.String string, char escape, boolean isPath)
Does an URL encoding of thestring
using theescape
character. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI genric syntax' RFC 2396, but without the escape character. IfisPath
istrue
, additionally the slash '/' is ignored, too.- Parameters:
string
- the string to encode.escape
- the escape character.isPath
- iftrue
, the string is treated as path- Returns:
- the escaped string
- Throws:
java.lang.NullPointerException
- ifstring
isnull
.
-
escapeXml
public static java.lang.String escapeXml(java.lang.String string)
Escapes all character that have specific meaning in XMl/HTML, namely '<', '>', '"', '&' by their isolating encoding aequivalents "<", ">", """, "&"- Parameters:
string
- the string to escape- Returns:
- the escaped string or
null
if the input string wasnull
-
escape
public static java.lang.String escape(java.lang.String string)
Does a URL encoding of thestring
. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI genric syntax' RFC 2396.- Parameters:
string
- the string to encode- Returns:
- the escaped string
- Throws:
java.lang.NullPointerException
- ifstring
isnull
.
-
escapePath
public static java.lang.String escapePath(java.lang.String path)
Does a URL encoding of thepath
. The characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI genric syntax' RFC 2396. In contrast to theescape(String)
method, not the entire path string is escaped, but every individual part (i.e. the slashes are not escaped).- Parameters:
path
- the path to encode- Returns:
- the escaped path
- Throws:
java.lang.NullPointerException
- ifpath
isnull
.
-
unescape
public static java.lang.String unescape(java.lang.String string, char escape)
Does a URL decoding of thestring
using theescape
character. Please note that in opposite to theURLDecoder
it does not transform the + into spaces.- Parameters:
string
- the string to decodeescape
- the escape character- Returns:
- the decoded string
- Throws:
java.lang.NullPointerException
- ifstring
isnull
.java.lang.ArrayIndexOutOfBoundsException
- if not enough character follow an escape characterjava.lang.IllegalArgumentException
- if the 2 characters following the escape character do not represent a hex-number.
-
unescape
public static java.lang.String unescape(java.lang.String string)
Does a URL decoding of thestring
. Please note that in opposite to theURLDecoder
it does not transform the + into spaces.- Parameters:
string
- the string to decode- Returns:
- the decoded string
- Throws:
java.lang.NullPointerException
- ifstring
isnull
.java.lang.ArrayIndexOutOfBoundsException
- if not enough character follow an escape characterjava.lang.IllegalArgumentException
- if the 2 characters following the escape character do not represent a hex-number.
-
dateToRfc1123String
public static java.lang.String dateToRfc1123String(java.util.Date date)
Returns a stringified date accoring to the date format specified in RTF1123. this is of the form: "Sun, 06 Nov 1994 08:49:37 GMT"
-
strftime
public static java.lang.String strftime(java.util.Date date, java.lang.String formatPattern, java.util.TimeZone zone)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function.- Parameters:
date
- The date value to be formattedformatPattern
- The pattern used to format the date. This pattern supports a subset of the pattern characters of the POSIXstrftime()
function. If this pattern is empty ornull
the default patterndd.MM.yyyy HH:mm:ss
is used.zone
- Defines for which time zone the date should be outputted. If this parameter isnull
, then the local time zone is taken.- Returns:
- the formatted date as a String.
-
strftime
public static java.lang.String strftime(java.util.Date date, java.lang.String formatPattern, boolean asUTC)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function.- Parameters:
date
- The date value to be formattedformatPattern
- The pattern used to format the date. This pattern supports a subset of the pattern characters of the POSIXstrftime()
function. If this pattern is empty ornull
the default patterndd.MM.yyyy HH:mm:ss
is used.asUTC
- Defines whether to interpret the date as belong to the UTC time zone or the local time zone.
-
strftime
public static java.lang.String strftime(java.util.Date date, boolean asUTC)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function. The default patterndd.MM.yyyy HH:mm:ss
is used to format the date.- Parameters:
date
- The date value to be formattedasUTC
- Defines whether to interpret the date as belong to the UTC time zone or the local time zone.
-
strftime
public static java.lang.String strftime(java.util.Date date)
Implements a date formatting routine supporting (a subset) of the POSIXstrftime()
function. The default patterndd.MM.yyyy HH:mm:ss
is used to format the date, which is interpreted to be in the local time zone.- Parameters:
date
- The date value to be formatted
-
parseDate
public static java.util.Date parseDate(java.lang.String dateString, java.lang.String formatPattern) throws java.text.ParseException
Parses the date string based on the format pattern which is in the format used by the Java platfrom SimpleDateFormat class.- Parameters:
dateString
- The date string to be parsedformatPattern
- the pattern to use for parsing. Ifnull
or empty, the same default pattern is used as withstrftime(Date, String, boolean)
, namelydd.MM.yyyy HH:mm:ss
.- Throws:
java.text.ParseException
- if the date string cannot be parsed accordinung to the format pattern.
-
parseDate
public static java.util.Date parseDate(java.lang.String dateString, java.lang.String formatPattern, boolean isUTC) throws java.text.ParseException
Parses the date string based on the format pattern which is in the format used by the Java platfrom SimpleDateFormat class.- Parameters:
dateString
- The date string to be parsedformatPattern
- the pattern to use for parsing. Ifnull
or empty, the same default pattern is used as withstrftime(Date, String, boolean)
, namelydd.MM.yyyy HH:mm:ss
.isUTC
- iftrue
the date string is considered in UTC, otherwise the default timezone of the host is used.- Throws:
java.text.ParseException
- if the date string cannot be parsed accordinung to the format pattern.
-
parseDate
public static java.util.Date parseDate(java.lang.String dateString) throws java.text.ParseException
Parses the date string based on the format pattern which is in the default formatdd.MM.yyyy HH:mm:ss
.- Parameters:
dateString
- The date string to be parsed- Throws:
java.text.ParseException
- if the date string cannot be parsed accordinung to the format pattern.
-
parseDate
public static java.util.Date parseDate(java.lang.String dateString, boolean isUTC) throws java.text.ParseException
Parses the date string based on the format pattern which is in the default formatdd.MM.yyyy HH:mm:ss
.- Parameters:
dateString
- The date string to be parsedisUTC
- iftrue
the date string is considered in UTC, otherwise the default timezone of the host is used.- Throws:
java.text.ParseException
- if the date string cannot be parsed accordinung to the format pattern.
-
sprintf
public static java.lang.String sprintf(java.lang.String format, java.lang.Object[] args)
This method implements the famous and ubiquituoussprintf
formatter in Java. The arguments to the method are the formatting string and the list of arguments defined by the formatting instructions contained in the formatting string.Each element in the argument array is either a
Number
object or any otherObject
type. Whenever the formatting string stipulates the corresponding argument to be numeric, it is assumed this array element to be aNumber
. If this is not the case anIllegalArgumentException
is thrown. If aString
argument is stipulated by the formatting string, a simple call to thetoString()
method of the object yields theString
required.SPECIFICATION
sprintf
accepts a series of arguments, applies to each a format specifier fromformat
, and stores the formatted data to the resultStrint
. The method throws anIllegalArgumentException
if either theformat
is incorrect, there are not enough arguments for theformat
or if any of the argument's type is wrong.sprintf
returns when it reaches the end of the format string. If there are more arguments than the format requires, excess arguments are ignored.format
is aString
containing two types of objects: ordinary characters (other than%
), which are copied unchanged to the output, and conversion specifications, each of which is introduced by%
. (To include%
in the output, use%%
in the format string.) A conversion specification has the following form:[ flags ] [ width ] [ "." prec ] [ size ] type
The fields of the conversion specification have the following meanings:
- flags
- an optional sequence of characters which control output
justification, numeric signs, decimal points, trailing zeroes, and
octal and hex prefixes. The flag characters are minus (
-
), plus (+
), space ("0
), and sharp (#
). They can appear in any combination.-
The result of the conversion is left justified, and the right is padded with blanks. If you do not use this flag, the result is right justified, and padded on the left. +
The result of a signed conversion (as determined by type) will always begin with a plus or minus sign. (If you do not use this flag, positive values do not begin with a plus sign.) " If the first character of a signed conversion specification is not a sign, or if a signed conversion results in no characters, the result will begin with a space. If the space ( +
) flag both appear, the space flag is ignored.0
If the type is d
,i
,o
,u
,x
,X
,e
,E
,f
,g
, orG
: leading zeroes, are used to pad the field width (following any indication of sign or base); no spaces are used for padding. If the zero (0
) and minus (-
) flags both appear, the zero (0
) flag will be ignored. Ford
,i
,o
,u
,x
,X
conversions, if a precision prec is specified, the zero (0
) flag is ignored.
Note that 0 is interpreted as a flag, not as the beginning of a field width.#
The result is to be converted to an alternative form, according to the type character: o
- Increases precision to force the first digit of the result to be a zero.
x
X
e
,E
, orf
g
orG
- all others
- Undefined.
- A non-zero result will have a
0x
prefix.- A non-zero result will have a
0X
prefix.- The result will always contain a decimal point even if no
digits follow the point. (Normally, a decimal point appears
only if a digit follows it.) Trailing zeroes are removed.
- Same as
e
orE
, but trailing zeroes arenot removed. - width
- width is an optional minimum field width. You can either
specify it directly as a decimal integer, or indirectly by using instead
an asterisk (
*
), in which case an integral numeric argument is used as the field width. Negative field widths are not supported; if you attempt to specify a negative field width, it is interpreted as a minus (i
) flag followed by a positive field width. - prec
- an optional field; if present, it is introduced with
`
.
' (a period). This field gives the maximum number of characters to print in a conversion; the minimum number of digits of an integer to print, for conversions with typed
,i
,o
,u
,x
, andX
; the maximum number of significant digits, for theg
andG
conversions; or the number of digits to print after the decimal point, fore
,E
, andf
conversions. You can specify the precision either directly as a decimal integer or indirectly by using an asterisk (*
), in which case an integral numeric argument is used as the precision. Supplying a negative precision is equivalent to omitting the precision. If only a period is specified the precision is zero. If a precision appears with any other conversion type than those listed here, the behavior is undefined. - size
h
,l
, andL
are optional size characters which override the default way thatsprintf
interprets the data type of the corresponding argument.h
forces the followingd
,i
,o
,u
,x
, orX
conversion type to apply to ashort
orunsigned short
. Similarily, anl
forces the followingd
,i
,o
,u
,x
, orX
conversion type to apply to along
orunsigned long
. If anh
or anl
appears with another conversion specifier, the behavior is undefined.L
forces a followinge
,E
,f
,g
, orG
conversion type to apply to along double
argument. IfL
appears with any other conversion type, the behavior is undefined.- type
- type specifies what kind of conversion
sprintf
performs. Here is a table of these:%
- prints the percent character (
%
) c
- prints arg as single character. That is the argument is
converted to a
String
of which only the first character is printed. s
- Prints characters until precision is reached or the
String
ends; takes anyObject
whosetoString()
method is called to get theString
to print. d
- prints a signed decimal integer; takes a
Number
(same asi
) d
- prints a signed decimal integer; takes a
Number
(same asd
) d
- prints a signed octal integer; takes a
Number
u
- prints a unsigned decimal integer; takes a
Number
. This conversion is not supported correctly by the Java implementation and is really the same asi
. x
- prints an unsigned hexadecimal integer (using abcdef as
digits beyond 9; takes a
Number
X
- prints an unsigned hexadecimal integer (using ABCDEF as
digits beyond 9; takes a
Number
f
- prints a signed value of the form [-]9999.9999; takes a
Number
e
- prints a signed value of the form [-]9.9999e[+|-]999; takes
a
Number
E
- prints the same way as
e
, but using E to introduce the exponent; takes aNumber
g
- prints a signed value in either
f
ore
form, based on given value and precision &emdash; trailing zeros and the decimal point are printed only if necessary; takes aNumber
G
- prints the same way as
g
, but usingE
for the exponent if an exponent is needed; takes aNumber
n
- Not supported in the Java implementation, throws an
IllegalArgumentException
if used. p
- Not supported in the Java implementation, throws an
IllegalArgumentException
if used.
IMPLEMENTATION NOTES
Due to the nature of the Java programming language, neither pointer conversions, nor
unsigned
andlong double
conversions are supported.Also the Java implementation only distinguishes between
Number
and otherObject
arguments. If a numeric argument is expected as per theformat
String
, the current argument must be aNumber
object that is converted to a basic type as expected. If aString
orchar
argument is expected, any Object is valid, whosetoString()
method is used to convert to aString
.- Parameters:
format
- The format string as known from the POSIX sprintf() C function.args
- The list of arguments to accomodate the format string. This argument list is supposed to contain at least as much entries as there are formatting options in the format string. If for a numeric option, the entry is not a number anIllegalArgumentException
is thrown.- Returns:
- The formatted
String
. An emptyString
is only returned if theformat
String
is empty. Anull
value is never returned. - Throws:
java.lang.NullPointerException
- if the formatting string or any of the argument values isnull
.java.lang.IllegalArgumentException
- if the formatting string has wrong format tags, if the formatting string has an incomplete formatting pattern at the end of the string, if the argument vector has not enough values to satisfy the formatting string or if an argument's type does not match the requirements of the format string.
-
sprintf
public static java.lang.String sprintf(java.lang.String format, java.lang.Object a0)
Formats a string according to format and argument. This method only supports string formats. Seesprintf(String, Object[])
for details.- Parameters:
format
- The format stringa0
- The single parameter- Returns:
- the result from
sprintf(format, new Object[]{ a0 })
. - Throws:
java.lang.IllegalArgumentException
- fromsprintf(String, Object[])
.
-
sprintf
public static java.lang.String sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1)
Formats a string according to format and argument. This method only supports string formats. Seesprintf(String, Object[])
for details.- Parameters:
format
- The format stringa0
- The first parametera1
- The second parameter- Returns:
- the result from
sprintf(format, new Object[]{ ... })
. - Throws:
java.lang.IllegalArgumentException
- fromsprintf(String, Object[])
.
-
sprintf
public static java.lang.String sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1, java.lang.Object a2)
Formats a string according to format and argument. This method only supports string formats. Seesprintf(String, Object[])
for details.- Parameters:
format
- The format stringa0
- The first parametera1
- The second parametera2
- The thrid parameter- Returns:
- the result from
sprintf(format, new Object[]{ ... })
. - Throws:
java.lang.IllegalArgumentException
- fromsprintf(String, Object[])
.
-
sprintf
public static java.lang.String sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1, java.lang.Object a2, java.lang.Object a3)
Formats a string according to format and argument. This method only supports string formats. Seesprintf(String, Object[])
for details.- Parameters:
format
- The format stringa0
- The first parametera1
- The second parametera2
- The thrid parametera3
- The fourth parameter- Returns:
- the result from
sprintf(format, new Object[]{ ... })
. - Throws:
java.lang.IllegalArgumentException
- fromsprintf(String, Object[])
.
-
sprintf
public static java.lang.String sprintf(java.lang.String format, java.lang.Object a0, java.lang.Object a1, java.lang.Object a2, java.lang.Object a3, java.lang.Object a4)
Formats a string according to format and argument. This method only supports string formats. Seesprintf(String, Object[])
for details.- Parameters:
format
- The format stringa0
- The first parametera1
- The second parametera2
- The thrid parametera3
- The fourth parametera4
- The fifth parameter- Returns:
- the result from
sprintf(format, new Object[]{ ... })
. - Throws:
java.lang.IllegalArgumentException
- fromsprintf(String, Object[])
.
-
createValidLabel
public static java.lang.String createValidLabel(java.lang.String labelhint)
Create a valid label out of an arbitrary string.- Parameters:
labelhint
- the given label string that we will examine and convert any illegal character if necessary- Returns:
- a valid label string
-
validateLabel
public static void validateLabel(java.lang.String labelhint)
Checks if the label is not empty and contains all valid label chars.- Parameters:
labelhint
- the labelhint to check- Throws:
java.lang.IllegalArgumentException
- if the label does not contain all valid chars.
-
joinFixSlash
public static java.lang.String joinFixSlash(java.lang.String first, java.lang.String second)
Deprecated.Join two paths or handles, fixing the slashes. All these will go to "/a/b": /a/ + /b/ (trailing slashes are removed) /a + /b /a/ + b /a/ + b/ /a// + /////b (i am paranoid) Note that leading slashes are untouched, so variations on the above without the leading slash on 'a' would go to "a/b".
-
replaceVariables
public static java.lang.String replaceVariables(java.util.Properties variables, java.lang.String value, boolean ignoreMissing) throws java.lang.IllegalArgumentException
Performs variable replacement on the given string value. Each${...}
sequence within the given value is replaced with the value of the named parser variable. If a variable is not found in the properties an IllegalArgumentException is thrown unlessignoreMissing
istrue
. In the later case, the missing variable is replaced by the empty string.- Parameters:
value
- the original valueignoreMissing
- iftrue
, missing variables are replaced by the empty string.- Returns:
- value after variable replacements
- Throws:
java.lang.IllegalArgumentException
- if the replacement of a referenced variable is not found
-
parseISO8601
public static java.util.Calendar parseISO8601(java.lang.String text)
Parses an ISO8601-compliant date/time string. (see ISO 8601). The currently supported format is:±YYYY-MM-DDThh:mm:ss.SSSTZD
where:±YYYY = four-digit year with optional sign where values <= 0 are denoting years BCE and values > 0 are denoting years CE, e.g. -0001 denotes the year 2 BCE, 0000 denotes the year 1 BCE, 0001 denotes the year 1 CE, and so on... MM = two-digit month (01=January, etc.) DD = two-digit day of month (01 through 31) hh = two digits of hour (00 through 23) (am/pm NOT allowed) mm = two digits of minute (00 through 59) ss = two digits of second (00 through 59) SSS = three digits of milliseconds (000 through 999) TZD = time zone designator, Z for Zulu (i.e. UTC) or an offset from UTC in the form of +hh:mm or -hh:mm
- Parameters:
text
- the date/time string to be parsed- Returns:
- a
Calendar
, ornull
if the input could not be parsed - Throws:
java.lang.IllegalArgumentException
- if anull
argument is passed
-
formatISO8601
public static java.lang.String formatISO8601(java.util.Calendar cal)
Formats aCalendar
value into an ISO8601-compliant date/time string.- Parameters:
cal
- the time value to be formatted into a date/time string.- Returns:
- the formatted date/time string.
- Throws:
java.lang.IllegalArgumentException
- if anull
argument is passed
-
-