Package com.globalmentor.text
Class Text
java.lang.Object
com.globalmentor.text.Text
Constants and utilities for text.
- Author:
- Garret Wilson
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The string representing the CR EOL character sequence.static final String
The string representing the CRLF EOL sequence.static final Pattern
The pattern that can split a line based upon linefeeds.static final String
The string representing the LF EOL character sequence.static final String
The name extension for text list files.static final MediaType
The media type for plain text:text/plain
.static final String
The MIME subtype oftext/plain
.static final String
The name extension for text files. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
Compares two strings for order in ascending order using the specified collator.static int
Compares two strings for order using the specified collator with the specified sort order.static final String
createControlString
(String string) Creates a control string according to ECMA-48, "Control Functions for Coded Character Sets", Section 5.6, "Control strings".static boolean
Determines if the given media type is one representing text in some form.static CharSequence
normalizeEol
(CharSequence charSequence) Normalizes end-of-line sequences in the character sequence toLF
.static CharSequence
normalizeEol
(CharSequence charSequence, CharSequence normalEol) Normalizes end-of-line sequences in the character sequence to a given sequence.static String
Re-encodes the given string to the new encoding (such as UTF-8), assuming the string was encoded from an array of bytes using the old encoding (e.g.
-
Field Details
-
PLAIN_SUBTYPE
The MIME subtype oftext/plain
.- See Also:
-
PLAIN_MEDIA_TYPE
The media type for plain text:text/plain
. -
LST_FILENAME_EXTENSION
The name extension for text list files. -
TXT_FILENAME_EXTENSION
The name extension for text files.- See Also:
-
CARRIAGE_RETURN_STRING
The string representing the CR EOL character sequence.- See Also:
-
LINE_FEED_STRING
The string representing the LF EOL character sequence.- See Also:
-
LINE_FEED_PATTERN
The pattern that can split a line based upon linefeeds.- See Also:
-
CRLF_STRING
The string representing the CRLF EOL sequence.
-
-
Constructor Details
-
Text
public Text()
-
-
Method Details
-
compare
Compares two strings for order in ascending order using the specified collator. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical strings are always considered equal. This method functions exactly as if the two strings were compared usingCollator.compare(String, String)
, except:- Identical strings are recognized as such without delegating to the actual
Collator.compare(String, String)
method. - This method allows
null
arguments, considering anull
string to be lower than a non-null
string.
Collator.compare(String, String)
, except that this method allowsnull
arguments.- Parameters:
string1
- The first string to be compared, ornull
if the string is not available.string2
- The second string to be compared, ornull
if the string is not available.collator
- The collator used to perform comparisons.- Returns:
- A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
- Throws:
NullPointerException
- if the given collator isnull
.ClassCastException
- if the arguments' types prevent them from being compared.- See Also:
- Identical strings are recognized as such without delegating to the actual
-
compare
Compares two strings for order using the specified collator with the specified sort order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical strings are always considered equal. This method functions exactly as if the two strings were compared usingCollator.compare(String, String)
, except:- Identical strings are recognized as such without delegating to the actual
Collator.compare(String, String)
method. - This method allows
null
arguments, considering anull
string to be lower than a non-null
string.
- Parameters:
string1
- The first string to be compared, ornull
if the string is not available.string2
- The second string to be compared, ornull
if the string is not available.collator
- The collator used to perform comparisons.sortOrder
- The order in which to perform comparisons.- Returns:
- A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
- Throws:
NullPointerException
- if the given collator and/or sort order isnull
.ClassCastException
- if the arguments' types prevent them from being compared.- See Also:
- Identical strings are recognized as such without delegating to the actual
-
createControlString
Creates a control string according to ECMA-48, "Control Functions for Coded Character Sets", Section 5.6, "Control strings". A control string begins with the Start of String control character (U+0098) and ends with a String Terminator control character (U+009C). ECMA-48 publication is also approved as ISO/IEC 6429.- Parameters:
string
- The string from which a control string will be created.- Returns:
- An ECMA-48 control string with the given string as its content.
- Throws:
NullPointerException
- if the given string isnull
.- See Also:
-
isText
Determines if the given media type is one representing text in some form.Text media types include:
text/*
application/xml
application/*+xml
application/xml-external-parsed-entity
application/*+xml-external-parsed-entity
(not formally defined)
- Parameters:
mediaType
- The media type of a resource, ornull
for no media type.- Returns:
true
if the given media type is one of several text media types.
-
recode
public static String recode(String string, String oldEncoding, String newEncoding) throws UnsupportedEncodingException Re-encodes the given string to the new encoding (such as UTF-8), assuming the string was encoded from an array of bytes using the old encoding (e.g. ISO-8859-1).- Parameters:
string
- The string to recode.oldEncoding
- The encoding used to create the string originally.newEncoding
- The new encoding to use when creating the string.- Returns:
- The a string created from encoding the characters in the specified new encoding.
- Throws:
UnsupportedEncodingException
- Thrown if either the old encoding or the new encoding is not supported.
-
normalizeEol
Normalizes end-of-line sequences in the character sequence toLF
. The following sequences are normalized:CR
LF
CRLF
- Implementation Specification:
- This implementation delegates to
normalizeEol(CharSequence, CharSequence)
, This implementation recognizes an input sequence that require no change, and simply returns that character sequence unchanged. - Parameters:
charSequence
- The character sequence to normalize.- Returns:
- A character sequence, which may be mutable, with the ends of lines normalized to
LF
. - Throws:
NullPointerException
- if the given character sequence and/or EOL characters isnull
.- See Also:
-
normalizeEol
public static CharSequence normalizeEol(@Nonnull CharSequence charSequence, @Nonnull CharSequence normalEol) Normalizes end-of-line sequences in the character sequence to a given sequence. The following sequences are normalized to the provided EOL:CR
LF
CRLF
- Implementation Specification:
- This implementation recognizes an input sequence that require no change if the normalization EOL sequence is a single character that is not
CR
, and simply returns that character sequence unchanged. - Parameters:
charSequence
- The character sequence to normalize.normalEol
- The end of line characters to which to normalize the ends of lines.- Returns:
- A character sequence, which may be mutable, with the ends of lines normalized to the given end-of-line character sequence.
- Throws:
NullPointerException
- if the given character sequence and/or EOL characters isnull
.
-