diff --git a/src/main/java/crazypants/enderio/config/recipes/RecipeFactory.java b/src/main/java/crazypants/enderio/config/recipes/RecipeFactory.java index 550c18228c..9877d62ebe 100644 --- a/src/main/java/crazypants/enderio/config/recipes/RecipeFactory.java +++ b/src/main/java/crazypants/enderio/config/recipes/RecipeFactory.java @@ -3,6 +3,7 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; @@ -36,16 +37,27 @@ public static T readFile(T target, String rootElement, St copyCore(fileName + "_core.xml"); // default first, so the user file has access to the aliases - defaultFileStream = RecipeFactory.class.getResourceAsStream("/assets/" + EnderIO.DOMAIN + "/config/" + fileName + "_core.xml"); + final String coreFile = "/assets/" + EnderIO.DOMAIN + "/config/" + fileName + "_core.xml"; + defaultFileStream = RecipeFactory.class.getResourceAsStream(coreFile); if (defaultFileStream == null) { - throw new IOException("Could not get resource /assets/" + EnderIO.DOMAIN + "/config/" + fileName + "_core.xml from classpath. "); + throw new IOException("Could not get resource " + coreFile + " from classpath. "); + } + try { + defaultConfig = readStax(target.copy(target), rootElement, defaultFileStream); + } catch (XMLStreamException e) { + printContentsOnError(RecipeFactory.class.getResourceAsStream(coreFile), coreFile); + throw e; } - defaultConfig = readStax(target.copy(target), rootElement, defaultFileStream); File configFile = new File(Config.configDirectory, fileName + "_user.xml"); if (configFile.exists()) { userFileStream = new FileInputStream(configFile); - userConfig = readStax(target, rootElement, userFileStream); + try { + userConfig = readStax(target, rootElement, userFileStream); + } catch (XMLStreamException e) { + printContentsOnError(new FileInputStream(configFile), configFile.toString()); + throw e; + } userConfig.addRecipes(defaultConfig); return userConfig; } else { @@ -66,6 +78,34 @@ public static T readFile(T target, String rootElement, St } } + protected static void printContentsOnError(InputStream stream, String filename) throws FileNotFoundException, IOException { + Log.error("Failed to parse xml from file '", filename, "'. Content:"); + try { + int data = 0; + while (data != -1) { + StringBuilder sb1 = new StringBuilder(), sb2 = new StringBuilder(); + for (int i = 0; i < 16; i++) { + data = stream.read(); + if (data != -1) { + sb1.append(String.format("%02x ", data)); + if (data > 32 && data < 128) { + sb2.appendCodePoint(data); + } else { + sb2.append("."); + } + } else { + sb1.append(" "); + } + } + Log.error(sb1, sb2); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + IOUtils.closeQuietly(stream); + } + } + private static T readStax(T target, String rootElement, InputStream in) throws XMLStreamException, InvalidRecipeConfigException { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); diff --git a/src/main/java/crazypants/enderio/config/recipes/RecipeLoader.java b/src/main/java/crazypants/enderio/config/recipes/RecipeLoader.java index a357c02056..96043ffffb 100644 --- a/src/main/java/crazypants/enderio/config/recipes/RecipeLoader.java +++ b/src/main/java/crazypants/enderio/config/recipes/RecipeLoader.java @@ -5,6 +5,7 @@ import javax.xml.stream.XMLStreamException; import crazypants.enderio.EnderIO; +import crazypants.enderio.Log; import crazypants.enderio.config.recipes.xml.Recipes; public class RecipeLoader { @@ -27,8 +28,12 @@ public static void addRecipes() { } catch (InvalidRecipeConfigException e) { recipeError(filename, e.getMessage()); } catch (IOException e) { + Log.error("IO error while reading file:"); + e.printStackTrace(); recipeError(filename, "IO error while reading file:" + e.getMessage()); } catch (XMLStreamException e) { + Log.error("File has malformed XML:"); + e.printStackTrace(); recipeError(filename, "File has malformed XML:" + e.getMessage()); } }