Skip to content

Commit

Permalink
Added input data dump for people who are unable to open files
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryLoenwind committed Mar 2, 2017
1 parent f790793 commit 07ea23f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/main/java/crazypants/enderio/config/recipes/RecipeFactory.java
Expand Up @@ -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;
Expand Down Expand Up @@ -36,16 +37,27 @@ public static <T extends RecipeRoot> 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 {
Expand All @@ -66,6 +78,34 @@ public static <T extends RecipeRoot> 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 extends RecipeRoot> T readStax(T target, String rootElement, InputStream in) throws XMLStreamException, InvalidRecipeConfigException {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
Expand Down
Expand Up @@ -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 {
Expand All @@ -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());
}
}
Expand Down

0 comments on commit 07ea23f

Please sign in to comment.