prefuse.data.parser
Class ParserFactory

java.lang.Object
  extended by prefuse.data.parser.ParserFactory
All Implemented Interfaces:
java.lang.Cloneable

public class ParserFactory
extends java.lang.Object
implements java.lang.Cloneable

Factory class that maintains a collection of parser instances and returns the appropriate parser based on a history of samples presented to the factory. The sample(String) method takes a text string and tests it against all available parsers, updating whether or not the parsers can successfully parse the value. This method is used in a more automated fashion by the TypeInferencer class.

Author:
jeffrey heer
See Also:
TypeInferencer

Constructor Summary
ParserFactory()
          Constructor.
ParserFactory(DataParser[] parsers)
          Constructor.
 
Method Summary
 java.lang.Object clone()
           
static ParserFactory getDefaultFactory()
          Returns the default parser factory.
protected  DataParser getParser()
          Returns the highest ranking parser that successfully can parse all the input samples viewed by this instance.
 DataParser getParser(java.lang.Class type)
          Returns a parser for the specified data type.
 DataParser getParser(java.lang.String[][] data, int col, int startRow)
          Analyzes a column of the given array of String values to determine an acceptable parser data type.
 DataParser getParser(java.lang.String[] data, int startRow)
          Analyzes the given array of String values to determine an acceptable parser data type.
protected  void reset()
          Reset the candidate parser settings, making each parser equally likely.
protected  void sample(java.lang.String val)
          Sample a data value against the parsers, updating the parser candidates.
static void setDefaultFactory(ParserFactory factory)
          Sets the default parser factory.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParserFactory

public ParserFactory()
Constructor. Uses a default collection of parsers, testing for the following data type in the followinf order of precedence: int, long, double, float, boolean, Date, Time, DateTime, String.


ParserFactory

public ParserFactory(DataParser[] parsers)

Constructor. Takes an array of parsers to test. After creating this instance, sample data values can be passed in using the sample() method, and this class will check the sample against the parsers, computing which parsers can successfully parse the sample. This process of elimination disregards inappropriate parsers. After a series of samples, the getParser() method can be used to retrieve the highest ranking candidate parser.

If no parser can parse all samples, a null value will be returned by getParser(). For this reason, it is recommended to always use a StringParser as the last element of the input array, as it is guaranteed to always parse successfully (by simply returning its input String).

The ordering of parsers in the array is taken to be the desired order of precendence of the parsers. For example, if both parser[0] and parser[2] can parse all the available samples, parser[0] will be returned.

Parameters:
parsers - the input DataParsers to use.
Method Detail

getDefaultFactory

public static ParserFactory getDefaultFactory()
Returns the default parser factory. The default factory tests for the following data types (in the provided order of precedence): int, long, double, float, boolean, Date, Time, DateTime, String.

Returns:
the default parser factory.

setDefaultFactory

public static void setDefaultFactory(ParserFactory factory)
Sets the default parser factory. This factory will be used by default by all readers to parse data values.

Parameters:
factory - the new default parser factory.

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object
See Also:
Object.clone()

reset

protected void reset()
Reset the candidate parser settings, making each parser equally likely.


sample

protected void sample(java.lang.String val)
Sample a data value against the parsers, updating the parser candidates.

Parameters:
val - the String value to sample

getParser

protected DataParser getParser()
Returns the highest ranking parser that successfully can parse all the input samples viewed by this instance. If no such parser exists, a null value is returned.

Returns:
the highest-ranking data parser, or null if none

getParser

public DataParser getParser(java.lang.Class type)
Returns a parser for the specified data type.

Parameters:
type - the Class for the data type to parse
Returns:
a parser for the given data type, or null if no such parser can be found.

getParser

public DataParser getParser(java.lang.String[] data,
                            int startRow)
Analyzes the given array of String values to determine an acceptable parser data type.

Parameters:
data - an array of String values to parse
startRow - the row from which to begin analyzing the data array, allowing header rows to be excluded.
Returns:
the appropriate parser for the inferred data type, of null if none.

getParser

public DataParser getParser(java.lang.String[][] data,
                            int col,
                            int startRow)
Analyzes a column of the given array of String values to determine an acceptable parser data type.

Parameters:
data - an 2D array of String values to parse
col - an index for the column to process
startRow - the row from which to begin analyzing the data array, allowing header rows to be excluded.
Returns:
the appropriate parser for the inferred data type, of null if none.


Copyright © 2007 Regents of the University of California