Updated definition handler

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@351 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
Jared Gould 2006-11-17 22:15:59 +00:00
parent 28c3b52178
commit 240cf19dad
8 changed files with 321 additions and 63 deletions

View File

@ -1,10 +1,19 @@
package enginuity.logger.xml;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JButton;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
public final class SaxParserFactory {
@ -18,4 +27,17 @@ public final class SaxParserFactory {
parserFactory.setXIncludeAware(false);
return parserFactory.newSAXParser();
}
public static void main(String args[]) {
try {
SAXParser parser = SaxParserFactory.getSaxParser();
BufferedInputStream b = new BufferedInputStream(new FileInputStream(new File("/ecu_defs.xml")));
System.out.println(b.available());
parser.parse(b, new DefaultHandler());
System.out.println(parser.isValidating());
} catch (Exception ex) {
System.err.println(ex);
}
}
}

View File

@ -1,6 +1,9 @@
package enginuity.newmaps.definition;
import enginuity.newmaps.ecudata.Scale;
import enginuity.newmaps.ecudata.Unit;
import java.util.StringTokenizer;
import java.util.Vector;
public final class AttributeParser {
@ -26,5 +29,20 @@ public final class AttributeParser {
return Scale.STORAGE_TYPE_CHAR;
}
}
public static String[] parseValueString(String s, String delim) {
StringTokenizer t = new StringTokenizer(s, delim);
Vector<String> values = new Vector<String>();
while (t.hasMoreTokens()) {
values.add(t.nextToken());
}
return (String[])values.toArray();
}
public static int parseUnitSystem(String s) {
if (s.equalsIgnoreCase("metric")) return Unit.SYSTEM_METRIC;
else if (s.equalsIgnoreCase("standard")) return Unit.SYSTEM_STANDARD;
else return Unit.SYSTEM_UNIVERSAL;
}
}

View File

@ -1,20 +1,25 @@
package enginuity.newmaps.definition;
import enginuity.newmaps.Rom;
import enginuity.newmaps.ecudata.ECUData;
import enginuity.newmaps.ecudata.Parameter;
import enginuity.newmaps.ecudata.Scale;
import enginuity.newmaps.ecudata.Switch;
import enginuity.newmaps.ecudata.Table2D;
import enginuity.newmaps.ecudata.Table3D;
import enginuity.newmaps.ecudata.Unit;
import enginuity.util.NamedSet;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import static java.lang.Boolean.parseBoolean;
import static java.lang.Float.parseFloat;
import static enginuity.util.HexUtil.hexToInt;
import static java.lang.Integer.parseInt;
import static enginuity.newmaps.definition.AttributeParser.parseEndian;
import static enginuity.newmaps.definition.AttributeParser.parseStorageType;
import static enginuity.newmaps.definition.AttributeParser.*;
import enginuity.newmaps.ecudata.Axis;
import enginuity.newmaps.ecudata.Category;
import enginuity.util.exception.NameableNotFoundException;
import java.util.Stack;
public class RomDefinitionHandler extends DefaultHandler {
@ -60,6 +65,7 @@ public class RomDefinitionHandler extends DefaultHandler {
private static final String ATTR_LOG_PARAM = "logparam";
private static final String ATTR_TO_REAL = "to_real";
private static final String ATTR_TO_BYTE = "to_byte";
private static final String ATTR_SYSTEM = "system";
private static final String ATTR_FORMAT = "format";
private static final String ATTR_COARSE_INCREMENT = "coarseincrement";
private static final String ATTR_FINE_INCREMENT = "fineincrement";
@ -67,50 +73,66 @@ public class RomDefinitionHandler extends DefaultHandler {
private RomTreeBuilder roms;
private Rom rom;
private NamedSet tables;
private NamedSet scales;
private NamedSet units;
private Category category;
private ECUData table;
private Scale scale;
private Axis axis;
private Unit unit;
private String dataValues;
private Stack<Category> categoryStack;
private Category categories;
private NamedSet<ECUData> tables;
private NamedSet<Scale> scales;
private NamedSet<Unit> units;
public RomDefinitionHandler(RomTreeBuilder roms) {
this.roms = roms;
// These lines may cause some problems down the line.. I can't think through it right now
categoryStack = new Stack<Category>();
category = new Category("Root");
categoryStack.add(category);
}
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (TAG_ROM.equalsIgnoreCase(qName)) {
//
// If "base" attribute is set, find base rom in collection
//
// TODO: Deal with roms that aren't found
String name = attributes.getValue(ATTR_NAME);
if (attributes.getValue(ATTR_BASE).equalsIgnoreCase(VAL_TRUE)) {
try {
try {
//
// If "base" attribute is set, find base rom in collection
//
String name = attributes.getValue(ATTR_NAME);
if (attributes.getValue(ATTR_BASE).length() > 0) {
rom = (Rom)roms.get(attributes.getValue(ATTR_NAME));
} catch (Exception ex) {
} else {
rom = new Rom(name);
}
} else {
rom = new Rom(name);
// Set all other attributes
rom.setName(attributes.getValue(ATTR_NAME));
rom.setIdAddress(hexToInt(attributes.getValue(ATTR_ID_ADDRESS)));
rom.setIdString(attributes.getValue(ATTR_ID_STRING));
rom.setDescription(attributes.getValue(ATTR_DESCRIPTION));
rom.setMemmodel(attributes.getValue(ATTR_MEMMODEL));
rom.setFlashmethod(attributes.getValue(ATTR_FLASH_METHOD));
rom.setCaseid(attributes.getValue(ATTR_CASE_ID));
rom.setObsolete(parseBoolean(attributes.getValue(ATTR_OBSOLETE)));
rom.setAbstract(parseBoolean(attributes.getValue(ATTR_ABSTRACT)));
} catch (NameableNotFoundException ex) {
// uhh.. do something
}
// Set all other attributes
rom.setName(attributes.getValue(ATTR_NAME));
rom.setIdAddress(hexToInt(attributes.getValue(ATTR_ID_ADDRESS)));
rom.setIdString(attributes.getValue(ATTR_ID_STRING));
rom.setDescription(attributes.getValue(ATTR_DESCRIPTION));
rom.setMemmodel(attributes.getValue(ATTR_MEMMODEL));
rom.setFlashmethod(attributes.getValue(ATTR_FLASH_METHOD));
rom.setCaseid(attributes.getValue(ATTR_CASE_ID));
rom.setObsolete(parseBoolean(attributes.getValue(ATTR_OBSOLETE)));
rom.setAbstract(parseBoolean(attributes.getValue(ATTR_ABSTRACT)));
} else if (TAG_PARAMETERS.equalsIgnoreCase(qName)) {
} else if (TAG_CATEGORY.equalsIgnoreCase(qName)) {
category = new Category(attributes.getValue(ATTR_NAME));
// Set all other attributes
category.setDescription(attributes.getValue(ATTR_DESCRIPTION));
} else if (TAG_TABLE3D.equalsIgnoreCase(qName)) {
//
@ -172,9 +194,8 @@ public class RomDefinitionHandler extends DefaultHandler {
table.setUserLevel(parseInt(attributes.getValue(ATTR_USER_LEVEL)));
table.setAddress(hexToInt(attributes.getValue(ATTR_NAME)));
// TODO: Deal with scale
//table(attributes.getValue(ATTR_NAME));
// TODO: Deal with scale
} else if (TAG_SWITCH.equalsIgnoreCase(qName)) {
@ -194,10 +215,7 @@ public class RomDefinitionHandler extends DefaultHandler {
table.setSize(parseInt(attributes.getValue(ATTR_SIZE)));
// TODO: Deal with scale
//table(attributes.getValue(ATTR_NAME));
} else if (TAG_SCALES.equalsIgnoreCase(qName)) {
} else if (TAG_SCALE.equalsIgnoreCase(qName)) {
@ -215,17 +233,35 @@ public class RomDefinitionHandler extends DefaultHandler {
scale.setEndian(parseEndian(attributes.getValue(ATTR_ENDIAN)));
scale.setStorageType(parseStorageType(attributes.getValue(ATTR_STORAGE_TYPE)));
scale.setLogParam(attributes.getValue(ATTR_LOG_PARAM));
} else if (TAG_AXIS.equalsIgnoreCase(qName) ||
TAG_X_AXIS.equalsIgnoreCase(qName) ||
TAG_Y_AXIS.equalsIgnoreCase(qName)) {
Axis axis = new Axis(attributes.getValue(ATTR_NAME));
// Set all other attributes
axis.setSize(parseInt(attributes.getValue(ATTR_SIZE)));
axis.setAddress(hexToInt(attributes.getValue(ATTR_ADDRESS)));
} else if (TAG_AXIS.equalsIgnoreCase(qName)) {
} else if (TAG_Y_AXIS.equalsIgnoreCase(qName)) {
} else if (TAG_X_AXIS.equalsIgnoreCase(qName)) {
// TODO: Deal with scales
} else if (TAG_DATA.equalsIgnoreCase(qName)) {
// TODO: Deal with data
} else if (TAG_UNIT.equalsIgnoreCase(qName)) {
Unit unit = new Unit(attributes.getValue(ATTR_NAME));
// Set all other attributes
unit.setSystem(parseUnitSystem(attributes.getValue(ATTR_SYSTEM)));
unit.setTo_byte(attributes.getValue(ATTR_TO_BYTE));
unit.setTo_real(attributes.getValue(ATTR_TO_REAL));
unit.setFormat(attributes.getValue(ATTR_FORMAT));
unit.setCoarseIncrement(parseFloat(attributes.getValue(ATTR_COARSE_INCREMENT)));
unit.setFineIncrement(parseFloat(attributes.getValue(ATTR_FINE_INCREMENT)));
} else if (TAG_STATE.equalsIgnoreCase(qName)) {
@ -235,6 +271,65 @@ public class RomDefinitionHandler extends DefaultHandler {
public void endElement(String uri, String localName, String qName) {
if (TAG_SCALE.equalsIgnoreCase(qName)) {
scales.add(scale);
// Clear object for next element
scale = null;
} else if (TAG_UNIT.equalsIgnoreCase(qName)) {
units.add(unit);
// Clear object for next element
unit = null;
} else if (TAG_CATEGORY.equalsIgnoreCase(qName)) {
// Ugh.. I think I'm at least along the right lines
categoryStack.pop().add(category);
// But I know it's not quite right. Very confusing.
} else if (TAG_TABLE3D.equalsIgnoreCase(qName) ||
TAG_TABLE2D.equalsIgnoreCase(qName) ||
TAG_PARAMETER.equalsIgnoreCase(qName) ||
TAG_SWITCH.equalsIgnoreCase(qName)) {
tables.add(table);
// Clear object for next element
table = null;
} else if (TAG_AXIS.equalsIgnoreCase(qName)) {
((Table2D)table).setAxis(axis);
// Clear object for next element
axis = null;
} else if (TAG_X_AXIS.equalsIgnoreCase(qName)) {
((Table3D)table).setXaxis(axis);
// Clear object for next element
axis = null;
} else if (TAG_Y_AXIS.equalsIgnoreCase(qName)) {
((Table3D)table).setYaxis(axis);
// Clear object for next element
axis = null;
} else if (TAG_ROM.equalsIgnoreCase(qName)) {
roms.add(rom);
}
}
}
}

View File

@ -0,0 +1,44 @@
package enginuity.newmaps.ecudata;
import enginuity.util.Nameable;
import enginuity.util.NamedSet;
public class Category extends NamedSet implements Nameable {
private String name;
private String description;
private NamedSet<Category> subcats;
private ECUData[] tables;
private Category() { }
public Category(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ECUData[] getEcuData() {
return tables;
}
public void setEcuData(ECUData[] ecuData) {
this.tables = tables;
}
}

View File

@ -1,5 +1,7 @@
package enginuity.newmaps.ecudata;
import static enginuity.newmaps.definition.AttributeParser.parseValueString;
public class SourceDefAxis extends Axis {
private String[] values;
@ -12,8 +14,8 @@ public class SourceDefAxis extends Axis {
return values;
}
public void setValues(String[] values) {
this.values = values;
public void setValues(String s, String delim) {
values = parseValueString(s, delim);
}
}

View File

@ -2,65 +2,78 @@ package enginuity.newmaps.ecudata;
public class Unit {
protected String name;
protected String to_real;
protected String to_byte;
protected String format;
protected float coarseIncrement;
protected float fineIncrement;
public static final int SYSTEM_STANDARD = 0;
public static final int SYSTEM_METRIC = 1;
public static final int SYSTEM_UNIVERSAL = 2;
private String name;
private String to_real;
private String to_byte;
private String format;
private int system;
private float coarseIncrement;
private float fineIncrement;
private Unit() { }
public Unit(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTo_real() {
return to_real;
}
public void setTo_real(String to_real) {
this.to_real = to_real;
}
public String getTo_byte() {
return to_byte;
}
public void setTo_byte(String to_byte) {
this.to_byte = to_byte;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public float getCoarseIncrement() {
return coarseIncrement;
}
public void setCoarseIncrement(float coarseIncrement) {
this.coarseIncrement = coarseIncrement;
}
public float getFineIncrement() {
return fineIncrement;
}
public void setFineIncrement(float fineIncrement) {
this.fineIncrement = fineIncrement;
}
public int getSystem() {
return system;
}
public void setSystem(int system) {
this.system = system;
}
}

View File

@ -0,0 +1,8 @@
package enginuity.newmaps.exception;
public class RomNotFoundException extends Exception {
public RomNotFoundException() {
}
}

View File

@ -1,9 +1,12 @@
package enginuity.util;
import enginuity.util.exception.NameableNotFoundException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
public class NamedSet {
public class NamedSet<E> implements Set<E> {
Vector<Nameable> objects = new Vector<Nameable>();
@ -50,4 +53,57 @@ public class NamedSet {
// Name not found, throw exception
throw new NameableNotFoundException();
}
public boolean isEmpty() {
return objects.isEmpty();
}
public boolean contains(Object o) {
return objects.contains(o);
}
public Iterator<E> iterator() {
return (Iterator<E>)objects.iterator();
}
public Object[] toArray() {
return objects.toArray();
}
public boolean add(E o) {
add((E) o);
return true;
}
public boolean remove(Object o) {
return objects.remove(o);
}
public boolean containsAll(Collection<?> c) {
return objects.containsAll(c);
}
public boolean addAll(Collection<? extends E> c) {
Iterator it = c.iterator();
while (it.hasNext()) {
add((E)it.next());
}
return true;
}
public boolean retainAll(Collection<?> c) {
return objects.retainAll(c);
}
public boolean removeAll(Collection<?> c) {
return objects.removeAll(c);
}
public void clear() {
objects.clear();
}
public <T> T[] toArray(T[] a) {
return null;
}
}