summaryrefslogtreecommitdiff
path: root/src/io/InputReader.java
diff options
context:
space:
mode:
authorMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-07 12:33:50 +0200
committerMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-07 12:33:50 +0200
commita3303f1fe119f963edec13c48286cc5d8cdcc5eb (patch)
treec2fef6ff8c7662ebc868f6a7fbd81353222e9822 /src/io/InputReader.java
parent2f203acc7f28afce9e704cf27fd59d11e28ec2d5 (diff)
downloadDatafiller-a3303f1fe119f963edec13c48286cc5d8cdcc5eb.tar.gz
Two different inputs (file and stdin).
* DataFiller fills the database with given input.
Diffstat (limited to 'src/io/InputReader.java')
-rw-r--r--src/io/InputReader.java51
1 files changed, 51 insertions, 0 deletions
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());
+ }
+}