More cleanup

This commit is contained in:
Robin K 2021-09-19 13:07:06 +02:00
parent 0ae900774a
commit 992e51ea3e
4 changed files with 102 additions and 108 deletions

View File

@ -63,7 +63,7 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
private static final Logger LOGGER = Logger.getLogger(Rom.class);
private static final ResourceBundle rb = new ResourceUtil().getBundle(
Rom.class.getName());
private RomID romID = new RomID();
private RomID romID;
private String fileName = "";
private File fullFileName = new File(".");
private byte[] binData;
@ -72,7 +72,8 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
private final Vector<TableTreeNode> tableNodes = new Vector<TableTreeNode>();
private LinkedList<ChecksumManager> checksumManagers = new LinkedList<ChecksumManager>();
public Rom() {
public Rom(RomID romID) {
this.romID = romID;
tableNodes.clear();
}
@ -201,8 +202,6 @@ public class Rom extends DefaultMutableTreeNode implements Serializable {
Table table = tableNodes.get(i).getTable();
try {
// if storageaddress has not been set (or is set to 0) omit table
//Why can the address not be zero? - Changed
if (table.getStorageAddress() >= 0) {
try {
table.populateTable(binData, this.getRomID().getRamOffset());

View File

@ -33,7 +33,7 @@ public class RomPropertyPanel extends javax.swing.JPanel {
private static final long serialVersionUID = 5583360728106071942L;
private static final ResourceBundle rb = new ResourceUtil().getBundle(
RomPropertyPanel.class.getName());
Rom rom = new Rom();
Rom rom;
public RomPropertyPanel(Rom rom) {
initComponents();

View File

@ -17,8 +17,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//DOM XML parser for ROMs
package com.romraider.xml;
import static com.romraider.xml.DOMHelper.unmarshallAttribute;
@ -46,18 +44,44 @@ public final class DOMRomUnmarshaller {
.getLogger(DOMRomUnmarshaller.class);
private JProgressPane progress = null;
private ChecksumManager checksumManager = null;
private TableScaleAttributeHandler tableScaleHandler = new TableScaleAttributeHandler();
private TableScaleUnmarshaller tableScaleHandler = new TableScaleUnmarshaller();
public Rom unmarshallXMLDefinition(Node rootNode, byte[] input,
JProgressPane progress) throws RomNotFoundException,
XMLParseException, StackOverflowError, Exception {
this.progress = progress;
// Unmarshall scales first
tableScaleHandler.unmarshallBaseScales(rootNode);
RomID romId = new RomID();
Node n = findRomNodeMatch(rootNode, null, input, romId);
if(n != null) {
Rom rom = new Rom(romId);
Rom output = unmarshallRom(n, rom);
// set ram offset
output.getRomID().setRamOffset(
output.getRomID().getFileSize()
- input.length);
return output;
}
throw new RomNotFoundException();
}
//Find the correct Rom Node either by xmlID or by input bytes
//Supplying both will return null
private Node findRomNodeMatch(Node rootNode, String xmlID, byte[] input, RomID romId) {
if(xmlID == null && input == null) return null;
if(xmlID != null && input != null) return null;
Node n;
NodeList nodes = rootNode.getChildNodes();
tableScaleHandler.unmarshallBaseScales(nodes);
// now unmarshall roms
for (int i = 0; i < nodes.getLength(); i++) {
n = nodes.item(i);
@ -68,30 +92,29 @@ public final class DOMRomUnmarshaller {
for (int z = 0; z < nodes2.getLength(); z++) {
n2 = nodes2.item(z);
if (n2.getNodeType() == ELEMENT_NODE
&& n2.getNodeName().equalsIgnoreCase("romid")) {
RomID romID = unmarshallRomID(n2, new RomID());
romId = unmarshallRomID(n2, romId);
//Check if bytes match in file
if (romID.checkMatch(input)) {
Rom output = unmarshallRom(n, new Rom());
// set ram offset
output.getRomID().setRamOffset(
output.getRomID().getFileSize()
- input.length);
return output;
if(input != null && romId.checkMatch(input)) {
return n;
}
//Check if the ID matches
else if(xmlID != null && romId.getXmlid().equalsIgnoreCase(xmlID)) {
return n;
}
//ROM only has one ID Node, so we can skip the rest after we found it
break;
}
}
}
}
throw new RomNotFoundException();
return null;
}
public Rom unmarshallRom(Node rootNode, Rom rom) throws XMLParseException,
@ -123,33 +146,25 @@ public final class DOMRomUnmarshaller {
} else if (n.getNodeName().equalsIgnoreCase("table")) {
Table table = null;
try {
table = rom.getTableByName(unmarshallAttribute(n, "name",
null));
table = rom.getTableByName(unmarshallAttribute(n, "name", null));
} catch (TableNotFoundException e) {
/*
* table does not
* already exist (do
* nothing)
*/
//Table does not already exist (do nothing)
} catch (InvalidTableNameException iex) {
// Table name is null or empty. Do nothing.
}
try {
table = tableScaleHandler.unmarshallTable(n, table, rom);
//rom.addTableByName(table);
if (table != null) {
//rom.removeTableByName(table);
rom.addTable(table);
}
} catch (TableIsOmittedException ex) {
// table is not supported in inherited def (skip)
if (table != null) {
//rom.removeTableByName(table);
rom.removeTable(table);
}
} catch (XMLParseException ex) {
LOGGER.error("Error unmarshalling rom", ex);
LOGGER.error(ex.getMessage());
}
} else if (n.getNodeName().equalsIgnoreCase("checksum")) {
rom.getRomID().setChecksum(unmarshallAttribute(n, "type", ""));
@ -167,32 +182,15 @@ public final class DOMRomUnmarshaller {
public Rom getBaseRom(Node rootNode, String xmlID, Rom rom)
throws XMLParseException, RomNotFoundException, StackOverflowError,
Exception {
Node n;
NodeList nodes = rootNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
n = nodes.item(i);
Node n = findRomNodeMatch(rootNode, xmlID, null, new RomID());
if (n.getNodeType() == ELEMENT_NODE
&& n.getNodeName().equalsIgnoreCase("rom")) {
Node n2;
NodeList nodes2 = n.getChildNodes();
for (int z = 0; z < nodes2.getLength(); z++) {
n2 = nodes2.item(z);
if (n2.getNodeType() == ELEMENT_NODE
&& n2.getNodeName().equalsIgnoreCase("romid")) {
RomID romID = unmarshallRomID(n2, new RomID());
if (romID.getXmlid().equalsIgnoreCase(xmlID)) {
if(n != null) {
Rom returnrom = unmarshallRom(n, rom);
returnrom.getRomID().setObsolete(false);
return returnrom;
}
}
}
}
}
throw new RomNotFoundException();
}
@ -204,56 +202,58 @@ public final class DOMRomUnmarshaller {
n = nodes.item(i);
if (n.getNodeType() == ELEMENT_NODE) {
String nodeName = n.getNodeName();
if (n.getNodeName().equalsIgnoreCase("xmlid")) {
if (nodeName.equalsIgnoreCase("xmlid")) {
romID.setXmlid(unmarshallText(n));
} else if (n.getNodeName()
.equalsIgnoreCase("internalidaddress")) {
} else if (nodeName.equalsIgnoreCase("internalidaddress")) {
romID.setInternalIdAddress(RomAttributeParser
.parseHexString(unmarshallText(n)));
} else if (n.getNodeName().equalsIgnoreCase("internalidstring")) {
} else if (nodeName.equalsIgnoreCase("internalidstring")) {
romID.setInternalIdString(unmarshallText(n));
if (romID.getInternalIdString() == null) {
romID.setInternalIdString("");
}
} else if (n.getNodeName().equalsIgnoreCase("caseid")) {
} else if (nodeName.equalsIgnoreCase("caseid")) {
romID.setCaseId(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("ecuid")) {
} else if (nodeName.equalsIgnoreCase("ecuid")) {
romID.setEcuId(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("make")) {
} else if (nodeName.equalsIgnoreCase("make")) {
romID.setMake(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("market")) {
} else if (nodeName.equalsIgnoreCase("market")) {
romID.setMarket(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("model")) {
} else if (nodeName.equalsIgnoreCase("model")) {
romID.setModel(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("submodel")) {
} else if (nodeName.equalsIgnoreCase("submodel")) {
romID.setSubModel(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("transmission")) {
} else if (nodeName.equalsIgnoreCase("transmission")) {
romID.setTransmission(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("year")) {
} else if (nodeName.equalsIgnoreCase("year")) {
romID.setYear(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("flashmethod")) {
} else if (nodeName.equalsIgnoreCase("flashmethod")) {
romID.setFlashMethod(unmarshallText(n));
} else if (n.getNodeName().equalsIgnoreCase("memmodel")) {
} else if (nodeName.equalsIgnoreCase("memmodel")) {
romID.setMemModel(unmarshallText(n));
tableScaleHandler.setMemModelEndian(unmarshallAttribute(n, "endian", null));
} else if (n.getNodeName().equalsIgnoreCase("filesize")) {
} else if (nodeName.equalsIgnoreCase("filesize")) {
romID.setFileSize(RomAttributeParser
.parseFileSize(unmarshallText(n)));
} else if (n.getNodeName().equalsIgnoreCase("obsolete")) {
} else if (nodeName.equalsIgnoreCase("obsolete")) {
romID.setObsolete(Boolean.parseBoolean(unmarshallText(n)));
} else { /* unexpected element in RomID (skip) */

View File

@ -51,22 +51,21 @@ import com.romraider.swing.DebugPanel;
import com.romraider.util.ObjectCloner;
import com.romraider.util.SettingsManager;
public class TableScaleAttributeHandler {
private static final Logger LOGGER = Logger.getLogger(TableScaleAttributeHandler.class);
public class TableScaleUnmarshaller {
private static final Logger LOGGER = Logger.getLogger(TableScaleUnmarshaller.class);
private final Map<String, Integer> tableNames = new HashMap<String, Integer>();
private final List<Scale> scales = new ArrayList<Scale>();
private String memModelEndian = null;
private final Scale rawScale = new Scale();
public void setMemModelEndian(String endian) {
memModelEndian = endian;
}
public void unmarshallBaseScales(NodeList nodes) {
public void unmarshallBaseScales(Node rootNode) {
NodeList nodes = rootNode.getChildNodes();
Node n;
// unmarshall scales first
for (int i = 0; i < nodes.getLength(); i++) {
n = nodes.item(i);
@ -88,6 +87,7 @@ public class TableScaleAttributeHandler {
if (!unmarshallAttribute(tableNode, "base", "none").equalsIgnoreCase(
"none")) { // copy base table for inheritance
try {
//Why is this needed?
table = (Table) ObjectCloner
.deepCopy(rom.getTableByName(unmarshallAttribute(tableNode,
"base", "none")));
@ -100,7 +100,6 @@ public class TableScaleAttributeHandler {
JOptionPane.showMessageDialog(ECUEditorManager.getECUEditor(),
new DebugPanel(ex, SettingsManager.getSettings().getSupportURL()), "Exception",
JOptionPane.ERROR_MESSAGE);
}
}
@ -108,46 +107,42 @@ public class TableScaleAttributeHandler {
// create new instance (otherwise it
// is inherited)
final String tn = unmarshallAttribute(tableNode, "name", "unknown");
final String type = unmarshallAttribute(tableNode, "type", "unknown");
final String type = unmarshallAttribute(tableNode, "type", "none");
if (tableNames.containsKey(tn) || type.contains("xis")) {
if (unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("3D")) {
if (type.equalsIgnoreCase("3D")) {
table = new Table3D();
table.getScales().add(rawScale);
((Table3D) table).getXAxis().getScales().add(rawScale);
((Table3D) table).getYAxis().getScales().add(rawScale);
} else if (unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("2D")) {
} else if (type.equalsIgnoreCase("2D")) {
table = new Table2D();
table.getScales().add(rawScale);
((Table2D) table).getAxis().getScales().add(rawScale);
} else if (unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("1D")) {
} else if (type.equalsIgnoreCase("1D")) {
table = new Table1D(Table.TableType.TABLE_1D);
} else if (unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("X Axis")
|| unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("Static X Axis")) {
} else if (type.equalsIgnoreCase("X Axis")
|| type.equalsIgnoreCase("Static X Axis")) {
table = new Table1D(Table.TableType.X_AXIS);
} else if (unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("Y Axis")
|| unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("Static Y Axis")) {
} else if (type.equalsIgnoreCase("Y Axis")
|| type.equalsIgnoreCase("Static Y Axis")) {
table = new Table1D(Table.TableType.Y_AXIS);
} else if (unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("Switch")) {
} else if (type.equalsIgnoreCase("Switch")) {
table = new TableSwitch();
} else if (unmarshallAttribute(tableNode, "type", "unknown")
.equalsIgnoreCase("BitwiseSwitch")) {
} else if (type.equalsIgnoreCase("BitwiseSwitch")) {
table = new TableBitwiseSwitch();
}
else if(type.equalsIgnoreCase("none")){
throw new XMLParseException("Table type unspecified for "
+ tableNode.getAttributes().getNamedItem("name"));
}
else {
throw new XMLParseException("Error loading table, "
throw new XMLParseException("Table type " + type + " unknown for "
+ tableNode.getAttributes().getNamedItem("name"));
}
}