Skip to content

Commit

Permalink
Add Convertible
Browse files Browse the repository at this point in the history
  • Loading branch information
voruti committed Mar 8, 2020
1 parent d6daa94 commit 235ec72
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 37 deletions.
90 changes: 60 additions & 30 deletions src/main/java/voruti/json2config/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Item> 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, "<init>", jsonFile);

itemsMap = new HashMap<>();
Map<String, IConvertible> convertibleMap = new HashMap<>();

File file = new File(jsonFile);
Scanner sc;
Expand Down Expand Up @@ -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<String> lines = convertibleMapToLines(convertibleMap);

// write file:
writeLinesToFile(lines, outputFile);
} catch (FileNotFoundException e) {
LOGGER.log(Level.SEVERE, "File can not be opened!", file);
e.printStackTrace();
Expand Down Expand Up @@ -245,28 +252,31 @@ 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<String> convertibleMapToLines(Map<String, IConvertible> map) {
LOGGER.entering(CLASS_NAME, "convertibleMapToLines", map);

List<String> newLines = new ArrayList<>();

if (itemsMap.size() > 0) {
if (map.size() > 0) {
List<String> 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);
}

lines.sort(Comparator.naturalOrder());

// adding empty lines between:
List<String> newLines = new ArrayList<>();
String last = lines.get(0).substring(0, 4);
for (String line : lines) {
String now = line.substring(0, 4);
Expand All @@ -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<String> 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;
}

}
16 changes: 16 additions & 0 deletions src/main/java/voruti/json2config/IConvertible.java
Original file line number Diff line number Diff line change
@@ -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);

}
13 changes: 7 additions & 6 deletions src/main/java/voruti/json2config/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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("")) {
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/voruti/json2config/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import voruti.json2config.Converter.Type;

/**
* @author voruti
*/
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 235ec72

Please sign in to comment.