Skip to content

Commit

Permalink
Saving progress
Browse files Browse the repository at this point in the history
git-svn-id: https://svn2.assembla.com/svn/romraider/branches/dev_xmlmerge@316 38686702-15cf-42e4-a595-3071df8bf5ea
  • Loading branch information
qoncept96 committed Jan 19, 2011
1 parent a841c79 commit 260e4ff
Show file tree
Hide file tree
Showing 16 changed files with 565 additions and 0 deletions.
1 change: 1 addition & 0 deletions .classpath
Expand Up @@ -29,5 +29,6 @@
<classpathentry kind="lib" path="lib/common/jfreechart-1.0.9.jar"/> <classpathentry kind="lib" path="lib/common/jfreechart-1.0.9.jar"/>
<classpathentry kind="lib" path="lib/common/jcommon-1.0.12.jar"/> <classpathentry kind="lib" path="lib/common/jcommon-1.0.12.jar"/>
<classpathentry kind="lib" path="lib/common/jinvoke-3.0.3.jar"/> <classpathentry kind="lib" path="lib/common/jinvoke-3.0.3.jar"/>
<classpathentry kind="lib" path="lib/common/xmlmerge-full.jar"/>
<classpathentry kind="output" path="build/classes"/> <classpathentry kind="output" path="build/classes"/>
</classpath> </classpath>
Binary file added lib/common/xmlmerge-full.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions src/com/romraider/metadata/AbstractTableMetadata.java
@@ -0,0 +1,5 @@
package com.romraider.metadata;

public abstract class AbstractTableMetadata {

}
77 changes: 77 additions & 0 deletions src/com/romraider/metadata/RomIndexID.java
@@ -0,0 +1,77 @@
package com.romraider.metadata;

import java.io.File;

public final class RomIndexID {

private String xmlid;
private String internalIDString;
private int internalIDAddress = -1; // Default to -1 to help identify abstract roms
private String include;
private File definitionFile;
private String ecuid;
private String year;
private String market;
private String make;
private String model;
private String submodel;
private String transmission;
private String memmodel;
private String flashMethod;

public String getXmlID() { return xmlid; }
public String getInternalIDString() { return internalIDString; }
public int getInternalIDAddress(){ return internalIDAddress; }
public String getInclude() { return include; }
public File getDefinitionFile() { return definitionFile; }
public String getXmlid() { return xmlid; }
public String getEcuid() { return ecuid; }
public String getYear() { return year; }
public String getMarket() { return market; }
public String getMake() { return make; }
public String getModel() { return model; }
public String getSubmodel() { return submodel; }
public String getTransmission() { return transmission; }
public String getMemmodel() { return memmodel; }
public String getFlashMethod() { return flashMethod; }

public void setXmlID(String xmlid) { this.xmlid = xmlid; }
public void setInternalIDString(String internalIDString){ this.internalIDString = internalIDString; }
public void setInternalIDAddress(int internalIDAddress) { this.internalIDAddress = internalIDAddress; }
public void setInclude(String include) { this.include = include; }
public void setDefinitionFile(File file) { this.definitionFile = file; }
public void setXmlid(String xmlid) { this.xmlid = xmlid; }
public void setEcuid(String ecuid) { this.ecuid = ecuid; }
public void setYear(String year) { this.year = year; }
public void setMarket(String market) { this.market = market; }
public void setMake(String make) { this.make = make; }
public void setModel(String model) { this.model = model; }
public void setSubmodel(String submodel) { this.submodel = submodel; }
public void setTransmission(String transmission) { this.transmission = transmission; }
public void setMemmodel(String memmodel) { this.memmodel = memmodel; }
public void setFlashMethod(String flashMethod) { this.flashMethod = flashMethod; }

public boolean isReady() {
// TODO: Validate romid is usable
return true;
}

public boolean isAbstract() {
if (internalIDString == null || internalIDAddress == -1) return true;
else return false;
}

public String toString() {
return "xmlid:" + xmlid +
" ecuid:" + ecuid +
" year:" + year +
" market:" + market +
" make:" + make +
" model:" + model +
" submodel:" + submodel +
" transmission:" + transmission +
" memmodel:" + memmodel +
" flashmethod:" + flashMethod;
}

}
5 changes: 5 additions & 0 deletions src/com/romraider/metadata/RomMetadata.java
@@ -0,0 +1,5 @@
package com.romraider.metadata;

public class RomMetadata {

}
82 changes: 82 additions & 0 deletions src/com/romraider/metadata/RomMetadataHandler.java
@@ -0,0 +1,82 @@
package com.romraider.metadata;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Vector;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.romraider.xml.DOMRomMetadataRefactorUtil;
import com.romraider.xml.DOMRomMetadataUnmarshaller;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class RomMetadataHandler {

public static RomMetadata getMetadata(File rom, RomMetadataIndex romIndex) throws IOException, RomNotFoundException, SAXException {

RomMetadata rm = new RomMetadata();

// Read rom file
FileInputStream fis = new FileInputStream(rom);
try {
byte[] buffer = new byte[(int)rom.length()];
fis.read(buffer);

// Identify rom and find in index
RomIndexID romid = identifyRom(buffer, romIndex);

// Build array of rom metadata hierarchy starting with lowest level
Vector<RomIndexID> hierarchy = new Vector<RomIndexID>();
getIncludeHierarchy(romid, romIndex, hierarchy);
Document[] refactoredDocuments = new Document[hierarchy.size()];

// Pass hierarchy to unmarshaller one at a time
for (int i = 0; i < hierarchy.size(); i++) {
// Refactor document to RomRaider spec
RomIndexID r = hierarchy.get(i);
InputSource src = new InputSource(new FileInputStream(r.getDefinitionFile()));
DOMParser parser = new DOMParser();
parser.parse(src);

refactoredDocuments[i] = DOMRomMetadataRefactorUtil.refactorDocument(parser.getDocument());
}

} finally {
fis.close();
}

return rm;
}


private static RomIndexID[] getIncludeHierarchy(RomIndexID base, RomMetadataIndex romIndex, Vector<RomIndexID> hierarchy) throws RomNotFoundException {
hierarchy.add(0, base);
if (base.getInclude() != null) {
getIncludeHierarchy(romIndex.getRomIDByXmlID(base.getInclude()), romIndex, hierarchy);
}
return hierarchy.toArray(new RomIndexID[hierarchy.size()]);
}


private static RomIndexID identifyRom(byte[] buffer, RomMetadataIndex romIndex) throws RomNotFoundException {
for (int i = 0; i < romIndex.size(); i++) {
RomIndexID romid = romIndex.getRomIDByIndex(i);

if (!romid.isAbstract() &&
buffer.length >= romid.getInternalIDAddress() + romid.getInternalIDString().length()) {

String testString = new String(buffer, romid.getInternalIDAddress(), romid.getInternalIDString().length());

if (testString.equalsIgnoreCase(romid.getInternalIDString())) {
return romid;
}
}
}
// If not found in index, throw exception
throw new RomNotFoundException();
}

}
35 changes: 35 additions & 0 deletions src/com/romraider/metadata/RomMetadataIndex.java
@@ -0,0 +1,35 @@
package com.romraider.metadata;


public class RomMetadataIndex {

RomIndexID[] romid;
int index = 0;

@SuppressWarnings("unused")
private RomMetadataIndex() {}

public RomMetadataIndex (RomIndexID[] romid) {
this.romid = romid;
}

public RomIndexID getRomIDByXmlID (String XmlID) throws RomNotFoundException {
for (int i = 0; i < romid.length; i++) {
if (romid[i].getXmlid().equalsIgnoreCase(XmlID)) return romid[i];
}
throw new RomNotFoundException();
}

public RomIndexID getRomID (int i) {
return romid[i];
}

public RomIndexID getRomIDByIndex (int i) {
return romid[i];
}

public int size() {
return romid.length;
}

}
118 changes: 118 additions & 0 deletions src/com/romraider/metadata/RomMetadataIndexBuilder.java
@@ -0,0 +1,118 @@
package com.romraider.metadata;

import java.io.BufferedWriter;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.management.modelmbean.XMLParseException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import ch.elca.el4j.services.xmlmerge.XmlMerge;
import ch.elca.el4j.services.xmlmerge.merge.DefaultXmlMerge;
import com.romraider.maps.Rom;
import com.romraider.swing.JProgressPane;
import com.romraider.util.FileListing;
import com.romraider.xml.DOMRomMetadataUnmarshaller;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public final class RomMetadataIndexBuilder {

public static RomMetadataIndex createIndex(File path) throws FileNotFoundException {
Vector<RomIndexID> romVector = new Vector<RomIndexID>();
List<File> files = FileListing.getFileListing(path);

// Read files recursively and parse xml id and address
for (File f : files) {
// Ignore directories
if (f.isFile()) {

try {
romVector.addAll(DOMRomMetadataUnmarshaller.unmarshallRomIDIndex(f));

} catch (Exception x) {
// TODO: Handle invalid definitions
}
}
}

RomIndexID[] romArray = romVector.toArray(new RomIndexID[romVector.size()]);
return new RomMetadataIndex(romArray);
}


public static void main(String[] args) {
// JG: Testing ...
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer(new javax.xml.transform.stream.StreamSource("c:\\users\\owner\\desktop\\rommetadata\\rr.xsl"));
transformer.transform(
new javax.xml.transform.stream.StreamSource("c:\\users\\owner\\desktop\\16BITBASE.xml"),
new javax.xml.transform.stream.StreamResult(new FileOutputStream("c:\\users\\owner\\desktop\\new.xml")));
} catch (Exception e) {
e.printStackTrace( );
}


/*long time1 = System.currentTimeMillis();
try {
File testdir = new File("c:\\users\\owner\\desktop\\rommetadata");
File input = new File("c:\\users\\owner\\desktop\\test.hex");
RomMetadataIndex romIndex = createIndex(testdir);
long time2 = System.currentTimeMillis();
System.out.println(romIndex.size() + " index built in " + (time2 - time1) + "ms");
RomMetadataHandler.getMetadata(input, romIndex);*/



/*InputSource src1 = new InputSource(new FileInputStream(new File("c:\\users\\owner\\desktop\\16BITBASE.xml")));
DOMParser parser = new DOMParser();
parser.parse(src1);
Document root1 = parser.getDocument();
InputSource src2 = new InputSource(new FileInputStream(new File("c:\\users\\owner\\desktop\\A4TF400E.xml")));
parser.parse(src2);
Document root2 = parser.getDocument();
Document[] docs = new Document[] {root1, root2};
XmlMerge merge = new DefaultXmlMerge();
Document mergedDoc = merge.merge(docs);
File output = new File("c:\\users\\owner\\desktop\\new.xml");
FileOutputStream fos = new FileOutputStream(output);
OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true);
of.setIndent(1);
of.setIndenting(true);
try {
XMLSerializer serializer = new XMLSerializer(fos, of);
serializer.serialize(mergedDoc);
fos.flush();
} finally {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}*/

}

}
5 changes: 5 additions & 0 deletions src/com/romraider/metadata/RomNotFoundException.java
@@ -0,0 +1,5 @@
package com.romraider.metadata;

public class RomNotFoundException extends Exception {

}
5 changes: 5 additions & 0 deletions src/com/romraider/metadata/Table1DMetadata.java
@@ -0,0 +1,5 @@
package com.romraider.metadata;

public class Table1DMetadata extends AbstractTableMetadata {

}
5 changes: 5 additions & 0 deletions src/com/romraider/metadata/Table2DMetadata.java
@@ -0,0 +1,5 @@
package com.romraider.metadata;

public class Table2DMetadata extends AbstractTableMetadata {

}
5 changes: 5 additions & 0 deletions src/com/romraider/metadata/Table3DMetadata.java
@@ -0,0 +1,5 @@
package com.romraider.metadata;

public class Table3DMetadata extends AbstractTableMetadata {

}

0 comments on commit 260e4ff

Please sign in to comment.