From 235ec7243d5dd0ecdd093646f2a1f697a13329fc Mon Sep 17 00:00:00 2001 From: voruti <45130096+voruti@users.noreply.github.com> Date: Sun, 8 Mar 2020 14:46:55 +0100 Subject: [PATCH] Add Convertible --- .../java/voruti/json2config/Converter.java | 90 ++++++++++++------- .../java/voruti/json2config/IConvertible.java | 16 ++++ src/main/java/voruti/json2config/Item.java | 13 +-- src/main/java/voruti/json2config/Starter.java | 4 +- 4 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 src/main/java/voruti/json2config/IConvertible.java diff --git a/src/main/java/voruti/json2config/Converter.java b/src/main/java/voruti/json2config/Converter.java index 6371e41..f7364d0 100644 --- a/src/main/java/voruti/json2config/Converter.java +++ b/src/main/java/voruti/json2config/Converter.java @@ -26,21 +26,24 @@ */ public class Converter { + public enum Type { + ITEM, THING + } + private static final String CLASS_NAME = Converter.class.getName(); private static final Logger LOGGER = Logger.getLogger(CLASS_NAME); - private Map itemsMap; - /** - * Converts {@code jsonFile} to {@code itemsFile}. + * Converts {@code jsonFile} to {@code outputFile}. * - * @param jsonFile path to file (input) - * @param itemsFile path to file (output) + * @param jsonFile path to file (input) + * @param outputFile path to file (output) + * @param type type of file to convert */ - public Converter(String jsonFile, String itemsFile) { + public Converter(String jsonFile, String outputFile, Type type) { LOGGER.entering(CLASS_NAME, "", jsonFile); - itemsMap = new HashMap<>(); + Map convertibleMap = new HashMap<>(); File file = new File(jsonFile); Scanner sc; @@ -69,10 +72,14 @@ public Converter(String jsonFile, String itemsFile) { Item item = createItem(val); LOGGER.log(Level.INFO, "Adding item={0} to itemsMap", item); - itemsMap.put(key, item); + convertibleMap.put(key, item); } - writeItems(itemsFile); + // get lines from map: + List lines = convertibleMapToLines(convertibleMap); + + // write file: + writeLinesToFile(lines, outputFile); } catch (FileNotFoundException e) { LOGGER.log(Level.SEVERE, "File can not be opened!", file); e.printStackTrace(); @@ -245,20 +252,24 @@ public Item createItem(JSONObject content) { } /** - * Writes the items into file {@code itemsFile}. + * Converts the {@code map} with objects of {@link IConvertible} implementing + * classes into {@link String} lines in form of a {@link List}. * - * @param itemsFile the file to write to + * @param map the map to convert + * @return a {@link List} with all lines as {@link String Strings} */ - public void writeItems(String itemsFile) { - LOGGER.entering(CLASS_NAME, "writeItems"); + public List convertibleMapToLines(Map map) { + LOGGER.entering(CLASS_NAME, "convertibleMapToLines", map); + + List newLines = new ArrayList<>(); - if (itemsMap.size() > 0) { + if (map.size() > 0) { List lines = new ArrayList<>(); - for (String key : itemsMap.keySet()) { - Item item = itemsMap.get(key); - LOGGER.log(Level.FINE, "Generating line for {0}", String.format("%1$s: %2$s", key, item)); - String line = item.toItemConfig(key); + for (String key : map.keySet()) { + IConvertible conv = map.get(key); + LOGGER.log(Level.FINE, "Generating line for {0}", String.format("%1$s: %2$s", key, conv)); + String line = conv.toConfigLine(key); LOGGER.log(Level.INFO, "Created line=[{0}]", line); lines.add(line); } @@ -266,7 +277,6 @@ public void writeItems(String itemsFile) { lines.sort(Comparator.naturalOrder()); // adding empty lines between: - List newLines = new ArrayList<>(); String last = lines.get(0).substring(0, 4); for (String line : lines) { String now = line.substring(0, 4); @@ -277,20 +287,40 @@ public void writeItems(String itemsFile) { last = now; } - - // writing to file: - try { - LOGGER.log(Level.INFO, "Writing lines to file={0}", itemsFile); - Files.write(Paths.get(itemsFile), newLines, Charset.defaultCharset()); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, "{0} at writing file with lines={1}", new Object[] { e.toString(), newLines }); - e.printStackTrace(); - } } else { - LOGGER.log(Level.WARNING, "No items to print"); + LOGGER.log(Level.WARNING, "No objects in map={0}", map); + } + + LOGGER.exiting(CLASS_NAME, "convertibleMapToLines", newLines); + return newLines; + } + + /** + * Writes every entry of {@code lines} in a separate line to {@code fileName}. + * + * @param lines the lines to write into the file + * @param fileName the file name of the file to write + * @return {@code true} if the writing operation was successful, {@code false} + * otherwise + */ + public boolean writeLinesToFile(List lines, String fileName) { + LOGGER.entering(CLASS_NAME, "writeLinesToFile", fileName); + + boolean returnVal; + + // writing to file: + try { + LOGGER.log(Level.INFO, "Writing lines to file={0}", fileName); + Files.write(Paths.get(fileName), lines, Charset.defaultCharset()); + returnVal = true; + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "{0} at writing file with lines={1}", new Object[] { e.toString(), lines }); + e.printStackTrace(); + returnVal = false; } - LOGGER.exiting(CLASS_NAME, "writeItems"); + LOGGER.exiting(CLASS_NAME, "writeLinesToFile", returnVal); + return returnVal; } } diff --git a/src/main/java/voruti/json2config/IConvertible.java b/src/main/java/voruti/json2config/IConvertible.java new file mode 100644 index 0000000..9ea2b91 --- /dev/null +++ b/src/main/java/voruti/json2config/IConvertible.java @@ -0,0 +1,16 @@ +package voruti.json2config; + +/** + * @author voruti + */ +public interface IConvertible { + + /** + * Converts a object/convertible to a config file line. + * + * @param name the name of the object/convertible + * @return a String representating a config file line + */ + public String toConfigLine(String name); + +} diff --git a/src/main/java/voruti/json2config/Item.java b/src/main/java/voruti/json2config/Item.java index 93a7c95..547ef28 100644 --- a/src/main/java/voruti/json2config/Item.java +++ b/src/main/java/voruti/json2config/Item.java @@ -11,7 +11,7 @@ * @author voruti * */ -public class Item extends PersistedItem { +public class Item extends PersistedItem implements IConvertible { private static final String CLASS_NAME = Converter.class.getName(); private static final Logger LOGGER = Logger.getLogger(CLASS_NAME); @@ -52,11 +52,12 @@ private String toString(Collection collection, int maxLen) { * Formats a valid .items-file line. Uses every field except dimension (it's not * used? / within itemType). * - * @param itemName + * @param name the name of the item * @return a item config line */ - public String toItemConfig(String itemName) { - LOGGER.entering(CLASS_NAME, "toItemConfig", itemName); + @Override + public String toConfigLine(String name) { + LOGGER.entering(CLASS_NAME, "toConfigLine", name); String baseItemTypeString = ""; if (!baseItemType.equalsIgnoreCase("")) { @@ -132,9 +133,9 @@ public String toItemConfig(String itemName) { tagsString += "]"; } - String output = String.format("%-30s %-40s %-20s %-20s %-20s %-20s", beginString, itemName, labelString, + String output = String.format("%-30s %-40s %-20s %-20s %-20s %-20s", beginString, name, labelString, categoryString, groupNamesString, tagsString).trim(); - LOGGER.exiting(CLASS_NAME, "toItemConfig", output); + LOGGER.exiting(CLASS_NAME, "toConfigLine", output); return output; } diff --git a/src/main/java/voruti/json2config/Starter.java b/src/main/java/voruti/json2config/Starter.java index 752afc6..e8248e6 100644 --- a/src/main/java/voruti/json2config/Starter.java +++ b/src/main/java/voruti/json2config/Starter.java @@ -6,6 +6,8 @@ import java.util.logging.Logger; import java.util.logging.SimpleFormatter; +import voruti.json2config.Converter.Type; + /** * @author voruti */ @@ -83,7 +85,7 @@ public static void main(String[] args) { // start: LOGGER.log(Level.INFO, "Starting program with jsonFile={0}, itemsFile={1}", new Object[] { jsonFile, itemsFile }); - new Converter(jsonFile, itemsFile); + new Converter(jsonFile, itemsFile, Type.ITEM); } }