Class FieldReader

java.lang.Object
de.tsl2.nano.scanner.FieldReader

public class FieldReader extends Object
reads any fields from any text file using a Scanner. all lines starting with COMMENT will be ignored.
 Features:
 - key/value properties.
 - any field-structure per line
 - calls your ICallback in a foreach() loop
 
you can combine these features to read complex data structures.
Version:
$Revision$
Author:
Tom
  • Field Details

  • Constructor Details

    • FieldReader

      public FieldReader()
  • Method Details

    • readTable

      public static Map<Object,List> readTable(String file, Class... columnTypes)
    • readTable

      public static Map<Object,List> readTable(InputStream stream, Class... columnTypes)
      delegates to readTable(InputStream, String, Locale, boolean, boolean, boolean, Class...) for simple tables without header and rowIDs.
    • readTable

      public static Map<Object,List> readTable(InputStream stream, String fieldDelimiter, Locale locale, boolean horizontal, boolean header, boolean rowIDs, Class... columnTypes)
      reads values in columns and rows from any file using a Scanner.
      Parameters:
      stream - input stream
      fieldDelimiter - delimiter between two values. normally DEL_CSV.
      locale - locale to format formats etc. normally the default locale Locale.getDefault().
      horizontal - if false, the map will be rotated to provide columns instead of rows
      header - if true, the first line will be read as header information
      rowIDs - if true, all first column values will be read as row-id objects - otherwise a line counter will be used.
      columnTypes - indicates the column value types. known Scanner types are: byte, boolean, short, int, long, float, double, BigInteger, BigDecimal. the given locale defines the format of that types.
      Returns:
      map holding its keys in the sequentially order. an optional header is stored on key HEADER. all other keys are rowIDs or line indexes.
    • readTable

      public static Map<Object,List> readTable(InputStream stream, String rowPattern)
      delegates to readTable(InputStream, boolean, boolean, boolean, String) for a simple table without header and row-ids.
    • readTable

      public static Map<Object,List> readTable(InputStream stream, boolean horizontal, boolean header, boolean rowIDs, String rowPattern)
      reads values in columns and rows from any file using a Scanner. giving a rows regular expression defines the column formats for each row. see Scanner for examples. the row pattern should have grouping with brackets like '(\w+) fish (\w+) fish (\w+)'.
      Parameters:
      stream - input stream
      fieldDelimiter - delimiter between two values. normally DEL_CSV.
      locale - locale to format formats etc. normally the default locale Locale.getDefault().
      horizontal - if false, the map will be rotated to provide columns instead of rows
      header - if true, the first line will be read as header information
      rowIDs - if true, all first column values will be read as row-id objects - otherwise a line counter will be used.
      rowPattern -
      Returns:
      map holding its keys in the sequentially order. an optional header is stored on key HEADER. all other keys are rowIDs or line indexes.
    • read

      public static Properties read(String file)
      delegates to #read(InputStream)
    • read

      public static Properties read(InputStream stream, boolean doClose)
      delegates to #read(InputStream, String, String, Locale)
    • read

      public static Properties read(InputStream stream, String keyValueDelimiter, String propDelimiter, Locale locale, boolean doClose)
      reads key/value properties from any file using a Scanner.
      Parameters:
      stream - input stream
      keyValueDelimiter - delimiter between key and value. normally DEL_KEYVALUE.
      propDelimiter - delimiter between two property entries. normally DEL_PROP
      locale - locale to format formats etc. normally the default locale Locale.getDefault().
      Returns:
      property map
    • forEach

      public static <RESULT> List<RESULT> forEach(InputStream stream, de.tsl2.nano.core.ICallback<RESULT> callback)
      convenience delegating to forEach(InputStream, String, String, ICallback) with standard block definitions.
    • forEach

      public static <RESULT> List<RESULT> forEach(InputStream stream, String blockExpression, String keyValueDelimiter, de.tsl2.nano.core.ICallback<RESULT> callback)
      runs through the stream, searching for the given blocks and providing each block content as property map to the callback.
      Parameters:
      stream - source stream
      blockExpression - defining the content of one block to read the properties from
      keyValueDelimiter - key-value delimiter
      callback - worker
      Returns:
      threadsafe list of loop-count elements containing all callback results (null results are allowed)