forked from spdx/tools-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareHelper.java
More file actions
400 lines (377 loc) · 11.6 KB
/
Copy pathCompareHelper.java
File metadata and controls
400 lines (377 loc) · 11.6 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
* Copyright (c) 2015 Source Auditor Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.spdx.tools.compare;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.Annotation;
import org.spdx.library.model.Checksum;
import org.spdx.library.model.ExternalRef;
import org.spdx.library.model.ModelObject;
import org.spdx.library.model.Relationship;
import org.spdx.library.model.SpdxElement;
import org.spdx.library.model.enumerations.FileType;
import org.spdx.library.model.license.AnyLicenseInfo;
import org.spdx.library.referencetype.ListedReferenceTypes;
/**
* Helper class for comparisons
* @author Gary O'Neall
*
*/
public class CompareHelper {
static final int MAX_CHARACTERS_PER_CELL = 32000;
/**
*
*/
private CompareHelper() {
// Static helper, should not be instantiated
}
/**
* @param annotation
* @return
* @throws InvalidSPDXAnalysisException
*/
public static String annotationToString(Annotation annotation) throws InvalidSPDXAnalysisException {
if (annotation == null) {
return "";
}
StringBuilder sb = new StringBuilder(annotation.getAnnotationDate());
sb.append(" ");
sb.append(annotation.getAnnotator());
sb.append(": ");
sb.append(annotation.getComment());
sb.append("[");
sb.append(annotation.getAnnotationType().toString());
sb.append("]");
return sb.toString();
}
/**
* Create a string from an array of checksums
* @param checksums
* @return
* @throws InvalidSPDXAnalysisException
*/
public static String checksumsToString(Collection<Checksum> checksums) throws InvalidSPDXAnalysisException {
if (checksums == null || checksums.size() == 0) {
return "";
}
List<String> cksumString = new ArrayList<>();
for (Checksum checksum:checksums) {
cksumString.add(checksumToString(checksum));
}
Collections.sort(cksumString);
StringBuilder sb = new StringBuilder(cksumString.get(0));
for (int i = 1; i < cksumString.size(); i++) {
sb.append("\n");
if (sb.length() + cksumString.get(i).length() > MAX_CHARACTERS_PER_CELL) {
int numRemaing = cksumString.size() - i;
sb.append('[');
sb.append(numRemaing);
sb.append(" more...]");
break;
}
sb.append(cksumString.get(i));
}
return sb.toString();
}
/**
* @param checksum
* @return
* @throws InvalidSPDXAnalysisException
*/
public static String checksumToString(Checksum checksum) throws InvalidSPDXAnalysisException {
if (checksum == null) {
return "";
}
StringBuilder sb = new StringBuilder(checksum.getAlgorithm().toString());
sb.append(' ');
sb.append(checksum.getValue());
return sb.toString();
}
/**
* @param licenseInfos
* @return
*/
public static String licenseInfosToString(Collection<AnyLicenseInfo> licenseInfos) {
if (licenseInfos == null || licenseInfos.size() == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
Iterator<AnyLicenseInfo> iter = licenseInfos.iterator();
sb.append(iter.next().toString());
while (iter.hasNext()) {
sb.append(", ");
sb.append(iter.next().toString());
}
return sb.toString();
}
/**
* @param annotations
* @return
* @throws InvalidSPDXAnalysisException
*/
public static String annotationsToString(Collection<Annotation> annotations) throws InvalidSPDXAnalysisException {
if (annotations == null || annotations.size() == 0) {
return "";
}
Iterator<Annotation> iter = annotations.iterator();
StringBuilder sb = new StringBuilder(annotationToString(iter.next()));
int numRemaining = annotations.size() - 1;
while (iter.hasNext()) {
sb.append("\n");
String annotation = annotationToString(iter.next());
numRemaining--;
if (sb.length() + annotation.length() > MAX_CHARACTERS_PER_CELL) {
sb.append('[');
sb.append(numRemaining);
sb.append(" more...]");
break;
}
sb.append(annotation);
}
return sb.toString();
}
public static String attributionsToString(Collection<String> attributions) {
if (attributions == null || attributions.size() == 0) {
return "";
}
Iterator<String> iter = attributions.iterator();
StringBuilder sb = new StringBuilder(iter.next());
while (iter.hasNext()) {
sb.append("\n");
sb.append(iter.next());
}
return sb.toString();
}
public static String relationshipToString(Relationship relationship) throws InvalidSPDXAnalysisException {
if (relationship == null) {
return "";
}
if (relationship.getRelationshipType() == null) {
return "Unknown relationship type";
}
StringBuilder sb = new StringBuilder(relationship.getRelationshipType().toString());
sb.append(":");
if (!relationship.getRelatedSpdxElement().isPresent()) {
sb.append("?NULL");
} else {
if (relationship.getRelatedSpdxElement().get().getName().isPresent()) {
sb.append('[');
sb.append(relationship.getRelatedSpdxElement().get().getName().get());
sb.append(']');
}
sb.append(relationship.getRelatedSpdxElement().get().getId());
}
if (relationship.getComment().isPresent() && !relationship.getComment().get().isEmpty()) {
sb.append('(');
sb.append(relationship.getComment().get());
sb.append(')');
}
return sb.toString();
}
/**
* @param relationships
* @return
* @throws InvalidSPDXAnalysisException
*/
public static String relationshipsToString(Collection<Relationship> relationships) throws InvalidSPDXAnalysisException {
if (relationships == null || relationships.size() == 0) {
return "";
}
Iterator<Relationship> iter = relationships.iterator();
StringBuilder sb = new StringBuilder(relationshipToString(iter.next()));
int numRemaining = relationships.size() - 1;
while (iter.hasNext()) {
sb.append("\n");
String nextRelationship = relationshipToString(iter.next());
numRemaining--;
if (sb.length() + nextRelationship.length() > MAX_CHARACTERS_PER_CELL) {
sb.append('[');
sb.append(numRemaining);
sb.append(" more...]");
break;
}
sb.append(nextRelationship);
}
return sb.toString();
}
public static String formatSpdxElementList(Collection<SpdxElement> elements) throws InvalidSPDXAnalysisException {
if (elements == null || elements.size() == 0) {
return "";
}
Iterator<SpdxElement> iter = elements.iterator();
StringBuilder sb = new StringBuilder(formatElement(iter.next()));
int numRemaining = elements.size() - 1;
while (iter.hasNext()) {
sb.append(", ");
String nextElement = formatElement(iter.next());
numRemaining--;
if (sb.length() + nextElement.length() > MAX_CHARACTERS_PER_CELL) {
sb.append('[');
sb.append(numRemaining);
sb.append(" more...]");
break;
}
sb.append(nextElement);
}
return sb.toString();
}
private static String formatElement(SpdxElement element) throws InvalidSPDXAnalysisException {
if (Objects.isNull(element) || element.getId() == null || element.getId().isEmpty()) {
return "[UNKNOWNID]";
} else {
StringBuilder sb = new StringBuilder(element.getId());
if (element.getName().isPresent()) {
sb.append('(');
sb.append(element.getName().get());
sb.append(')');
}
return sb.toString();
}
}
/**
* @param fileTypes
* @return
*/
public static String fileTypesToString(FileType[] fileTypes) {
if (fileTypes == null || fileTypes.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder(fileTypes[0].toString());
for (int i = 1;i < fileTypes.length; i++) {
sb.append(", ");
String fileType = fileTypes[i].toString();
if (sb.length() + fileType.length() > MAX_CHARACTERS_PER_CELL) {
int numRemaing = fileTypes.length - i;
sb.append('[');
sb.append(numRemaing);
sb.append(" more...]");
break;
}
sb.append(fileType);
}
return sb.toString();
}
/**
* Convert external refs to a friendly string
* @param externalRefs
* @return
* @throws InvalidSPDXAnalysisException
*/
public static String externalRefsToString(Collection<ExternalRef> externalRefs, String docNamespace) throws InvalidSPDXAnalysisException {
if (externalRefs == null || externalRefs.size() == 0) {
return "";
}
Iterator<ExternalRef> iter = externalRefs.iterator();
StringBuilder sb = new StringBuilder(externalRefToString(iter.next(), docNamespace));
while (iter.hasNext()) {
sb.append("; ");
sb.append(externalRefToString(iter.next(), docNamespace));
}
return sb.toString();
}
/**
* Convert a single external ref to a friendly string
* @param externalRef
* @param docNamespace
* @return
* @throws InvalidSPDXAnalysisException
*/
public static String externalRefToString(ExternalRef externalRef, String docNamespace) throws InvalidSPDXAnalysisException {
String category = null;
if (externalRef.getReferenceCategory() == null) {
category = "OTHER";
} else {
category = externalRef.getReferenceCategory().toString();
}
String referenceType = null;
if (externalRef.getReferenceType() == null) {
referenceType = "[MISSING]";
} else {
try {
referenceType = ListedReferenceTypes.getListedReferenceTypes().getListedReferenceName(new URI(externalRef.getReferenceType().getIndividualURI()));
} catch (InvalidSPDXAnalysisException e) {
referenceType = null;
} catch (URISyntaxException e) {
referenceType = null;
}
if (referenceType == null) {
referenceType = externalRef.getReferenceType().getIndividualURI();
if (docNamespace != null && !docNamespace.isEmpty() && referenceType.startsWith(docNamespace)) {
referenceType = referenceType.substring(docNamespace.length());
}
}
}
String referenceLocator = externalRef.getReferenceLocator();
if (referenceLocator == null) {
referenceLocator = "[MISSING]";
}
String retval = category + " " + referenceType + " " + referenceLocator;
if (externalRef.getComment().isPresent() && !externalRef.getComment().get().isEmpty()) {
retval = retval + "(" + externalRef.getComment() + ")";
}
return retval;
}
public static String checksumToString(Optional<Checksum> checksum) throws InvalidSPDXAnalysisException {
if (checksum.isPresent()) {
return checksumToString(checksum.get());
} else {
return "[NONE]";
}
}
public static boolean equivalent(Optional<? extends ModelObject> c1, Optional<? extends ModelObject> c2) throws InvalidSPDXAnalysisException {
if (!c1.isPresent()) {
return !c2.isPresent();
}
if (c2.isPresent()) {
return (c1.get().equivalent(c2.get()));
} else {
return false;
}
}
public static boolean equivalent(Collection<? extends ModelObject> collection1, Collection<? extends ModelObject> collection2) throws InvalidSPDXAnalysisException {
if (Objects.isNull(collection1)) {
return Objects.isNull(collection2);
}
if (Objects.isNull(collection2)) {
return false;
}
if (collection1.size() != collection2.size()) {
return false;
}
for (ModelObject o1:collection1) {
boolean found = false;
for (ModelObject o2:collection2) {
if (o1.equivalent(o2)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
}