Upgrade to Java NIO
This commit is contained in:
@@ -25,9 +25,10 @@ import provider.wz.WZFiles;
|
||||
import provider.wz.XMLWZFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class DataProviderFactory {
|
||||
private static DataProvider getWZ(File in) {
|
||||
private static DataProvider getWZ(Path in) {
|
||||
return new XMLWZFile(in);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package provider.wz;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public enum WZFiles {
|
||||
QUEST("Quest"),
|
||||
@@ -25,12 +27,12 @@ public enum WZFiles {
|
||||
this.fileName = name + ".wz";
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return new File(DIRECTORY, fileName);
|
||||
public Path getFile() {
|
||||
return Paths.get(DIRECTORY).resolve(fileName);
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return getFile().getPath();
|
||||
return getFile().toString();
|
||||
}
|
||||
|
||||
private static String getWzDirectory() {
|
||||
|
||||
@@ -37,15 +37,16 @@ import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class XMLDomMapleData implements Data {
|
||||
private final Node node;
|
||||
private File imageDataDir;
|
||||
private Path imageDataDir;
|
||||
|
||||
public XMLDomMapleData(FileInputStream fis, File imageDataDir) {
|
||||
public XMLDomMapleData(FileInputStream fis, Path imageDataDir) {
|
||||
try {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
@@ -91,7 +92,7 @@ public class XMLDomMapleData implements Data {
|
||||
}
|
||||
|
||||
XMLDomMapleData ret = new XMLDomMapleData(myNode);
|
||||
ret.imageDataDir = new File(imageDataDir, getName() + "/" + path).getParentFile();
|
||||
ret.imageDataDir = imageDataDir.resolve(getName().trim()).resolve(path).getParent();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -103,12 +104,12 @@ public class XMLDomMapleData implements Data {
|
||||
for (int i = 0; i < childNodes.getLength(); i++) {
|
||||
Node childNode = childNodes.item(i);
|
||||
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
XMLDomMapleData child = new XMLDomMapleData(childNode);
|
||||
child.imageDataDir = new File(imageDataDir, getName());
|
||||
XMLDomMapleData child = new XMLDomMapleData(childNode);
|
||||
child.imageDataDir = imageDataDir.resolve(getName().trim());
|
||||
ret.add(child);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -193,7 +194,7 @@ public class XMLDomMapleData implements Data {
|
||||
return null;
|
||||
}
|
||||
XMLDomMapleData parentData = new XMLDomMapleData(parentNode);
|
||||
parentData.imageDataDir = imageDataDir.getParentFile();
|
||||
parentData.imageDataDir = imageDataDir.getParent();
|
||||
return parentData;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,53 +29,59 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class XMLWZFile implements DataProvider {
|
||||
private final File root;
|
||||
private final Path root;
|
||||
private final WZDirectoryEntry rootForNavigation;
|
||||
|
||||
public XMLWZFile(File fileIn) {
|
||||
public XMLWZFile(Path fileIn) {
|
||||
root = fileIn;
|
||||
rootForNavigation = new WZDirectoryEntry(fileIn.getName(), 0, 0, null);
|
||||
rootForNavigation = new WZDirectoryEntry(fileIn.getFileName().toString(), 0, 0, null);
|
||||
fillMapleDataEntitys(root, rootForNavigation);
|
||||
}
|
||||
|
||||
private void fillMapleDataEntitys(File lroot, WZDirectoryEntry wzdir) {
|
||||
for (File file : lroot.listFiles()) {
|
||||
String fileName = file.getName();
|
||||
if (file.isDirectory() && !fileName.endsWith(".img")) {
|
||||
WZDirectoryEntry newDir = new WZDirectoryEntry(fileName, 0, 0, wzdir);
|
||||
wzdir.addDirectory(newDir);
|
||||
fillMapleDataEntitys(file, newDir);
|
||||
} else if (fileName.endsWith(".xml")) {
|
||||
wzdir.addFile(new WZFileEntry(fileName.substring(0, fileName.length() - 4), 0, 0, wzdir));
|
||||
private void fillMapleDataEntitys(Path lroot, WZDirectoryEntry wzdir) {
|
||||
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(lroot)) {
|
||||
for (Path path : stream) {
|
||||
String fileName = path.getFileName().toString();
|
||||
if(Files.isDirectory(path) && !fileName.endsWith(".img") ) {
|
||||
WZDirectoryEntry newDir = new WZDirectoryEntry(fileName, 0, 0, wzdir);
|
||||
wzdir.addDirectory(newDir);
|
||||
fillMapleDataEntitys(path, newDir);
|
||||
} else if (fileName.endsWith(".xml")) {
|
||||
wzdir.addFile(new WZFileEntry(fileName.substring(0, fileName.length() - 4), 0, 0, wzdir));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Data getData(String path) {
|
||||
File dataFile = new File(root, path + ".xml");
|
||||
File imageDataDir = new File(root, path);
|
||||
if (!dataFile.exists()) {
|
||||
Path dataFile = root.resolve(path + ".xml");
|
||||
//File(root, path + ".xml");
|
||||
Path imageDataDir = root.resolve(path);
|
||||
//File imageDataDir = new File(root, path);
|
||||
if (!Files.exists(dataFile)) {
|
||||
return null;//bitches
|
||||
}
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream(dataFile);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException("Datafile " + path + " does not exist in " + root.getAbsolutePath());
|
||||
}
|
||||
final XMLDomMapleData domMapleData;
|
||||
try {
|
||||
domMapleData = new XMLDomMapleData(fis, imageDataDir.getParentFile());
|
||||
} finally {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try(FileInputStream fis = new FileInputStream(dataFile.toString()) ) {
|
||||
domMapleData = new XMLDomMapleData(fis, imageDataDir.getParent());
|
||||
}catch (FileNotFoundException e) {
|
||||
throw new RuntimeException("Datafile " + path + " does not exist in " + root.toAbsolutePath());
|
||||
}catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
return domMapleData;
|
||||
}
|
||||
|
||||
@@ -83,4 +89,4 @@ public class XMLWZFile implements DataProvider {
|
||||
public DataDirectoryEntry getRoot() {
|
||||
return rootForNavigation;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user