-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
353 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package voruti.json2config.model; | ||
|
||
public interface IAppendable extends IConvertible { | ||
|
||
/** | ||
* | ||
* @return | ||
*/ | ||
String getItemName(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/voruti/json2config/model/json/JsonMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package voruti.json2config.model.json; | ||
|
||
import java.util.List; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import lombok.Getter; | ||
import voruti.json2config.model.IAppendable; | ||
|
||
@Getter | ||
public class JsonMetadata implements IAppendable { | ||
|
||
private Value value; | ||
|
||
@Override | ||
public String toConfigLine(String lineBefore) { | ||
// first metadata or append: | ||
String format = "%s=\"%s\"}"; | ||
if (lineBefore.endsWith("}")) { | ||
lineBefore = lineBefore.substring(0, lineBefore.length() - 1); | ||
format = "%s, " + format; | ||
} else { | ||
format = "%s {" + format; | ||
} | ||
return String.format(format, lineBefore, value.key.segments.get(0), value.value).strip(); | ||
} | ||
|
||
@Getter | ||
public static class Value { | ||
private Key key; | ||
private String value; | ||
|
||
private static class Key { | ||
private List<String> segments; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public String getItemName() { | ||
return value.key.segments.get(1); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package voruti.json2config.service; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import voruti.json2config.model.IAppendable; | ||
|
||
@Slf4j | ||
public class Appender { | ||
|
||
private Appender() { | ||
} | ||
|
||
/** | ||
* Returns a {@link List} of Strings containing the names of all items in | ||
* {@code fileName}. | ||
* | ||
* @param fileName the file(-Name) to open and search for items | ||
* @return a {@link List} containing the names of the items | ||
*/ | ||
public static List<String> getItemNamesFromFile(String fileName) { | ||
try { | ||
return Arrays.stream(SharedService.openFileToString(fileName).split("\n")) | ||
.filter(line -> !line.isEmpty() && !line.toLowerCase().startsWith("group")) | ||
.map(Appender::searchNameInLine) | ||
.filter(itemName -> !itemName.isEmpty()) | ||
.collect(Collectors.toList()); | ||
} catch (IOException e) { | ||
log.error(Constants.LOG_CANT_OPEN_FILE, fileName); | ||
} | ||
|
||
return List.of(); | ||
} | ||
|
||
/** | ||
* Appends the {@code appendable} after the item in {@code fileName}. | ||
* | ||
* @param appendable the data to append after the item | ||
* @param fileName the file to search for the item | ||
* @return {@code true} if the {@code appendable} could be appended, {@code false} otherwise | ||
*/ | ||
public static boolean appendToItemInFile(IAppendable appendable, String fileName) { | ||
boolean successful = false; | ||
|
||
try { | ||
List<String> originalLines = Arrays.asList(SharedService.openFileToString(fileName).split("\n")); | ||
List<String> modifiedLines = originalLines.stream() | ||
.map(line -> { | ||
if (!line.isEmpty() && !line.toLowerCase().startsWith("group")) { | ||
String readItemName = searchNameInLine(line); | ||
if (!readItemName.isEmpty() && readItemName.equals(appendable.getItemName())) { | ||
return appendable.toConfigLine(line); | ||
} | ||
} | ||
// return unmodified line: | ||
return line; | ||
}) | ||
.collect(Collectors.toList()); | ||
|
||
if (!originalLines.equals(modifiedLines)) { | ||
successful = SharedService.writeLinesToFile(modifiedLines, fileName); | ||
} | ||
} catch (IOException e) { | ||
log.error(Constants.LOG_CANT_OPEN_FILE, fileName); | ||
} | ||
|
||
return successful; | ||
} | ||
|
||
/** | ||
* Searches for an item name in the {@code line}. | ||
* | ||
* @param line the line to search in | ||
* @return name of the item if found, {@code null} otherwise | ||
*/ | ||
public static String searchNameInLine(String line) { | ||
String[] arr = line.strip().split("\\s+"); | ||
|
||
String itemName = ""; | ||
if (arr.length >= 2) { | ||
itemName = arr[1]; | ||
} | ||
|
||
return itemName; | ||
} | ||
|
||
/** | ||
* Searches for ".items" files in the {@code directory}. | ||
* | ||
* @param directory the directory to search in | ||
* @return a {@link List} with all file paths of ".items" files | ||
*/ | ||
public static List<String> findItemsFilesInDir(String directory) { | ||
return Arrays.stream(Objects.requireNonNull(new File(directory).listFiles((dir, filename) -> filename.endsWith(".items")))) | ||
.map(File::getAbsolutePath) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.