Index works, need to finish up parser and update schema

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@382 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
Jared Gould 2006-12-08 12:30:16 +00:00
parent e7fda2998e
commit 6ac72d03b1
9 changed files with 177 additions and 116 deletions

View File

@ -1,17 +1,18 @@
package enginuity.newmaps.definition;
import enginuity.newmaps.definition.index.Index;
import enginuity.newmaps.ecudata.*;
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.*;
import enginuity.newmaps.definition.index.Index;
import enginuity.newmaps.ecudata.*;
import enginuity.util.NamedSet;
import enginuity.newmaps.definition.index.IndexItem;
import enginuity.newmaps.xml.SaxParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import enginuity.util.exception.NameableNotFoundException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
@ -111,12 +112,12 @@ public class RomDefinitionHandler extends DefaultHandler {
try {
// Look through parsed roms first
rom = (Rom)roms.get(attr.getValue(ATTR_BASE));
System.out.println(rom.getName() + "(" + attr.getValue(ATTR_BASE) + ") found in parsed");
} catch (NameableNotFoundException ex) {
// Or try opening the file
try {
rom = (Rom)getParentRom((IndexItem)index.get(attr.getValue(ATTR_BASE)));
System.out.println(rom.getName() + "(" + attr.getValue(ATTR_BASE) + ") found in unparsed");
} catch (NameableNotFoundException ex2) {
// No base definition found, cannot continue
return;
@ -153,8 +154,6 @@ public class RomDefinitionHandler extends DefaultHandler {
rom.setObsolete(parseBoolean(attr.getValue(ATTR_OBSOLETE)));
if (attr.getIndex(ATTR_ABSTRACT) > -1)
rom.setAbstract(parseBoolean(attr.getValue(ATTR_ABSTRACT)));
} else if (TAG_CATEGORY.equalsIgnoreCase(qName)) {
@ -476,7 +475,15 @@ public class RomDefinitionHandler extends DefaultHandler {
tables = new NamedSet<ECUData>();
scales = new NamedSet<Scale>();
units = new NamedSet<Unit>();
}
} else if (TAG_DATA.equalsIgnoreCase(qName)) {
// Set static axis values
if (axis instanceof SourceDefAxis) {
((SourceDefAxis)axis).setValues(charBuffer+"", " ");
}
}
}

View File

@ -4,6 +4,8 @@ import enginuity.util.NamedSet;
import java.io.Serializable;
import java.util.Iterator;
import static enginuity.util.MD5Checksum.getMD5Checksum;
import enginuity.util.Nameable;
import enginuity.util.exception.NameableNotFoundException;
import java.io.File;
public class Index extends NamedSet<IndexItem> implements Serializable {
@ -13,31 +15,27 @@ public class Index extends NamedSet<IndexItem> implements Serializable {
Iterator it = iterator();
while (it.hasNext()) {
sb.append(it.next()+"\n");
sb.append(((IndexItem)it.next()).getFile()+"\n");
}
return sb+"";
}
public boolean fileCurrent(File file) {
public boolean fileCurrent(File file, IndexItem item) {
// Checks whether file exists and matches checksum
Iterator it = iterator();
while (it.hasNext()) {
IndexItem item = (IndexItem)it.next();
if (item.getFile().getAbsolutePath().equals(file.getAbsolutePath())) {
// Item found, return whether checksum matches
try {
return item.getChecksum().equals(getMD5Checksum(file.getAbsolutePath()));
} catch (Exception ex) {
return false;
}
if (item.getFile().equals(file)) {
// Item found, return whether checksum matches
try {
return item.getChecksum().equals(getMD5Checksum(file.getPath()));
} catch (Exception ex) {
return false;
}
} else {
return false;
}
// Iterator has finished without match
return false;
}
@ -55,11 +53,48 @@ public class Index extends NamedSet<IndexItem> implements Serializable {
IndexItem parentItem = (IndexItem)get(item.getBase());
if (item.getIdAddress() == 0) item.setIdAddress(parentItem.getIdAddress());
} catch (Exception ex) { }
} else if (item.isAbstract()) {
} catch (Exception ex) { }
}
}
}
}
public void cleanup() {
Iterator it = iterator();
while (it.hasNext()) {
IndexItem item = (IndexItem)it.next();
try {
if (!item.getFile().exists()) remove(item);
} catch (NameableNotFoundException ex) {
// ignore
}
}
}
public IndexItem get(File file) throws NameableNotFoundException {
Iterator it = iterator();
while (it.hasNext()) {
IndexItem item = (IndexItem)it.next();
try {
if (item.getFile().getPath().equals(file.getPath())) {
return item;
}
} catch (Exception ex) { }
}
throw new NameableNotFoundException(file.getName());
}
public void add(Nameable n) {
try {
IndexItem item = (IndexItem)n;
item.setChecksum(getMD5Checksum(item.getFile().getPath()));
} catch (Exception ex) {
}
super.add((Nameable)n);
}
}

View File

@ -8,49 +8,28 @@ import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import static enginuity.util.MD5Checksum.getMD5Checksum;
import enginuity.util.exception.NameableNotFoundException;
public class IndexBuilder {
public static final String INDEX_FILE_NAME = "index.xml";
public static final String INDEX_FILE_NAME = "index.dat";
public static final String MEMMODEL_FILE_NAME = "memmodels.xml";
private File file;
private File dir;
private Index index;
public IndexBuilder(File file, Index index) {
this.file = file;
public IndexBuilder(File dir, Index index) {
this.dir = dir;
this.index = index;
// Clean up existing defs
index.cleanup();
// Process all definition files
traverse(file);
traverse(dir);
// Output index
save(index, file);
}
private static void save(Index index, File file) {
index.fixInheritance();
try {
FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile() + "/" + INDEX_FILE_NAME);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(index);
oos.flush();
oos.close();
// Open stuff
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file.getAbsoluteFile() + "/" + INDEX_FILE_NAME));
System.out.println((Index)ois.readObject());
ois.close();
} catch (Exception ex) {
// TODO: Exception handling
ex.printStackTrace();
}
save(index, dir);
}
private void traverse(File file) {
@ -59,35 +38,63 @@ public class IndexBuilder {
for (int i=0; i<children.length; i++) {
traverse(new File(file, children[i]));
}
} else {
} else {
processFile(file);
}
}
private void processFile(File file) {
if (!file.getName().equalsIgnoreCase(INDEX_FILE_NAME) &&
!file.getName().equalsIgnoreCase(MEMMODEL_FILE_NAME) &&
!index.fileCurrent(file)) {
!file.getName().equalsIgnoreCase(MEMMODEL_FILE_NAME)) {
IndexItem idxItem = null;
try {
IndexHandler handler = new IndexHandler();
SaxParserFactory.getSaxParser().parse(new BufferedInputStream(new FileInputStream(file)), handler);
IndexItem item = handler.getItem();
item.setFile(file);
item.setChecksum(getMD5Checksum(file.getAbsolutePath()));
index.add(item);
idxItem = index.get(file);
} catch (NameableNotFoundException ex) {
idxItem = new IndexItem();
}
if (!index.fileCurrent(file, idxItem)) {
} catch (Exception ex) {
// TODO: Handle exceptions
ex.printStackTrace();
try {
IndexHandler handler = new IndexHandler();
SaxParserFactory.getSaxParser().parse(new BufferedInputStream(new FileInputStream(file)), handler);
IndexItem item = handler.getItem();
item.setFile(file);
index.add(item);
} catch (Exception ex) {
// TODO: Handle exceptions
ex.printStackTrace();
}
}
}
}
private static void save(Index index, File file) {
index.fixInheritance();
try {
FileOutputStream fos = new FileOutputStream(file + "/" + INDEX_FILE_NAME);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(index);
oos.flush();
oos.close();
} catch (Exception ex) {
// TODO: Exception handling
ex.printStackTrace();
}
}
public static void main(String[] args) {
IndexBuilder b = new IndexBuilder(new File("/newdefs"), new Index());
try {
File file = new File("/newdefs");
IndexBuilder b = new IndexBuilder(file, IndexUtil.getIndex(file));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

View File

@ -3,6 +3,7 @@ package enginuity.newmaps.definition.index;
import enginuity.util.Nameable;
import java.io.File;
import java.io.Serializable;
import static enginuity.util.MD5Checksum.getMD5Checksum;
public class IndexItem implements Nameable, Serializable {
@ -37,6 +38,11 @@ public class IndexItem implements Nameable, Serializable {
public void setFile(File file) {
this.file = file;
//System.out.println(file.getAbsolutePath());
try {
this.checksum = getMD5Checksum(file.getAbsolutePath());
//System.out.println(checksum);
} catch (Exception ex) { }
}
public int getIdAddress() {

View File

@ -2,20 +2,19 @@ package enginuity.newmaps.definition.index;
import enginuity.newmaps.definition.RomDefinitionHandler;
import enginuity.newmaps.xml.SaxParserFactory;
import static enginuity.util.MD5Checksum.getMD5Checksum;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Iterator;
import static enginuity.util.MD5Checksum.getMD5Checksum;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public abstract class IndexUtil {
public static void validateChecksums(File dir) {
public static void validateChecksums(File dir) {
Index index = null;
try {
index = getIndex(dir);
@ -23,12 +22,12 @@ public abstract class IndexUtil {
// Index file not found, create new
new IndexBuilder(dir, index);
return;
}
}
// If no exceptions, iterate through index checking checksums
Iterator it = index.iterator();
while (it.hasNext()) {
IndexItem item = (IndexItem) it.next();
IndexItem item = (IndexItem)it.next();
try {
if (!item.getChecksum().equalsIgnoreCase(getMD5Checksum(item.getFile().getAbsolutePath()))) {
@ -39,22 +38,26 @@ public abstract class IndexUtil {
}
}
}
public static Index getIndex(File dir) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(dir.getAbsoluteFile() + "/" + IndexBuilder.INDEX_FILE_NAME));
return (Index) ois.readObject();
public static Index getIndex(File dir) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(dir.getAbsoluteFile() + "/" + IndexBuilder.INDEX_FILE_NAME));
return (Index)ois.readObject();
} catch (Exception ex) {
return new Index();
}
}
public static void testMemUsage() {
try {
File dir = new File("/newdefs");
Index index = getIndex(dir);
//RomTreeBuilder builder = new RomTreeBuilder();
RomDefinitionHandler handler = new RomDefinitionHandler(index);
/*Iterator it = index.iterator();
Iterator it = index.iterator();
int i = 0;
long time = 0;
while (it.hasNext()) {
@ -65,27 +68,27 @@ public abstract class IndexUtil {
SaxParserFactory.getSaxParser().parse(inputStream1, handler);
time += (System.currentTimeMillis() - start);
}*/
long start = System.currentTimeMillis();
IndexItem item = (IndexItem) index.get(10);
}
/*long start = System.currentTimeMillis();
IndexItem item = (IndexItem)index.get(10);
//System.out.println("Adding " + item.getFile() + " (#" + ++i + ")");
InputStream inputStream1 = new BufferedInputStream(new FileInputStream(item.getFile()));
SaxParserFactory.getSaxParser().parse(inputStream1, handler);
System.out.println(System.currentTimeMillis() - start);
SaxParserFactory.getSaxParser().parse(inputStream1, handler);
System.out.println(System.currentTimeMillis() - start);*/
/*JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
testMemUsage();
}
}

View File

@ -1,4 +1,4 @@
package enginuity.newmaps.memmodel;
package enginuity.newmaps.definition.memmodel;
import enginuity.util.NamedSet;

View File

@ -1,4 +1,4 @@
package enginuity.newmaps.memmodel;
package enginuity.newmaps.definition.memmodel;
public class Segment {

View File

@ -1,4 +1,4 @@
package enginuity.newmaps.memmodel.definition;
package enginuity.newmaps.definition.memmodel.definition;
import org.xml.sax.helpers.DefaultHandler;

View File

@ -129,9 +129,7 @@ public class DefinitionBuilder {
StringBuffer path = new StringBuffer(folder.getAbsolutePath()+"/");
path.append(rom.getRomID().getMake()+"/");
new File(path+"").mkdir();
path.append(rom.getRomID().getModel()+"/");
new File(path+"").mkdir();
path.append(rom.getRomID().getSubModel()+"/");
path.append(rom.getRomID().getMemModel()+"/");
new File(path+"").mkdir();
path.append(rom.getRomIDString() + ".xml");
@ -381,6 +379,11 @@ public class DefinitionBuilder {
node.setAttribute("static", "true");
noOverride = false;
}
// TODO: Test this..
if (axis.isLocked() && (parentAxis == null || !parentAxis.isLocked())) {
node.setAttribute("static", "true");
noOverride = false;
}
if (axis.getDescription().length() > 0 && (parentAxis == null || !axis.getDescription().equalsIgnoreCase(parentAxis.getDescription()))) {
//desc.appendChild(doc.createTextNode(axis.getDescription()));
//noOverride = false;
@ -458,7 +461,7 @@ public class DefinitionBuilder {
for (int i = 0; i < values.length; i++) {
sb.append(values[i]+"");
if (i < values.length - 1) sb.append("|");
if (i < values.length - 1) sb.append(" ");
}
node.setAttribute("data", sb+"");