diff options
author | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-07 12:33:50 +0200 |
---|---|---|
committer | Maurice Laveaux <m.laveaux@student.tue.nl> | 2014-05-07 12:33:50 +0200 |
commit | a3303f1fe119f963edec13c48286cc5d8cdcc5eb (patch) | |
tree | c2fef6ff8c7662ebc868f6a7fbd81353222e9822 /src/io | |
parent | 2f203acc7f28afce9e704cf27fd59d11e28ec2d5 (diff) | |
download | Datafiller-a3303f1fe119f963edec13c48286cc5d8cdcc5eb.tar.gz |
Two different inputs (file and stdin).
* DataFiller fills the database with given input.
Diffstat (limited to 'src/io')
-rw-r--r-- | src/io/DataReader.java | 32 | ||||
-rw-r--r-- | src/io/InputReader.java | 51 |
2 files changed, 83 insertions, 0 deletions
diff --git a/src/io/DataReader.java b/src/io/DataReader.java new file mode 100644 index 0000000..67dca2b --- /dev/null +++ b/src/io/DataReader.java @@ -0,0 +1,32 @@ +package io; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import main.ResultListener; + +/** + * Create a data reader that reads the input file. + * + * @author Maurice Laveaux + */ +public class DataReader { + + /* The listener that receives the data input. */ + private final ResultListener m_listener; + + /* The input stream for the given file. */ + private final FileInputStream m_input; + + /** + * @param filename The filename to read from. + * @param listener The listener of the read tweets. + */ + public DataReader(final String filename, final ResultListener listener) { + try { + m_listener = listener; + m_input = new FileInputStream(filename); + } catch (FileNotFoundException ex) { + throw new IllegalArgumentException("the given filename \"" + filename + "\" doesn't exist."); + } + } +} diff --git a/src/io/InputReader.java b/src/io/InputReader.java new file mode 100644 index 0000000..ef22c97 --- /dev/null +++ b/src/io/InputReader.java @@ -0,0 +1,51 @@ +package io; + +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; +import main.ResultListener; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Read from standard input and inserts these tweets into the database. + * + * @author Maurice Laveaux + */ +public class InputReader { + + /* The listener for standard input tweets. */ + private final ResultListener m_listener; + + /** + * @param listener The given listener for the input. + */ + public InputReader(final ResultListener listener) { + m_listener = listener; + } + + /* Create a reader for the standard input, read each line and tweetReceived + * listener when a valid JSONObject is generated. + */ + public void startLoop() { + Scanner input = new Scanner(System.in); + + while (input.hasNext()) { + try { + JSONObject tweet = new JSONObject(input.nextLine()); + + if (tweet.has("tweet-id")) { + throw new JSONException("not a valid tweet JSON object"); + } + + m_listener.tweetReceived(tweet); + } catch (JSONException ex) { + getLogger().log(Level.INFO, null, ex); + } + } + } + + private Logger getLogger() { + return Logger.getLogger(InputReader.class.getName()); + } +} |