de.danielnaber.languagetool.tools
Class StringTools

Object
  extended by StringTools

public final class StringTools
extends Object

Tools for reading files etc.

Author:
Daniel Naber

Method Summary
static String addSpace(String word, Language language)
          Adds spaces before words that are not punctuation.
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 getContext(int fromPos, int toPos, String fileContents)
           
static String getContext(int fromPos, int toPos, String fileContents, 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 isWhitespace(String str)
          Checks if a string contains only whitespace, including all Unicode whitespace.
static String listToString(List<String> l, String delimiter)
           
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)
          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).


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.


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)
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

listToString

public static String listToString(List<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 fileContents,
                                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 space before.
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.


Copyright © 2005-2007 Daniel Naber