Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import javax.swing.JOptionPane;
- import nu.xom.Builder;
- import nu.xom.Document;
- import nu.xom.Element;
- import nu.xom.Elements;
- public class Main {
- public static void main(String[] args) {
- try {
- HashMap<Integer, String> partNames = loadPartNames();
- HashMap<Integer, Material> materials = loadMaterials();
- String fileName = JOptionPane.showInputDialog("Place the LXF file you want to use in the same directory as this jar.\nThen write the file name (with extension) in the box below.");
- if(!(new File(fileName).exists())) {
- JOptionPane.showMessageDialog(null, "The file\n"+(new File(fileName).getAbsolutePath())+"\ncould not be found :(");
- }
- Element root = openLXFFile(fileName);
- String fileNameNoExtension = fileName.substring(0, fileName.length() - 4);
- generatePartList(partNames, materials, root, fileNameNoExtension);
- JOptionPane.showMessageDialog(null, "Conversion complete!\nYou can find the new html file with the same name as the LXF in the same directory as this jar.");
- } catch (Exception e) {
- System.out.println("This means that something has gone wrong. Let me know what by sending me what's written below. Thanks!");
- e.printStackTrace();
- }
- }
- private static void generatePartList(HashMap<Integer, String> partNames, HashMap<Integer, Material> materials, Element root, String fileNameNoExtension) throws Exception {
- HashMap<Integer, HashMap<Material, Integer>> partCounts = new HashMap<Integer, HashMap<Material, Integer>>();
- Elements brickElements = root.getChildElements();
- for(int i = 0; i < brickElements.size(); i++) {
- Element brickElement = brickElements.get(i);
- Element partElement = brickElement.getFirstChildElement("Part");
- int designID = Integer.parseInt(partElement.getAttributeValue("designID"));
- int materialID = Integer.parseInt(partElement.getAttributeValue("materials").split(",")[0]);
- Material material = materials.get(materialID);
- register(partCounts, designID, material);
- }
- File output = new File(fileNameNoExtension + ".html");
- if(output.exists()) {
- output.delete();
- }
- output.createNewFile();
- FileWriter fileWriter = new FileWriter(output);
- BufferedWriter writer = new BufferedWriter(fileWriter);
- writer.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n ");
- writer.write("<html><body>\n");
- writer.write("<table><tr><td><b>part ID</b></td><td><b>part count</b></td><td><b>part name</b></td><td><b>colour</b></td>\n");
- for(int designID : partCounts.keySet()) {
- HashMap<Material, Integer> partMap = partCounts.get(designID);
- for(Material material : partMap.keySet()){
- writer.write("<tr><td>"+designID+"</td><td>"+partMap.get(material)+"</td><td>"+partNames.get(designID)+"</td><td style=\"background: rgba("+material.red+", "+material.green+", "+material.blue+", "+((double)material.alpha / 255d)+"); \"><br></td>\n");
- }
- }
- writer.write("</table></body></html>");
- writer.close();
- fileWriter.close();
- }
- private static void register(HashMap<Integer, HashMap<Material, Integer>> partCounts, int designID, Material material) {
- HashMap<Material, Integer> partMap;
- if(!partCounts.containsKey(designID)) {
- partMap = new HashMap<Material, Integer>();
- partCounts.put(designID, partMap);
- } else {
- partMap = partCounts.get(designID);
- }
- if(!partMap.containsKey(material)) {
- partMap.put(material, 1);
- } else {
- int partCount = partMap.get(material);
- partCount++;
- partMap.put(material, partCount);
- System.out.println(partCount);
- }
- }
- private static Element openLXFFile(String fileName) throws Exception {
- ZipFile lxfFile = new ZipFile(fileName);
- ZipEntry entry = lxfFile.getEntry("IMAGE100.LXFML");
- Builder builder = new Builder();
- Document doc = builder.build(lxfFile.getInputStream(entry));
- Element root = doc.getRootElement();
- return root.getFirstChildElement("Bricks");
- }
- private static HashMap<Integer, Material> loadMaterials() throws Exception {
- Builder builder = new Builder();
- Document doc = builder.build(new FileInputStream("res/materials.xml"));
- Element root = doc.getRootElement();
- Elements materialElements = root.getChildElements();
- HashMap<Integer, Material> materials = new HashMap<Integer, Material>();
- for(int i = 0; i < materialElements.size(); i++) {
- Element materialElement = materialElements.get(i);
- int materialID = Integer.parseInt(materialElement.getAttributeValue("MatID"));
- int red = Integer.parseInt(materialElement.getAttributeValue("Red"));
- int green = Integer.parseInt(materialElement.getAttributeValue("Green"));
- int blue = Integer.parseInt(materialElement.getAttributeValue("Blue"));
- int alpha = Integer.parseInt(materialElement.getAttributeValue("Alpha"));
- materials.put(materialID, new Material(red, green, blue, alpha));
- }
- return materials;
- }
- private static HashMap<Integer, String> loadPartNames() throws IOException {
- File namesFile = new File("res/partNames.txt");
- FileInputStream inStream = new FileInputStream(namesFile);
- InputStreamReader inStreamReader = new InputStreamReader(inStream);
- BufferedReader reader = new BufferedReader(inStreamReader);
- HashMap<Integer, String> partNames = new HashMap<Integer, String>();
- while(reader.ready()) {
- String line = reader.readLine();
- String[] parts = line.split("\t");
- int partID = Integer.parseInt(parts[0]);
- partNames.put(partID, parts[1]);
- }
- return partNames;
- }
- }
- //FILE: Material.java
- public class Material {
- public final int red;
- public final int green;
- public final int blue;
- public final int alpha;
- public Material(int red, int green, int blue, int alpha) {
- this.red = red;
- this.green = green;
- this.blue = blue;
- this.alpha = alpha;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement