forked from spdx/tools-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerify.java
More file actions
208 lines (196 loc) · 7.64 KB
/
Copy pathVerify.java
File metadata and controls
208 lines (196 loc) · 7.64 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
/**
* 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;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.Version;
import org.spdx.library.model.SpdxDocument;
import org.spdx.storage.ISerializableModelStore;
import org.spdx.tagvaluestore.TagValueStore;
import org.spdx.tools.SpdxToolsHelper.SerFileType;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
/**
* Verifies an SPDX document and lists any verification errors
* @author Gary O'Neall
*
*/
public class Verify {
static final int MIN_ARGS = 1;
static final int MAX_ARGS = 2;
static final int ERROR_STATUS = 1;
private static final String JSON_SCHEMA_RESOURCE_V2_3 = "/resources/spdx-schema-v2.3.json";
private static final String JSON_SCHEMA_RESOURCE_V2_2 = "/resources/spdx-schema-v2.2.json";
/**
* @param args args[0] SPDX file path; args[1] [RDFXML|JSON|XLS|XLSX|YAML|TAG] an optional file type - if not present, file type of the to file will be used
*/
public static void main(String[] args) {
if (args.length < MIN_ARGS) {
System.err
.println("Usage:\n Verify file\nwhere file is the file path to an SPDX file");
System.exit(ERROR_STATUS);
}
if (args.length > MAX_ARGS) {
System.out.printf("Warning: Extra arguments will be ignored");
}
List<String> verify = null;
try {
SerFileType fileType = null;
if (args.length > 1) {
try {
fileType = SpdxToolsHelper.strToFileType(args[1]);
} catch (Exception ex) {
System.err.println("Invalid file type: "+args[1]);
System.exit(ERROR_STATUS);
}
} else {
fileType = SpdxToolsHelper.fileToFileType(new File(args[0]));
}
verify = verify(args[0], fileType);
} catch (SpdxVerificationException e) {
System.out.println(e.getMessage());
System.exit(ERROR_STATUS);
} catch (InvalidFileNameException e) {
System.err.println("Invalid file name: "+args[0]);
System.exit(ERROR_STATUS);
}
if (verify.size() > 0) {
System.out.println("This SPDX Document is not valid due to:");
for (int i = 0; i < verify.size(); i++) {
System.out.print("\t" + verify.get(i)+"\n");
}
System.exit(ERROR_STATUS);
} else {
System.out.println("This SPDX Document is valid.");
}
}
/**
* Verify a an SPDX file
* @param filePath File path to the SPDX file to be verified
* @param fileType
* @return A list of verification errors - if empty, the SPDX file is valid
* @throws InvalidFileNameException
* @throws IOException
* @throws SpdxVerificationException
* @throws Errors where the SPDX file can not be parsed or the filename is invalid
*/
public static List<String> verify(String filePath, SerFileType fileType) throws SpdxVerificationException {
Objects.requireNonNull(filePath);
Objects.requireNonNull(fileType);
File file = new File(filePath);
if (!file.exists()) {
throw new SpdxVerificationException("File "+filePath+" not found.");
}
if (!file.isFile()) {
throw new SpdxVerificationException(filePath+" is not a file.");
}
ISerializableModelStore store = null;
try {
store = SpdxToolsHelper.fileTypeToStore(fileType);
} catch (InvalidSPDXAnalysisException e) {
throw new SpdxVerificationException("Error converting fileType to store",e);
}
SpdxDocument doc = null;
try {
doc = SpdxToolsHelper.readDocumentFromFile(store, file);
} catch (FileNotFoundException e) {
throw new SpdxVerificationException("File "+filePath+ " not found.",e);
} catch (IOException e) {
throw new SpdxVerificationException("IO Error reading SPDX file",e);
} catch (InvalidSPDXAnalysisException e) {
throw new SpdxVerificationException("Analysis exception processing SPDX file: "+e.getMessage(),e);
}
List<String> retval = new ArrayList<String>();
if (store instanceof TagValueStore) {
// add in any parser warnings
retval.addAll(((TagValueStore)store).getWarnings());
}
if (SerFileType.JSON.equals(fileType)) {
try {
String jsonSchemaResource = Version.versionLessThan(Version.TWO_POINT_THREE_VERSION, doc.getSpecVersion()) ?
JSON_SCHEMA_RESOURCE_V2_2 : JSON_SCHEMA_RESOURCE_V2_3;
JsonNode spdxJsonSchema = JsonLoader.fromResource(jsonSchemaResource);
final JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(spdxJsonSchema);
JsonNode spdxDocJson = JsonLoader.fromFile(file);
ProcessingReport report = schema.validateUnchecked(spdxDocJson, true);
report.spliterator().forEachRemaining(msg -> {
JsonNode msgJson = msg.asJson();
if (!msg.getMessage().contains("$id")) { // Known warning - this is in the draft 7 spec - perhaps a bug in the validator?
JsonNode instance = msgJson.findValue("instance");
String warningStr = msg.getMessage();
if (Objects.nonNull(instance)) {
warningStr = warningStr + " for " + instance.toString();
}
retval.add(warningStr);
}
});
} catch (IOException e) {
retval.add("Unable to validate JSON file against schema due to I/O Error");
} catch (InvalidSPDXAnalysisException e) {
retval.add("Unable to validate JSON file against schema due to error in SPDX file");
} catch (ProcessingException e) {
retval.add("Unable to validate JSON file against schema due to processing exception");
}
}
List<String> verify;
try {
verify = doc.verify(doc.getSpecVersion());
} catch (InvalidSPDXAnalysisException e) {
retval.add("Error processing verify for the specific version");
verify = doc.verify();
}
if (!verify.isEmpty()) {
for (String verifyMsg:verify) {
if (!retval.contains(verifyMsg)) {
retval.add(verifyMsg);
}
}
}
return retval;
}
/**
* Verify a tag/value file
* @param filePath File path to the SPDX Tag Value file to be verified
* @return A list of verification errors - if empty, the SPDX file is valid
* @throws SpdxVerificationException Errors where the SPDX Tag Value file can not be parsed or the filename is invalid
*/
public static List<String> verifyTagFile(String filePath) throws SpdxVerificationException {
return verify(filePath, SerFileType.TAG);
}
/**
* Verify an RDF/XML file
* @param filePath File path to the SPDX RDF/XML file to be verified
* @return SpdxDocument
* @throws SpdxVerificationException Errors where the SPDX RDF/XML file can not be parsed or the filename is invalid
*/
public static List<String> verifyRDFFile(String filePath) throws SpdxVerificationException {
return verify(filePath, SerFileType.RDFXML);
}
public void usage() {
System.out.println("Verify filepath [RDFXML|JSON|XLS|XLSX|YAML|TAG]");
System.out.println(" where filepath is a path to the SPDX file and [RDFXML|JSON|XLS|XLSX|YAML|TAG] is an optional file type - if not present, file type of the to file will be used");
}
}