util
Class TextParser

java.lang.Object
  extended by util.TextParser

public class TextParser
extends Object

Collection of basic string formatting operations. These have the purpose of simplifying the use and creation of human readable configuration files. Each operation has both an encode and decode version. Decoding renders text into usable data structures while encoding reverts it back into a human readable form. These are simple tools, not meant for heavy operations (most running in O(n) time).

Version:
May 24, 2007

Method Summary
static LinkedHashMap<String,String> decodeLabels(List<String> text, String divider)
          Parses each line of text for a sentinel, associating the text before the first instance to the text after in a mapping.
static ArrayList<String> decodeLineDivisions(String str, String divider)
          Takes a line of text and parses a list of segments divided by a token (excluding the divider itself).
static ArrayList<ArrayList<String>> decodeSubsections(List<String> text, String divider)
          Separates text into its subsections based upon a given sentinel.
static ArrayList<String> encodeLabels(Map<String,String> data, String divider)
          Collapses mappings of text into a single listing of key and value divided by a specified sentinel.
static String encodeLineDivisions(List<String> text, String divider)
          Provides a list of text as a single string with elements divided by a given delimiter.
static ArrayList<String> encodeSubsections(List<ArrayList<String>> data, String divider)
          Collapses lists of text into a single section, dividing segments with a specified sentinel.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

decodeLabels

public static LinkedHashMap<String,String> decodeLabels(List<String> text,
                                                        String divider)
Parses each line of text for a sentinel, associating the text before the first instance to the text after in a mapping. Lines missing the sentinel are mapped to null. Order is preserved but duplicate keys will overwrite previous entries. For instance with a ": " divider the following:
 Location: Wonderland
 Climate: Funky
 Population: 426
 Species: 426
 Climate: Especially Funky
 The divider is: ': ' // Entry has multiple instances of divider.
 Blank Entry: // Entry has a space after the colon.
 Other Notes: // Entry ends with the colon.
 Includes the devouring of many 'magic' substances.
 
would return a mapping:
 Index 0: ("Location" -> "Wonderland")
 Index 1: ("Climate" -> "Especially Funky")
 Index 2: ("Population" -> "426")
 Index 3: ("Species" -> "426")
 Index 4: ("The divider is" -> "': '")
 Index 5: ("Blank Entry" -> "")
 Index 6: ("Other Notes:" -> null)
 Index 7: ("Includes the devouring of many 'magic' substances." -> null)
 

Parameters:
text - text to be parsed
divider - sentinel on which to divide mapped key and value
Returns:
mapping of text before sentinel to anything after
Throws:
IllegalArgumentException - if divider is an empty string

encodeLabels

public static ArrayList<String> encodeLabels(Map<String,String> data,
                                             String divider)
Collapses mappings of text into a single listing of key and value divided by a specified sentinel. Keys with null values are added without a divider.

Parameters:
data - text mapping to be collapsed
divider - sentinel used to divide keys and values
Returns:
list representation of mapped relationships

decodeSubsections

public static ArrayList<ArrayList<String>> decodeSubsections(List<String> text,
                                                             String divider)
Separates text into its subsections based upon a given sentinel. For instance with a "-----" divider the following:
 Name: Alice
 Age: 15
 -----
 Name: White Rabbit
 Age: 6
 Quote: 'I'm late! I'm Late!'
 
 -----
 -----
 Name: Mad Hatter
 Age: 22
 Divider is '-----'
 -----
 
would return a list:
 Index 0: ("Name: Alice", "Age: 15")
 Index 1: ("Name: White Rabbit", "Age: 6", "Quote: 'I'm late! I'm Late!'", "")
 Index 2: ()
 Index 3: ("Name: Mad Hatter", "Age: 22", "Divider is '-----'")
 Index 4: ()
 

Parameters:
text - text to be parsed
divider - sentinels on which to break text
Returns:
list of subsections excluding divider

encodeSubsections

public static ArrayList<String> encodeSubsections(List<ArrayList<String>> data,
                                                  String divider)
Collapses lists of text into a single section, dividing segments with a specified sentinel. If divider is null then no sentinel is used.

Parameters:
data - divided list of text to be collapsed
divider - sentinels used to divide text (excluded if null)
Returns:
collapsed listing of text

decodeLineDivisions

public static ArrayList<String> decodeLineDivisions(String str,
                                                    String divider)
Takes a line of text and parses a list of segments divided by a token (excluding the divider itself). For an example, "one-for-all" with the divider of "-" would return a list consisting of {"one", "for", "all"}. This can also be a simple means of breaking a string into a collection based on newline markers. If the text is an empty string then this returns a collection containing an empty string.

Parameters:
str - text to be parsed
divider - token to break segments on
Returns:
list of entries separated by the dividing token
Throws:
IllegalArgumentException - if divider is an empty string

encodeLineDivisions

public static String encodeLineDivisions(List<String> text,
                                         String divider)
Provides a list of text as a single string with elements divided by a given delimiter. For example parsing the following with a newline marker:
 Index 0: "`What a curious feeling!' "
 Index 1: "said Alice; `I must be "
 Index 2: "shutting up like a telescope.'"
 
would return:
 "`What a curious feeling!' 
 said Alice; `I must be 
 shutting up like a telescope.'"
 

Parameters:
text - text to be parsed
divider - delimiter used to divide text
Returns:
string containing complete text of the collection separated by the divider
Throws:
IllegalArgumentException - if text is empty