forked from TheCyaniteProject/exit_code_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExitParser.java
More file actions
441 lines (402 loc) · 20.5 KB
/
Copy pathExitParser.java
File metadata and controls
441 lines (402 loc) · 20.5 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package exitcode;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.security.*;
import java.util.ArrayList;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.lang.Exception;
import java.util.zip.*;
// Internal dependancies: Logger, Main
public class ExitParser {
public static Document getDocument(String docString) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(docString));
}
catch(Exception ex) {
Logger.error(ex.getMessage());
}
return null;
}
public static void createNewSave(File pathToFile) throws IOException {
pathToFile.getParentFile().mkdirs();
InputStream stream = null;
OutputStream resStreamOut = null;
String jarFolder;
try {
stream = ExitParser.class.getResourceAsStream("/resources/save.ecsave");//note that each / is a directory down in the "jar tree" been the jar the root of the tree
if(stream == null) {
throw new Exception("Cannot get resource \"/resources/save.ecsave\" from Jar file.");
}
int readBytes;
byte[] buffer = new byte[4096];
resStreamOut = new FileOutputStream(pathToFile);
while ((readBytes = stream.read(buffer)) > 0) {
resStreamOut.write(buffer, 0, readBytes);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
stream.close();
resStreamOut.close();
}
}
public static String getBuildDefaultSystem(String hostname) {
String system = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n\n" +
"<system>\n" +
" <info>\n" +
" <system name=\"PiXElos\"/>\n" +
" <host name=\"%s\"/>\n" +
" <owner uid=\"1000\"/>\n" +
" <top dir=\"/\"/>\n" +
" </info>\n" +
"\n" +
" <hardware>\n" +
" <type name=\"Laptop\"/>\n" +
" <cpu ghz=\"1.0\" cores=\"1\" type=\"1\" upgradeable=\"false\"/>\n" +
" <ram mb=\"500\" max_mb=\"500\" upgradeable=\"false\"/>\n" +
" <storage mb=\"1000\" type=\"1\" upgradeable=\"false\"/>\n" +
" </hardware>\n" +
"\n" +
" <files>\n" +
" <dir group=\"-\" groupp=\"r-x\" name=\"root\" otherp=\"r-x\" owner=\"0\" ownerp=\"r-x\">\n" +
" <dir group=\"-\" groupp=\"r-x\" name=\"Desktop\" otherp=\"r-x\" owner=\"0\" ownerp=\"r-x\">\n" +
" </dir>\n" +
" <dir group=\"-\" groupp=\"r-x\" name=\"Downloads\" otherp=\"r-x\" owner=\"0\" ownerp=\"r-x\">\n" +
" </dir>\n" +
" <dir group=\"-\" groupp=\"r-x\" name=\"Documents\" otherp=\"r-x\" owner=\"0\" ownerp=\"r-x\">\n" +
" </dir>\n" +
" </dir>\n" +
" <dir group=\"0\" groupp=\"r-x\" name=\"home\" otherp=\"r-x\" owner=\"0\" ownerp=\"rwx\">\n" +
" </dir>\n" +
" <dir group=\"0\" groupp=\"r-x\" name=\"bin\" otherp=\"r-x\" owner=\"0\" ownerp=\"rwx\">\n" +
" </dir>\n" +
" </files>\n" +
"</system>", hostname);
return system;
}
public static void createNewSystem(String inzipfile, String data) {
Path zipFilePath = Paths.get(Main.savefile.toString());
try ( java.nio.file.FileSystem fileSystem = FileSystems.newFileSystem(zipFilePath, null) ) {
Path fileInsideZipPath = fileSystem.getPath(inzipfile);
Logger.message(inzipfile);
File file = new File("saves/.temp");
try( PrintWriter out = new PrintWriter(file) ){
out.println(data);
} catch (Exception ex) { ex.printStackTrace(); }
Path myFilePath = Paths.get("saves/.temp");
if (Files.exists(fileInsideZipPath)) {
Files.delete(fileInsideZipPath);
}
Files.copy(myFilePath, fileInsideZipPath); // --
fileSystem.close();
file.delete();
} catch (Exception ex) { ex.printStackTrace(); }
}
/**
* Returns Document from file.zip:system.xml
*
* Grabs file.zip location from Main.savefile
*
* @param inzipfile The system.xml file within the zip to open.
*
* @return Document
*/
public static Document getDocFromZip(String inzipfile) {
try {
ZipFile zipFile = new ZipFile(Main.savefile);
ZipEntry entry = zipFile.getEntry(inzipfile);
InputStream stream = zipFile.getInputStream(entry);
Document doc = readXml(stream);
stream.close();
zipFile.close();
return doc;
} catch (Exception ex) {
Logger.error(ex.getMessage());
}
return null;
}
public static void writeDocToZip(Document document, String inzipfile) {
Path zipFilePath = Paths.get(Main.savefile.toString());
try ( java.nio.file.FileSystem fileSystem = FileSystems.newFileSystem(zipFilePath, null) ) {
Path fileInsideZipPath = fileSystem.getPath(inzipfile);
writeXml(document, "saves/.temp");
File file = new File("saves/.temp");
Path myFilePath = Paths.get("saves/.temp");
if (Files.exists(fileInsideZipPath)) {
Files.delete(fileInsideZipPath);
}
Files.copy(myFilePath, fileInsideZipPath);
fileSystem.close();
file.delete();
} catch (Exception ex) {
if (ex.getMessage().contains("FileSystemException")) {
// TODO -- Notifacation
} else {
ex.printStackTrace();
}
}
}
public static NodeList getNextSet(NodeList data, String attrName) {
try {
for(int i=0; i < data.getLength(); i++){
Element dataElement = (Element)data.item(i);
return dataElement.getElementsByTagName(attrName);
}
} catch (Exception ex) {
Logger.error(ex.getMessage());
}
return null;
}
public static Node getNextSetAttr(Node data, String attrName) {
try {
Element dataElement = (Element) data;
Node childNode = dataElement.getFirstChild();
while( childNode.getNextSibling()!=null ){
childNode = childNode.getNextSibling();
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
if (childNode.getNodeName().trim().equals("dir")) {
Element childElement = (Element) childNode;
if(childElement.hasAttribute("name")) {
if (childElement.getAttribute("name").contains(attrName)) {
return childNode;
}
}
}
}
}
}
catch(Exception ex) {
Logger.error(ex.getMessage());
}
return null;
}
public static String getElementValue(NodeList data, String elementName) {
try {
for(int i=0; i < data.getLength(); i++){
Element dataElement = (Element)data.item(i);
NodeList nodeList = dataElement.getElementsByTagName(elementName);
Element nodeElement = (Element)nodeList.item(0);
NodeList elementList = nodeElement.getChildNodes();
return ((Node)elementList.item(0)).getNodeValue();
}
}
catch(Exception ex) {
Logger.error(ex.getMessage());
}
return null;
}
public static String getAttributeValue(NodeList data, String elementName, String attrName) {
try {
for(int i=0; i < data.getLength(); i++){
Element dataElement = (Element)data.item(i);
NodeList nodeList = dataElement.getElementsByTagName(elementName);
Element nodeElement = (Element)nodeList.item(0);
if(nodeElement.hasAttribute(attrName)){
return nodeElement.getAttribute(attrName);
} else {
return null;
}
}
}
catch(Exception ex) {
Logger.error(ex.getMessage());
}
return null;
}
public static String getSettingValue(String fileName, String dataName, String elementName) {
try {
Document xmlDoc = ExitParser.getDocument(fileName);
NodeList dataList = xmlDoc.getElementsByTagName(dataName);
return ExitParser.getElementValue(dataList, elementName);
} catch(Exception e) {
return null;
}
}
public static void setSettingValue(String fileName, String dataName, String elementName, String elementValue) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(fileName);
// Get the root element
Node data = doc.getFirstChild();
NodeList dataList = doc.getElementsByTagName(dataName);
Node elemValue = null;
try {
for(int i=0; i < dataList.getLength(); i++){
Node dataItem = dataList.item(i);
Element dataElement = (Element)dataItem;
NodeList nodeList = dataElement.getElementsByTagName(elementName);
Element nodeElement = (Element)nodeList.item(0);
NodeList elementList = nodeElement.getChildNodes();
elemValue = elementList.item(0);
}
}
catch(Exception ex) {
Logger.error(ex.getMessage());
}
// I am not doing any thing with it just for showing you
String currentElemValue = elemValue.getNodeValue();
elemValue.setTextContent(elementValue);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(fileName));
transformer.transform(source, result);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getSettingValue_player(String elementName) {
return ExitParser.getSettingValue("./profiles/" + LoginScreen.USERNAME + "/player.xml", "playerdata", elementName);
}
public static void setSettingValue_player(String elementName, String elementValue) {
ExitParser.setSettingValue("./profiles/" + LoginScreen.USERNAME + "/player.xml", "playerdata", elementName, elementValue);
}
public static String getSettingValue_theme(String elementName) {
return ExitParser.getSettingValue("./theme.xml", "themedata", elementName);
}
public static void setSettingValue_theme(String elementName, String elementValue) {
ExitParser.setSettingValue("./theme.xml", "themedata", elementName, elementValue);
}
public static String getSettingValue_system(String elementName) {
return ExitParser.getSettingValue("./config.xml", "systemdata", elementName);
}
public static void setSettingValue_system(String elementName, String elementValue) {
ExitParser.setSettingValue("./config.xml", "systemdata", elementName, elementValue);
}
public static String passHasher(String string) {
try{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(string.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch(Exception ex){
throw new RuntimeException(ex);
}
}
public static ArrayList<String> getWebsite(String website) { // http://www.website.com/web/
String website2 = website;
website2 = website
.toLowerCase()
.replace("http://", "")
.replace("http:/", "")
.replace("https://", "")
.replace("https:/", "")
.replace("browser://", "")
.replace("browser:/", "")
.replace("ftp://", "")
.replace("ftp:/", "")
.replace("www.", "")
.trim();
String website3 = website2;
if (website3.contains("/")) {
website3 = website3.split("/", 2)[0];
}
try {
ArrayList<String> web_list = new ArrayList<String>();
if (ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "ingame").contains("true")) {
if (ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "jarfile").contains("true")) {
if (website2.contains("/")) {
File subdir = new File((ExitParser.class.getResource(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "sublocation")+ website2).toURI().toString()) + website2.split("/", 2)[1]);
if (subdir.isFile() | subdir.isDirectory()) {
web_list.add((ExitParser.class.getResource(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "sublocation")+ website2).toURI().toString()) + website2.split("/", 2)[1]);
} else {
Logger.debug(subdir.toString());
return getWebsite("error404");
}
web_list.add(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "prefix") + website2);
} else {
web_list.add(ExitParser.class.getResource(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "location")).toURI().toString());
web_list.add(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "prefix") + website2);
}
return web_list;
} else {
if (website2.contains("/")) {
File subdir = new File(System.getProperty("user.dir"), (ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "sublocation")) + website2.split("/", 2)[1]);
if (subdir.isFile() | subdir.isDirectory()) {
web_list.add((new File(System.getProperty("user.dir"), (ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "sublocation"))).toURI().toString()) + website2.split("/", 2)[1]);
} else {
subdir = (new File((ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "sublocation")) + website2.split("/", 2)[1]));
if (subdir.isFile() | subdir.isDirectory()) {
web_list.add((new File((ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "sublocation")) + website2.split("/", 2)[1])).toURI().toString());
} else {
Logger.debug(subdir.toString());
return getWebsite("error404");
}
}
web_list.add(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "prefix") + website2);
} else {
if ((new File(System.getProperty("user.dir"), (ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "location")))).isFile()) {
web_list.add((new File(System.getProperty("user.dir"), (ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "location"))).toURI().toString()));
} else {
if ((new File((ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "location")))).isFile()) {
web_list.add((new File((ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "location"))).toURI().toString()));
} else {
return getWebsite("error404");
}
}
web_list.add(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "prefix") + website2);
}
return web_list;
}
} else {
web_list.add(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "location"));
web_list.add(ExitParser.getSettingValue(ExitParser.class.getResource("/web/websites.xml").toURI().toString(), website3.replace(".", "_"), "location"));
return web_list;
}
} catch(Exception e) {
Logger.error(e.getMessage());
return getWebsite("error404");
}
}
public static void writeXml(Document document, String fileName) throws TransformerConfigurationException, TransformerException, FileNotFoundException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(fileName));
}
public static Document readXml(InputStream stream) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
db.setEntityResolver(new NullResolver());
return db.parse(stream);
}
}
class NullResolver implements EntityResolver {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException,
IOException {
return new InputSource(new StringReader(""));
}
}