net.sf.flatpack.util
Class ParserUtils

java.lang.Object
  extended by net.sf.flatpack.util.ParserUtils

public final class ParserUtils
extends java.lang.Object

Static utilities that are used to perform parsing in the DataSet class These can also be used for low level parsing, if not wishing to use the DataSet class.

Author:
Paul Zepernick, Benoit Xhenseval

Method Summary
static java.util.Map buidColumnIndexMap(java.util.List columns, Parser p)
          Build a map of name/position based on a list of ColumnMetaData.
static java.util.List buildMDFromSQLTable(java.sql.Connection con, java.lang.String dataDefinition)
          Returns a definition of pz column metadata from a given pz datastructure held in an SQL database
static java.util.List buildMDFromSQLTable(java.sql.Connection con, java.lang.String dataDefinition, Parser parser)
          Returns a definition of pz column metadata from a given pz datastructure held in an SQL database
static java.util.Map calculateRecordLengths(MetaData columnMD)
           
static void closeReader(java.io.InputStream reader)
          Closes the given reader
static void closeReader(java.io.Reader reader)
          Closes the given reader
static java.io.InputStream createInputStream(java.io.File file)
          Create an InputStream based on a File.
static java.lang.String getCMDKeyForDelimitedFile(MetaData columnMD, java.util.List lineElements)
           
static int getColumnIndex(java.lang.String key, MetaData columnMD, java.lang.String colName, Parser p)
           
static java.util.List getColumnMDFromFile(java.io.File theFile, java.lang.String delimiter, java.lang.String qualifier)
          Returns a list of ColumnMetaData objects.
static java.util.List getColumnMetaData(java.lang.String key, MetaData columnMD)
           
static int getDelimiterOffset(java.lang.String line, int start, char delimiter)
          reads from the specified point in the line and returns how many chars to the specified delimiter
static MetaData getPZMetaDataFromFile(java.lang.String line, char delimiter, char qualifier, Parser p)
          Returns a list of ColumnMetaData objects.
static boolean isListElementsEmpty(java.util.List l)
          Checks a list of elements to see if every element in the list is empty.
static boolean isMultiLine(char[] chrArry, char delimiter, char qualifier)
          Determines if the given line is the first part of a multiline record
static java.util.Properties loadConvertProperties()
          Retrieves the conversion table for use with the getObject() method in IDataSet
static java.lang.String lTrim(java.lang.String value)
          Removes empty space from the beginning of a string
static java.lang.String lTrimKeepTabs(java.lang.String value)
          Removes empty space from the beginning of a string, except for tabs
static java.lang.String padding(int repeat, char padChar)
           Returns padding using the specified delimiter repeated to a given length.
static java.lang.String removeChar(char theChar, java.lang.String theString)
          Removes a single string character from a given string
static java.lang.String rTrim(java.lang.String value)
          Removes empty space from the end of a string
static java.lang.Object runPzConverter(java.util.Properties classXref, java.lang.String value, java.lang.Class typeToReturn)
          Converts a String value to the appropriate Object via the correct net.sf.flatpack.converter.PZConverter implementation
static java.util.List splitLine(java.lang.String line, char delimiter, char qualifier, int initialSize)
          Returns an ArrayList of items in a delimited string.
static java.lang.String stripNonDoubleChars(java.lang.String value)
          Removes chars from the String that could not be parsed into a Double value
static java.lang.String stripNonLongChars(java.lang.String value)
          Removes chars from the String that could not be parsed into a Long value PZStringUtils.stripNonLongChars("1000.25") = "1000" Method will truncate everything to the right of the decimal place when encountered.
static java.lang.String trimToNull(java.lang.String value)
          Will return a null if the String is empty returns the trimmed string otherwise.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

splitLine

public static java.util.List splitLine(java.lang.String line,
                                       char delimiter,
                                       char qualifier,
                                       int initialSize)
Returns an ArrayList of items in a delimited string. If there is no qualifier around the text, the qualifier parameter can be left null, or empty. There should not be any line breaks in the string. Each line of the file should be passed in individually. Elements which are not qualified will have leading and trailing white space removed. This includes unqualified elements, which may be contained in an unqualified parse: "data", data ,"data" Special thanks to Benoit for contributing this much improved speedy parser :0)

Parameters:
line - - String of data to be parsed
delimiter - - Delimiter seperating each element
qualifier - - qualifier which is surrounding the text
initialSize - - intial capacity of the List size
Returns:
List

getDelimiterOffset

public static int getDelimiterOffset(java.lang.String line,
                                     int start,
                                     char delimiter)
reads from the specified point in the line and returns how many chars to the specified delimiter

Parameters:
line -
start -
delimiter -
Returns:
int

lTrim

public static java.lang.String lTrim(java.lang.String value)
Removes empty space from the beginning of a string

Parameters:
value - - to be trimmed
Returns:
String

lTrimKeepTabs

public static java.lang.String lTrimKeepTabs(java.lang.String value)
Removes empty space from the beginning of a string, except for tabs

Parameters:
value - - to be trimmed
Returns:
String

rTrim

public static java.lang.String rTrim(java.lang.String value)
Removes empty space from the end of a string

Parameters:
value - - to be trimmed
Returns:
String

trimToNull

public static java.lang.String trimToNull(java.lang.String value)
Will return a null if the String is empty returns the trimmed string otherwise.

Parameters:
value - to be trimmed
Returns:
String

removeChar

public static java.lang.String removeChar(char theChar,
                                          java.lang.String theString)
Removes a single string character from a given string

Parameters:
theChar - - string char
theString - - string to search
Returns:
String

getPZMetaDataFromFile

public static MetaData getPZMetaDataFromFile(java.lang.String line,
                                             char delimiter,
                                             char qualifier,
                                             Parser p)
Returns a list of ColumnMetaData objects. This is for use with delimited files. The first line of the file which contains data will be used as the column names

Parameters:
line -
delimiter -
qualifier -
p - PZParser used to specify additional option when working with the ColumnMetaData. Can be null
Returns:
PZMetaData

getColumnMDFromFile

public static java.util.List getColumnMDFromFile(java.io.File theFile,
                                                 java.lang.String delimiter,
                                                 java.lang.String qualifier)
                                          throws java.io.IOException
Returns a list of ColumnMetaData objects. This is for use with delimited files. The first line of the file which contains data will be used as the column names

Parameters:
theFile -
delimiter -
qualifier -
Returns:
ArrayList - ColumnMetaData
Throws:
java.io.FileNotFoundException
java.io.IOException

isMultiLine

public static boolean isMultiLine(char[] chrArry,
                                  char delimiter,
                                  char qualifier)
Determines if the given line is the first part of a multiline record

Parameters:
chrArry - - char data of the line
delimiter - - delimiter being used
qualifier - - qualifier being used
Returns:
boolean

calculateRecordLengths

public static java.util.Map calculateRecordLengths(MetaData columnMD)

getCMDKeyForDelimitedFile

public static java.lang.String getCMDKeyForDelimitedFile(MetaData columnMD,
                                                         java.util.List lineElements)

getColumnMetaData

public static java.util.List getColumnMetaData(java.lang.String key,
                                               MetaData columnMD)

getColumnIndex

public static int getColumnIndex(java.lang.String key,
                                 MetaData columnMD,
                                 java.lang.String colName,
                                 Parser p)

createInputStream

public static java.io.InputStream createInputStream(java.io.File file)
                                             throws java.io.FileNotFoundException
Create an InputStream based on a File.

Parameters:
file - The file.
Returns:
the InputStream.
Throws:
java.io.FileNotFoundException

closeReader

public static void closeReader(java.io.Reader reader)
Closes the given reader

Parameters:
reader -

closeReader

public static void closeReader(java.io.InputStream reader)
Closes the given reader

Parameters:
reader -

padding

public static java.lang.String padding(int repeat,
                                       char padChar)

Returns padding using the specified delimiter repeated to a given length.

                PZStringUtils.padding(0, 'e')  = ""
                PZStringUtils.padding(3, 'e')  = "eee"
                PZStringUtils.padding(-2, 'e') = IndexOutOfBoundsException
 

Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using #repeat(String, int) instead.

Parameters:
repeat - number of times to repeat delim
padChar - character to repeat
Returns:
String with repeated character
Throws:
java.lang.IndexOutOfBoundsException - if repeat < 0
See Also:
#repeat(String, int)

buidColumnIndexMap

public static java.util.Map buidColumnIndexMap(java.util.List columns,
                                               Parser p)
Build a map of name/position based on a list of ColumnMetaData.

Parameters:
columns -
p - Reference to Parser which can provide additional options on how the map should be build. This can be NULL.
Returns:
a new Map

stripNonLongChars

public static java.lang.String stripNonLongChars(java.lang.String value)
Removes chars from the String that could not be parsed into a Long value PZStringUtils.stripNonLongChars("1000.25") = "1000" Method will truncate everything to the right of the decimal place when encountered.

Parameters:
value -
Returns:
String

stripNonDoubleChars

public static java.lang.String stripNonDoubleChars(java.lang.String value)
Removes chars from the String that could not be parsed into a Double value

Parameters:
value -
Returns:
String

loadConvertProperties

public static java.util.Properties loadConvertProperties()
                                                  throws java.io.IOException
Retrieves the conversion table for use with the getObject() method in IDataSet

Returns:
Properties Properties contained in the pzconvert.properties file
Throws:
java.io.IOException

isListElementsEmpty

public static boolean isListElementsEmpty(java.util.List l)
Checks a list of elements to see if every element in the list is empty.

Parameters:
l - List of
Returns:
boolean true when all elements are empty

runPzConverter

public static java.lang.Object runPzConverter(java.util.Properties classXref,
                                              java.lang.String value,
                                              java.lang.Class typeToReturn)
Converts a String value to the appropriate Object via the correct net.sf.flatpack.converter.PZConverter implementation

Parameters:
classXref - Properties holding class cross reference
value - Value to be converted to the Object
typeToReturn - Type of object to be returned
Returns:
Object
Throws:
PZConvertExeption

buildMDFromSQLTable

public static java.util.List buildMDFromSQLTable(java.sql.Connection con,
                                                 java.lang.String dataDefinition)
                                          throws java.sql.SQLException
Returns a definition of pz column metadata from a given pz datastructure held in an SQL database

Parameters:
con - Database connection containing the Datafile and Datastructure tables
dataDefinition - Name of the data definition stored in the Datafile table
Returns:
List
Throws:
java.sql.SQLException

buildMDFromSQLTable

public static java.util.List buildMDFromSQLTable(java.sql.Connection con,
                                                 java.lang.String dataDefinition,
                                                 Parser parser)
                                          throws java.sql.SQLException
Returns a definition of pz column metadata from a given pz datastructure held in an SQL database

Parameters:
con - Database connection containing the Datafile and Datastructure tables
dataDefinition - Name of the data definition stored in the Datafile table
parser - Instance of the parser being used for the file. It will be checked to get the table names for the DATASTRUCTURE table and DATAFILE table.
Returns:
List
Throws:
java.sql.SQLException


Copyright © 2006-2011 Appendium - Portfolio Financing Platform. All Rights Reserved.