|
@@ -1,3 +1,7 @@
|
|
1
|
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
2
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
3
|
+import sun.tools.java.Identifier;
|
|
4
|
+
|
1
|
5
|
import java.io.BufferedReader;
|
2
|
6
|
import java.io.IOException;
|
3
|
7
|
import java.io.InputStream;
|
|
@@ -5,22 +9,48 @@ import java.io.InputStreamReader;
|
5
|
9
|
import java.util.ArrayList;
|
6
|
10
|
import java.util.List;
|
7
|
11
|
|
8
|
|
-public class SimpleShell {
|
|
12
|
+/**
|
|
13
|
+ * ============JACKSON============ vs ==============GSON==============
|
|
14
|
+ * @JACKSON
|
|
15
|
+ * Consistent multipurpose library for processing JSON formatted data
|
|
16
|
+ * Not as simple as Gson(my baby)
|
|
17
|
+ * Fast and ergonomic
|
|
18
|
+ * Reads and writes JSON as discrete events
|
|
19
|
+ * Provides a mutable internal memory tree interpretation of JSON data
|
|
20
|
+ * Converts JSON to and from POJO's
|
|
21
|
+ * @GSON
|
|
22
|
+ * Does the same as jackson minus the need for replacing java annotations within classes
|
|
23
|
+ * Simple method calls
|
|
24
|
+ * Supports complex objects
|
|
25
|
+ * Long range support of java generics
|
|
26
|
+ * Allows custom representation for objects
|
|
27
|
+ * Allows pre-existing static objects can be converted
|
|
28
|
+ * Its just better bruh
|
|
29
|
+ */
|
9
|
30
|
|
|
31
|
+public class SimpleShell {
|
10
|
32
|
|
11
|
|
- public static void prettyPrint(String output) {
|
12
|
|
- // yep, make an effort to format things nicely, eh?
|
13
|
|
- System.out.println(output);
|
|
33
|
+ public static void prettyPrint(String output) throws IOException {
|
|
34
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
35
|
+ try {
|
|
36
|
+ Object jsonFormat = objectMapper.readValue(output, Object.class);
|
|
37
|
+ String formatted = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonFormat);
|
|
38
|
+ System.out.println(formatted);
|
|
39
|
+ } catch (IOException e) {
|
|
40
|
+ e.printStackTrace();
|
|
41
|
+ }
|
|
42
|
+ //System.out.println(output);
|
14
|
43
|
}
|
15
|
|
- public static void main(String[] args) throws java.io.IOException {
|
16
|
44
|
|
17
|
|
- YouAreEll webber = new YouAreEll();
|
|
45
|
+ public static void main(String[] args) throws java.io.IOException {
|
|
46
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
47
|
+ YouAreEll url = new YouAreEll();
|
18
|
48
|
String commandLine;
|
19
|
49
|
BufferedReader console = new BufferedReader
|
20
|
50
|
(new InputStreamReader(System.in));
|
21
|
51
|
|
22
|
52
|
ProcessBuilder pb = new ProcessBuilder();
|
23
|
|
- List<String> history = new ArrayList<String>();
|
|
53
|
+ List<String> commandHistory = new ArrayList<String>();
|
24
|
54
|
int index = 0;
|
25
|
55
|
//we break out with <ctrl c>
|
26
|
56
|
while (true) {
|
|
@@ -29,8 +59,8 @@ public class SimpleShell {
|
29
|
59
|
commandLine = console.readLine();
|
30
|
60
|
|
31
|
61
|
//input parsed into array of strings(command and arguments)
|
32
|
|
- String[] commands = commandLine.split(" ");
|
33
|
|
- List<String> list = new ArrayList<String>();
|
|
62
|
+ String[] commandWords = commandLine.split(" ");
|
|
63
|
+ List<String> commandList = new ArrayList<String>();
|
34
|
64
|
|
35
|
65
|
//if the user entered a return, just loop again
|
36
|
66
|
if (commandLine.equals(""))
|
|
@@ -41,49 +71,47 @@ public class SimpleShell {
|
41
|
71
|
}
|
42
|
72
|
|
43
|
73
|
//loop through to see if parsing worked
|
44
|
|
- for (int i = 0; i < commands.length; i++) {
|
|
74
|
+ for (int i = 0; i < commandWords.length; i++) {
|
45
|
75
|
//System.out.println(commands[i]); //***check to see if parsing/split worked***
|
46
|
|
- list.add(commands[i]);
|
47
|
|
-
|
|
76
|
+ commandList.add(commandWords[i]);
|
48
|
77
|
}
|
49
|
|
- System.out.print(list); //***check to see if list was added correctly***
|
50
|
|
- history.addAll(list);
|
|
78
|
+ System.out.print(commandList); //***check to see if list was added correctly***
|
|
79
|
+ commandHistory.addAll(commandList);
|
51
|
80
|
try {
|
52
|
81
|
//display history of shell with index
|
53
|
|
- if (list.get(list.size() - 1).equals("history")) {
|
54
|
|
- for (String s : history)
|
|
82
|
+ if (commandList.get(commandList.size() - 1).equals("history")) {
|
|
83
|
+ for (String s : commandHistory)
|
55
|
84
|
System.out.println((index++) + " " + s);
|
56
|
85
|
continue;
|
57
|
86
|
}
|
58
|
|
-
|
59
|
|
- // Specific Commands.
|
|
87
|
+ //Specific commands
|
60
|
88
|
|
61
|
89
|
// ids
|
62
|
|
- if (list.contains("ids")) {
|
63
|
|
- String results = webber.get_ids();
|
|
90
|
+ if (commandList.contains("ids")) {
|
|
91
|
+ String results = url.get_ids();
|
64
|
92
|
SimpleShell.prettyPrint(results);
|
65
|
93
|
continue;
|
66
|
94
|
}
|
67
|
95
|
|
68
|
96
|
// messages
|
69
|
|
- if (list.contains("messages")) {
|
70
|
|
- String results = webber.get_messages();
|
|
97
|
+ if (commandList.contains("messages")) {
|
|
98
|
+ String results = url.get_messages();
|
71
|
99
|
SimpleShell.prettyPrint(results);
|
72
|
100
|
continue;
|
73
|
101
|
}
|
74
|
102
|
// you need to add a bunch more.
|
75
|
103
|
|
76
|
104
|
//!! command returns the last command in history
|
77
|
|
- if (list.get(list.size() - 1).equals("!!")) {
|
78
|
|
- pb.command(history.get(history.size() - 2));
|
|
105
|
+ if (commandList.get(commandList.size() - 1).equals("!!")) {
|
|
106
|
+ pb.command(commandHistory.get(commandHistory.size() - 2));
|
79
|
107
|
|
80
|
108
|
}//!<integer value i> command
|
81
|
|
- else if (list.get(list.size() - 1).charAt(0) == '!') {
|
82
|
|
- int b = Character.getNumericValue(list.get(list.size() - 1).charAt(1));
|
83
|
|
- if (b <= history.size())//check if integer entered isn't bigger than history size
|
84
|
|
- pb.command(history.get(b));
|
|
109
|
+ else if (commandList.get(commandList.size() - 1).charAt(0) == '!') {
|
|
110
|
+ int b = Character.getNumericValue(commandList.get(commandList.size() - 1).charAt(1));
|
|
111
|
+ if (b <= commandHistory.size())//check if integer entered isn't bigger than history size
|
|
112
|
+ pb.command(commandHistory.get(b));
|
85
|
113
|
} else {
|
86
|
|
- pb.command(list);
|
|
114
|
+ pb.command(commandList);
|
87
|
115
|
}
|
88
|
116
|
|
89
|
117
|
// wait, wait, what curiousness is this?
|
|
@@ -99,8 +127,6 @@ public class SimpleShell {
|
99
|
127
|
while ((line = br.readLine()) != null)
|
100
|
128
|
System.out.println(line);
|
101
|
129
|
br.close();
|
102
|
|
-
|
103
|
|
-
|
104
|
130
|
}
|
105
|
131
|
|
106
|
132
|
//catch ioexception, output appropriate message, resume waiting for input
|
|
@@ -108,7 +134,8 @@ public class SimpleShell {
|
108
|
134
|
System.out.println("Input Error, Please try again!");
|
109
|
135
|
}
|
110
|
136
|
// So what, do you suppose, is the meaning of this comment?
|
111
|
|
- /** The steps are:
|
|
137
|
+ /**@TODO
|
|
138
|
+ * The steps are:
|
112
|
139
|
* 1. parse the input to obtain the command and any parameters
|
113
|
140
|
* 2. create a ProcessBuilder object
|
114
|
141
|
* 3. start the process
|
|
@@ -117,8 +144,5 @@ public class SimpleShell {
|
117
|
144
|
*/
|
118
|
145
|
|
119
|
146
|
}
|
120
|
|
-
|
121
|
|
-
|
122
|
147
|
}
|
123
|
|
-
|
124
|
148
|
}
|