From 6a503772e2595cc49580988b3df0e906b7d2572b Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 8 Dec 2015 22:08:22 +0100 Subject: [PATCH] Close Scanner in tools/permissions; make use of more generic services - Close scanner for all cases - Replace read from file with the one from the File service - Rename file service class to FileUtils to better reflect its purpose --- .../permissions/PermissionNodesGatherer.java | 21 ++++--------------- .../permissions/PermissionsListWriter.java | 21 +++++++++---------- ...eneratedFileWriter.java => FileUtils.java} | 7 +++---- 3 files changed, 17 insertions(+), 32 deletions(-) rename src/tools/utils/{GeneratedFileWriter.java => FileUtils.java} (89%) diff --git a/src/tools/permissions/PermissionNodesGatherer.java b/src/tools/permissions/PermissionNodesGatherer.java index 5b61adbef..4f3855748 100644 --- a/src/tools/permissions/PermissionNodesGatherer.java +++ b/src/tools/permissions/PermissionNodesGatherer.java @@ -3,12 +3,9 @@ package permissions; import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PlayerPermission; -import fr.xephi.authme.util.StringUtils; +import utils.FileUtils; +import utils.ToolsConstants; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.EnumSet; import java.util.HashMap; import java.util.Map; @@ -23,10 +20,6 @@ import java.util.regex.Pattern; */ public class PermissionNodesGatherer { - /** The folder in which the implementations of {@link PermissionNode} reside. */ - private static final String PERMISSION_NODE_SOURCE_FOLDER = - "src/main/java/fr/xephi/authme/permission/"; - /** * Regular expression that should match the JavaDoc comment above an enum, including * the name of the enum value. The first group (i.e. {@code \\1}) should be the JavaDoc description; @@ -100,14 +93,8 @@ public class PermissionNodesGatherer { * @return Source code of the file */ private static & PermissionNode> String getSourceForClass(Class clazz) { - String classFile = PERMISSION_NODE_SOURCE_FOLDER + clazz.getSimpleName() + ".java"; - Charset cs = Charset.forName("utf-8"); - try { - return StringUtils.join("\n", - Files.readAllLines(Paths.get(classFile), cs)); - } catch (IOException e) { - throw new RuntimeException("Failed to get the source for class '" + clazz.getSimpleName() + "'"); - } + String classFile = ToolsConstants.MAIN_SOURCE_ROOT + clazz.getName().replace(".", "/") + ".java"; + return FileUtils.readFromFile(classFile); } } diff --git a/src/tools/permissions/PermissionsListWriter.java b/src/tools/permissions/PermissionsListWriter.java index 7d361b1c5..3f675ad5c 100644 --- a/src/tools/permissions/PermissionsListWriter.java +++ b/src/tools/permissions/PermissionsListWriter.java @@ -1,7 +1,7 @@ package permissions; import utils.ANewMap; -import utils.GeneratedFileWriter; +import utils.FileUtils; import utils.TagReplacer; import utils.ToolsConstants; @@ -23,17 +23,16 @@ public class PermissionsListWriter { System.out.println("Include description? [Enter 'n' for no]"); boolean includeDescription = !matches("n", scanner); - if (!includeDescription) { - outputSimpleList(); - return; + boolean writeToFile = false; + if (includeDescription) { + System.out.println("Write to file? [Enter 'n' for console output]"); + writeToFile = !matches("n", scanner); } - - System.out.println("Write to file? [Enter 'n' for console output]"); - boolean writeToFile = !matches("n", scanner); scanner.close(); - - if (writeToFile) { + if (!includeDescription) { + outputSimpleList(); + } else if (writeToFile) { generateAndWriteFile(); } else { System.out.println(generatePermissionsList()); @@ -45,7 +44,7 @@ public class PermissionsListWriter { final String permissionsTagValue = generatePermissionsList(); Map tags = ANewMap.with("permissions", permissionsTagValue).build(); - GeneratedFileWriter.generateFileFromTemplate( + FileUtils.generateFileFromTemplate( ToolsConstants.TOOLS_SOURCE_ROOT + "permissions/permission_nodes.tpl.md", PERMISSIONS_OUTPUT_FILE, tags); System.out.println("Wrote to '" + PERMISSIONS_OUTPUT_FILE + "'"); System.out.println("Before committing, please verify the output!"); @@ -55,7 +54,7 @@ public class PermissionsListWriter { PermissionNodesGatherer gatherer = new PermissionNodesGatherer(); Map permissions = gatherer.gatherNodesWithJavaDoc(); - final String template = GeneratedFileWriter.readFromToolsFile("permissions/permission_node_entry.tpl.md"); + final String template = FileUtils.readFromToolsFile("permissions/permission_node_entry.tpl.md"); StringBuilder sb = new StringBuilder(); for (Map.Entry entry : permissions.entrySet()) { diff --git a/src/tools/utils/GeneratedFileWriter.java b/src/tools/utils/FileUtils.java similarity index 89% rename from src/tools/utils/GeneratedFileWriter.java rename to src/tools/utils/FileUtils.java index 06cbcaf3d..fb88ae27c 100644 --- a/src/tools/utils/GeneratedFileWriter.java +++ b/src/tools/utils/FileUtils.java @@ -7,13 +7,13 @@ import java.nio.file.Paths; import java.util.Map; /** - * Utility class for writing a generated file with a timestamp. + * Utility class for reading from and writing to files. */ -public final class GeneratedFileWriter { +public final class FileUtils { private final static Charset CHARSET = Charset.forName("utf-8"); - private GeneratedFileWriter() { + private FileUtils() { } public static void generateFileFromTemplate(String templateFile, String destinationFile, Map tags) { @@ -43,5 +43,4 @@ public final class GeneratedFileWriter { return readFromFile(ToolsConstants.TOOLS_SOURCE_ROOT + file); } - }