/
FileModel.java
335 lines (289 loc) · 9.46 KB
/
FileModel.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package org.jboss.windup.graph.model.resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.EnumUtils;
import org.jboss.windup.graph.Indexed;
import org.jboss.windup.graph.frames.FrameBooleanDefaultValue;
import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.ProjectModel;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.frames.Adjacency;
import com.tinkerpop.frames.Property;
import com.tinkerpop.frames.modules.javahandler.JavaHandler;
import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
import com.tinkerpop.frames.modules.typedgraph.TypeValue;
/**
* Represents a File on disk.
*/
@TypeValue(FileModel.TYPE)
public interface FileModel extends ResourceModel
{
String TYPE = "FileResource";
String ARCHIVE_FILES = "archiveFiles";
String PARENT_FILE = "parentFile";
String SHA1_HASH = "sha1Hash";
String MD5_HASH = "md5Hash";
String FILE_TO_PROJECT_MODEL = "fileToProjectModel";
String FILE_NAME = "fileName";
String FILE_PATH = "filePath";
String IS_DIRECTORY = "isDirectory";
String WINDUP_GENERATED = "windupGenerated";
String PRETTY_PATH = "fileModelPrettyPath";
String PRETTY_PATH_WITHIN_PROJECT = "fileModelPrettyPathWithinProject";
String PARSE_ERROR = "parseError";
String ON_PARSE_ERROR = "onParseError";
enum OnParseError {
IGNORE, WARN;
OnParseError fromName(String name){
return EnumUtils.getEnum(OnParseError.class, StringUtils.upperCase(name));
}
}
/**
* Contains the File Name (the last component of the path). Eg, a file /tmp/foo/bar/file.txt would have fileName set to "file.txt"
*/
@Property(FILE_NAME)
String getFileName();
/**
* Contains the File Name (the last component of the path). Eg, a file /tmp/foo/bar/file.txt would have fileName set to "file.txt"
*/
@Indexed
@Property(FILE_NAME)
void setFileName(String filename);
/**
* Contains the full path to the file (eg, /tmp/foo/bar/file.txt)
*/
@Indexed
@Property(FILE_PATH)
String getFilePath();
/**
* Contains the full path to the file (eg, /tmp/foo/bar/file.txt)
*/
// implemented via a handler that makes sure the isDirectory property is set as well
@JavaHandler
void setFilePath(String filePath);
/**
* Indicates whether the file is a directory or not
*/
@Property(IS_DIRECTORY)
boolean isDirectory();
/**
* Contains a MD5 Hash of the file
*/
@Property(MD5_HASH)
String getMD5Hash();
/**
* Contains a MD5 Hash of the file
*/
@Property(MD5_HASH)
void setMD5Hash(String md5Hash);
/**
* Contains a SHA1 Hash of the file
*/
@Property(SHA1_HASH)
String getSHA1Hash();
/**
* Contains a SHA1 Hash of the file
*/
@Property(SHA1_HASH)
void setSHA1Hash(String sha1Hash);
/**
* Did Windup encounter problems when parsing this file? If so, this will contain a description of the error.
*/
@Property(PARSE_ERROR)
String getParseError();
/**
* Did Windup encounter problems when parsing this file? If so, this will contain a description of the error.
*/
@Property(PARSE_ERROR)
void setParseError(String message);
/**
* What to do with a parsing error?
*/
@Property(ON_PARSE_ERROR)
OnParseError getOnParseError();
/**
* What to do with a parsing error?
*/
@Property(ON_PARSE_ERROR)
void setOnParseError(OnParseError ignore);
/**
* Parent directory
*/
@Adjacency(label = PARENT_FILE, direction = Direction.OUT)
FileModel getParentFile();
/**
* Parent directory
*/
@Adjacency(label = PARENT_FILE, direction = Direction.OUT)
void setParentFile(FileModel parentFile);
/**
* Files contained within this directory
*
* @return
*/
@Adjacency(label = PARENT_FILE, direction = Direction.IN)
Iterable<FileModel> getFilesInDirectory();
/**
* Add a file to this directory
*/
@Adjacency(label = PARENT_FILE, direction = Direction.IN)
void addFileToDirectory(FileModel fileModel);
/**
* Indicates the archive that contained this file
*/
@Adjacency(label = ARCHIVE_FILES, direction = Direction.IN)
ArchiveModel getParentArchive();
/**
* Sets the archive that contained this file
*/
@Adjacency(label = ARCHIVE_FILES, direction = Direction.IN)
void setParentArchive(ArchiveModel parentArchive);
/**
* Gets the ProjectModel that this file is a part of
*/
@Adjacency(label = FILE_TO_PROJECT_MODEL, direction = Direction.OUT)
ProjectModel getProjectModel();
/**
* Sets the ProjectModel that this file is a part of
*/
@Adjacency(label = FILE_TO_PROJECT_MODEL, direction = Direction.OUT)
void setProjectModel(ProjectModel projectModel);
/**
* Gets a {@link File} object representing this file
*/
@JavaHandler
File asFile() throws RuntimeException;
/**
* Returns an open {@link InputStream} for reading from this file
*/
@JavaHandler
InputStream asInputStream() throws RuntimeException;
/**
* Returns the path of this file within the archive (including all subarchives, etc)
*/
@JavaHandler
String getPrettyPath();
/**
* Returns the path of this file within the parent project (format suitable for reporting)
*/
@JavaHandler
String getPrettyPathWithinProject();
/**
* Returns the application that this file is a part of. This is especially useful in the case of analyzing multiple application's, as we often
* need to know which application a particular file is associated with.
*
* This is a shortcut for calling getProjectModel().getRootProjectModel().
*/
@JavaHandler
ProjectModel getApplication();
/**
* Specifies if the given file was generated by windup or it originates from application.
*/
@Property(WINDUP_GENERATED)
Boolean isWindupGenerated();
/**
* Specifies if the given file was generated by windup or it originates from application.
*/
@FrameBooleanDefaultValue(false)
@Property(WINDUP_GENERATED)
void setWindupGenerated(boolean generated);
abstract class Impl implements FileModel, JavaHandlerContext<Vertex>
{
public ProjectModel getApplication()
{
ProjectModel projectModel = getProjectModel();
if (projectModel == null)
return null;
return projectModel.getRootProjectModel();
}
public String getPrettyPathWithinProject()
{
String result;
ProjectModel projectModel = getProjectModel();
if (projectModel == null)
{
// no project, just return the whole path
result = getPrettyPath();
}
else if (projectModel.getRootFileModel().getFilePath().equals(getFilePath()))
{
result = "";
}
else
{
String filename = getFileName();
if (getParentFile() == null)
{
if (getParentArchive() != null)
{
result = getParentArchive().getPrettyPathWithinProject();
}
else
{
result = filename;
}
}
else
{
String parentPrettyPath = getParentFile().getPrettyPathWithinProject();
result = StringUtils.isEmpty(parentPrettyPath) ? filename : parentPrettyPath + "/" + filename;
}
}
return result;
}
public String getPrettyPath()
{
String filename = getFileName();
String result;
if (getParentFile() == null)
{
if (getParentArchive() != null)
{
result = getParentArchive().getPrettyPath();
}
else
{
result = filename;
}
}
else
{
result = getParentFile().getPrettyPath() + "/" + filename;
}
return result;
}
public void setFilePath(String filePath)
{
File file = new File(filePath);
// set the isDirectory attribute
it().setProperty(IS_DIRECTORY, file.isDirectory());
it().setProperty(FILE_PATH, file.getAbsolutePath());
it().setProperty(FILE_NAME, file.getName());
}
@Override
public InputStream asInputStream() throws RuntimeException
{
try
{
if (this.getFilePath() == null)
return null;
File file = new File(getFilePath());
return new FileInputStream(file);
}
catch (Exception e)
{
throw new RuntimeException("Exception reading resource.", e);
}
}
@Override
public File asFile() throws RuntimeException
{
if (this.getFilePath() == null)
return null;
return new File(getFilePath());
}
}
}