Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://svn2.assembla.com/svn/romraider/branches/dev_xmlmerge@316 38686702-15cf-42e4-a595-3071df8bf5ea
- Loading branch information
Showing
16 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
package com.romraider.metadata; | |||
|
|||
public abstract class AbstractTableMetadata { | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
package com.romraider.metadata; | |||
|
|||
public class RomMetadata { | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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(); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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
118
src/com/romraider/metadata/RomMetadataIndexBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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(); | |||
}*/ | |||
|
|||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
package com.romraider.metadata; | |||
|
|||
public class RomNotFoundException extends Exception { | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
package com.romraider.metadata; | |||
|
|||
public class Table1DMetadata extends AbstractTableMetadata { | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
package com.romraider.metadata; | |||
|
|||
public class Table2DMetadata extends AbstractTableMetadata { | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
package com.romraider.metadata; | |||
|
|||
public class Table3DMetadata extends AbstractTableMetadata { | |||
|
|||
} |
Oops, something went wrong.