de.danielnaber.languagetool.tools
Class StringTools

Object
  extended by StringTools

public final class StringTools
extends Object

Tools for reading files etc.

Author:
Daniel Naber

Nested Class Summary
static class StringTools.XmlPrintMode
          Constants for printing XML rule matches.
 
Method Summary
static String addSpace(String word, Language language)
          Adds spaces before words that are not punctuation.
static String asString(CharSequence s)
           
static void assureSet(String s, String varName)
          Throw exception if the given string is null or empty or only whitespace.
static String escapeHTML(String s)
          Escapes these characters: less than, bigger than, quote, ampersand.
static String escapeXML(String s)
          Calls escapeHTML(String).
static String filterXML(String str)
          Simple XML filtering routing
static String getContext(int fromPos, int toPos, String fileContents)
           
static String getContext(int fromPos, int toPos, String contents, int contextSize)
           
static String getLabel(String label)
          Returns translation of the UI element without the control character "&".
static char getMnemonic(String label)
          Returns mnemonic of a UI element.
static String getOOoLabel(String label)
          Returns the UI element string with mnemonics encoded in OpenOffice.org convention (using "~").
static boolean isAllUppercase(String str)
          Returns true if str is made up of all-uppercase characters (ignoring characters for which no upper-/lowercase distinction exists).
static boolean isCapitalizedWord(String str)
           
static boolean isEmpty(String str)
          Helper method to replace calls to "".equals().
static boolean isMixedCase(String str)
           
static boolean isPositiveNumber(char ch)
           
static boolean isWhitespace(String str)
          Checks if a string contains only whitespace, including all Unicode whitespace.
static String listToString(Collection<String> l, String delimiter)
           
static String lowercaseFirstChar(String str)
          Return str modified so that its first character is now an lowercase character.
static String readerToString(Reader reader)
           
static String readFile(InputStream file)
          Read a file's content.
static String readFile(InputStream file, String encoding)
          Read the text file using the given encoding.
static String ruleMatchesToXML(List<RuleMatch> ruleMatches, String text, int contextSize)
          Deprecated. Use #ruleMatchesToXML(List,String,int,XmlPrintMode) instead
static String ruleMatchesToXML(List<RuleMatch> ruleMatches, String text, int contextSize, StringTools.XmlPrintMode xmlMode)
          Get an XML representation of the given rule matches.
static boolean startsWithUppercase(String str)
          Whether the first character of str is an uppercase character.
static String streamToString(InputStream is)
           
static String trimWhitespace(String str)
          Filters any whitespace characters.
static String uppercaseFirstChar(String str)
          Return str modified so that its first character is now an uppercase character.
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

assureSet

public static void assureSet(String s,
                             String varName)
Throw exception if the given string is null or empty or only whitespace.


readFile

public static String readFile(InputStream file)
                       throws IOException
Read a file's content.

Throws:
IOException

readFile

public static String readFile(InputStream file,
                              String encoding)
                       throws IOException
Read the text file using the given encoding.

Parameters:
file - InputStream to a file to be read
encoding - the file's character encoding (e.g. iso-8859-1)
Returns:
a string with the file's content, lines separated by \n
Throws:
IOException

isAllUppercase

public static boolean isAllUppercase(String str)
Returns true if str is made up of all-uppercase characters (ignoring characters for which no upper-/lowercase distinction exists).


isMixedCase

public static boolean isMixedCase(String str)
Parameters:
str - - input str Returns true if str is MixedCase.

isCapitalizedWord

public static boolean isCapitalizedWord(String str)
Parameters:
str - - input string

startsWithUppercase

public static boolean startsWithUppercase(String str)
Whether the first character of str is an uppercase character.


uppercaseFirstChar

public static String uppercaseFirstChar(String str)
Return str modified so that its first character is now an uppercase character. If str starts with non-alphabetic characters, such as quotes or parentheses, the first character is determined as the first alphabetic character.


lowercaseFirstChar

public static String lowercaseFirstChar(String str)
Return str modified so that its first character is now an lowercase character. If str starts with non-alphabetic characters, such as quotes or parentheses, the first character is determined as the first alphabetic character.


readerToString

public static String readerToString(Reader reader)
                             throws IOException
Throws:
IOException

streamToString

public static String streamToString(InputStream is)
                             throws IOException
Throws:
IOException

escapeXML

public static String escapeXML(String s)
Calls escapeHTML(String).


escapeHTML

public static String escapeHTML(String s)
Escapes these characters: less than, bigger than, quote, ampersand.


ruleMatchesToXML

public static String ruleMatchesToXML(List<RuleMatch> ruleMatches,
                                      String text,
                                      int contextSize)
Deprecated. Use #ruleMatchesToXML(List,String,int,XmlPrintMode) instead

Get an XML representation of the given rule matches.

Parameters:
text - the original text that was checked, used to get the context of the matches
contextSize - the desired context size in characters

ruleMatchesToXML

public static String ruleMatchesToXML(List<RuleMatch> ruleMatches,
                                      String text,
                                      int contextSize,
                                      StringTools.XmlPrintMode xmlMode)
Get an XML representation of the given rule matches.

Parameters:
text - the original text that was checked, used to get the context of the matches
contextSize - the desired context size in characters
xmlMode - TODO

listToString

public static String listToString(Collection<String> l,
                                  String delimiter)

getContext

public static String getContext(int fromPos,
                                int toPos,
                                String fileContents)

getContext

public static String getContext(int fromPos,
                                int toPos,
                                String contents,
                                int contextSize)

trimWhitespace

public static String trimWhitespace(String str)
Filters any whitespace characters. Useful for trimming the contents of token elements that cannot possibly contain any spaces.

Parameters:
str - String to be filtered.
Returns:
Filtered string.

addSpace

public static String addSpace(String word,
                              Language language)
Adds spaces before words that are not punctuation.

Parameters:
word - Word to add the preceding space.
language - Language of the word (to check typography conventions). Currently French convention of not adding spaces only before '.' and ',' is implemented; other languages assume that before ,.;:!? no spaces should be added.
Returns:
String containing a space or an empty string.

getLabel

public static String getLabel(String label)
Returns translation of the UI element without the control character "&". To have "&" in the UI, use "&&".

Parameters:
label - Label to convert.
Returns:
String UI element string without mnemonics.

getOOoLabel

public static String getOOoLabel(String label)
Returns the UI element string with mnemonics encoded in OpenOffice.org convention (using "~").

Parameters:
label - Label to convert
Returns:
String UI element with ~ replacing &.

getMnemonic

public static char getMnemonic(String label)
Returns mnemonic of a UI element.

Parameters:
label - String Label of the UI element
Returns:
@char Mnemonic of the UI element, or in case of no mnemonic set.

isWhitespace

public static boolean isWhitespace(String str)
Checks if a string contains only whitespace, including all Unicode whitespace.

Parameters:
str - String to check
Returns:
true if the string is whitespace-only.

isPositiveNumber

public static boolean isPositiveNumber(char ch)
Parameters:
ch - Character to check
Returns:
True if the character is a positive number (decimal digit from 1 to 9).

isEmpty

public static boolean isEmpty(String str)
Helper method to replace calls to "".equals().

Parameters:
str - String to check
Returns:
true if string is empty OR null

filterXML

public static String filterXML(String str)
Simple XML filtering routing

Parameters:
str - XML string to be filtered.
Returns:
Filtered string without XML tags.

asString

public static String asString(CharSequence s)


Copyright © 2005-2009 Daniel Naber