Package adams.data.io.input
Class AbstractSpreadSheetReader
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.logging.CustomLoggingLevelObject
-
- adams.core.option.AbstractOptionHandler
-
- adams.data.io.input.AbstractSpreadSheetReader
-
- All Implemented Interfaces:
AdditionalInformationHandler
,Destroyable
,ErrorProvider
,GlobalInfoSupporter
,EncodingSupporter
,FileFormatHandler
,LoggingLevelHandler
,LoggingSupporter
,OptionHandler
,SizeOfHandler
,Stoppable
,StoppableWithFeedback
,SpreadSheetReader
,DataRowTypeHandler
,SpreadSheetTypeHandler
,Serializable
- Direct Known Subclasses:
AbstractMultiSheetSpreadSheetReader
,AbstractSpreadSheetReaderWithMissingValueSupport
,AbstractWekaSpreadSheetReader
,AccessSpreadSheetReader
,PNGSpreadSheetReader
,PropertiesSpreadSheetReader
,SimpleArffSpreadSheetReader
,SimpleStreamSpreadSheetReader
,SqlDumpSpreadSheetReader
public abstract class AbstractSpreadSheetReader extends AbstractOptionHandler implements SpreadSheetReader, EncodingSupporter, FileFormatHandler, SpreadSheetTypeHandler, AdditionalInformationHandler
Ancestor for classes that can read spreadsheets.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractSpreadSheetReader.InputType
How to read the data.
-
Field Summary
Fields Modifier and Type Field Description protected DataRow
m_DataRowType
the data row type to use.protected BaseCharset
m_Encoding
the encoding to use.protected String
m_LastError
the last error that occurred.protected SpreadSheet
m_SpreadSheetType
the type of spreadsheet to use.protected boolean
m_Stopped
whether the read process was stopped through an external source.static String
OPTION_INPUT
static String
OPTION_OUTPUT
-
Fields inherited from class adams.core.option.AbstractOptionHandler
m_OptionManager
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description AbstractSpreadSheetReader()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected boolean
canDecompress(String filename)
Returns whether the file should get decompressed, i.e.,supportsCompressedInput()
returns true and the filename ends with ".gz".protected void
check()
Hook method to perform some checks before performing the actual read.String
dataRowTypeTipText()
Returns the tip text for this property.void
defineOptions()
Adds options to the internal list of options.protected SpreadSheet
doRead(File file)
Performs the actual reading.protected SpreadSheet
doRead(InputStream in)
Performs the actual reading.protected SpreadSheet
doRead(Reader r)
Performs the actual reading.String
encodingTipText()
Returns the tip text for this property.String
getAdditionalInformation()
Returns the additional information.DataRow
getDataRowType()
Returns the type of data row to use.protected DataRow
getDefaultDataRowType()
Returns the default row type.String
getDefaultFormatExtension()
Returns the default extension of the format.protected SpreadSheet
getDefaultSpreadSheet()
Returns the default spreadsheet type.BaseCharset
getEncoding()
Returns the encoding to use.abstract String
getFormatDescription()
Returns a string describing the format (used in the file chooser).abstract String[]
getFormatExtensions()
Returns the extension(s) of the format.protected abstract AbstractSpreadSheetReader.InputType
getInputType()
Returns how to read the data, from a file, stream or reader.String
getLastError()
Returns the error that occurred during the last read.static String[]
getReaders()
Returns a list with classnames of readers.SpreadSheet
getSpreadSheetType()
Returns the type of spreadsheet to use.boolean
hasLastError()
Returns whether an error was encountered during the last read.protected void
initialize()
Initializes the members.boolean
isStopped()
Returns whether the reading was stopped.SpreadSheet
read(File file)
Reads the spreadsheet from the specified file.SpreadSheet
read(InputStream stream)
Reads the spreadsheet from the stream.SpreadSheet
read(Reader r)
Reads the spreadsheet from the given reader.SpreadSheet
read(String filename)
Reads the spreadsheet from the given file.static void
runReader(Class env, Class reader, String[] args)
Runs the reader from commandline.void
setDataRowType(DataRow value)
Sets the type of data row to use.void
setEncoding(BaseCharset value)
Sets the encoding to use.protected void
setLastError(String value)
Sets the value for the last error that occurred during read.void
setSpreadSheetType(SpreadSheet value)
Sets the type of spreadsheet to use.String
spreadSheetTypeTipText()
Returns the tip text for this property.void
stopExecution()
Stops the reading (might not be immediate, depending on reader).protected boolean
supportsCompressedInput()
Returns whether to automatically handle gzip compressed files (AbstractSpreadSheetReader.InputType.READER
,AbstractSpreadSheetReader.InputType.STREAM
).-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, destroy, finishInit, getDefaultLoggingLevel, getOptionManager, globalInfo, loggingLevelTipText, newOptionManager, reset, setLoggingLevel, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.core.Destroyable
destroy
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel
-
Methods inherited from interface adams.core.option.OptionHandler
cleanUpOptions, getOptionManager, toCommandLine
-
Methods inherited from interface adams.data.io.input.SpreadSheetReader
getCorrespondingWriter
-
-
-
-
Field Detail
-
OPTION_INPUT
public static final String OPTION_INPUT
- See Also:
- Constant Field Values
-
OPTION_OUTPUT
public static final String OPTION_OUTPUT
- See Also:
- Constant Field Values
-
m_DataRowType
protected DataRow m_DataRowType
the data row type to use.
-
m_SpreadSheetType
protected SpreadSheet m_SpreadSheetType
the type of spreadsheet to use.
-
m_Stopped
protected boolean m_Stopped
whether the read process was stopped through an external source.
-
m_Encoding
protected BaseCharset m_Encoding
the encoding to use.
-
m_LastError
protected String m_LastError
the last error that occurred.
-
-
Method Detail
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptions
in interfaceOptionHandler
- Overrides:
defineOptions
in classAbstractOptionHandler
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initialize
in classAbstractOptionHandler
-
getFormatDescription
public abstract String getFormatDescription()
Returns a string describing the format (used in the file chooser).- Specified by:
getFormatDescription
in interfaceFileFormatHandler
- Specified by:
getFormatDescription
in interfaceSpreadSheetReader
- Returns:
- a description suitable for displaying in the file chooser
-
getFormatExtensions
public abstract String[] getFormatExtensions()
Returns the extension(s) of the format.- Specified by:
getFormatExtensions
in interfaceFileFormatHandler
- Specified by:
getFormatExtensions
in interfaceSpreadSheetReader
- Returns:
- the extension (without the dot!)
-
getDefaultFormatExtension
public String getDefaultFormatExtension()
Returns the default extension of the format.- Specified by:
getDefaultFormatExtension
in interfaceFileFormatHandler
- Specified by:
getDefaultFormatExtension
in interfaceSpreadSheetReader
- Returns:
- the default extension (without the dot!)
-
getAdditionalInformation
public String getAdditionalInformation()
Returns the additional information.- Specified by:
getAdditionalInformation
in interfaceAdditionalInformationHandler
- Returns:
- the additional information, null or 0-length string for no information
-
getDefaultSpreadSheet
protected SpreadSheet getDefaultSpreadSheet()
Returns the default spreadsheet type.- Returns:
- the default
-
getDefaultDataRowType
protected DataRow getDefaultDataRowType()
Returns the default row type.- Returns:
- the default
-
setDataRowType
public void setDataRowType(DataRow value)
Sets the type of data row to use.- Specified by:
setDataRowType
in interfaceDataRowTypeHandler
- Specified by:
setDataRowType
in interfaceSpreadSheetReader
- Parameters:
value
- the type
-
getDataRowType
public DataRow getDataRowType()
Returns the type of data row to use.- Specified by:
getDataRowType
in interfaceDataRowTypeHandler
- Specified by:
getDataRowType
in interfaceSpreadSheetReader
- Returns:
- the type
-
dataRowTypeTipText
public String dataRowTypeTipText()
Returns the tip text for this property.- Specified by:
dataRowTypeTipText
in interfaceDataRowTypeHandler
- Specified by:
dataRowTypeTipText
in interfaceSpreadSheetReader
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setSpreadSheetType
public void setSpreadSheetType(SpreadSheet value)
Sets the type of spreadsheet to use.- Specified by:
setSpreadSheetType
in interfaceSpreadSheetReader
- Specified by:
setSpreadSheetType
in interfaceSpreadSheetTypeHandler
- Parameters:
value
- the type
-
getSpreadSheetType
public SpreadSheet getSpreadSheetType()
Returns the type of spreadsheet to use.- Specified by:
getSpreadSheetType
in interfaceSpreadSheetReader
- Specified by:
getSpreadSheetType
in interfaceSpreadSheetTypeHandler
- Returns:
- the type
-
spreadSheetTypeTipText
public String spreadSheetTypeTipText()
Returns the tip text for this property.- Specified by:
spreadSheetTypeTipText
in interfaceSpreadSheetReader
- Specified by:
spreadSheetTypeTipText
in interfaceSpreadSheetTypeHandler
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
setEncoding
public void setEncoding(BaseCharset value)
Sets the encoding to use.- Specified by:
setEncoding
in interfaceEncodingSupporter
- Parameters:
value
- the encoding, e.g. "UTF-8" or "UTF-16", empty string for default
-
getEncoding
public BaseCharset getEncoding()
Returns the encoding to use.- Specified by:
getEncoding
in interfaceEncodingSupporter
- Returns:
- the encoding, e.g. "UTF-8" or "UTF-16", empty string for default
-
encodingTipText
public String encodingTipText()
Returns the tip text for this property.- Specified by:
encodingTipText
in interfaceEncodingSupporter
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
read
public SpreadSheet read(File file)
Reads the spreadsheet from the specified file.- Specified by:
read
in interfaceSpreadSheetReader
- Parameters:
file
- the file to read from- Returns:
- null in case of an error, otherwise the spreadsheet
-
getInputType
protected abstract AbstractSpreadSheetReader.InputType getInputType()
Returns how to read the data, from a file, stream or reader.- Returns:
- how to read the data
-
supportsCompressedInput
protected boolean supportsCompressedInput()
Returns whether to automatically handle gzip compressed files (AbstractSpreadSheetReader.InputType.READER
,AbstractSpreadSheetReader.InputType.STREAM
).
Default implementation returns false.- Returns:
- true if to automatically decompress
-
canDecompress
protected boolean canDecompress(String filename)
Returns whether the file should get decompressed, i.e.,supportsCompressedInput()
returns true and the filename ends with ".gz".- Parameters:
filename
- the filename to check- Returns:
- true if decompression should occur
-
check
protected void check()
Hook method to perform some checks before performing the actual read.
Default implementation does nothing.
-
read
public SpreadSheet read(String filename)
Reads the spreadsheet from the given file. For input typesAbstractSpreadSheetReader.InputType.STREAM
andAbstractSpreadSheetReader.InputType.READER
, decompression (ie gzip) is automatically handled if the filename ends in ".gz".- Specified by:
read
in interfaceSpreadSheetReader
- Parameters:
filename
- the file to read from- Returns:
- the spreadsheet or null in case of an error
- See Also:
supportsCompressedInput()
-
read
public SpreadSheet read(InputStream stream)
Reads the spreadsheet from the stream. The caller must ensure to close the stream.- Specified by:
read
in interfaceSpreadSheetReader
- Parameters:
stream
- the stream to read from- Returns:
- the spreadsheet or null in case of an error
-
read
public SpreadSheet read(Reader r)
Reads the spreadsheet from the given reader. The caller must ensure to close the reader.- Specified by:
read
in interfaceSpreadSheetReader
- Parameters:
r
- the reader to read from- Returns:
- the spreadsheet or null in case of an error
-
doRead
protected SpreadSheet doRead(File file)
Performs the actual reading. Must handle compression itself, ifsupportsCompressedInput()
returns true.
Default implementation returns null.- Parameters:
file
- the file to read from- Returns:
- the spreadsheet or null in case of an error
- See Also:
getInputType()
,supportsCompressedInput()
-
doRead
protected SpreadSheet doRead(Reader r)
Performs the actual reading.
Default implementation returns null.- Parameters:
r
- the reader to read from- Returns:
- the spreadsheet or null in case of an error
- See Also:
getInputType()
-
doRead
protected SpreadSheet doRead(InputStream in)
Performs the actual reading.
Default implementation returns null.- Parameters:
in
- the input stream to read from- Returns:
- the spreadsheet or null in case of an error
- See Also:
getInputType()
-
stopExecution
public void stopExecution()
Stops the reading (might not be immediate, depending on reader).- Specified by:
stopExecution
in interfaceSpreadSheetReader
- Specified by:
stopExecution
in interfaceStoppable
-
isStopped
public boolean isStopped()
Returns whether the reading was stopped.- Specified by:
isStopped
in interfaceSpreadSheetReader
- Specified by:
isStopped
in interfaceStoppableWithFeedback
- Returns:
- true if stopped
-
hasLastError
public boolean hasLastError()
Returns whether an error was encountered during the last read.- Specified by:
hasLastError
in interfaceErrorProvider
- Specified by:
hasLastError
in interfaceSpreadSheetReader
- Returns:
- true if an error occurred
-
setLastError
protected void setLastError(String value)
Sets the value for the last error that occurred during read.- Parameters:
value
- the error string, null if none occurred
-
getLastError
public String getLastError()
Returns the error that occurred during the last read.- Specified by:
getLastError
in interfaceErrorProvider
- Specified by:
getLastError
in interfaceSpreadSheetReader
- Returns:
- the error string, null if none occurred
-
getReaders
public static String[] getReaders()
Returns a list with classnames of readers.- Returns:
- the reader classnames
-
runReader
public static void runReader(Class env, Class reader, String[] args)
Runs the reader from commandline.- Parameters:
env
- the environment class to usereader
- the reader to executeargs
- the commandline arguments, use --input to specify the input file/dir and --output-dir to specify directory to save them as .spec files
-
-