Package org.apache.pivot.serialization
Class CSVSerializer
- java.lang.Object
-
- org.apache.pivot.serialization.CSVSerializer
-
- All Implemented Interfaces:
Serializer<List<?>>
public class CSVSerializer extends java.lang.Object implements Serializer<List<?>>
Implementation of theSerializerinterface that reads data from and writes data to a comma-separated value (CSV) file.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCSV_EXTENSIONstatic java.nio.charset.CharsetDEFAULT_CHARSETstatic java.lang.reflect.TypeDEFAULT_ITEM_TYPEstatic java.lang.StringMIME_TYPE
-
Constructor Summary
Constructors Constructor Description CSVSerializer()CSVSerializer(java.lang.reflect.Type itemType)CSVSerializer(java.nio.charset.Charset charset)CSVSerializer(java.nio.charset.Charset charset, java.lang.reflect.Type itemType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.nio.charset.CharsetgetCharset()Returns the character set used to encode/decode the CSV data.ListenerList<CSVSerializerListener>getCSVSerializerListeners()java.lang.reflect.TypegetItemType()Returns the type of the item that will be instantiated by the serializer during a read operation.Sequence<java.lang.String>getKeys()Returns the keys that will be read or written by this serializer.java.lang.StringgetMIMEType(List<?> objects)Returns the MIME type of the data read and written by this serializer.booleangetWriteKeys()Returns the serializer's write keys flag.booleanisVerbose()Returns the serializer's verbosity flag.List<?>readObject(java.io.InputStream inputStream)Reads values from a comma-separated value stream.List<?>readObject(java.io.Reader reader)Reads values from a comma-separated value stream.voidsetKeys(java.lang.String... keys)Sets the keys that will be read or written by this serializer.voidsetKeys(Sequence<java.lang.String> keys)Sets the keys that will be read or written by this serializer.voidsetVerbose(boolean verbose)Sets the serializer's verbosity flag.voidsetWriteKeys(boolean writeKeys)Sets the serializer's write keys flag.voidwriteObject(List<?> items, java.io.OutputStream outputStream)Writes values to a comma-separated value stream.voidwriteObject(List<?> items, java.io.Writer writer)Writes values to a comma-separated value stream.
-
-
-
Field Detail
-
DEFAULT_CHARSET
public static final java.nio.charset.Charset DEFAULT_CHARSET
-
DEFAULT_ITEM_TYPE
public static final java.lang.reflect.Type DEFAULT_ITEM_TYPE
-
CSV_EXTENSION
public static final java.lang.String CSV_EXTENSION
- See Also:
- Constant Field Values
-
MIME_TYPE
public static final java.lang.String MIME_TYPE
- See Also:
- Constant Field Values
-
-
Method Detail
-
getCharset
public java.nio.charset.Charset getCharset()
Returns the character set used to encode/decode the CSV data.- Returns:
- The current character set.
-
getItemType
public java.lang.reflect.Type getItemType()
Returns the type of the item that will be instantiated by the serializer during a read operation.- Returns:
- The type of the item to be returned by the serializer.
-
getKeys
public Sequence<java.lang.String> getKeys()
Returns the keys that will be read or written by this serializer.- Returns:
- The sequence of read/write keys.
-
setKeys
public void setKeys(Sequence<java.lang.String> keys)
Sets the keys that will be read or written by this serializer.- Parameters:
keys- The keys to be read/written.- Throws:
java.lang.IllegalArgumentException- fornullinput.
-
setKeys
public void setKeys(java.lang.String... keys)
Sets the keys that will be read or written by this serializer.- Parameters:
keys- The list of keys to be read/written.- Throws:
java.lang.IllegalArgumentException- fornullinput.
-
getWriteKeys
public boolean getWriteKeys()
Returns the serializer's write keys flag.- Returns:
trueif keys will be written,falseotherwise.- See Also:
setWriteKeys(boolean)
-
setWriteKeys
public void setWriteKeys(boolean writeKeys)
Sets the serializer's write keys flag.- Parameters:
writeKeys- Iftrue, the first line of the output will contain the keys. Otherwise, the first line will contain the first line of data.
-
isVerbose
public boolean isVerbose()
Returns the serializer's verbosity flag.- Returns:
trueif the serializer is echoing input,falseif not.
-
setVerbose
public void setVerbose(boolean verbose)
Sets the serializer's verbosity flag. When verbosity is enabled, all data read or written will be echoed to the console.- Parameters:
verbose- Whether or not to echo the input.
-
readObject
public List<?> readObject(java.io.InputStream inputStream) throws java.io.IOException, SerializationException
Reads values from a comma-separated value stream.- Specified by:
readObjectin interfaceSerializer<List<?>>- Parameters:
inputStream- The input stream from which data will be read.- Returns:
- The list of values read from the stream.
- Throws:
java.io.IOException- for any errors during reading.SerializationException- for any formatting errors with the data.java.lang.IllegalArgumentException- fornullinput stream.- See Also:
readObject(Reader)
-
readObject
public List<?> readObject(java.io.Reader reader) throws java.io.IOException, SerializationException
Reads values from a comma-separated value stream.- Parameters:
reader- The reader from which data will be read.- Returns:
- A list containing the data read from the CSV file. The list items
are instances of
Dictionary<String, Object>populated by mapping columns in the CSV file to keys in the key sequence.If no keys have been specified when this method is called, they are assumed to be defined in the first line of the file.
- Throws:
java.io.IOException- for any errors during reading.SerializationException- for any formatting errors with the data.java.lang.IllegalArgumentException- fornullinput reader.
-
writeObject
public void writeObject(List<?> items, java.io.OutputStream outputStream) throws java.io.IOException, SerializationException
Writes values to a comma-separated value stream.- Specified by:
writeObjectin interfaceSerializer<List<?>>- Parameters:
items- The list of items to write.outputStream- The output stream to which data will be written.- Throws:
java.io.IOException- for any errors during writing.SerializationException- for any formatting errors with the data.java.lang.IllegalArgumentException- fornullinput arguments.- See Also:
writeObject(List, Writer)
-
writeObject
public void writeObject(List<?> items, java.io.Writer writer) throws java.io.IOException
Writes values to a comma-separated value stream.- Parameters:
items- A list containing the data to write to the CSV file. List items must be instances ofDictionary<String, Object>. The dictionary values will be written out in the order specified by the key sequence.writer- The writer to which data will be written.- Throws:
java.io.IOException- for any errors during writing.java.lang.IllegalArgumentException- fornullinput arguments.
-
getMIMEType
public java.lang.String getMIMEType(List<?> objects)
Description copied from interface:SerializerReturns the MIME type of the data read and written by this serializer.- Specified by:
getMIMETypein interfaceSerializer<List<?>>- Parameters:
objects- If provided, allows the serializer to attach parameters to the returned MIME type containing more detailed information about the data. Ifnull, the base MIME type is returned.- Returns:
- The MIME type of the current data.
-
getCSVSerializerListeners
public ListenerList<CSVSerializerListener> getCSVSerializerListeners()
-
-