ariba.util.io
Class Mapping

java.lang.Object
  extended by ariba.util.io.Mapping
All Implemented Interfaces:
CSVConsumer

public final class Mapping
extends java.lang.Object
implements CSVConsumer

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

Mapping

public Mapping(java.net.URL fileURL,
               java.lang.String defaultValue,
               boolean hasHeader)
Instantiates an instance of this class.

Parameters:
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.

Mapping

public Mapping(java.net.URL fileURL,
               java.lang.String encoding,
               java.lang.String defaultValue)
Instantiates an instance of this class.

Parameters:
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.

Mapping

public Mapping(java.net.URL fileURL,
               java.lang.String encoding,
               java.lang.String defaultValue,
               boolean hasHeader)
Instantiates an instance of this class.

Parameters:
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

read

public void read()
          throws java.io.IOException
Reads the contents of the mapping file.

Throws:
java.io.IOException - when I/O errors occurs.
See Also:
map

map

public java.lang.String map(java.lang.String key)
Returns the value of the given key. Note that read should have been called before calling this method.

Parameters:
key - the key to map.
Returns:
the mapped value
See Also:
read(), mapCopy()

reverseMap

public java.util.List reverseMap(java.lang.String value)
Returns the list of keys for the given value. It could return null or empty list on a not matched case. The returned list is an immutable list. Note that read should have been called before calling this method.

Parameters:
value - the value to map.
Returns:
the mapped list of keys
See Also:
read(), mapCopy()

mapCopy

public java.util.Map mapCopy()
Returns a copy of the map.

Returns:
a copy of the map.
See Also:
map

consumeLineOfTokens

public void consumeLineOfTokens(java.lang.String filePath,
                                int lineNumber,
                                java.util.List line)
Description copied from interface: CSVConsumer
Called once per CSV line read.

Specified by:
consumeLineOfTokens in interface CSVConsumer
Parameters:
filePath - the CSV source file
lineNumber - the current line being reported, 1-based.
line - a List of tokens parsed from a one line in the file


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.