diff options
Diffstat (limited to 'src/main/Main.java')
-rw-r--r-- | src/main/Main.java | 114 |
1 files changed, 80 insertions, 34 deletions
diff --git a/src/main/Main.java b/src/main/Main.java index 4d11951..dec057a 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -1,47 +1,93 @@ package main; -import java.io.File; -import java.io.FileNotFoundException; -import java.util.Scanner; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.json.JSONObject; + +import database.DBConnection; +import io.DataReader; +import io.InputReader; +import java.util.Arrays; + /** - * - * @author maurice + * The main class. */ public class Main { - + /** - * - * @param args + * The main method of the application. + * @param args the global arguments to pass to the program. */ public static void main(String[] args) { - // TODO code application logic here - - Scanner scanner=null; try { - scanner = new Scanner(new File("tweets-en.txt")); - } catch (FileNotFoundException ex) { - Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); - } - try { - while(scanner.hasNextLine()){ - - - JSONObject tweet = new JSONObject(scanner.nextLine()); - JSONObject user = (JSONObject) tweet.get("user"); - Post p = new Post(tweet,user); - p.start(); - } - - - - } catch (Exception e){ - e.printStackTrace(); + Main main = new Main(args); + } catch (IllegalArgumentException ex) { + System.err.println(ex.getMessage()); + System.exit(1); } - - + } + + private String m_hostaddress; + + private String m_filename; + + public Main(String[] args) { + /* parse the global options. */ + parseGlobalOptions(args); + + if (m_hostaddress == null) { + throw new IllegalArgumentException("Missing --ip to specify the hostaddress."); + } + DBConnection connection = new DBConnection(m_hostaddress, "5432", "Twitter", "postgres", "2IOC02"); + /* create the object that fills the database */ + DataFiller filler = new DataFiller(connection); + + if (m_filename == null) { + InputReader reader = new InputReader(filler); + + reader.startLoop(); + } else { + DataReader reader = new DataReader(m_filename, filler); + } + connection.close(); } + + private void parseGlobalOptions(String[] args) { + /* parse global options */ + for (int i = 0; i < args.length; i++) { + if ("--help".equals(args[i])) { + printHelp(); + } else if("--dbhost".equals(args[i])) { + m_hostaddress = getParam(args, ++i); + } else if (args[i].startsWith("-")) { + throw new IllegalArgumentException("Invalid option: " + args[i]); + } else { + /* This should be the filename */ + m_filename = getParam(args, i); + } + } + } + + /** Read an extra option for a command. */ + private String getParam(String[] args, Integer index) { + if (index + 1 <= args.length) { + index++; + return args[index - 1]; + } else { + throw new IllegalArgumentException("An extra option was missing."); + } + } + + private void printHelp() { + for (String line : HELP) { + System.out.println(line); + } + } + + private final static String[] HELP = { + "Global options:", + " --help Print this help text.", + " --ip <ip> Specify the database ip address.", + " --file <tweet> <profile> Specify the tweet and profile filenames ", + " ", + " if no --file was specified read from standard input." + }; } |