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 { String line = input.nextLine(); if(line.equals("exit")) { return; } JSONObject tweet = new JSONObject(line); if (!tweet.has("user")) { 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()); } }