| java.lang.Object java.text.Format org.geotools.io.LineFormat
LineFormat | public class LineFormat extends Format (Code) | | Parses a line of text data. This class is mostly used for parsing lines in a matrix or a table.
Each column may contains numbers, dates, or other objects parseable by some
Format implementations. The example below reads dates in the first column and numbers in all
remaining columns.
final LineParser parser = new LineFormat(new Format[] {
java.text.DateFormat.getDateTimeInstance ,
java.text.NumberFormat.getNumberInstance });
LineFormat may be used for reading a matrix with an unknow number of columns,
while requiring that all lines have the same number of columns. The example below gets the
number of columns while reading the first line, and ensure that all subsequent lines have
the same number of columns. If one line violate this condition, then a
ParseException will be thrown. The check if performed by the
getValues(double[]) method when
the
data array is non-nul.
double[] data=null;
final
java.io.BufferedReader in = new
java.io.BufferedReader (new
java.io.FileReader ("MATRIX.TXT"));
for (
String line; (line=in.readLine()) != null;) {
parser.setLine(line);
data = parser.getValues(data);
// ... process 'data' here ...
});
This code can work as well with dates instead of numbers. In this case, the values returned
will be microseconds ellapsed since January 1st, 1970.
A
ParseException may be thrown because a string can't be parsed, because an object
can't be converted into a number or because a line don't have the expected number of columns.
In all case, it is possible to gets the index of the first problem found using
ParseException.getErrorOffset .
since: 2.0 version: $Id: LineFormat.java 25467 2007-05-08 16:30:30Z desruisseaux $ author: Martin Desruisseaux |
Constructor Summary | |
public | LineFormat() Constructs a new line parser for the default locale. | public | LineFormat(Locale locale) Constructs a new line parser for the specified locale. | public | LineFormat(Format format) Constructs a new line parser using the specified format for every columns. | public | LineFormat(Format[] formats) Constructs a new line parser using the specified format objects. |
Method Summary | |
public void | clear() Clear this parser. | public Object | clone() Returns a clone of this parser. | public StringBuffer | format(Object values, StringBuffer toAppendTo, FieldPosition position) Formats an object and appends the resulting text to a given string buffer.
This method invokes
(values) ,
then formats all columns using the
Format object specified at
construction time. | public Object | getValue(int index) Returns the value at the specified index. | public int | getValueCount() Returns the number of elements found in the last line parsed by
LineFormat.setLine(String) . | public double[] | getValues(double[] array) Copies all values to the specified array. | public float[] | getValues(float[] array) Copies all values to the specified array. | public long[] | getValues(long[] array) Copies all values to the specified array. | public int[] | getValues(int[] array) Copies all values to the specified array. | public short[] | getValues(short[] array) Copies all values to the specified array. | public byte[] | getValues(byte[] array) Copies all values to the specified array. | public Object | parseObject(String source, ParsePosition position) Parses text from a string to produce an object. | public Object | parseObject(String source) Parses text from the beginning of the given string to produce an object. | public int | setLine(String line) Parse the specified line. | public int | setLine(String line, int lower, int upper) Parse a substring of the specified line. | public void | setValue(int index, Object value) Set or add a value to current line. | public void | setValues(Object values) Set all values in the current line. | public String | toString() Returns a string representation of current line. |
LineFormat | public LineFormat()(Code) | | Constructs a new line parser for the default locale.
|
LineFormat | public LineFormat(Locale locale)(Code) | | Constructs a new line parser for the specified locale. For example
Locale.US may be used for reading numbers using the dot as decimal separator.
|
LineFormat | public LineFormat(Format[] formats) throws IllegalArgumentException(Code) | | Constructs a new line parser using the specified format objects. For example the first
column will be parsed using
formats[0] ; the second column will be parsed using
formats[1] , etc. If there is more columns than formats, then the
last format object is reused for all remaining columns.
Parameters: formats - The formats to use for parsing. throws: IllegalArgumentException - if formats is null or an element of format is null. |
clone | public Object clone()(Code) | | Returns a clone of this parser. In current implementation, this
clone is not for usage in concurrent thread.
|
format | public StringBuffer format(Object values, StringBuffer toAppendTo, FieldPosition position)(Code) | | Formats an object and appends the resulting text to a given string buffer.
This method invokes
(values) ,
then formats all columns using the
Format object specified at
construction time. Columns are separated by tabulation.
since: 2.4 |
getValues | public double[] getValues(double[] array) throws ParseException(Code) | | Copies all values to the specified array. This method is typically invoked after
LineFormat.setLine(String) for fetching the values just parsed. If
array is
null, this method creates and returns a new array with a length equals to number
of elements parsed. If
array is not null, then this method will thrown an
exception if the array length is not exactly equals to the number of elements
parsed.
Parameters: array - The array to copy values into. array if it was not null, or a new array otherwise. throws: ParseException - If array was not null and its length is not equals tothe number of elements parsed, or if at least one element can't be parsed. |
getValues | public float[] getValues(float[] array) throws ParseException(Code) | | Copies all values to the specified array. This method is typically invoked after
LineFormat.setLine(String) for fetching the values just parsed. If
array is
null, this method creates and returns a new array with a length equals to number
of elements parsed. If
array is not null, then this method will thrown an
exception if the array length is not exactly equals to the number of elements
parsed.
Parameters: array - The array to copy values into. array if it was not null, or a new array otherwise. throws: ParseException - If array was not null and its length is not equals tothe number of elements parsed, or if at least one element can't be parsed. |
getValues | public long[] getValues(long[] array) throws ParseException(Code) | | Copies all values to the specified array. This method is typically invoked after
LineFormat.setLine(String) for fetching the values just parsed. If
array is
null, this method creates and returns a new array with a length equals to number
of elements parsed. If
array is not null, then this method will thrown an
exception if the array length is not exactly equals to the number of elements
parsed.
Parameters: array - The array to copy values into. array if it was not null, or a new array otherwise. throws: ParseException - If array was not null and its length is not equals tothe number of elements parsed, or if at least one element can't be parsed. |
getValues | public int[] getValues(int[] array) throws ParseException(Code) | | Copies all values to the specified array. This method is typically invoked after
LineFormat.setLine(String) for fetching the values just parsed. If
array is
null, this method creates and returns a new array with a length equals to number
of elements parsed. If
array is not null, then this method will thrown an
exception if the array length is not exactly equals to the number of elements
parsed.
Parameters: array - The array to copy values into. array if it was not null, or a new array otherwise. throws: ParseException - If array was not null and its length is not equals tothe number of elements parsed, or if at least one element can't be parsed. |
getValues | public short[] getValues(short[] array) throws ParseException(Code) | | Copies all values to the specified array. This method is typically invoked after
LineFormat.setLine(String) for fetching the values just parsed. If
array is
null, this method creates and returns a new array with a length equals to number
of elements parsed. If
array is not null, then this method will thrown an
exception if the array length is not exactly equals to the number of elements
parsed.
Parameters: array - The array to copy values into. array if it was not null, or a new array otherwise. throws: ParseException - If array was not null and its length is not equals tothe number of elements parsed, or if at least one element can't be parsed. |
getValues | public byte[] getValues(byte[] array) throws ParseException(Code) | | Copies all values to the specified array. This method is typically invoked after
LineFormat.setLine(String) for fetching the values just parsed. If
array is
null, this method creates and returns a new array with a length equals to number
of elements parsed. If
array is not null, then this method will thrown an
exception if the array length is not exactly equals to the number of elements
parsed.
Parameters: array - The array to copy values into. array if it was not null, or a new array otherwise. throws: ParseException - If array was not null and its length is not equals tothe number of elements parsed, or if at least one element can't be parsed. |
parseObject | public Object parseObject(String source) throws ParseException(Code) | | Parses text from the beginning of the given string to produce an object.
since: 2.4 |
setLine | public int setLine(String line) throws ParseException(Code) | | Parse the specified line. The content is immediately parsed and values
can be obtained using one of the
getValues(...) method.
Parameters: line - The line to parse. The number of elements parsed in the specified line.The same information can be obtained with LineFormat.getValueCount. throws: ParseException - If at least one column can't be parsed. |
setLine | public int setLine(String line, int lower, int upper) throws ParseException(Code) | | Parse a substring of the specified line. The content is immediately parsed
and values can be obtained using one of the
getValues(...) method.
Parameters: line - The line to parse. Parameters: lower - Index of the first character in line to parse. Parameters: upper - Index after the last character in line to parse. The number of elements parsed in the specified line.The same information can be obtained with LineFormat.getValueCount. throws: ParseException - If at least one column can't be parsed. |
setValues | public void setValues(Object values) throws IllegalArgumentException(Code) | | Set all values in the current line. The
values argument must be an array,
which may be of primitive type.
Parameters: values - The array to set as values. throws: IllegalArgumentException - if values is not an array. since: 2.4 |
toString | public String toString()(Code) | | Returns a string representation of current line. All columns are formatted using
the
Format object specified at construction time. Columns are separated
by tabulation.
|
|
|