forked from spdx/tools-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentSheet.java
More file actions
399 lines (371 loc) · 13.2 KB
/
Copy pathDocumentSheet.java
File metadata and controls
399 lines (371 loc) · 13.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* Copyright (c) 2013 Source Auditor Inc.
* Copyright (c) 2013 Black Duck Software 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.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.utility.compare.SpdxCompareException;
import org.spdx.utility.compare.SpdxComparer;
/**
* Sheet to hold compare information at the docment level:
* Created, Data License, Document Comment
* The first row summarizes which fields are different, the subsequent rows are the
* specific date from each result
* @author Gary O'Neall
*
*/
public class DocumentSheet extends AbstractSheet {
int NUM_COLS = 12;
int DOCUMENT_NAME_COL = 0;
int SPDX_VERSION_COL = DOCUMENT_NAME_COL + 1;
int DATA_LICENSE_COL = SPDX_VERSION_COL + 1;
int SPDX_IDENTIFIER_COL = DATA_LICENSE_COL + 1;
int DOCUMENT_NAMESPACE_COL = SPDX_IDENTIFIER_COL + 1;
int SPDX_DOCUMENT_CONTENT_COL = DOCUMENT_NAMESPACE_COL + 1;
int DOCUMENT_COMMENT_COL = SPDX_DOCUMENT_CONTENT_COL + 1;
int CREATION_DATE_COL = DOCUMENT_COMMENT_COL + 1;
int CREATOR_COMMENT_COL = CREATION_DATE_COL + 1;
int LICENSE_LIST_VERSION_COL = CREATOR_COMMENT_COL + 1;
int ANNOTATION_COL = LICENSE_LIST_VERSION_COL + 1;
int RELATIONSHIP_COL = ANNOTATION_COL + 1;
static final boolean[] REQUIRED = new boolean[] {true, true, true,
true, true, true, false, true, false, false, false, false};
static final String[] HEADER_TITLES = new String[] {"Document Name", "SPDX Version",
"Data License", "ID", "Document Namespace", "Document Describes",
"Document Comment", "Creation Date", "Creator Comment", "Lic. List. Ver.",
"Annotations", "Relationships"};
static final int[] COLUMN_WIDTHS = new int[] {30, 15, 15, 15, 60, 40, 60,
22, 60, 22, 80, 80};
private static final String DIFFERENT_STRING = "Diff";
private static final String EQUAL_STRING = "Equals";
/**
* @param workbook
* @param sheetName
*/
public DocumentSheet(Workbook workbook, String sheetName) {
super(workbook, sheetName);
}
/* (non-Javadoc)
* @see org.spdx.spdxspreadsheet.AbstractSheet#verify()
*/
@Override
public String verify() {
try {
if (sheet == null) {
return "Worksheet for SPDX Package Info does not exist";
}
Row firstRow = sheet.getRow(firstRowNum);
for (int i = 0; i < NUM_COLS; i++) {
Cell cell = firstRow.getCell(i+firstCellNum);
if (cell == null ||
cell.getStringCellValue() == null ||
!cell.getStringCellValue().equals(HEADER_TITLES[i])) {
return "Column "+HEADER_TITLES[i]+" missing for SPDX Package Info worksheet";
}
}
return null;
} catch (Exception ex) {
return "Error in verifying SPDX Package Info work sheet: "+ex.getMessage();
}
}
/**
* @param wb
* @param sheetName
*/
public static void create(Workbook wb, String sheetName) {
int sheetNum = wb.getSheetIndex(sheetName);
if (sheetNum >= 0) {
wb.removeSheetAt(sheetNum);
}
Sheet sheet = wb.createSheet(sheetName);
CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb);
CellStyle defaultStyle = AbstractSheet.createLeftWrapStyle(wb);
Row row = sheet.createRow(0);
for (int i = 0; i < HEADER_TITLES.length; i++) {
sheet.setColumnWidth(i, COLUMN_WIDTHS[i]*256);
sheet.setDefaultColumnStyle(i, defaultStyle);
Cell cell = row.createCell(i);
cell.setCellStyle(headerStyle);
cell.setCellValue(HEADER_TITLES[i]);
}
}
/**
* Import compare results from a comparison
* @param comparer Comparer which compared the documents
* @param docNames Document names - order must be the same as the documents provided
* @throws InvalidSPDXAnalysisException
*/
public void importCompareResults(SpdxComparer comparer, List<String> docNames) throws SpdxCompareException, InvalidSPDXAnalysisException {
if (comparer.getNumSpdxDocs() != docNames.size()) {
throw(new SpdxCompareException("Number of document names does not match the number of SPDX documents"));
}
this.clear();
// create the rows
for (int i = 0; i < docNames.size()+1; i++) {
addRow();
}
importDocumentNames(comparer);
importSpdxVersion(comparer);
importDataLicense(comparer);
importSpdxId(comparer);
importDocumentNamespace(comparer);
importDocumentDescribes(comparer);
importDocumentComments(comparer);
importCreationDate(comparer);
importCreatorComment(comparer);
importLicenseListVersions(comparer);
importAnnotations(comparer);
importRelationships(comparer);
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importRelationships(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
Cell cell = sheet.getRow(getFirstDataRow()).createCell(ANNOTATION_COL);
if (comparer.isDocumentAnnotationsEquals()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(ANNOTATION_COL);
cell.setCellValue(CompareHelper.annotationsToString((comparer.getSpdxDoc(i).getAnnotations())));
}
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importAnnotations(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
Cell cell = sheet.getRow(getFirstDataRow()).createCell(RELATIONSHIP_COL);
if (comparer.isDocumentRelationshipsEquals()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(RELATIONSHIP_COL);
cell.setCellValue(CompareHelper.relationshipsToString(comparer.getSpdxDoc(i).getRelationships()));
}
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importDocumentDescribes(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
Cell cell = sheet.getRow(getFirstDataRow()).createCell(SPDX_DOCUMENT_CONTENT_COL);
if (comparer.isDocumentContentsEquals()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(SPDX_DOCUMENT_CONTENT_COL);
cell.setCellValue(CompareHelper.formatSpdxElementList(comparer.getSpdxDoc(i).getDocumentDescribes()));
}
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importDocumentNamespace(SpdxComparer comparer) throws InvalidSPDXAnalysisException, SpdxCompareException {
Cell cell = sheet.getRow(getFirstDataRow()).createCell(DOCUMENT_NAMESPACE_COL);
cell.setCellValue("N/A");
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(DOCUMENT_NAMESPACE_COL);
if (comparer.getSpdxDoc(i).getDocumentUri() != null) {
cell.setCellValue(comparer.getSpdxDoc(i).getDocumentUri());
}
}
}
/**
* @param comparer
* @throws SpdxCompareException
*/
private void importSpdxId(SpdxComparer comparer) throws SpdxCompareException {
Cell cell = sheet.getRow(getFirstDataRow()).createCell(SPDX_IDENTIFIER_COL);
cell.setCellValue("N/A");
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(SPDX_IDENTIFIER_COL);
if (comparer.getSpdxDoc(i).getId() != null) {
cell.setCellValue(comparer.getSpdxDoc(i).getId());
}
}
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importLicenseListVersions(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
Cell cell = sheet.getRow(getFirstDataRow()).createCell(LICENSE_LIST_VERSION_COL);
if (comparer.isLicenseListVersionEqual()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(LICENSE_LIST_VERSION_COL);
if (comparer.getSpdxDoc(i).getCreationInfo().getLicenseListVersion().isPresent()) {
cell.setCellValue(comparer.getSpdxDoc(i).getCreationInfo().getLicenseListVersion().get());
}
}
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importSpdxVersion(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
// comparison row
Cell cell = sheet.getRow(getFirstDataRow()).createCell(SPDX_VERSION_COL);
if (comparer.isSpdxVersionEqual()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(SPDX_VERSION_COL);
if (comparer.getSpdxDoc(i).getSpecVersion() != null) {
cell.setCellValue(comparer.getSpdxDoc(i).getSpecVersion());
}
}
}
/**
* @param comparer
*/
private void importDataLicense(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
// comparison row
Cell cell = sheet.getRow(getFirstDataRow()).createCell(DATA_LICENSE_COL);
if (comparer.isDataLicenseEqual()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(DATA_LICENSE_COL);
if (comparer.getSpdxDoc(i).getDataLicense() != null) {
cell.setCellValue(comparer.getSpdxDoc(i).getDataLicense().toString());
}
}
}
/**
* @param comparer
*/
private void importDocumentComments(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
// comparison row
Cell cell = sheet.getRow(getFirstDataRow()).createCell(DOCUMENT_COMMENT_COL);
if (comparer.isDocumentCommentsEqual()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(DOCUMENT_COMMENT_COL);
if (comparer.getSpdxDoc(i).getComment().isPresent()) {
cell.setCellValue(comparer.getSpdxDoc(i).getComment().get());
}
}
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importCreatorComment(SpdxComparer comparer) throws InvalidSPDXAnalysisException, SpdxCompareException {
// comparison row
Cell cell = sheet.getRow(getFirstDataRow()).createCell(CREATOR_COMMENT_COL);
if (comparer.isCreatorInformationEqual()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(CREATOR_COMMENT_COL);
if (comparer.getSpdxDoc(i).getCreationInfo().getComment().isPresent()) {
cell.setCellValue(comparer.getSpdxDoc(i).getCreationInfo().getComment().get());
}
}
}
/**
* @param comparer
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importCreationDate(SpdxComparer comparer) throws InvalidSPDXAnalysisException, SpdxCompareException {
// comparison row
Cell cell = sheet.getRow(getFirstDataRow()).createCell(CREATION_DATE_COL);
if (comparer.isCreatorInformationEqual()) {
setCellEqualValue(cell);
} else {
setCellDifferentValue(cell);
}
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(CREATION_DATE_COL);
cell.setCellValue(comparer.getSpdxDoc(i).getCreationInfo().getCreated());
}
}
/**
* @param cell
*/
private void setCellDifferentValue(Cell cell) {
cell.setCellValue(DIFFERENT_STRING);
cell.setCellStyle(yellowWrapped);
}
/**
* @param cell
*/
private void setCellEqualValue(Cell cell) {
cell.setCellValue(EQUAL_STRING);
cell.setCellStyle(greenWrapped);
}
/**
* @param docNames
* @throws SpdxCompareException
* @throws InvalidSPDXAnalysisException
*/
private void importDocumentNames(SpdxComparer comparer) throws SpdxCompareException, InvalidSPDXAnalysisException {
// comparison row
Cell cell = sheet.getRow(getFirstDataRow()).createCell(DOCUMENT_NAME_COL);
cell.setCellValue("Compare Results");
// data rows
for (int i = 0; i < comparer.getNumSpdxDocs(); i++) {
cell = sheet.getRow(getFirstDataRow()+i+1).createCell(DOCUMENT_NAME_COL);
if (comparer.getSpdxDoc(i).getName().isPresent()) {
cell.setCellValue(comparer.getSpdxDoc(i).getName().get());
}
}
}
}