Package net.sf.basedb.util.basefile
Class BaseFileParser
java.lang.Object
net.sf.basedb.util.basefile.BaseFileParser
Parser for serial and matrix BASEfile:s. This class will setup and
perform the line-by-line parsing, but it doesn't handle the data.
Data is handled section-wise by registering
BaseFileSectionParser
:s
with setSectionParser(String, BaseFileSectionParser)
.
Sections that doens't have a registered parser are skipped.- Version:
- 2.14
- Author:
- Nicklas
- Last modified
- $Date: 2011-03-16 12:48:47 +0100 (on, 16 mar 2011) $
-
Field Summary
Modifier and TypeFieldDescriptionprivate final Map<String,
BaseFileSectionParser> private AbsoluteProgressReporter
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Copy redefined column names from the given parser into this parser.protected FlatFileParser
getInitializedFlatFileParser
(InputStream stream, String charset) Creates aFlatFileParser
for parsing a BASEfile.getRedefinedColumnName
(String section, String defaultName) Get the redefined column name for a section.getRequiredHeader
(FlatFileParser ffp, String header, String section, String filename) Get the value of a required header.getRequiredHeader
(FlatFileParser ffp, String header, String split, String section, String filename) Get the value of a header as a list of sub-values.int
getRequiredIndex
(List<String> values, String value, String header, String section, int line, String filename) Get the index of value in a list of values or throw an exception if the value is not found.int
getSectionCount
(String section) The number of times we have seen a certain section in the file.getSectionParser
(String section) Get the parser that is currently registered for a section.protected void
increaseSectionCount
(String section, int count) Counts the number of times a section has been seen in the file.parse
(InputStream in, String charset) Parse the given input stream.void
setProgress
(long completed, String message) Update the progress of the parsing.void
setProgressReporter
(AbsoluteProgressReporter progress) Set a progress reporter that will be used to report how the parsing is progressing.void
setRedefinedColumnName
(String section, String defaultName, String redefinedName) Redefined a column name for a specified section.void
setSectionParser
(String section, BaseFileSectionParser parser) Adds a section parser.
-
Field Details
-
parsers
-
progress
-
sectionCount
-
redefinedColumnNames
-
-
Constructor Details
-
BaseFileParser
public BaseFileParser()Creates a new parser object.
-
-
Method Details
-
setSectionParser
Adds a section parser. If a parser already exists for the given section it is replaced.- Parameters:
section
- The name of the section the parser handlesparser
- The parser, or null to remove a previously registered parser
-
getSectionParser
Get the parser that is currently registered for a section.- Parameters:
section
- The name of the section- Returns:
- A section parser, or null if no parser is registered
-
setRedefinedColumnName
Redefined a column name for a specified section. This function is useful to configure parsers that normally look for, for example, the column 'reporter' to instead look for 'Reporter ID'.- Parameters:
section
- The section the redefined name is valid fordefaultName
- The default name (eg. 'reporter')redefinedName
- The redefined name (ef. 'Reporter ID')
-
getRedefinedColumnName
Get the redefined column name for a section. If the name has not been redefined, the default name is returned.- Parameters:
section
- The section the redefined name is valid fordefaultName
- The default name- Returns:
- The redefined name, or the default name if it has not been redefined
-
copyRedefinedColumnNames
Copy redefined column names from the given parser into this parser. Column names that have already been redefined in this parser will be overwritten only if they have also been redefined in the other parser.- Parameters:
parser
- The parser to copy redefined column names from
-
setProgressReporter
Set a progress reporter that will be used to report how the parsing is progressing.- Parameters:
progress
- A progress reporter, or null to not report progress
-
parse
Parse the given input stream.- Parameters:
in
- The data stream to parsecharset
- The character set used by the data, or null to use the configured default- Returns:
- The FlatFileParser the was used to parse the BASEfile
- Throws:
IOException
- If there is any problem with the parsing
-
getSectionCount
The number of times we have seen a certain section in the file.- Parameters:
section
- The name of the section
-
increaseSectionCount
Counts the number of times a section has been seen in the file.- Parameters:
section
- The name of the sectioncount
- The count to add to the current count
-
setProgress
Update the progress of the parsing.- See Also:
-
getInitializedFlatFileParser
Creates aFlatFileParser
for parsing a BASEfile.- Parameters:
stream
- The stream that the parser should read from.- Returns:
- The initialised parser
-
getRequiredHeader
Get the value of a required header. This method callsFlatFileParser.getHeader(String)
to get the value of the header. If the header is missing or has an empty value an exception is thrown.- Parameters:
ffp
- The flat file parserheader
- The name of the headersection
- The name of the section that is being parsedfilename
- The name of the file that is being parsed (used to create the error message in case the header is missing)- Returns:
- The value of the header
-
getRequiredHeader
public List<String> getRequiredHeader(FlatFileParser ffp, String header, String split, String section, String filename) Get the value of a header as a list of sub-values. If the header is missing or empty an exception is thrown. Otherwise the 'split' regular expression is used to the split the value into a list of values.- Parameters:
ffp
- The flat file parserheader
- The name of the headersplit
- A regular expression used to split the valuesection
- The name of the section that is being parsedfilename
- The name of the file that is being parsed (used to create the error message in case the header is missing)- Returns:
- A list with the values of the header
-
getRequiredIndex
public int getRequiredIndex(List<String> values, String value, String header, String section, int line, String filename) Get the index of value in a list of values or throw an exception if the value is not found. The lookup is case-insensitive.- Parameters:
values
- The list of values to look invalue
- The value to look forheader
- The name of the header the values are fromsection
- The name of the section that is being parsedline
- The current line number in the parsed filefilename
- The The name of the file that is being parsed (used to create the error message in case the header is missing)- Returns:
- The index of the value
-