1
|
|
|
package es.webbeta.serializer; |
2
|
|
|
|
3
|
|
|
import es.webbeta.serializer.base.FileUtils; |
4
|
|
|
import es.webbeta.serializer.base.MetadataAccessor; |
5
|
|
|
|
6
|
|
|
import java.io.IOException; |
7
|
|
|
import java.nio.file.Files; |
8
|
|
|
import java.nio.file.Path; |
9
|
|
|
import java.nio.file.Paths; |
10
|
|
|
|
11
|
|
|
public class FileMetadataAccessor implements MetadataAccessor { |
12
|
|
|
|
13
|
|
|
private static final String YAML_EXT = ".yaml"; |
14
|
|
|
private static final String YML_EXT = ".yml"; |
15
|
|
|
|
16
|
|
|
protected Path metadataPath = null; |
17
|
|
|
|
18
|
|
|
private Path buildPath(Class<?> klass, String extension) { |
19
|
|
|
return Paths.get(metadataPath.toString(), klass.getCanonicalName() + extension); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
private Boolean hasYmlMetadata(Class<?> klass) { |
23
|
|
|
return Files.exists(buildPath(klass, YML_EXT)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private Boolean hasYamlMetadata(Class<?> klass) { |
27
|
|
|
return Files.exists(buildPath(klass, YAML_EXT)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
@Override |
31
|
|
|
public void setMetadataPath(Path path) { |
32
|
|
|
metadataPath = path; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
@Override |
36
|
|
|
public Boolean hasMetadata(Class<?> klass) { |
37
|
|
|
return hasYmlMetadata(klass) || |
38
|
|
|
hasYamlMetadata(klass); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
@Override |
42
|
|
|
public String getMetadataContent(Class<?> klass) { |
43
|
|
|
String contents = null; |
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
if (hasYmlMetadata(klass)) |
47
|
|
|
contents = FileUtils.getContent(buildPath(klass, YML_EXT)); |
48
|
|
|
else if(hasYamlMetadata(klass)) |
49
|
|
|
contents = FileUtils.getContent(buildPath(klass, YAML_EXT)); |
50
|
|
|
} catch (IOException ignored) {} |
|
|
|
|
51
|
|
|
|
52
|
|
|
return contents; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|