net.i2p.addressbook
Class ConfigParser

java.lang.Object
  extended by net.i2p.addressbook.ConfigParser

public class ConfigParser
extends java.lang.Object

Utility class providing methods to parse and write files in config file format, and subscription file format. TODO: Change file encoding from default to UTF-8? Or switch to the DataHelper loadProps/storeProps methods?

Author:
Ragnarok

Constructor Summary
ConfigParser()
           
 
Method Summary
static java.util.Map parse(java.io.BufferedReader input)
          Return a Map using the contents of BufferedReader input. input must have a single key, value pair on each line, in the format: key=value.
static java.util.Map parse(java.io.File file)
          Return a Map using the contents of the File file.
static java.util.Map parse(java.io.File file, java.util.Map map)
          Return a Map using the contents of the File file.
static java.util.Map parse(java.lang.String string)
          Return a Map using the contents of the String string.
static java.util.List parseSubscriptions(java.io.BufferedReader input)
          Return a List where each element is a line from the BufferedReader input.
static java.util.List parseSubscriptions(java.io.File file)
          Return a List where each element is a line from the File file.
static java.util.List parseSubscriptions(java.io.File file, java.util.List list)
          Return a List using the contents of the File file.
static java.util.List parseSubscriptions(java.lang.String string)
          Return a List where each element is a line from the String string.
static java.lang.String stripComments(java.lang.String inputLine)
          Strip the comments from a String.
static void write(java.util.Map map, java.io.BufferedWriter output)
          Write contents of Map map to BufferedWriter output.
static void write(java.util.Map map, java.io.File file)
          Write contents of Map map to the File file.
static void writeSubscriptions(java.util.List list, java.io.BufferedWriter output)
          Write contents of List list to BufferedReader output.
static void writeSubscriptions(java.util.List list, java.io.File file)
          Write contents of List list to File file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConfigParser

public ConfigParser()
Method Detail

stripComments

public static java.lang.String stripComments(java.lang.String inputLine)
Strip the comments from a String. Lines that begin with '#' and ';' are considered comments, as well as any part of a line after a '#'.

Parameters:
inputLine - A String to strip comments from.
Returns:
A String without comments, but otherwise identical to inputLine.

parse

public static java.util.Map parse(java.io.BufferedReader input)
                           throws java.io.IOException
Return a Map using the contents of BufferedReader input. input must have a single key, value pair on each line, in the format: key=value. Lines starting with '#' or ';' are considered comments, and ignored. Lines that are obviously not in the format key=value are also ignored. The key is converted to lower case.

Parameters:
input - A BufferedReader with lines in key=value format to parse into a Map.
Returns:
A Map containing the key, value pairs from input.
Throws:
java.io.IOException - if the BufferedReader cannot be read.

parse

public static java.util.Map parse(java.io.File file)
                           throws java.io.IOException
Return a Map using the contents of the File file. See parseBufferedReader for details of the input format.

Parameters:
file - A File to parse.
Returns:
A Map containing the key, value pairs from file.
Throws:
java.io.IOException - if file cannot be read.

parse

public static java.util.Map parse(java.lang.String string)
                           throws java.io.IOException
Return a Map using the contents of the String string. See parseBufferedReader for details of the input format.

Parameters:
string - A String to parse.
Returns:
A Map containing the key, value pairs from string.
Throws:
java.io.IOException - if file cannot be read.

parse

public static java.util.Map parse(java.io.File file,
                                  java.util.Map map)
Return a Map using the contents of the File file. If file cannot be read, use map instead, and write the result to where file should have been.

Parameters:
file - A File to attempt to parse.
map - A Map to use as the default, if file fails.
Returns:
A Map containing the key, value pairs from file, or if file cannot be read, map.

parseSubscriptions

public static java.util.List parseSubscriptions(java.io.BufferedReader input)
                                         throws java.io.IOException
Return a List where each element is a line from the BufferedReader input.

Parameters:
input - A BufferedReader to parse.
Returns:
A List consisting of one element for each line in input.
Throws:
java.io.IOException - if input cannot be read.

parseSubscriptions

public static java.util.List parseSubscriptions(java.io.File file)
                                         throws java.io.IOException
Return a List where each element is a line from the File file.

Parameters:
file - A File to parse.
Returns:
A List consisting of one element for each line in file.
Throws:
java.io.IOException - if file cannot be read.

parseSubscriptions

public static java.util.List parseSubscriptions(java.lang.String string)
                                         throws java.io.IOException
Return a List where each element is a line from the String string.

Parameters:
string - A String to parse.
Returns:
A List consisting of one element for each line in string.
Throws:
java.io.IOException - if string cannot be read.

parseSubscriptions

public static java.util.List parseSubscriptions(java.io.File file,
                                                java.util.List list)
Return a List using the contents of the File file. If file cannot be read, use list instead, and write the result to where file should have been.

Parameters:
file - A File to attempt to parse.
list - list of files to parse
Returns:
A List consisting of one element for each line in file, or if file cannot be read, list.

write

public static void write(java.util.Map map,
                         java.io.BufferedWriter output)
                  throws java.io.IOException
Write contents of Map map to BufferedWriter output. Output is written with one key, value pair on each line, in the format: key=value.

Parameters:
map - A Map to write to output.
output - A BufferedWriter to write the Map to.
Throws:
java.io.IOException - if the BufferedWriter cannot be written to.

write

public static void write(java.util.Map map,
                         java.io.File file)
                  throws java.io.IOException
Write contents of Map map to the File file. Output is written with one key, value pair on each line, in the format: key=value.

Parameters:
map - A Map to write to file.
file - A File to write the Map to.
Throws:
java.io.IOException - if file cannot be written to.

writeSubscriptions

public static void writeSubscriptions(java.util.List list,
                                      java.io.BufferedWriter output)
                               throws java.io.IOException
Write contents of List list to BufferedReader output. Output is written with each element of list on a new line.

Parameters:
list - A List to write to file.
output - A BufferedReader to write list to.
Throws:
java.io.IOException - if output cannot be written to.

writeSubscriptions

public static void writeSubscriptions(java.util.List list,
                                      java.io.File file)
                               throws java.io.IOException
Write contents of List list to File file. Output is written with each element of list on a new line.

Parameters:
list - A List to write to file.
file - A File to write list to.
Throws:
java.io.IOException - if output cannot be written to.