Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

csv.util
Class BeanWriter

java.lang.Object
  extended by csv.util.BeanWriter

public class BeanWriter
extends java.lang.Object

Writes beans to an underlying table writer. The attributes written are either determined by inspection of first bean to be written or by explicitely setting them through special methods.

Author:
ralph

Constructor Summary
BeanWriter(TableWriter writer, boolean writeHeaderRow)
          Constructor.
 
Method Summary
protected  void addGetter(java.lang.Class<?> clazz, java.lang.String attribute)
          Adds a getter method to the methods being used for retrieveing column values.
protected  void addGetter(java.lang.String attributeName, java.lang.reflect.Method method)
          Adds a getter method to the methods being used for retrieveing column values.
 void close()
          Closes the underlying writer.
 java.lang.Object[] convertToColumns(java.lang.Object bean)
          Converts the given bean to an object array.
protected  void createAttributeList(java.lang.Class<?> clazz)
          Creates the attribute list by introspection.
protected  void createAttributeList(java.lang.Class<?> clazz, java.util.List<java.lang.String> attributes)
          Creates the attribute list from given list.
protected  void createAttributeList(java.lang.Class<?> clazz, java.lang.String[] attributes)
          Creates the attribute list from given list.
protected  java.lang.Object getAttribute(int index, java.lang.Object bean)
          Returns the value for given bean at given column index.
protected  java.lang.Object getAttribute(java.lang.reflect.Method method, java.lang.Object bean)
          Returns the attribute value for given bean.
protected  java.lang.Object getAttribute(java.lang.String attribute, java.lang.Object bean)
          Returns the attribute value for given bean.
protected  java.lang.String getAttributeName(java.lang.reflect.Method m)
          Returns the attribute name derived from method name
 TableWriter getWriter()
          Returns the current writer.
protected  boolean isValidGetterMethod(java.lang.reflect.Method method)
          Returns true if method conforms to JavaBean style of a Getter.
 boolean isWriteHeaderRow()
          Returns true when a header row with attribute names shall be written.
protected  void setWriteHeaderRow(boolean writeHeaderRow)
          Sets whether a header row with attribute names shall be written.
protected  void setWriter(TableWriter writer)
          Sets the writer.
 void writeBean(java.lang.Object bean)
          Writes the bean to the underlying table writer.
 int writeBeans(java.util.Collection<? extends java.lang.Object> collection)
          Copies the beans from the collection to this bean writer.
 int writeBeans(java.util.Iterator<? extends java.lang.Object> i)
          Copies the beans from the collection to this bean writer.
 int writeBeans(java.lang.Object[] arr)
          Copies the beans from the collection to this bean writer.
protected  void writeHeaderRow()
          Writes the header row to the underlying table writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanWriter

public BeanWriter(TableWriter writer,
                  boolean writeHeaderRow)
Constructor.

Parameters:
writer - the underlying writer
writeHeaderRow - whether a header row with attribute names shall be written
Method Detail

isWriteHeaderRow

public boolean isWriteHeaderRow()
Returns true when a header row with attribute names shall be written.

Returns:
the writeHeaderRow

setWriteHeaderRow

protected void setWriteHeaderRow(boolean writeHeaderRow)
Sets whether a header row with attribute names shall be written.

Parameters:
writeHeaderRow - true if header row shall be written.

writeBean

public void writeBean(java.lang.Object bean)
               throws java.io.IOException
Writes the bean to the underlying table writer.

Parameters:
bean - the bean to be written
Throws:
java.io.IOException - if bean cannot be written
See Also:
TableWriter.printRow(Object[]), convertToColumns(Object)

close

public void close()
Closes the underlying writer.


getWriter

public TableWriter getWriter()
Returns the current writer.

Returns:
the writer

setWriter

protected void setWriter(TableWriter writer)
Sets the writer.

Parameters:
writer - the writer

convertToColumns

public java.lang.Object[] convertToColumns(java.lang.Object bean)
Converts the given bean to an object array.

Parameters:
bean - bean to be converted
Returns:
object array with attribute values

getAttribute

protected java.lang.Object getAttribute(int index,
                                        java.lang.Object bean)
Returns the value for given bean at given column index.

Parameters:
index - column index
bean - bean object
Returns:
value in column

getAttribute

protected java.lang.Object getAttribute(java.lang.String attribute,
                                        java.lang.Object bean)
Returns the attribute value for given bean.

Parameters:
attribute - name of attribute
bean - bean object
Returns:
value of attribute

getAttribute

protected java.lang.Object getAttribute(java.lang.reflect.Method method,
                                        java.lang.Object bean)
Returns the attribute value for given bean.

Parameters:
method - method that will deliver the value
bean - bean object
Returns:
value of attribute

createAttributeList

protected void createAttributeList(java.lang.Class<?> clazz,
                                   java.util.List<java.lang.String> attributes)
Creates the attribute list from given list. All JavaBean getter methods will be taken over.

Parameters:
clazz - Class to introspect
attributes - list of attributes

createAttributeList

protected void createAttributeList(java.lang.Class<?> clazz,
                                   java.lang.String[] attributes)
Creates the attribute list from given list. All JavaBean getter methods will be taken over.

Parameters:
clazz - Class to introspect
attributes - list of attributes

createAttributeList

protected void createAttributeList(java.lang.Class<?> clazz)
Creates the attribute list by introspection. All JavaBean getter methods will be taken over.

Parameters:
clazz - Class to introspect

addGetter

protected void addGetter(java.lang.Class<?> clazz,
                         java.lang.String attribute)
Adds a getter method to the methods being used for retrieveing column values. The metthod must have a return type and no arguments.

Parameters:
clazz - Class to introspect
attribute - name of attribute

isValidGetterMethod

protected boolean isValidGetterMethod(java.lang.reflect.Method method)
Returns true if method conforms to JavaBean style of a Getter.

Parameters:
method - method
Returns:
true if method is a getter.

addGetter

protected void addGetter(java.lang.String attributeName,
                         java.lang.reflect.Method method)
Adds a getter method to the methods being used for retrieveing column values. The metthod must have a return type and no arguments.

Parameters:
attributeName - name of attribute
method - method name.

writeHeaderRow

protected void writeHeaderRow()
Writes the header row to the underlying table writer.


getAttributeName

protected java.lang.String getAttributeName(java.lang.reflect.Method m)
Returns the attribute name derived from method name

Parameters:
m - method
Returns:
attribute name

writeBeans

public int writeBeans(java.util.Collection<? extends java.lang.Object> collection)
               throws java.io.IOException
Copies the beans from the collection to this bean writer.

Parameters:
collection - collection that contains JavaBeans
Returns:
number of rows written
Throws:
java.io.IOException - when there is a problem with the writer.

writeBeans

public int writeBeans(java.util.Iterator<? extends java.lang.Object> i)
               throws java.io.IOException
Copies the beans from the collection to this bean writer.

Parameters:
i - iterator that delivers JavaBeans
Returns:
number of rows written
Throws:
java.io.IOException - when there is a problem with the writer.

writeBeans

public int writeBeans(java.lang.Object[] arr)
               throws java.io.IOException
Copies the beans from the collection to this bean writer.

Parameters:
arr - array with beans
Returns:
number of rows written
Throws:
java.io.IOException - when there is a problem with the writer.

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.