class NoozFileProcessor
extends java.lang.Object
This class provides helper methods to read a Nooz-style data file and turn it
into NewsStory
and NewsMaker
objects.
Note that the field newsMakers
and all methods in this class are
static because we don't need to make several NoozFileProcessor
objects and have them maintain their own lists of news makers. Instead, we
simply need a collection of useful methods to create a list and return it to
the calling method that will keep track of it.
Modifier and Type | Field and Description |
---|---|
private static NewsMakerList |
newsMakers
The list of news makers.
|
Constructor and Description |
---|
NoozFileProcessor() |
Modifier and Type | Method and Description |
---|---|
private static java.time.LocalDate |
decodeDate(java.lang.String dateString)
This method decodes the date, which is encoded in the file as YYYYMMDD.
|
private static int |
decodeLength(java.lang.String lengthString)
This method decodes the length, which is encoded in the file as numeral
string.
|
private static java.lang.String |
decodeNewsmakerName(java.lang.String[] parts,
int startingIndex)
This method decodes a news maker name from one or more the parts of the
input line.
|
private static PartOfDay |
decodePartOfDay(java.lang.String partOfDayCode)
This method decodes the part of the day, which is represented by a
numeral in the data file.
|
private static void |
processLine(java.lang.String line,
java.util.Map<java.lang.String,java.lang.String> sourceMap,
java.util.Map<java.lang.String,java.lang.String> topicMap,
java.util.Map<java.lang.String,java.lang.String> subjectMap)
The method for turning each line from a Nooz-style file into data and
saving that data.
|
static NewsMakerList |
readNoozFile(java.lang.String fileName,
java.util.Map<java.lang.String,java.lang.String> sourceMap,
java.util.Map<java.lang.String,java.lang.String> topicMap,
java.util.Map<java.lang.String,java.lang.String> subjectMap)
The primary method for reading the Nooz-style data file.
|
static void |
writeNewsStoriesFile(java.lang.String outputFileName,
java.lang.String listOfStories)
The method for writing a list of news stories to a data file as text.
|
private static NewsMakerList newsMakers
public static NewsMakerList readNoozFile(java.lang.String fileName, java.util.Map<java.lang.String,java.lang.String> sourceMap, java.util.Map<java.lang.String,java.lang.String> topicMap, java.util.Map<java.lang.String,java.lang.String> subjectMap) throws java.io.IOException
It opens the file, reads through it line by line until it reaches the
end, and closes the file. Each line is passed to processLine
to parse and turn into data that is actually added to the list.
In addition to the name of the Nooz-style file to read, this method takes
three maps from brief numeral codes to longer textual descriptions. The
maps are from source code (e.g., "1") to source name (e.g., "New York
Times"), from topic code (e.g., "1") to topic description (e.g.,
"Government Agencies/Legislatures"), and from subject matter code (e.g.,
"2") to subject matter description (e.g., "Immigration debate"). Note
that while all of these maps use numerals as keys, these are represented
as String
objects rather than Integer
objects.
Note that this program doesn't attempt to deal with I/O errors. This is allowable at this point to keep this project relatively simple and because we haven't covered this topic yet. However, this is something to be refined in the future.
fileName
- The name of the Nooz style data file to read.sourceMap
- The map from source code to source name.topicMap
- The map from topic code to topic description.subjectMap
- The map from subject code to subject description.java.io.IOException
- If there is an I/O problem reading the data file.public static void writeNewsStoriesFile(java.lang.String outputFileName, java.lang.String listOfStories) throws java.io.IOException
String
.outputFileName
- The name of the file to which to write the stories.listOfStories
- The stories to write.java.io.IOException
- If there is an I/O problem writing the file.private static void processLine(java.lang.String line, java.util.Map<java.lang.String,java.lang.String> sourceMap, java.util.Map<java.lang.String,java.lang.String> topicMap, java.util.Map<java.lang.String,java.lang.String> subjectMap)
This method breaks the line into parts, passes the parts to appropriate helper methods that decode them and return data of appropriate types for the components of news stories and news makers, puts these components together into a news story of the proper type and two news makers, and adds these to lists of news stories and news makers, respectively.
Note that some helper methods from Nooz 2.0 have been eliminated because the maps do so much of the work for us.
line
- The line to process.sourceMap
- The map from source code to source name.topicMap
- The map from topic code to topic description.subjectMap
- The map from subject code to subject description.private static java.time.LocalDate decodeDate(java.lang.String dateString)
This method catches NumberFormatExceptions
and deals with
them locally by printing messages to the error stream. In later
assignments, we'll cover better alternatives for handling exceptions.
dateString
- The encoded date.null
if the date cannot be
decoded.private static int decodeLength(java.lang.String lengthString)
This method catches NumberFormatExceptions
and deals with
them locally by printing messages to the error stream. In later
assignments, we'll cover better alternatives for handling exceptions.
lengthString
- The length as a String.private static java.lang.String decodeNewsmakerName(java.lang.String[] parts, int startingIndex)
Note that there are three distinctly different cases that need to be dealt with by this method.
parts
- The array of all of the parts of the line, which was separated
based on commas.startingIndex
- The starting index for the news maker name to decode. This can
vary for the second news maker based on whether the first news
maker name contained a comma.private static PartOfDay decodePartOfDay(java.lang.String partOfDayCode)
partOfDayCode
- The numeral specifying the part of the day.