Difference between revisions of "TextUtils"
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | Class FQN: '''it.greenvulcano.util.txt''' | + | Class FQN: '''it.greenvulcano.util.txt.TextUtils''' |
− | == | + | ==TextUtils public static methods== |
− | + | <syntaxhighlight lang="java5"> | |
− | <syntaxhighlight lang="java5"> | + | /** |
− | + | * Compiles pattern as a regular expression then checks if exists a match in text. | |
− | + | * | |
− | + | * @param pattern | |
− | + | * @param text | |
− | + | * @return true if the pattern match the text | |
− | + | */ | |
− | + | public static boolean matches(String pattern, String text) | |
− | |||
− | |||
− | + | /** | |
− | + | * Replaces within input string all the occurrences of the | |
− | + | * substring toBeReplaced with occurrences of the substring replacement. | |
− | + | * | |
− | + | * @param input | |
− | + | * the input String. | |
− | + | * @param toBeReplaced | |
− | + | * the substring to be replaced within input string. | |
− | + | * @param replacement | |
− | + | * the string used to replace substring toBeReplaced within input string. | |
− | + | * @return the input string, with all occurrences of the substring | |
− | + | * toBeReplaced replaced by occurrences of the substring replacement. | |
− | + | */ | |
− | + | public static String replaceSubstring(String input, String toBeReplaced, String replacement); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | /** | |
− | + | * Replaces placeholders value in the input string. | |
− | + | * | |
− | + | * @param input | |
− | + | * the input text | |
− | + | * @param phPrefix | |
− | + | * the placeholder prefix | |
+ | * @param phSuffix | ||
+ | * the placeholder suffix | ||
+ | * @param phValues | ||
+ | * the placeholder values | ||
+ | * @return the string with placeholders replaced | ||
+ | * | ||
+ | */ | ||
+ | public static String replacePlaceholder(String input, String phPrefix, String phSuffix, Hashtable<String, String> phValues) | ||
+ | |||
+ | /** | ||
+ | * This method tokenizes a given string using another given string as | ||
+ | * separator and returns a List containing found tokens. The | ||
+ | * returned List is NEVER null (it may have zero elements, anyway). | ||
+ | * | ||
+ | * @param theString | ||
+ | * the string to be tokenized. | ||
+ | * @param separatorString | ||
+ | * the string separator between tokens. | ||
+ | * @return a List containing found tokens. | ||
+ | */ | ||
+ | public static List<String> splitByStringSeparator(String theString, String separatorString) | ||
+ | |||
+ | /** | ||
+ | * Replaces JavaScript invalid chars within input String with the | ||
+ | * corresponding escapes. | ||
+ | * | ||
+ | * @param input the input String. | ||
+ | * @return the input string, with JS invalid chars replaced by the | ||
+ | * corresponding escapes. | ||
+ | */ | ||
+ | public static String replaceJSInvalidChars(String input) | ||
+ | |||
+ | /** | ||
+ | * Replaces SQL invalid chars within input String with the | ||
+ | * corresponding escapes. | ||
+ | * | ||
+ | * @param input | ||
+ | * the input String. | ||
+ | * @return the input string, with SQL invalid chars replaced by the | ||
+ | * corresponding escapes. | ||
+ | */ | ||
+ | public static String replaceSQLInvalidChars(String input) | ||
+ | |||
+ | /** | ||
+ | * Dumps the content of a byte array as chars | ||
+ | * | ||
+ | * @param arr | ||
+ | * a byte array | ||
+ | * @return A representation of the given byte array as a sequence of chars | ||
+ | */ | ||
+ | public static String dumpByteArrayAsChars(byte[] arr) | ||
+ | |||
+ | /** | ||
+ | * Generates random chars string of given length. | ||
+ | * | ||
+ | * @param length | ||
+ | * @return a random string of given length. | ||
+ | */ | ||
+ | public static String generateRandomString(int length) | ||
+ | |||
+ | /** | ||
+ | * Returns a String representation of the given throwable stack-trace. | ||
+ | * | ||
+ | * @param throwable | ||
+ | * @return the stack-trace | ||
+ | */ | ||
+ | public static String getStackTrace(Throwable throwable) | ||
+ | |||
+ | /** | ||
+ | * Converts the following definitions to the corresponding EOL char/s: | ||
+ | * - \n or LF : line feed | ||
+ | * - \r or CR : carriage return | ||
+ | * - \r\n or CR-LF : carriage return and line feed | ||
+ | * - native : OS native EOL | ||
+ | * | ||
+ | * @param def | ||
+ | * the definition to convert | ||
+ | * @return the EOL char/s or null | ||
+ | */ | ||
+ | public static String getEOL(String def) | ||
+ | |||
+ | /** | ||
+ | * Opens and loads the content of a text file into a String | ||
+ | * | ||
+ | * @param filename | ||
+ | * the name of the file | ||
+ | * @return The content of the text file as a String | ||
+ | * @throws FileNotFoundException | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static String readFile(String filename) throws FileNotFoundException, IOException | ||
+ | |||
+ | /** | ||
+ | * Opens and loads the content of a text file into a String | ||
+ | * | ||
+ | * @param file | ||
+ | * @return The content of the text file as a String | ||
+ | * @throws FileNotFoundException | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static String readFile(File file) throws FileNotFoundException, IOException | ||
+ | |||
+ | /** | ||
+ | * Opens and loads the content of a text file (read from classpath) into a | ||
+ | * String | ||
+ | * | ||
+ | * @param filename | ||
+ | * the name of the file | ||
+ | * @return The content of the text file as a String | ||
+ | * @throws FileNotFoundException | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static String readFileFromCP(String filename) throws FileNotFoundException, IOException | ||
+ | |||
+ | /** | ||
+ | * Opens and loads the content of a text file (read from URL) into a | ||
+ | * String | ||
+ | * | ||
+ | * @param url | ||
+ | * the url of the file | ||
+ | * @return The content of the text file as a String | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static String readFileFromURL(URL url) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Opens and loads the content of a text file into a list of String | ||
+ | * | ||
+ | * @param filename | ||
+ | * the name of the file | ||
+ | * @return The content of the text file as a list of String | ||
+ | * @throws FileNotFoundException | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static List<String> readFileAsLines(String filename) throws FileNotFoundException, IOException | ||
+ | |||
+ | /** | ||
+ | * Opens and loads the content of a text file (read from classpath) into a | ||
+ | * list of String | ||
+ | * | ||
+ | * @param filename | ||
+ | * the name of the file | ||
+ | * @return The content of the text file as a list of String | ||
+ | * @throws FileNotFoundException | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static List<String> readFileAsLinesFromCP(String filename) throws FileNotFoundException, IOException | ||
+ | |||
+ | /** | ||
+ | * Opens and loads the content of a text file (read from URL) into a | ||
+ | * list of String | ||
+ | * | ||
+ | * @param url | ||
+ | * the url of the file | ||
+ | * @return The content of the text file as a list of String | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static List<String> readFileAsLinesFromURL(URL url) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a new file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The String to be written into the file | ||
+ | * @param filename | ||
+ | * The name of the file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(String contentString, String filename) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The String to be written into the file | ||
+ | * @param filename | ||
+ | * The name of the file | ||
+ | * @param append | ||
+ | * If true the data are appended to existent file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(String contentString, String filename, boolean append) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a new file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The StringBuffer to be written into the file | ||
+ | * @param filename | ||
+ | * The name of the file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(StringBuffer contentString, String filename) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The StringBuffer to be written into the file | ||
+ | * @param filename | ||
+ | * The name of the file | ||
+ | * @param append | ||
+ | * If true the data are appended to existent file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(StringBuffer contentString, String filename, boolean append) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a new file | ||
+ | * | ||
+ | * @param contentStrings | ||
+ | * The list of strings to be written into the file | ||
+ | * @param filename | ||
+ | * The name of the file | ||
+ | * @param endline | ||
+ | * The line terminator, if null or empty is used '\n' | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(List<String> contentStrings, String filename, String endline) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a file | ||
+ | * | ||
+ | * @param contentStrings | ||
+ | * The list of strings to be written into the file | ||
+ | * @param filename | ||
+ | * The name of the file | ||
+ | * @param endline | ||
+ | * The line terminator, if null or empty is used '\n' | ||
+ | * @param append | ||
+ | * If true the data are appended to existent file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(List<String> contentStrings, String filename, String endline, boolean append) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a new file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The String to be written into the file | ||
+ | * @param file | ||
+ | * The destination file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(String contentString, File file) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The String to be written into the file | ||
+ | * @param file | ||
+ | * The destination file | ||
+ | * @param append | ||
+ | * If true the data are appended to existent file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(String contentString, File file, boolean append) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a new file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The StringBuffer to be written into the file | ||
+ | * @param file | ||
+ | * The destination file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(StringBuffer contentString, File file) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a file | ||
+ | * | ||
+ | * @param contentString | ||
+ | * The StringBuffer to be written into the file | ||
+ | * @param file | ||
+ | * The destination file | ||
+ | * @param append | ||
+ | * If true the data are appended to existent file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(StringBuffer contentString, File file, boolean append) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a new file | ||
+ | * | ||
+ | * @param contentStrings | ||
+ | * The StringBuffer to be written into the file | ||
+ | * @param file | ||
+ | * The destination file | ||
+ | * @param endline | ||
+ | * The line terminator, if null or empty is used '\n' | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(List<String> contentStrings, File file, String endline) throws IOException | ||
+ | |||
+ | /** | ||
+ | * Writes a text String into a file | ||
+ | * | ||
+ | * @param contentStrings | ||
+ | * The StringBuffer to be written into the file | ||
+ | * @param file | ||
+ | * The destination file | ||
+ | * @param endline | ||
+ | * The line terminator, if null or empty is used '\n' | ||
+ | * @param append | ||
+ | * If true the data are appended to existent file | ||
+ | * @throws IOException | ||
+ | */ | ||
+ | public static void writeFile(List<String> contentStrings, File file, String endline, boolean append) throws IOException | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | Follows some usage examples: | ||
+ | <syntaxhighlight lang="java5"> | ||
+ | /* We assume that #map contains the following mappings: | ||
+ | * prop1 -> value1 | ||
+ | * prop2 -> value2 | ||
+ | */ | ||
+ | #input = "text text prop{{prop1}} text text prop{{prop2}}", | ||
+ | #value = @it.greenvulcano.util.txt.TextUtils@replacePlaceholder(#input, "prop{{", "}}", #map) | ||
+ | |||
+ | // #value contains: "text text value1 text text value2" | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 17:51, 21 December 2012
Class FQN: it.greenvulcano.util.txt.TextUtils
TextUtils public static methods
/**
* Compiles pattern as a regular expression then checks if exists a match in text.
*
* @param pattern
* @param text
* @return true if the pattern match the text
*/
public static boolean matches(String pattern, String text)
/**
* Replaces within input string all the occurrences of the
* substring toBeReplaced with occurrences of the substring replacement.
*
* @param input
* the input String.
* @param toBeReplaced
* the substring to be replaced within input string.
* @param replacement
* the string used to replace substring toBeReplaced within input string.
* @return the input string, with all occurrences of the substring
* toBeReplaced replaced by occurrences of the substring replacement.
*/
public static String replaceSubstring(String input, String toBeReplaced, String replacement);
/**
* Replaces placeholders value in the input string.
*
* @param input
* the input text
* @param phPrefix
* the placeholder prefix
* @param phSuffix
* the placeholder suffix
* @param phValues
* the placeholder values
* @return the string with placeholders replaced
*
*/
public static String replacePlaceholder(String input, String phPrefix, String phSuffix, Hashtable<String, String> phValues)
/**
* This method tokenizes a given string using another given string as
* separator and returns a List containing found tokens. The
* returned List is NEVER null (it may have zero elements, anyway).
*
* @param theString
* the string to be tokenized.
* @param separatorString
* the string separator between tokens.
* @return a List containing found tokens.
*/
public static List<String> splitByStringSeparator(String theString, String separatorString)
/**
* Replaces JavaScript invalid chars within input String with the
* corresponding escapes.
*
* @param input the input String.
* @return the input string, with JS invalid chars replaced by the
* corresponding escapes.
*/
public static String replaceJSInvalidChars(String input)
/**
* Replaces SQL invalid chars within input String with the
* corresponding escapes.
*
* @param input
* the input String.
* @return the input string, with SQL invalid chars replaced by the
* corresponding escapes.
*/
public static String replaceSQLInvalidChars(String input)
/**
* Dumps the content of a byte array as chars
*
* @param arr
* a byte array
* @return A representation of the given byte array as a sequence of chars
*/
public static String dumpByteArrayAsChars(byte[] arr)
/**
* Generates random chars string of given length.
*
* @param length
* @return a random string of given length.
*/
public static String generateRandomString(int length)
/**
* Returns a String representation of the given throwable stack-trace.
*
* @param throwable
* @return the stack-trace
*/
public static String getStackTrace(Throwable throwable)
/**
* Converts the following definitions to the corresponding EOL char/s:
* - \n or LF : line feed
* - \r or CR : carriage return
* - \r\n or CR-LF : carriage return and line feed
* - native : OS native EOL
*
* @param def
* the definition to convert
* @return the EOL char/s or null
*/
public static String getEOL(String def)
/**
* Opens and loads the content of a text file into a String
*
* @param filename
* the name of the file
* @return The content of the text file as a String
* @throws FileNotFoundException
* @throws IOException
*/
public static String readFile(String filename) throws FileNotFoundException, IOException
/**
* Opens and loads the content of a text file into a String
*
* @param file
* @return The content of the text file as a String
* @throws FileNotFoundException
* @throws IOException
*/
public static String readFile(File file) throws FileNotFoundException, IOException
/**
* Opens and loads the content of a text file (read from classpath) into a
* String
*
* @param filename
* the name of the file
* @return The content of the text file as a String
* @throws FileNotFoundException
* @throws IOException
*/
public static String readFileFromCP(String filename) throws FileNotFoundException, IOException
/**
* Opens and loads the content of a text file (read from URL) into a
* String
*
* @param url
* the url of the file
* @return The content of the text file as a String
* @throws IOException
*/
public static String readFileFromURL(URL url) throws IOException
/**
* Opens and loads the content of a text file into a list of String
*
* @param filename
* the name of the file
* @return The content of the text file as a list of String
* @throws FileNotFoundException
* @throws IOException
*/
public static List<String> readFileAsLines(String filename) throws FileNotFoundException, IOException
/**
* Opens and loads the content of a text file (read from classpath) into a
* list of String
*
* @param filename
* the name of the file
* @return The content of the text file as a list of String
* @throws FileNotFoundException
* @throws IOException
*/
public static List<String> readFileAsLinesFromCP(String filename) throws FileNotFoundException, IOException
/**
* Opens and loads the content of a text file (read from URL) into a
* list of String
*
* @param url
* the url of the file
* @return The content of the text file as a list of String
* @throws IOException
*/
public static List<String> readFileAsLinesFromURL(URL url) throws IOException
/**
* Writes a text String into a new file
*
* @param contentString
* The String to be written into the file
* @param filename
* The name of the file
* @throws IOException
*/
public static void writeFile(String contentString, String filename) throws IOException
/**
* Writes a text String into a file
*
* @param contentString
* The String to be written into the file
* @param filename
* The name of the file
* @param append
* If true the data are appended to existent file
* @throws IOException
*/
public static void writeFile(String contentString, String filename, boolean append) throws IOException
/**
* Writes a text String into a new file
*
* @param contentString
* The StringBuffer to be written into the file
* @param filename
* The name of the file
* @throws IOException
*/
public static void writeFile(StringBuffer contentString, String filename) throws IOException
/**
* Writes a text String into a file
*
* @param contentString
* The StringBuffer to be written into the file
* @param filename
* The name of the file
* @param append
* If true the data are appended to existent file
* @throws IOException
*/
public static void writeFile(StringBuffer contentString, String filename, boolean append) throws IOException
/**
* Writes a text String into a new file
*
* @param contentStrings
* The list of strings to be written into the file
* @param filename
* The name of the file
* @param endline
* The line terminator, if null or empty is used '\n'
* @throws IOException
*/
public static void writeFile(List<String> contentStrings, String filename, String endline) throws IOException
/**
* Writes a text String into a file
*
* @param contentStrings
* The list of strings to be written into the file
* @param filename
* The name of the file
* @param endline
* The line terminator, if null or empty is used '\n'
* @param append
* If true the data are appended to existent file
* @throws IOException
*/
public static void writeFile(List<String> contentStrings, String filename, String endline, boolean append) throws IOException
/**
* Writes a text String into a new file
*
* @param contentString
* The String to be written into the file
* @param file
* The destination file
* @throws IOException
*/
public static void writeFile(String contentString, File file) throws IOException
/**
* Writes a text String into a file
*
* @param contentString
* The String to be written into the file
* @param file
* The destination file
* @param append
* If true the data are appended to existent file
* @throws IOException
*/
public static void writeFile(String contentString, File file, boolean append) throws IOException
/**
* Writes a text String into a new file
*
* @param contentString
* The StringBuffer to be written into the file
* @param file
* The destination file
* @throws IOException
*/
public static void writeFile(StringBuffer contentString, File file) throws IOException
/**
* Writes a text String into a file
*
* @param contentString
* The StringBuffer to be written into the file
* @param file
* The destination file
* @param append
* If true the data are appended to existent file
* @throws IOException
*/
public static void writeFile(StringBuffer contentString, File file, boolean append) throws IOException
/**
* Writes a text String into a new file
*
* @param contentStrings
* The StringBuffer to be written into the file
* @param file
* The destination file
* @param endline
* The line terminator, if null or empty is used '\n'
* @throws IOException
*/
public static void writeFile(List<String> contentStrings, File file, String endline) throws IOException
/**
* Writes a text String into a file
*
* @param contentStrings
* The StringBuffer to be written into the file
* @param file
* The destination file
* @param endline
* The line terminator, if null or empty is used '\n'
* @param append
* If true the data are appended to existent file
* @throws IOException
*/
public static void writeFile(List<String> contentStrings, File file, String endline, boolean append) throws IOException
Follows some usage examples:
/* We assume that #map contains the following mappings:
* prop1 -> value1
* prop2 -> value2
*/
#input = "text text prop{{prop1}} text text prop{{prop2}}",
#value = @it.greenvulcano.util.txt.TextUtils@replacePlaceholder(#input, "prop{{", "}}", #map)
// #value contains: "text text value1 text text value2"