1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.swid.plugin.entry;
28
29 import gov.nist.secauto.swid.builder.resource.PathRelativizer;
30
31 import org.codehaus.plexus.components.io.fileselectors.FileInfo;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.file.Path;
36 import java.util.List;
37
38 public abstract class AbstractFileEntry implements FileEntry {
39
40 public AbstractFileEntry() {
41 }
42
43
44
45
46
47
48 protected abstract Path getBase();
49
50
51
52
53
54
55 @Override
56 public abstract Path getPath();
57
58
59
60
61
62
63 protected Path getRelativePath() {
64 return getBase().relativize(getPath());
65 }
66
67 protected abstract String getOutputBase();
68
69 @Override
70 public String getOutputRelativePath() {
71 StringBuilder builder = new StringBuilder();
72
73 String base = getOutputBase();
74 if (base != null) {
75 builder.append(base);
76 builder.append('/');
77 }
78
79 Path relativePath = getRelativePath();
80 String relativePathString = relativePath.toString();
81 String seperator = relativePath.getFileSystem().getSeparator();
82 if (!"/".equals(seperator)) {
83 relativePathString = relativePathString.replace(seperator, "/");
84 }
85 builder.append(relativePathString);
86 return builder.toString();
87
88 }
89
90 @Override
91 public List<String> getRelativePathSegements(String swidTagPath) {
92 return PathRelativizer.relativize(swidTagPath, getOutputRelativePath());
93 }
94
95 @Override
96 public FileInfo asFileInfo() {
97 return new ResourceFileInfo();
98 }
99
100 private class ResourceFileInfo implements FileInfo {
101
102 @Override
103 public String getName() {
104 return AbstractFileEntry.this.getOutputRelativePath().toString();
105 }
106
107 @Override
108 public InputStream getContents() throws IOException {
109 return AbstractFileEntry.this.getInputStream();
110 }
111
112 @Override
113 public boolean isFile() {
114 return true;
115 }
116
117 @Override
118 public boolean isDirectory() {
119 return false;
120 }
121
122 @Override
123 public boolean isSymbolicLink() {
124 return false;
125 }
126 }
127 }