-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate.java
167 lines (143 loc) · 4.72 KB
/
template.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
public class Module_template
{
private String cwd = System.getProperty("user.dir");
private final String SUCC_CODE = "0";
private final String ERR_CODE = "1";
private final String JAVA_EOL = getLineSeparator();
private String getLineSeparator()
{
String os_name = System.getProperty("os.name");
if (os_name.startsWith("Windows"))
return "\r\n";
else
return "\n";
}
private byte[] hex2bin(String data) throws Exception
{
if ((data.length() % 2) == 1)
throw new Exception("hex2bin(): data cannot have an odd number of digits");
byte[] data_bytes = new byte[data.length() / 2];
for (int i = 0; i < data.length(); i += 2)
{
String hs = data.substring(i, i + 2);
try
{
int val = Integer.parseInt(hs, 16);
data_bytes[i/2] = (byte)val;
}
catch(Exception e)
{
throw new Exception("hex2bin() failed to convert hex:'" + hs + "' to byte");
}
}
return data_bytes;
}
private String bin2hex(byte[] ba) throws Exception
{
try
{
StringBuilder sb = new StringBuilder(ba.length * 2);
for (byte b: ba)
sb.append(String.format("%02x", b));
return sb.toString();
}
catch(Exception e)
{
throw new Exception("bin2hex() failed");
}
}
private String hex2str(String data) throws Exception
{
byte[] data_bytes = hex2bin(data);
String data_string = new String(data_bytes);
return data_string;
}
private String sanitizePath(String currPath)
{
currPath = currPath.replace("\"", "");
currPath = currPath.replace("'", "");
currPath = currPath.replace("\\", "/");
return currPath;
}
private String normalizePath(String currPath) throws IOException
{
currPath = sanitizePath(currPath);
File filepath = new File(currPath);
if (filepath.isAbsolute())
{
return filepath.getCanonicalPath();
}
else
{
File new_filepath = new File(this.cwd + File.separator + currPath);
return new_filepath.getCanonicalPath();
}
}
private String[] parseArgs(String args)
{
String regex = "\"[^\"]+\"|'[^']+'|\\S+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(args);
List<String> arguments = new ArrayList<String>();
while (matcher.find())
arguments.add(matcher.group(0));
String[] arguments_arr = new String[arguments.size()];
arguments_arr = arguments.toArray(arguments_arr);
return arguments_arr;
}
private String changeCWD(String cwd) throws Exception
{
File target_dir = new File(cwd).getAbsoluteFile();
if (target_dir.exists() == false)
throw new Exception("Directory: '" + cwd + "': does not exist");
if (target_dir.canRead() == false)
throw new Exception("Can't move to path: '" + cwd + "': permission denied");
if (target_dir.isDirectory() == false)
throw new Exception("Path: '" + cwd + "': is not a directory");
System.setProperty("user.dir", target_dir.getCanonicalPath());
return sanitizePath(target_dir.getCanonicalPath());
}
private String[] doAction(String param_one, String param_two, String param_three)
{
String result = "";
try
{
result = "do something" + JAVA_EOL;
}
catch(Exception ex)
{
return new String[]{ERR_CODE, ex.getMessage() + JAVA_EOL};
}
return new String[]{SUCC_CODE, result};
}
public String[] execute(String[] args)
{
if (args.length != 1)
return new String[]{ERR_CODE, "Invalid arguments provided. Only one directory is allowed to be moved" + JAVA_EOL};
return doAction(args[0]);
}
public String[] go(String module_cwd, String module_args)
{
try
{
this.cwd = changeCWD(hex2str(module_cwd));
String[] args = parseArgs(hex2str(module_args));
return execute(args);
}
catch(Exception ex)
{
return new String[]{ERR_CODE, ex.getMessage() + JAVA_EOL};
}
}
public static void main(String[] args)
{
Module_template m = new Module_template();
String[] results = m.execute(args);
System.out.println(results[1]);
return;
}
}