1
2
3
4 package de.powerstat.phplib.templateengine.intern;
5
6
7 import java.io.BufferedReader;
8 import java.io.File;
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.nio.charset.StandardCharsets;
14 import java.nio.file.Files;
15 import java.nio.file.StandardOpenOption;
16 import java.util.Map;
17 import java.util.Objects;
18 import java.util.concurrent.ConcurrentHashMap;
19
20 import org.apache.logging.log4j.LogManager;
21 import org.apache.logging.log4j.Logger;
22
23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24
25
26
27
28
29 public final class FileManager
30 {
31
32
33
34 private static final Logger LOGGER = LogManager.getLogger(FileManager.class);
35
36
37
38
39 private static final int MAX_TEMPLATE_SIZE = 1048576;
40
41
42
43
44 private static final String FILEPATH_SEPARATOR = "/";
45
46
47
48
49 private final VariableManager variableManager;
50
51
52
53
54 private final Map<String, File> files = new ConcurrentHashMap<>();
55
56
57
58
59
60
61
62
63
64 @SuppressFBWarnings("EI_EXPOSE_REP2")
65 public FileManager(final VariableManager vManager, final FileManager fManager)
66 {
67 super();
68 Objects.requireNonNull(vManager, "vManager");
69 Objects.requireNonNull(fManager, "fManager");
70 this.variableManager = vManager;
71 for (final Map.Entry<String, File> entry : fManager.files.entrySet())
72 {
73 this.files.put(entry.getKey(), entry.getValue());
74 }
75 }
76
77
78
79
80
81
82
83 @SuppressFBWarnings("EI_EXPOSE_REP2")
84 public FileManager(final VariableManager vManager)
85 {
86 super();
87 this.variableManager = vManager;
88 }
89
90
91
92
93
94
95
96
97
98
99
100 @SuppressWarnings({"PMD.LinguisticNaming", "java:S3457"})
101 public boolean addFile(final String newVarname, final File newFile)
102 {
103
104 boolean exists = newFile.exists();
105 if (exists)
106 {
107 if (newFile.length() > FileManager.MAX_TEMPLATE_SIZE)
108 {
109 throw new IllegalArgumentException("newFile to large");
110 }
111 this.files.put(newVarname, newFile);
112 }
113 else
114 {
115 try (InputStream stream = this.getClass().getResourceAsStream(FileManager.FILEPATH_SEPARATOR + newFile.getName()))
116 {
117 if (stream != null)
118 {
119 exists = true;
120 this.files.put(newVarname, newFile);
121 }
122 }
123 catch (final IOException ignored)
124 {
125
126 FileManager.LOGGER.warn("File does not exist: " + newFile.getAbsolutePath(), ignored);
127 }
128 }
129 return exists;
130 }
131
132
133
134
135
136
137
138
139 public boolean existsFile(final String varname)
140 {
141 final var file = this.files.get(varname);
142 return (file != null);
143 }
144
145
146
147
148
149
150
151
152
153
154 @SuppressWarnings("PMD.CloseResource")
155 public boolean loadFile(final String varname) throws IOException
156 {
157
158 if (this.variableManager.existsVar(varname))
159 {
160 return true;
161 }
162 final var file = this.files.get(varname);
163 if (file == null)
164 {
165 return false;
166 }
167 InputStream istream = this.getClass().getResourceAsStream(FileManager.FILEPATH_SEPARATOR + file.getName());
168 if (istream == null)
169 {
170 istream = Files.newInputStream(this.files.get(varname).toPath(), StandardOpenOption.READ);
171 }
172 final var fileBuffer = new StringBuilder();
173 try (var reader = new BufferedReader(new InputStreamReader(istream, StandardCharsets.UTF_8)))
174 {
175 String line = reader.readLine();
176 while (line != null)
177 {
178 fileBuffer.append(line);
179 fileBuffer.append('\n');
180 line = reader.readLine();
181 }
182 }
183 if (fileBuffer.length() == 0)
184 {
185 return false;
186 }
187 this.variableManager.setVar(varname, fileBuffer.toString());
188 return true;
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202 @Override
203 public String toString()
204 {
205 return new StringBuilder().append("FileManager[").append("files=").append(this.files.values().stream().map(File::getName).reduce((s1, s2) -> s1 + ", " + s2)).append(']').toString();
206 }
207
208
209
210
211
212
213
214
215 @Override
216 public int hashCode()
217 {
218 return Objects.hash(this.files);
219 }
220
221
222
223
224
225
226
227
228
229 @Override
230 public boolean equals(final Object obj)
231 {
232 if (this == obj)
233 {
234 return true;
235 }
236 if (!(obj instanceof FileManager))
237 {
238 return false;
239 }
240 final FileManager other = (FileManager)obj;
241 return this.files.equals(other.files) && this.variableManager.equals(other.variableManager);
242 }
243
244 }