|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectariba.util.io.Mapping
public final class Mapping
Read in mapping file (value, mappedValue) and provide mapping. The mapping file is a csv file, with the first 2 columns of each row representing the key and its corresponding value. Any rows with less than 2 columns are skipped, and extra columns in exess of 2 of each row are also skipped.
The file can optionally include in its first row the encoding scheme to use. For exmaple, the line "8859_1,," specifies 8859_1 encoding is to be used. For such cases, this class should be instantiated with the encoding parameter being null.
Most files also contain in its first line (if no encoding is specified) or 2nd line (if encoding is specified) header information (such as the names of the columns). This header will not be read. However, some files does not contain the header. For these files, the class should be instantiated with the hasHeader parameter being false, otherwise, the first key-value pair will be skipped.
Comments are indicated by a leading '#' character. Note that the keys (that is the first column of each row) must not begin with '#'. Currently even wrapping quotes around '#' will be treated as comments. Any comments if any, must appear after the encoding line (if any) and the header (if any).
Note that this class is not synchronized. It is expected that the mapping be first generated when the mapping file is read. Only after that should the key-value pairs (or the entire map) be accessed.
Constructor Summary | |
---|---|
Mapping(java.net.URL fileURL,
java.lang.String defaultValue,
boolean hasHeader)
Instantiates an instance of this class. |
|
Mapping(java.net.URL fileURL,
java.lang.String encoding,
java.lang.String defaultValue)
Instantiates an instance of this class. |
|
Mapping(java.net.URL fileURL,
java.lang.String encoding,
java.lang.String defaultValue,
boolean hasHeader)
Instantiates an instance of this class. |
Method Summary | |
---|---|
void |
consumeLineOfTokens(java.lang.String filePath,
int lineNumber,
java.util.List line)
Called once per CSV line read. |
java.lang.String |
map(java.lang.String key)
Returns the value of the given key. |
java.util.Map |
mapCopy()
Returns a copy of the map. |
void |
read()
Reads the contents of the mapping file. |
java.util.List |
reverseMap(java.lang.String value)
Returns the list of keys for the given value. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Mapping(java.net.URL fileURL, java.lang.String defaultValue, boolean hasHeader)
fileURL
- the URL of the mapping file. Must be a valid URL.defaultValue
- the value to be used as the value of any
key that does not have any mapped value.hasHeader
- if true, specifies that the mapping file has
a header (which is the first line of the file if no encoding
is specified, or the second line in the file if encoding is
specified). The header, if present, will not be read.public Mapping(java.net.URL fileURL, java.lang.String encoding, java.lang.String defaultValue)
fileURL
- the URL of the mapping file. Must be a valid URL.encoding
- the encoding to be used. If null and if the
encoding specified in the file is a supported encoding, it
will be used. If it is not a supported encoding, the system
default encoding will be used.defaultValue
- the value to be used as the value of any
key that does not have any mapped value.public Mapping(java.net.URL fileURL, java.lang.String encoding, java.lang.String defaultValue, boolean hasHeader)
fileURL
- the URL of the mapping file. Must be a valid URL.encoding
- the encoding to be used. If null and if the
encoding specified in the file is a supported encoding, it
will be used. If it is not a supported encoding, the system
default encoding will be used.defaultValue
- the value to be used as the value of any
key that does not have any mapped value.hasHeader
- if true, specifies that the mapping file has
a header (which is the first line of the file if no encoding
is specified, or the second line in the file if encoding is
specified). The header, if present, will not be read.Method Detail |
---|
public void read() throws java.io.IOException
java.io.IOException
- when I/O errors occurs.map
public java.lang.String map(java.lang.String key)
key
- the key to map.
read()
,
mapCopy()
public java.util.List reverseMap(java.lang.String value)
value
- the value to map.
read()
,
mapCopy()
public java.util.Map mapCopy()
map
public void consumeLineOfTokens(java.lang.String filePath, int lineNumber, java.util.List line)
CSVConsumer
consumeLineOfTokens
in interface CSVConsumer
filePath
- the CSV source filelineNumber
- the current line being reported, 1-based.line
- a List of tokens parsed from a one line in the file
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |