Reformat and clean up "provider" package

This commit is contained in:
P0nk
2021-09-09 23:26:19 +02:00
parent 6be6ea9927
commit 9b1dc76854
4 changed files with 161 additions and 161 deletions

View File

@@ -29,9 +29,9 @@ import provider.DataFileEntry;
import java.util.*; import java.util.*;
public class WZDirectoryEntry extends WZEntry implements DataDirectoryEntry { public class WZDirectoryEntry extends WZEntry implements DataDirectoryEntry {
private List<DataDirectoryEntry> subdirs = new ArrayList<>(); private final List<DataDirectoryEntry> subdirs = new ArrayList<>();
private List<DataFileEntry> files = new ArrayList<>(); private final List<DataFileEntry> files = new ArrayList<>();
private Map<String, DataEntry> entries = new HashMap<>(); private final Map<String, DataEntry> entries = new HashMap<>();
public WZDirectoryEntry(String name, int size, int checksum, DataEntity parent) { public WZDirectoryEntry(String name, int size, int checksum, DataEntity parent) {
super(name, size, checksum, parent); super(name, size, checksum, parent);

View File

@@ -25,11 +25,11 @@ import provider.DataEntity;
import provider.DataEntry; import provider.DataEntry;
public class WZEntry implements DataEntry { public class WZEntry implements DataEntry {
private String name; private final String name;
private int size; private final int size;
private int checksum; private final int checksum;
private int offset; private int offset;
private DataEntity parent; private final DataEntity parent;
public WZEntry(String name, int size, int checksum, DataEntity parent) { public WZEntry(String name, int size, int checksum, DataEntity parent) {
super(); super();

View File

@@ -42,168 +42,168 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
public class XMLDomMapleData implements Data { public class XMLDomMapleData implements Data {
private Node node; private final Node node;
private File imageDataDir; private File imageDataDir;
public XMLDomMapleData(FileInputStream fis, File imageDataDir) { public XMLDomMapleData(FileInputStream fis, File imageDataDir) {
try { try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(fis); Document document = documentBuilder.parse(fis);
this.node = document.getFirstChild(); this.node = document.getFirstChild();
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (SAXException e) { } catch (SAXException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
this.imageDataDir = imageDataDir; this.imageDataDir = imageDataDir;
} }
private XMLDomMapleData(Node node) { private XMLDomMapleData(Node node) {
this.node = node; this.node = node;
} }
@Override @Override
public synchronized Data getChildByPath(String path) { // the whole XML reading system seems susceptible to give nulls on strenuous read scenarios public synchronized Data getChildByPath(String path) { // the whole XML reading system seems susceptible to give nulls on strenuous read scenarios
String[] segments = path.split("/"); String[] segments = path.split("/");
if (segments[0].equals("..")) { if (segments[0].equals("..")) {
return ((Data) getParent()).getChildByPath(path.substring(path.indexOf("/") + 1)); return ((Data) getParent()).getChildByPath(path.substring(path.indexOf("/") + 1));
} }
Node myNode; Node myNode;
myNode = node; myNode = node;
for (String s : segments) { for (String s : segments) {
NodeList childNodes = myNode.getChildNodes(); NodeList childNodes = myNode.getChildNodes();
boolean foundChild = false; boolean foundChild = false;
for (int i = 0; i < childNodes.getLength(); i++) { for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i); Node childNode = childNodes.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getAttributes().getNamedItem("name").getNodeValue().equals(s)) { if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getAttributes().getNamedItem("name").getNodeValue().equals(s)) {
myNode = childNode; myNode = childNode;
foundChild = true; foundChild = true;
break; break;
}
}
if (!foundChild) {
return null;
}
} }
}
if (!foundChild) {
return null;
}
}
XMLDomMapleData ret = new XMLDomMapleData(myNode); XMLDomMapleData ret = new XMLDomMapleData(myNode);
ret.imageDataDir = new File(imageDataDir, getName() + "/" + path).getParentFile(); ret.imageDataDir = new File(imageDataDir, getName() + "/" + path).getParentFile();
return ret; return ret;
} }
@Override @Override
public synchronized List<Data> getChildren() { public synchronized List<Data> getChildren() {
List<Data> ret = new ArrayList<>(); List<Data> ret = new ArrayList<>();
NodeList childNodes = node.getChildNodes(); NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) { for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i); Node childNode = childNodes.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE) { if (childNode.getNodeType() == Node.ELEMENT_NODE) {
XMLDomMapleData child = new XMLDomMapleData(childNode); XMLDomMapleData child = new XMLDomMapleData(childNode);
child.imageDataDir = new File(imageDataDir, getName()); child.imageDataDir = new File(imageDataDir, getName());
ret.add(child); ret.add(child);
} }
} }
return ret; return ret;
} }
@Override
public synchronized Object getData() {
NamedNodeMap attributes = node.getAttributes();
DataType type = getType();
switch (type) {
case DOUBLE:
case FLOAT:
case INT:
case SHORT: {
String value = attributes.getNamedItem("value").getNodeValue();
Number nval = GameConstants.parseNumber(value);
@Override
public synchronized Object getData() {
NamedNodeMap attributes = node.getAttributes();
DataType type = getType();
switch (type) { switch (type) {
case DOUBLE: case DOUBLE:
case FLOAT: return nval.doubleValue();
case INT: case FLOAT:
case SHORT: { return nval.floatValue();
String value = attributes.getNamedItem("value").getNodeValue(); case INT:
Number nval = GameConstants.parseNumber(value); return nval.intValue();
case SHORT:
switch (type) { return nval.shortValue();
case DOUBLE: default:
return nval.doubleValue();
case FLOAT:
return nval.floatValue();
case INT:
return nval.intValue();
case SHORT:
return nval.shortValue();
default:
return null;
}
}
case STRING:
case UOL: {
String value = attributes.getNamedItem("value").getNodeValue();
return value;
}
case VECTOR: {
String x = attributes.getNamedItem("x").getNodeValue();
String y = attributes.getNamedItem("y").getNodeValue();
return new Point(Integer.parseInt(x), Integer.parseInt(y));
}
default:
return null;
}
}
@Override
public synchronized DataType getType() {
String nodeName = node.getNodeName();
switch (nodeName) {
case "imgdir":
return DataType.PROPERTY;
case "canvas":
return DataType.CANVAS;
case "convex":
return DataType.CONVEX;
case "sound":
return DataType.SOUND;
case "uol":
return DataType.UOL;
case "double":
return DataType.DOUBLE;
case "float":
return DataType.FLOAT;
case "int":
return DataType.INT;
case "short":
return DataType.SHORT;
case "string":
return DataType.STRING;
case "vector":
return DataType.VECTOR;
case "null":
return DataType.IMG_0x00;
}
return null;
}
@Override
public synchronized DataEntity getParent() {
Node parentNode;
parentNode = node.getParentNode();
if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
return null; return null;
} }
XMLDomMapleData parentData = new XMLDomMapleData(parentNode); }
parentData.imageDataDir = imageDataDir.getParentFile(); case STRING:
return parentData; case UOL: {
} String value = attributes.getNamedItem("value").getNodeValue();
return value;
}
case VECTOR: {
String x = attributes.getNamedItem("x").getNodeValue();
String y = attributes.getNamedItem("y").getNodeValue();
return new Point(Integer.parseInt(x), Integer.parseInt(y));
}
default:
return null;
}
}
@Override @Override
public synchronized String getName() { public synchronized DataType getType() {
return node.getAttributes().getNamedItem("name").getNodeValue(); String nodeName = node.getNodeName();
}
@Override switch (nodeName) {
public synchronized Iterator<Data> iterator() { case "imgdir":
return getChildren().iterator(); return DataType.PROPERTY;
} case "canvas":
return DataType.CANVAS;
case "convex":
return DataType.CONVEX;
case "sound":
return DataType.SOUND;
case "uol":
return DataType.UOL;
case "double":
return DataType.DOUBLE;
case "float":
return DataType.FLOAT;
case "int":
return DataType.INT;
case "short":
return DataType.SHORT;
case "string":
return DataType.STRING;
case "vector":
return DataType.VECTOR;
case "null":
return DataType.IMG_0x00;
}
return null;
}
@Override
public synchronized DataEntity getParent() {
Node parentNode;
parentNode = node.getParentNode();
if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
return null;
}
XMLDomMapleData parentData = new XMLDomMapleData(parentNode);
parentData.imageDataDir = imageDataDir.getParentFile();
return parentData;
}
@Override
public synchronized String getName() {
return node.getAttributes().getNamedItem("name").getNodeValue();
}
@Override
public synchronized Iterator<Data> iterator() {
return getChildren().iterator();
}
} }

View File

@@ -31,8 +31,8 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
public class XMLWZFile implements DataProvider { public class XMLWZFile implements DataProvider {
private File root; private final File root;
private WZDirectoryEntry rootForNavigation; private final WZDirectoryEntry rootForNavigation;
public XMLWZFile(File fileIn) { public XMLWZFile(File fileIn) {
root = fileIn; root = fileIn;