-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSkylineTool.java
More file actions
382 lines (324 loc) · 10.2 KB
/
SkylineTool.java
File metadata and controls
382 lines (324 loc) · 10.2 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package org.labkey.skylinetoolsstore.model;
import org.apache.commons.lang3.StringUtils;
import org.labkey.api.data.Container;
import org.labkey.api.data.Entity;
import org.labkey.api.files.FileContentService;
import org.labkey.api.settings.AppProps;
import org.labkey.api.util.Pair;
import org.labkey.api.webdav.WebdavService;
import org.labkey.skylinetoolsstore.SkylineToolsStoreController;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.imageio.ImageIO;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Objects;
public class SkylineTool extends Entity
{
private String _zipName;
private String _name;
private String _authors;
private String _organization;
private String _provider;
private String _version;
private String _languages;
private String _description;
private String _identifier;
private byte[] _icon = null;
private Integer _downloads = 0;
private boolean _latest = false;
private Integer _rowId;
public SkylineTool()
{
}
public SkylineTool(BufferedReader reader) throws IOException
{
parseProperties(reader);
}
public String getZipName()
{
return _zipName;
}
public void setZipName(String zipName)
{
_zipName = zipName;
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public String getAuthors()
{
return _authors;
}
public void setAuthors(String authors)
{
_authors = authors;
}
public String getOrganization()
{
return _organization;
}
public void setOrganization(String organization)
{
_organization = organization;
}
public String getProvider()
{
return _provider;
}
public void setProvider(String provider)
{
_provider = provider;
}
public String getVersion()
{
return _version;
}
public void setLanguages(String languages)
{
_languages = languages;
}
public String getLanguages()
{
return _languages;
}
public void setVersion(String version)
{
_version = version;
}
public String getDescription()
{
return _description;
}
public void setDescription(String description)
{
_description = description;
}
public String getIdentifier()
{
return _identifier;
}
public void setIdentifier(String identifier)
{
_identifier = identifier;
}
public Integer getDownloads()
{
return _downloads;
}
public void setDownloads(Integer downloads)
{
_downloads = downloads;
}
public boolean getLatest()
{
return _latest;
}
public void setLatest(boolean latest)
{
_latest = latest;
}
public byte[] getIcon()
{
return _icon;
}
public void setIcon(byte[] icon)
{
_icon = icon;
}
public Integer getRowId()
{
return _rowId;
}
public void setRowId(Integer rowId)
{
_rowId = rowId;
}
public boolean equals(Object obj)
{
if (!(obj instanceof SkylineTool p))
return false;
return Objects.equals(_name, p.getName()) &&
Objects.equals(_authors, p.getAuthors()) &&
Objects.equals(_provider, p.getProvider()) &&
Objects.equals(_version, p.getVersion()) &&
Objects.equals(_languages, p.getLanguages()) &&
Objects.equals(_description, p.getDescription()) &&
Objects.equals(_identifier, p.getIdentifier());
}
protected Pair<String, String> getNextProperty(BufferedReader reader) throws IOException
{
String line;
int splitter = 0;
while ((line = reader.readLine()) != null)
{
line = line.trim();
if (!line.isEmpty() && line.charAt(0) != '#' && (splitter = line.indexOf('=')) != -1)
break;
}
if (line == null)
return null;
String propName = line.substring(0, splitter).trim();
String propValue = (splitter != line.length() - 1) ? line.substring(splitter + 1).trim() : "";
while (propValue.endsWith("\\") && (line = reader.readLine()) != null)
{
line = line.trim();
if (line.isEmpty() || line.charAt(0) == '#')
continue;
propValue = propValue.substring(0, propValue.length() - 1) + '\n' + line;
}
return new Pair<>(propName, propValue);
}
public void setProperty(String propName, String propValue)
{
switch (propName.toLowerCase())
{
case "name":
this.setName(propValue);
break;
case "version":
this.setVersion(propValue);
break;
case "author":
this.setAuthors(propValue);
break;
case "languages":
this.setLanguages(propValue);
break;
case "organization":
this.setOrganization(propValue);
break;
case "description":
// Description may have quotes around it
if (propValue.length() > 1 && propValue.startsWith("\"") && propValue.endsWith("\""))
propValue = propValue.substring(1, propValue.length() - 1).trim();
this.setDescription(propValue);
break;
case "provider":
this.setProvider(propValue);
break;
case "identifier":
this.setIdentifier(propValue);
break;
}
}
public void parseProperties(BufferedReader reader) throws IOException
{
Pair<String, String> pair;
while ((pair = getNextProperty(reader)) != null)
setProperty(pair.first, pair.second);
}
public ArrayList<String> getMissingValues()
{
// Name, version, and identifier are required
ArrayList<String> missingValues = new ArrayList();
if (StringUtils.trimToNull(_name) == null)
missingValues.add("Name");
if (StringUtils.trimToNull(_version) == null)
missingValues.add("Version");
if (StringUtils.trimToNull(_identifier) == null)
missingValues.add("Identifier");
return missingValues;
}
public void writeIconToFile(File file, String format) throws IOException
{
if (_icon == null)
return;
try (ByteArrayInputStream iconInputStream = new ByteArrayInputStream(_icon);
FileOutputStream iconOutputStream = new FileOutputStream(file))
{
ImageIO.write(ImageIO.read(iconInputStream), format, iconOutputStream);
}
catch (IOException e)
{
throw e;
}
}
public String getFolderUrl()
{
return AppProps.getInstance().getContextPath() + "/files" + lookupContainer().getPath() + "/";
}
public boolean hasDocumentation()
{
Path localPath = SkylineToolsStoreController.getLocalPath(lookupContainer());
return localPath != null && Files.exists(localPath.resolve("docs/index.html"));
}
public String getDocsUrl()
{
org.labkey.api.util.Path path = WebdavService.getPath()
.append(lookupContainer().getParsedPath())
.append(FileContentService.FILES_LINK)
.append(new org.labkey.api.util.Path("docs", "index.html"));
return AppProps.getInstance().getContextPath() + path.encode();
}
public String getIconUrl()
{
return (SkylineToolsStoreController.makeFile(lookupContainer(), "icon.png").exists()) ?
getFolderUrl() + "icon.png" :
AppProps.getInstance().getContextPath() + "/skylinetoolsstore/img/placeholder.png";
}
public String getPrettyCreated()
{
Calendar uploadCal = Calendar.getInstance();
uploadCal.setTime(getCreated());
return new SimpleDateFormat("MMM d, yyyy").format(uploadCal.getTime());
}
public InputStream getInfoPropertiesStream(InputStream in, String propName, String newValue) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
newValue = newValue.replace("\r\n", "\\\r\n");
boolean foundProperty = false;
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null)
{
String originalLine = line;
line = line.trim();
int splitter;
String curPropName;
if (!line.isEmpty() && line.charAt(0) != '#' && (splitter = line.indexOf('=')) != -1
&& (curPropName = line.substring(0, splitter).trim()).equalsIgnoreCase(propName))
{
foundProperty = true;
boolean wasMultiline = false;
while ((line = reader.readLine()) != null && line.trim().endsWith("\\"))
wasMultiline = true;
if (!newValue.isEmpty())
{
sb.append(curPropName).append(" = ").append(newValue).append(reader.ready() ? "\r\n" : "");
}
// Consume the last line of a property value that had multiple lines
if (!wasMultiline)
continue;
}
else
sb.append(originalLine).append(reader.ready() ? "\r\n" : "");
line = reader.readLine();
}
if (!foundProperty)
sb.append("\r\n").append(propName).append(" = ").append(newValue);
return new ByteArrayInputStream(sb.toString().getBytes());
}
public Container getContainerParent()
{
Container c = lookupContainer();
if(c != null)
{
return c.getParent();
}
return null;
}
}