You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyExtensionStorage.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2015 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.processor;
  17. import javax.annotation.processing.FilerException;
  18. import javax.tools.FileObject;
  19. import javax.tools.StandardLocation;
  20. import java.io.BufferedReader;
  21. import java.io.BufferedWriter;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.io.Reader;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import java.util.regex.Pattern;
  30. /**
  31. * @author Decebal Suiu
  32. */
  33. public class LegacyExtensionStorage extends ExtensionStorage {
  34. public static final String EXTENSIONS_RESOURCE = "META-INF/extensions.idx";
  35. private static final Pattern COMMENT = Pattern.compile("#.*");
  36. private static final Pattern WHITESPACE = Pattern.compile("\\s+");
  37. public LegacyExtensionStorage(ExtensionAnnotationProcessor processor) {
  38. super(processor);
  39. }
  40. public static void read(Reader reader, Set<String> entries) throws IOException {
  41. BufferedReader bufferedReader = new BufferedReader(reader);
  42. String line;
  43. while ((line = bufferedReader.readLine()) != null) {
  44. line = COMMENT.matcher(line).replaceFirst("");
  45. line = WHITESPACE.matcher(line).replaceAll("");
  46. if (line.length() > 0) {
  47. entries.add(line);
  48. }
  49. }
  50. bufferedReader.close();
  51. }
  52. @Override
  53. public Map<String, Set<String>> read() {
  54. Map<String, Set<String>> extensions = new HashMap<>();
  55. try {
  56. FileObject file = getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE);
  57. // TODO try to calculate the extension point
  58. Set<String> entries = new HashSet<>();
  59. read(file.openReader(true), entries);
  60. extensions.put(null, entries);
  61. } catch (FileNotFoundException e) {
  62. // ignore
  63. } catch (FilerException e) {
  64. // re-opening the file for reading or after writing is ignorable
  65. } catch (IOException e) {
  66. error(e.getMessage());
  67. }
  68. return extensions;
  69. }
  70. @Override
  71. public void write(Map<String, Set<String>> extensions) {
  72. try {
  73. FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE);
  74. BufferedWriter writer = new BufferedWriter(file.openWriter());
  75. writer.write("# Generated by PF4J"); // write header
  76. writer.newLine();
  77. for (Map.Entry<String, Set<String>> entry : extensions.entrySet()) {
  78. for (String extension : entry.getValue()) {
  79. writer.write(extension);
  80. writer.newLine();
  81. }
  82. }
  83. writer.close();
  84. } catch (FileNotFoundException e) {
  85. // it's the first time, create the file
  86. } catch (FilerException e) {
  87. // re-opening the file for reading or after writing is ignorable
  88. } catch (IOException e) {
  89. error(e.toString());
  90. }
  91. }
  92. }