forked from TheCyaniteProject/exit_code_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileNavigator.java
More file actions
330 lines (293 loc) · 11.2 KB
/
Copy pathFileNavigator.java
File metadata and controls
330 lines (293 loc) · 11.2 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package exitcode;
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
import java.lang.Exception;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
// Internal dependancies: ExitParser, Console, Command, Logger
public class FileNavigator {
private String system = Main.defaultSystem;
private String location;
private String homeSystem;
private String home;
private String uid = "1000";
/**
* A process for navigating the files of a system.
*
* @param homeLocation path/to/system.xml file, located within the .ecsave file.
*
* @param homePath The path/to/home directory within the in-game file system.
*/
public FileNavigator(String homeLocation, String homePath) {
setHome(homeLocation, homePath);
}
/**
* Sets the homeLocation, and homePath of the fileSystem.
*
* @param homeLocation path/to/system.xml file, located within the .ecsave file.
*
* @param homePath The path/to/home directory within the in-game file system.
*/
public void setHome(String homeLocation, String homePath) {
this.homeSystem = homeLocation;
this.home = homePath;
}
public ArrayList<String> getHome() {
ArrayList<String> homeList = new ArrayList<String>();
homeList.add(homeSystem);
homeList.add(home);
return homeList;
}
public void setSystem(String system) {
this.system = system;
}
public String getSystem() {
return this.system;
}
public Node getFile(String path) {
path = path.replace("\\", "/");
String fileName;
String path2;
Console console = new Console("/", this);
Document doc = ExitParser.getDocFromZip(console.getNavigator().getSystem());
NodeList dataList = doc.getElementsByTagName("info");
if (path.endsWith("/")) {
return null;
} else {
fileName = (new File(path)).getName();
path2 = path.replace(fileName, "");
if (!path2.trim().equals(ExitParser.getAttributeValue(dataList, "top", "dir"))) {
path2 = (new File(path)).getParent();
}
}
Node childNode = ((Element)getDirectory(path2)).getFirstChild();
try {
while( childNode.getNextSibling()!=null ){
childNode = childNode.getNextSibling();
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
if (childNode.getNodeName().trim().equals("file")) {
Element childElement = (Element) childNode;
if(childElement.hasAttribute("name")) {
if (childElement.getAttribute("name").contains(fileName.trim())) {
return childNode;
}
}
}
}
}
}
catch(Exception ex) {
Logger.error(ex.getMessage());
}
return null;
}
public void makeFile(Node node, String fileName, String data) {
Element newFile = node.getOwnerDocument().createElement("file");
Element nodeElement = (Element) node;
nodeElement.appendChild(newFile);
newFile.appendChild(nodeElement.getOwnerDocument().createCDATASection(data));
newFile.setAttribute("name", fileName);
newFile.setAttribute("owner", this.uid);
newFile.setAttribute("group", this.uid);
newFile.setAttribute("ownerp", "rwx");
newFile.setAttribute("groupp", "r-x");
newFile.setAttribute("otherp", "r-x");
}
public void makeFile(Node node, String fileName) {
makeFile(node, fileName, " ");
}
public static String readStringFromFile(Node node) {
if (node == null) {
return null;
}
return node.getTextContent();
}
public static Object readObjectFromFile(Node node) {
try {
byte b[] = readStringFromFile(node).getBytes();
ByteArrayInputStream byteArray = new ByteArrayInputStream(b);
ObjectInputStream objectIn = new ObjectInputStream(byteArray);
return (Object) objectIn.readObject();
} catch (Exception e) {
Logger.error(e.getMessage());
}
return null;
}
public static void writeStringToFile(Node node, String string) {
if (node.hasChildNodes()) {
Node childNode = node.getFirstChild();
node.removeChild(childNode);
while( childNode.getNextSibling()!=null ){
childNode = childNode.getNextSibling();
node.removeChild(childNode);
}
}
node.appendChild(node.getOwnerDocument().createCDATASection(string));
}
public static void writeObjectToFile(Node node, Object object) {
try {
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ObjectOutputStream objectOut = new ObjectOutputStream(byteArray);
objectOut.writeObject(object);
objectOut.flush();
writeStringToFile(node, byteArray.toString());
} catch (Exception e) {
Logger.error(e.getMessage());
}
}
public void sendToOblivion(Node node) { // Removes the node | Used to delete files and folders or anything else.
node.getParentNode().removeChild(node);
}
public Boolean checkFile(String path) {
path = path.replace("\\", "/");
String fileName;
String path2;
Console console = new Console("/", this);
Document doc = ExitParser.getDocFromZip(console.getNavigator().getSystem());
NodeList dataList = doc.getElementsByTagName("info");
if (path.endsWith("/")) {
return false;
} else {
fileName = (new File(path)).getName();
path2 = path.replace(fileName, "");
if (!path2.trim().equals(ExitParser.getAttributeValue(dataList, "top", "dir"))) {
path2 = (new File(path)).getParent();
}
}
Node childNode = ((Element)getDirectory(path2)).getFirstChild();
try {
while( childNode.getNextSibling()!=null ){
childNode = childNode.getNextSibling();
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
if (childNode.getNodeName().trim().equals("file")) {
Element childElement = (Element) childNode;
if(childElement.hasAttribute("name")) {
if (childElement.getAttribute("name").contains(fileName.trim())) {
return true;
}
}
}
}
}
}
catch(Exception ex) {
Logger.error(ex.getMessage());
}
return false;
}
public Node getDirectory(String path) {
Document doc = ExitParser.getDocFromZip(system);
NodeList dataList = doc.getElementsByTagName("files");
Node node = dataList.item(0);
Node pathItem = parsePath(node, path);
return pathItem;
}
public String getCurrentDirectory() {
return this.location;
}
public String getCWD() {
return getCurrentDirectory();
}
public void setDirectory(String path) {
this.location = path;
}
public void makeDirectory(Node node, String folderName) { // TODO
Element newFolder = node.getOwnerDocument().createElement("dir");
Element nodeElement = (Element) node;
nodeElement.appendChild(newFolder);
newFolder.setAttribute("name", folderName);
newFolder.setAttribute("owner", this.uid);
newFolder.setAttribute("group", this.uid);
newFolder.setAttribute("ownerp", "rwx");
newFolder.setAttribute("groupp", "r-x");
newFolder.setAttribute("otherp", "r-x");
}
public Boolean checkDirectory(String path) {
Node dir = getDirectory(path);
return !(dir == null);
}
public static Map<String, Boolean> getBoolFromInt(Integer Int){
ArrayList<Boolean> result = new ArrayList<>();
char[] intArray = Int.toString().toCharArray();
String[] perms = {"owner.read", "owner.write", "owner.execute",
"group.read", "group.write", "group.execute",
"other.read", "other.write", "other.execute"};
for(char Char : intArray){
switch(Char){
case '0':
result.add(Boolean.FALSE);
result.add(Boolean.FALSE);
result.add(Boolean.FALSE);
break;
case '1':
result.add(Boolean.FALSE);
result.add(Boolean.FALSE);
result.add(Boolean.TRUE);
break;
case '2':
result.add(Boolean.FALSE);
result.add(Boolean.TRUE);
result.add(Boolean.FALSE);
break;
case '3':
result.add(Boolean.FALSE);
result.add(Boolean.TRUE);
result.add(Boolean.TRUE);
break;
case '4':
result.add(Boolean.TRUE);
result.add(Boolean.FALSE);
result.add(Boolean.FALSE);
break;
case '5':
result.add(Boolean.TRUE);
result.add(Boolean.FALSE);
result.add(Boolean.TRUE);
break;
case '6':
result.add(Boolean.TRUE);
result.add(Boolean.TRUE);
result.add(Boolean.FALSE);
break;
case '7':
result.add(Boolean.TRUE);
result.add(Boolean.TRUE);
result.add(Boolean.TRUE);
break;
}
}
Map<String, Boolean> permissions = new HashMap<>();
for(int i = 0; i < result.size(); i++){
permissions.put(perms[i], result.get(i));
}
return permissions;
}
public Node parsePath(Node node, String path) {
Document doc = ExitParser.getDocFromZip(system);
NodeList dataList = doc.getElementsByTagName("info");
if (path.contains("\\")) {
path = path.replace("\\", "/");
} else if (path.trim().equals("")) {
return null;
}
if (path.trim().equals(ExitParser.getAttributeValue(dataList, "top", "dir"))) {
return node;
}
if (path.startsWith(ExitParser.getAttributeValue(dataList, "top", "dir"))) {
path = path.replaceFirst(Pattern.quote(ExitParser.getAttributeValue(dataList, "top", "dir")), "");
}
List<String> pathList = new ArrayList<String>();
if (path.contains("/")) {
pathList = new ArrayList<String>(Arrays.asList(path.split("/")));
} else {
pathList.add(path);
}
for (String string : pathList) {
node = ExitParser.getNextSetAttr(node, string);
}
return node;
}
}