Index mostly complete, still needs checksums

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@374 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
Jared Gould 2006-12-05 23:58:43 +00:00
parent 9d50858f21
commit a455ff6d34
5 changed files with 211 additions and 11 deletions

View File

@ -1,7 +1,20 @@
package enginuity.newmaps.definition.index;
import enginuity.util.NamedSet;
import java.io.Serializable;
import java.util.Iterator;
public class Index<IndexItem> extends NamedSet {
public class Index<IndexItem> extends NamedSet implements Serializable {
public String toString() {
StringBuffer sb = new StringBuffer();
Iterator it = iterator();
while (it.hasNext()) {
sb.append(it.next()+"\n");
}
return sb+"";
}
}

View File

@ -1,10 +1,103 @@
package enginuity.newmaps.definition.index;
import enginuity.newmaps.xml.SaxParserFactory;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
public class IndexBuilder {
private static final String INDEX_FILE_NAME = "index.xml";
public IndexBuilder(File src) {
private File file;
private Index index = new Index();
public IndexBuilder(File file) {
this.file = file;
// Process all definition files
traverse(file);
// Output index
save();
}
private void save() {
//
// Inherit storage addresses where necessary
//
Iterator it1 = index.iterator();
while (it1.hasNext()) {
IndexItem item = (IndexItem)it1.next();
if (!item.isAbstract() && item.getIdAddress() == 0) {
Iterator it2 = index.iterator();
while (it2.hasNext()) {
IndexItem parentItem = (IndexItem)it2.next();
if (parentItem.getName().equalsIgnoreCase(item.getBase())) {
item.setIdAddress(parentItem.getIdAddress());
break;
}
}
}
}
//System.out.println(index);
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();
}
}
private void traverse(File file) {
processFile(file);
if (file.isDirectory()) {
String[] children = file.list();
for (int i=0; i<children.length; i++) {
traverse(new File(file, children[i]));
}
}
}
private void processFile(File file) {
if (file.isFile() && !file.getName().equalsIgnoreCase(INDEX_FILE_NAME)) {
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();
}
}
}
public static void main(String[] args) {
IndexBuilder b = new IndexBuilder(new File("/newdefs"));
}
}

View File

@ -0,0 +1,45 @@
package enginuity.newmaps.definition.index;
import enginuity.util.HexUtil;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
public class IndexHandler extends DefaultHandler {
private static final String TAG_ROM = "rom";
private static final String ATTR_NAME = "name";
private static final String ATTR_BASE = "base";
private static final String ATTR_ADDRESS = "idaddress";
private static final String ATTR_ID = "idstring";
private static final String ATTR_ABSTRACT = "abstract";
IndexItem item = new IndexItem();
public void startElement(String uri, String localName, String qName, Attributes attr) {
if (qName.equalsIgnoreCase(TAG_ROM)) {
// Set all attributes if they exist
item.setName(attr.getValue(ATTR_NAME));
if (attr.getIndex(ATTR_BASE) > -1 && attr.getValue(ATTR_BASE).length() > 0)
item.setBase(attr.getValue(ATTR_BASE));
if (attr.getIndex(ATTR_ADDRESS) > -1 && attr.getValue(ATTR_ADDRESS).length() > 0)
item.setIdAddress(HexUtil.hexToInt(attr.getValue(ATTR_ADDRESS)));
if (attr.getIndex(ATTR_ID) > -1 && attr.getValue(ATTR_ID).length() > 0)
item.setIdString(attr.getValue(ATTR_ID));
if (attr.getIndex(ATTR_ABSTRACT) > -1 && attr.getValue(ATTR_ABSTRACT).length() > 0)
item.setAbstract(Boolean.parseBoolean(attr.getValue(ATTR_ABSTRACT)));
}
}
public IndexItem getItem() {
return item;
}
}

View File

@ -2,18 +2,17 @@ package enginuity.newmaps.definition.index;
import enginuity.util.Nameable;
import java.io.File;
import java.io.Serializable;
public class IndexItem implements Nameable {
public class IndexItem implements Nameable, Serializable {
private String name;
private String base;
private File file;
private int idAddress;
private String idString;
private String name = "";
private String base = "";
private File file = new File("");
private int idAddress = 0;
private String idString = "";
private boolean isAbstract = false;
public IndexItem(String name) {
this.name = name;
}
public String getName() {
return name;
@ -54,5 +53,22 @@ public class IndexItem implements Nameable {
public void setIdString(String idString) {
this.idString = idString;
}
public boolean isAbstract() {
return isAbstract;
}
public void setAbstract(boolean isAbstract) {
this.isAbstract = isAbstract;
}
public String toString() {
return "Name: " + name +
"Base: " + base +
"File: " + file +
"Address: " + idAddress +
"ID: " + idString +
"Abstract: " + isAbstract;
}
}

View File

@ -0,0 +1,33 @@
package enginuity.util;
import java.io.*;
import java.security.MessageDigest;
public class MD5Checksum {
public static byte[] createChecksum(String filename) throws Exception {
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
public static String getMD5Checksum(String filename) throws Exception {
byte[] b = createChecksum(filename);
String result = "";
for (int i=0; i < b.length; i++) {
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
}