Class CSVInterface


public class CSVInterface extends FILEInterface
Interface for CSV files, used for resources and outputs. Settings (children of the interface element):
  • divider: column divider, default TAB (also for the value TAB)
  • linestoread: lines to read, e.g. 1-200,250
  • skipheaderlines: number of lines to skip at the start of the file
  • skipabovesize: size in bytes above which an existing output file causes the project to be skipped (with several such outputs, all files have to exceed their limit)
  • unit2fieldname: add the unit to the column names of outputs
  • datamode: how arrays are stored, COL (one column per element, default) or ROW (one line per element)
The first line of the data is the header.
Author:
Andreas Enders
  • Field Details

    • iDivider

      protected String iDivider
      column divider, default TAB
    • iSkipAboveSize

      protected long iSkipAboveSize
      size in bytes above which an existing output file causes the project to be skipped; -1 for no limit
    • iLinesToRead

      protected Integer[] iLinesToRead
      lines to read, or null for all
    • iUnit2Fieldname

      protected boolean iUnit2Fieldname
      add unit to field name in output
    • iDataMode

      protected CSVInterface.DATA_MODE iDataMode
      data mode for IO to read/write array values
    • iSkipHeaderLines

      protected Integer iSkipHeaderLines
      number of header lines to skip before reading the data
  • Constructor Details

    • CSVInterface

      protected CSVInterface(String aName, org.jdom2.Element aInterfaceElement)
      Creates the interface from its element, see the class description for the settings.
      Parameters:
      aName - name of the interface
      aInterfaceElement - the interface element
    • CSVInterface

      public CSVInterface(String aName, int aPoolSize, String aFileName, String aContent, String aDivider, long aSkipAboveSize, Integer[] aLinesToRead, Integer aSkipHeaderLines, Boolean aAddUnit2FieldName, CSVInterface.DATA_MODE aDataMode)
      Creates the interface without element.
      Parameters:
      aName - name of the interface
      aPoolSize - number of pooled files
      aFileName - the file name, may contain tokens
      aContent - the data as string instead of a file, may be null
      aDivider - column divider, TAB if empty
      aSkipAboveSize - size limit in bytes, -1 for no limit
      aLinesToRead - lines to read, or null for all
      aSkipHeaderLines - number of lines to skip at the start of the file, may be null
      aAddUnit2FieldName - add the unit to the column names of outputs (must not be null)
      aDataMode - how arrays are stored
  • Method Details

    • read

      public FWSimResourceCache read(FWSimSession aFwSimSession, String aProjectID, Object aCache, LinkedHashMap<String,String> aKeyMap, FWSimInputAdapter aResourceAdapter, String aUniqueID, FWSimInterface.INTERFACE_TYPE aInterfaceType) throws MissingSimResourceException
      Converts the lines of a CSV file (CSVTable, first line is the header) into a resource cache. The columns the resource reads are converted once in the shared table, so other resources and sessions reading the same file use the converted values (see convertColumns(CSVTable, LinkedHashMap, MultiHashMap, FWSimResourceCache)). Lines with too few columns or without key are skipped with a warning. Arrays are read in column mode (one column per element) or in row mode (consecutive lines with the same key form one array). If the adapter caches to the database, or for the project data (resource PROJECT_DATA, always cached), the lines are also written to a table.
      Specified by:
      read in class FWSimInterface<FilePool>
      Parameters:
      aFwSimSession - the session
      aProjectID - id of the current project
      aCache - the data read, in the format of the interface (e.g. String[][], result set, XML element)
      aKeyMap - key variables of the resource
      aResourceAdapter - the adapter the cache belongs to
      aUniqueID - unique id of the source data
      aInterfaceType - type of the source interface
      Returns:
      the new resource cache
      Throws:
      MissingSimResourceException - if the data cannot be converted
      See Also:
    • getInterface

      public FilePool getInterface(FWSimVarMap aVarMap, FWSimVariable.CONTENT_TYPE aContentType)
      Returns the file pool (with the divider of this interface); it is created on the first call.
      Overrides:
      getInterface in class FILEInterface
      Parameters:
      aVarMap - variable map used for replacing tokens (e.g. in file names), may be null
      aContentType - res for reading, out for writing
      Returns:
      the file pool, or null without file name and content
      See Also:
    • getDivider

      public String getDivider()
      Returns:
      the column divider (TAB if not configured)
    • setDivider

      public void setDivider(String aDivider)
      Sets the column divider (empty or TAB means TAB); the file pool is created again on the next use.
      Parameters:
      aDivider - the column divider
    • getLinesToRead

      public Integer[] getLinesToRead()
      Returns:
      the lines to read, or null for all
    • getSkipHeaderLines

      public Integer getSkipHeaderLines()
      Returns:
      the number of lines to skip at the start of the file, or null
    • getSkipAboveSize

      public long getSkipAboveSize()
      Returns:
      the size limit in bytes for skipping projects, -1 for no limit
    • getDataMode

      public CSVInterface.DATA_MODE getDataMode()
      Returns:
      how arrays are stored in the file
    • getAddUnitToFieldName

      public boolean getAddUnitToFieldName()
      Returns:
      true if the unit is added to the column names of outputs
    • toXML

      public org.jdom2.Element toXML()
      Adds divider, lines to read, header lines to skip, skip-above-size limit, unit2fieldname and data mode (settings differing from the default) to the element of the base class.
      Overrides:
      toXML in class FILEInterface
      Returns:
      the interface element