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 classAbstractSpreadSheetReader.InputTypeHow to read the data.
-
Field Summary
Fields Modifier and Type Field Description protected DataRowm_DataRowTypethe data row type to use.protected BaseCharsetm_Encodingthe encoding to use.protected Stringm_LastErrorthe last error that occurred.protected SpreadSheetm_SpreadSheetTypethe type of spreadsheet to use.protected booleanm_Stoppedwhether the read process was stopped through an external source.static StringOPTION_INPUTstatic StringOPTION_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 booleancanDecompress(String filename)Returns whether the file should get decompressed, i.e.,supportsCompressedInput()returns true and the filename ends with ".gz".protected voidcheck()Hook method to perform some checks before performing the actual read.StringdataRowTypeTipText()Returns the tip text for this property.voiddefineOptions()Adds options to the internal list of options.protected SpreadSheetdoRead(File file)Performs the actual reading.protected SpreadSheetdoRead(InputStream in)Performs the actual reading.protected SpreadSheetdoRead(Reader r)Performs the actual reading.StringencodingTipText()Returns the tip text for this property.StringgetAdditionalInformation()Returns the additional information.DataRowgetDataRowType()Returns the type of data row to use.protected DataRowgetDefaultDataRowType()Returns the default row type.StringgetDefaultFormatExtension()Returns the default extension of the format.protected SpreadSheetgetDefaultSpreadSheet()Returns the default spreadsheet type.BaseCharsetgetEncoding()Returns the encoding to use.abstract StringgetFormatDescription()Returns a string describing the format (used in the file chooser).abstract String[]getFormatExtensions()Returns the extension(s) of the format.protected abstract AbstractSpreadSheetReader.InputTypegetInputType()Returns how to read the data, from a file, stream or reader.StringgetLastError()Returns the error that occurred during the last read.static String[]getReaders()Returns a list with classnames of readers.SpreadSheetgetSpreadSheetType()Returns the type of spreadsheet to use.booleanhasLastError()Returns whether an error was encountered during the last read.protected voidinitialize()Initializes the members.booleanisStopped()Returns whether the reading was stopped.SpreadSheetread(File file)Reads the spreadsheet from the specified file.SpreadSheetread(InputStream stream)Reads the spreadsheet from the stream.SpreadSheetread(Reader r)Reads the spreadsheet from the given reader.SpreadSheetread(String filename)Reads the spreadsheet from the given file.static voidrunReader(Class env, Class reader, String[] args)Runs the reader from commandline.voidsetDataRowType(DataRow value)Sets the type of data row to use.voidsetEncoding(BaseCharset value)Sets the encoding to use.protected voidsetLastError(String value)Sets the value for the last error that occurred during read.voidsetSpreadSheetType(SpreadSheet value)Sets the type of spreadsheet to use.StringspreadSheetTypeTipText()Returns the tip text for this property.voidstopExecution()Stops the reading (might not be immediate, depending on reader).protected booleansupportsCompressedInput()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:
defineOptionsin interfaceOptionHandler- Overrides:
defineOptionsin classAbstractOptionHandler
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initializein classAbstractOptionHandler
-
getFormatDescription
public abstract String getFormatDescription()
Returns a string describing the format (used in the file chooser).- Specified by:
getFormatDescriptionin interfaceFileFormatHandler- Specified by:
getFormatDescriptionin 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:
getFormatExtensionsin interfaceFileFormatHandler- Specified by:
getFormatExtensionsin interfaceSpreadSheetReader- Returns:
- the extension (without the dot!)
-
getDefaultFormatExtension
public String getDefaultFormatExtension()
Returns the default extension of the format.- Specified by:
getDefaultFormatExtensionin interfaceFileFormatHandler- Specified by:
getDefaultFormatExtensionin interfaceSpreadSheetReader- Returns:
- the default extension (without the dot!)
-
getAdditionalInformation
public String getAdditionalInformation()
Returns the additional information.- Specified by:
getAdditionalInformationin 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:
setDataRowTypein interfaceDataRowTypeHandler- Specified by:
setDataRowTypein interfaceSpreadSheetReader- Parameters:
value- the type
-
getDataRowType
public DataRow getDataRowType()
Returns the type of data row to use.- Specified by:
getDataRowTypein interfaceDataRowTypeHandler- Specified by:
getDataRowTypein interfaceSpreadSheetReader- Returns:
- the type
-
dataRowTypeTipText
public String dataRowTypeTipText()
Returns the tip text for this property.- Specified by:
dataRowTypeTipTextin interfaceDataRowTypeHandler- Specified by:
dataRowTypeTipTextin 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:
setSpreadSheetTypein interfaceSpreadSheetReader- Specified by:
setSpreadSheetTypein interfaceSpreadSheetTypeHandler- Parameters:
value- the type
-
getSpreadSheetType
public SpreadSheet getSpreadSheetType()
Returns the type of spreadsheet to use.- Specified by:
getSpreadSheetTypein interfaceSpreadSheetReader- Specified by:
getSpreadSheetTypein interfaceSpreadSheetTypeHandler- Returns:
- the type
-
spreadSheetTypeTipText
public String spreadSheetTypeTipText()
Returns the tip text for this property.- Specified by:
spreadSheetTypeTipTextin interfaceSpreadSheetReader- Specified by:
spreadSheetTypeTipTextin 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:
setEncodingin 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:
getEncodingin 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:
encodingTipTextin 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:
readin 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.STREAMandAbstractSpreadSheetReader.InputType.READER, decompression (ie gzip) is automatically handled if the filename ends in ".gz".- Specified by:
readin 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:
readin 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:
readin 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:
stopExecutionin interfaceSpreadSheetReader- Specified by:
stopExecutionin interfaceStoppable
-
isStopped
public boolean isStopped()
Returns whether the reading was stopped.- Specified by:
isStoppedin interfaceSpreadSheetReader- Specified by:
isStoppedin interfaceStoppableWithFeedback- Returns:
- true if stopped
-
hasLastError
public boolean hasLastError()
Returns whether an error was encountered during the last read.- Specified by:
hasLastErrorin interfaceErrorProvider- Specified by:
hasLastErrorin 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:
getLastErrorin interfaceErrorProvider- Specified by:
getLastErrorin 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
-
-