public class Quoter
extends java.lang.Object
This class is used to parse the quoter utility parameters and execute the equivalent logic of the Progress utility.
The quoter utility will put the elements identified and extracted from input between double quotes (in the case where a double quote is found inside it will be doubled) the result being sent to output which may be a stream or a file.
The quoter utility can be called with the following parameters:
If specified, the input will be taken from the file, and in case the file doesn't exist or the file does not have read permission, an error will be sent to output announcing the source cannot be read.
If the file parameter is not specified the input will be taken from System.in.
When this option is used the input is read line by line and each line will be split using the specified delimiter before being sent to output.
Ex: OS-COMMAND QUOTER /home/table.csv -D , >/home/table.q
Delimiter and column options can never be used together!
The column ranges must be separated in the form start-end and separated by: , . No spaces must be present!
This option will read input line by line and if presented with valid column ranges will split the line into columns from the starting position of the column range to the end position of the column range (Ex of column range: 1-4,5-15,... the first column will start from the first character up to the 4'th character included).
Usage Ex: OS-COMMAND QUOTER /home/table.csv -C 1-2,4-20,22-26 >/home/table.q
Delimiter and column options can never be used together!
Error Handling.
When errors are encountered quoter will send the error message to the output, then quit.
The following errors messages will be output:
This error is sent to output when quoter is called with an option different from -C or -D.
This error is sent to output when there are more than one arguments that are not options or options attributes.
This error is sent to output when -c option doesn't have a value after the option.
This error is sent to output when -d option doesn't have a value after the option or that value is not a single character.
This error is sent when an invalid range is detected within the column ranges: This can happen because of the starting index being greater than the end index or, one or both of the indexes are not a number.
This error is sent when the input file cannot be read (doesn't exist or the user doesn't have read permission).
This error is sent when quoter is used with both column and delimiter options.
Modifier and Type | Class and Description |
---|---|
private static class |
Quoter.ColumnRange
Utility class to handle column ranges
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
COLL_ERR
Error message for missing column range
|
private static java.lang.String |
COLUMNS_OPTION
Columns option indicator.
|
private static java.lang.String |
DELIM_ERR
Error message for missing delimiter.
|
private static java.lang.String |
DELIMITER_OPTION
Delimiter option indicator.
|
private static java.lang.String |
INCOMPATIBLE_OPTIONS_ERR
Error message for when both -C and -D options are used at the same time.
|
private static java.lang.String |
INPUT_ERR
Error message for multiple inputs.
|
private static java.lang.String |
INPUT_NOT_AVAILABLE_ERR
Error message for when input is not available for read.
|
private static java.lang.String |
INVALID_RANGE_ERR
Error for invalid column range.
|
private static java.lang.String |
QUOTE
The quote character used to delimit output strings.
|
private static java.lang.String |
QUOTE_2X
The escaped quote character (by doubling it) used inside output strings.
|
private static java.lang.String |
QUOTER
quoter detection variable.
|
private static java.lang.String |
QUOTER_EXE
quoter.exe detection variable
|
private static java.lang.String |
UNKNOWN_OPTION_ERR
Error message for unknown option.
|
Modifier | Constructor and Description |
---|---|
private |
Quoter()
Private constructor to stop class instantiation
|
Modifier and Type | Method and Description |
---|---|
private static java.util.List<Quoter.ColumnRange> |
getColumnRange(java.lang.String colRange,
java.io.BufferedWriter destFile)
Utility method that parses a
String containing column range
and returns the List of ColumnRange that
contains the character range from a line where the column can be found. |
private static boolean |
isOption(java.lang.String string)
Utility method to see if a string starts with - and can be a command
line option.
|
static boolean |
isQuoterCommand(java.lang.String command)
Utility method that checks if a string argument is a call to the quoter
utility ot not.
|
static void |
main(java.lang.String[] args)
The entry point method of the Quoter class.
|
private static void |
process(java.io.BufferedReader inputReader,
java.io.BufferedWriter outputWriter,
java.lang.String delimiter,
java.lang.String colRange)
Utility method that checks for option's values and process them before
calling the main quoter logic.
|
private static java.lang.String |
quote(java.lang.String quoted)
Utility method that quotes a
String using double-quote and
doubles any double quote it finds inside inside the String
variable |
private static boolean |
quoterWorker(java.io.BufferedReader readFile,
java.io.BufferedWriter destFile,
java.util.List<Quoter.ColumnRange> columnRanges,
java.lang.String delimiter)
Utility method that handles the logic of quoter utility.
|
private static void |
tryClose(java.io.Closeable stream)
Utility method that closes the given stream.
|
private static void |
writeColumnsLine(java.lang.String line,
java.util.List<Quoter.ColumnRange> columnRanges,
java.io.BufferedWriter writer)
Utility method that identifies the columns from an input
String line extracts and quotes them then outputs them to
the destination file |
private static void |
writeDelimitedLine(java.lang.String[] tokens,
java.io.BufferedWriter writer)
Utility method that writes into the destination file the line of quoted
tokens (row of columns parsed from the input file).
|
private static final java.lang.String INVALID_RANGE_ERR
private static final java.lang.String UNKNOWN_OPTION_ERR
private static final java.lang.String INPUT_ERR
private static final java.lang.String COLL_ERR
private static final java.lang.String DELIM_ERR
private static final java.lang.String INPUT_NOT_AVAILABLE_ERR
private static final java.lang.String INCOMPATIBLE_OPTIONS_ERR
private static final java.lang.String QUOTER
private static final java.lang.String QUOTER_EXE
private static final java.lang.String COLUMNS_OPTION
private static final java.lang.String DELIMITER_OPTION
private static final java.lang.String QUOTE
private static final java.lang.String QUOTE_2X
public static void main(java.lang.String[] args)
This will check for valid parameters and option usage of quoter before calling the mainm quoter logic.
args
- The parameters given to the quoter utilitypublic static boolean isQuoterCommand(java.lang.String command)
command
- The String
argument which is to be tested for
identifying it as a quoter argument or not.true
if the parameter is identified as
a call to quoter utility or false
otherwise.private static void process(java.io.BufferedReader inputReader, java.io.BufferedWriter outputWriter, java.lang.String delimiter, java.lang.String colRange) throws java.lang.Exception
inputReader
- The input file nameoutputWriter
- The output file namedelimiter
- The delimiter if anycolRange
- The column ranges to be parsedjava.lang.Exception
- In case something goes wrong.This signals abort.private static void writeDelimitedLine(java.lang.String[] tokens, java.io.BufferedWriter writer) throws java.io.IOException
tokens
- The array
of String
values
representing a row of columns parsed from the input filewriter
- The BufferedWriter
used to output the quoted
characters.java.io.IOException
- In case something went wrong in the writing process.
This indicates that the main quoter method should close opened resources and terminate.
private static java.lang.String quote(java.lang.String quoted)
String
using double-quote and
doubles any double quote it finds inside inside the String
variablequoted
- The String
to be quoted.String
value.private static boolean isOption(java.lang.String string)
string
- The String
to be checkedtrue
if string starts with - , false
otherwiseprivate static void writeColumnsLine(java.lang.String line, java.util.List<Quoter.ColumnRange> columnRanges, java.io.BufferedWriter writer) throws java.io.IOException
String
line extracts and quotes them then outputs them to
the destination fileline
- The String
line representing a row from the input
file.columnRanges
- The List
of ColumnRange
used to
identify the column from the input line.writer
- The BufferedWriter
used to output the quoted
characters.java.io.IOException
- In case something went wrong in the writing process.
This indicates that the main quoter method should close opened resources and terminate.
private static java.util.List<Quoter.ColumnRange> getColumnRange(java.lang.String colRange, java.io.BufferedWriter destFile) throws java.lang.Exception
String
containing column range
and returns the List
of ColumnRange
that
contains the character range from a line where the column can be found.colRange
- The String
representation of the column ranges.destFile
- The BufferedWriter
used to output the quoted
characters or any error messages in this caseList
of ColumnRange
.java.lang.Exception
- In case there are invalid column ranges.
This indicates that the main quoter method should close opened resources and terminate.
private static boolean quoterWorker(java.io.BufferedReader readFile, java.io.BufferedWriter destFile, java.util.List<Quoter.ColumnRange> columnRanges, java.lang.String delimiter)
readFile
- The input BufferedReader
from which the info
will be read.destFile
- The destination BufferedWriter
file to which the
quoted lines will be output.columnRanges
- The List
of ColumnRange
used to
identify the column from the input line.delimiter
- The String
character that is used to split the
information from a line into columns.true
if operation was successful or
false
if the operation failed at some point.private static void tryClose(java.io.Closeable stream)
stream
- The stream to be closed.