From: Decebal Suiu Date: Mon, 4 Jan 2016 10:00:33 +0000 (+0200) Subject: improve *ExtensionStorage.read methods, remove comments and whitespaces X-Git-Tag: release-0.12.0~7^2~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=342825b1ef8c7a343101168a888bdc4cfcc5dc31;p=pf4j.git improve *ExtensionStorage.read methods, remove comments and whitespaces --- diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/processor/LegacyExtensionStorage.java b/pf4j/src/main/java/ro/fortsoft/pf4j/processor/LegacyExtensionStorage.java index 84f9720..e742178 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/processor/LegacyExtensionStorage.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/processor/LegacyExtensionStorage.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; /** * @author Decebal Suiu @@ -34,6 +35,9 @@ public class LegacyExtensionStorage extends ExtensionStorage { public static final String EXTENSIONS_RESOURCE = "META-INF/extensions.idx"; + private static final Pattern COMMENT = Pattern.compile("#.*"); + private static final Pattern WHITESPACE = Pattern.compile("\\s+"); + public LegacyExtensionStorage(ExtensionAnnotationProcessor processor) { super(processor); } @@ -43,11 +47,16 @@ public class LegacyExtensionStorage extends ExtensionStorage { String line; while ((line = bufferedReader.readLine()) != null) { - entries.add(line); + line = COMMENT.matcher(line).replaceFirst(""); + line = WHITESPACE.matcher(line).replaceAll(""); + if (line.length() > 0) { + entries.add(line); + } } bufferedReader.close(); } + @Override public Map> read() { Map> extensions = new HashMap<>(); @@ -73,6 +82,7 @@ public class LegacyExtensionStorage extends ExtensionStorage { FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE); BufferedWriter writer = new BufferedWriter(file.openWriter()); writer.write("# Generated by PF4J"); // write header + writer.newLine(); for (Map.Entry> entry : extensions.entrySet()) { for (String extension : entry.getValue()) { writer.write(extension); diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/processor/ServiceProviderExtensionStorage.java b/pf4j/src/main/java/ro/fortsoft/pf4j/processor/ServiceProviderExtensionStorage.java index 0cb1264..8fb6b35 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/processor/ServiceProviderExtensionStorage.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/processor/ServiceProviderExtensionStorage.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; /** * @author Decebal Suiu @@ -34,6 +35,9 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage { public static final String EXTENSIONS_RESOURCE = "META-INF/services"; + private static final Pattern COMMENT = Pattern.compile("#.*"); + private static final Pattern WHITESPACE = Pattern.compile("\\s+"); + public ServiceProviderExtensionStorage(ExtensionAnnotationProcessor processor) { super(processor); } @@ -43,7 +47,11 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage { String line; while ((line = bufferedReader.readLine()) != null) { - entries.add(line); + line = COMMENT.matcher(line).replaceFirst(""); + line = WHITESPACE.matcher(line).replaceAll(""); + if (line.length() > 0) { + entries.add(line); + } } bufferedReader.close(); @@ -78,10 +86,15 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage { FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE + "/" + extensionPoint); BufferedWriter writer = new BufferedWriter(file.openWriter()); + // write header writer.write("# Generated by PF4J"); // write header + writer.newLine(); + // write extensions for (String extension : entry.getValue()) { - if (processor.getOldExtensions().containsKey(extensionPoint)) writer.write(extension); + if (!isExtensionOld(extensionPoint, extension)) { + writer.write(" # pf4j extension"); + } writer.newLine(); } writer.close(); @@ -93,4 +106,9 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage { } } + private boolean isExtensionOld(String extensionPoint, String extension) { + return processor.getOldExtensions().containsKey(extensionPoint) + && processor.getOldExtensions().get(extensionPoint).contains(extension); + } + }