diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/CommandParser.java | 113 | ||||
-rw-r--r-- | src/main/CommandQueue.java | 39 | ||||
-rw-r--r-- | src/main/ICommand.java | 14 | ||||
-rw-r--r-- | src/main/RequestCommand.java | 118 |
4 files changed, 0 insertions, 284 deletions
diff --git a/src/main/CommandParser.java b/src/main/CommandParser.java deleted file mode 100644 index 97f01fc..0000000 --- a/src/main/CommandParser.java +++ /dev/null @@ -1,113 +0,0 @@ -package main; - -import java.lang.reflect.Array; -import java.util.Arrays; -import mining.TwitterApi; - -/** - * Class that creates executable commands from input. - * @author Maurice Laveaux - */ -public class CommandParser { - // the queue of commands. - private final CommandQueue m_queue; - - // the twitter api. - private final TwitterApi m_api; - - /** - * Default constructor, requires the TwitterApi - * @param queue A valid CommandQueue that can execute requests. - * @param api the TwitterApi to use for the commands. - */ - public CommandParser(CommandQueue queue, TwitterApi api) { - m_queue = queue; - m_api = api; - } - - /** - * Parses a string and creates an executable command for it. - * @param command The string to parse the command from. - */ - final public void parse(final String command) { - // split the code for each space ' '. - String[] seperated = command.split(" "); - - // parse the seperated code for each argument. - parse(seperated); - } - - /** - * Parses a string and creates an executable command for it. - * @param seperated A space seperated string for each argument. - */ - final public void parse(final String[] seperated) { - if (seperated.length == 0) { - return; - } - - // The main command. - String command = seperated[0]; - - // the first command issued - switch (command) { - case "help": - showFullHelp(); - return; - case "-h": - showFullHelp(); - return; - case "quit": - // TODO: implement less hack way of quiting the program. - System.exit(0); - default: - break; - } - - // there is a command without errors but it was not switched. - if (seperated.length == 1) { - showUsageHelp(command); - return; - } - - // commands with multiple parameters - String[] extra = Arrays.copyOfRange(seperated, 1, seperated.length); - - if (seperated.length < 3) { - showUsageHelp(extra.toString()); - return; - } - - String[] parameters = Arrays.copyOfRange(extra, 1, extra.length); - - try { - switch (command) { - case "get": - m_queue.add(new RequestCommand(m_api, extra[0], parameters)); - return; - } - } - catch (IllegalArgumentException ex) { - System.out.println(ex); - } - } - - /** Show the basic usage help in console */ - private void showUsageHelp(String unknown) { - System.out.println("Unknown argument: \"" + unknown + "\"."); - System.out.println(" Use help, -h for more help."); - } - - /** Show the full manual page in the console */ - private void showFullHelp() { - System.out.println("Usage: [command] [arguments], see below for options."); - - System.out.println("\n command:"); - System.out.println(" set, use the twitter Search/REST API."); - System.out.println(" stream, use the twitter Stream API."); - - System.out.println("\n arguments:"); - System.out.println(" <main>, the information to get from api.twitter.com."); - System.out.println(" <json_member>, any number of arguments for the information"); - } -}
\ No newline at end of file diff --git a/src/main/CommandQueue.java b/src/main/CommandQueue.java deleted file mode 100644 index b3fde3e..0000000 --- a/src/main/CommandQueue.java +++ /dev/null @@ -1,39 +0,0 @@ -package main; - -import java.util.LinkedList; -import java.util.Queue; - -/** - * The queue of commands to be executed every step. - * - * @author Maurice Laveaux - */ -public class CommandQueue { - - // the list of commands executed in this order. - private final Queue<ICommand> m_commands; - - public CommandQueue() { - m_commands = new LinkedList(); - } - - /** - * Add another command to the last position in the queue. - * - * @param command Any command that can be executed. - */ - public void add(ICommand command) { - m_commands.offer(command); - } - - /** - * Execute every command that is in the queue. - */ - public void executeAll() { - while (!m_commands.isEmpty()) { - ICommand command = m_commands.poll(); - - command.execute(); - } - } -} diff --git a/src/main/ICommand.java b/src/main/ICommand.java deleted file mode 100644 index 14419a2..0000000 --- a/src/main/ICommand.java +++ /dev/null @@ -1,14 +0,0 @@ -package main; - -/** - * The standard interface for any command - * - * @author Maurice Laveaux - */ -public interface ICommand { - - /** - * Execute all the steps to complete this Command. - */ - public void execute(); -} diff --git a/src/main/RequestCommand.java b/src/main/RequestCommand.java deleted file mode 100644 index 42ff41b..0000000 --- a/src/main/RequestCommand.java +++ /dev/null @@ -1,118 +0,0 @@ -package main; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import mining.TwitterApi; -import mining.TwitterApi.Builder; -import org.json.JSONException; -import org.json.JSONObject; - -/** - * Basic contained class for a Pair, because Java doesn't provide this. - * - * @author Maurice Laveaux - */ -class Parameter { - - public Parameter(String variable, String value) { - this.variable = variable; - this.value = value; - } - - public String variable; - public String value; -} - -/** - * A single request/response command that executes once. - * - * @author Maurice Laveaux - */ -public class RequestCommand implements ICommand { - - /** - * The api to execute requests on - */ - private final TwitterApi m_api; - - private final String m_command; - - /** - * The list of parameters that are executed - */ - private final ArrayList<Parameter> m_parameters; - - /** - * Contruct a RequestCommand that executes the request and writes the - * results into a file. - * - * @param api the main twitter api. - * @param command the command of the twitter api f.e. users/show. - * @param parameters the parameters for the executed command. - */ - public RequestCommand(TwitterApi api, String command, String[] parameters) { - m_api = api; - m_command = command; - - m_parameters = new ArrayList(); - - if (parameters.length < 1) { - throw new IllegalArgumentException("Not enough parameters specified."); - } - - for (String param : parameters) { - // parse the parameter to a variable with a value. - m_parameters.add(parseParam(param)); - } - } - - @Override - public void execute() { - try { - // create the building object with the command. - Builder builder = m_api.build(m_command); - - // add every parameter to the builder. - for (Parameter param : m_parameters) { - builder = builder.param(param.variable, param.value); - } - - JSONObject result = builder.request(); - - // TODO: add the results to the main database. - // For now write to a file - File file = new File("database.txt"); - - FileWriter writer = new FileWriter(file, true); - - writer.write("\n\r" + result.toString()); - writer.flush(); - - System.out.println(result.toString(4)); - } catch (JSONException ex) { - /* cannot happen */ - throw new RuntimeException(ex); - } catch (IOException ex) { - // TODO: when ratelimit reached, add event back to queue. - throw new RuntimeException(ex); - } - } - - /** - * Parses the input string to a parameter. - * - * @param input A single line in the form variable=value. - * @return A pair of strings for variable and value. - */ - private Parameter parseParam(final String input) { - String[] seperated = input.split("="); - - if (seperated.length < 2) { - throw new IllegalArgumentException("parameter did not contain <variable>=<value>."); - } - - return new Parameter(seperated[0], seperated[1]); - } -} |