-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathCycloneDxSchema.java
More file actions
308 lines (280 loc) · 11.7 KB
/
CycloneDxSchema.java
File metadata and controls
308 lines (280 loc) · 11.7 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
/*
* This file is part of CycloneDX Core (Java).
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) OWASP Foundation. All Rights Reserved.
*/
package org.cyclonedx;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SchemaValidatorsConfig;
import com.networknt.schema.SpecVersionDetector;
import com.networknt.schema.resource.MapSchemaMapper;
import org.cyclonedx.generators.json.BomJsonGenerator;
import org.cyclonedx.generators.xml.BomXmlGenerator;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* CycloneDxSchema is a base class that provides schema information to {@link BomXmlGenerator},
* {@link BomJsonGenerator}, and {@link org.cyclonedx.parsers.Parser}. The class can be extended for other
* implementations as well.
*
* @since 1.1.0
*/
public abstract class CycloneDxSchema
{
public static final String NS_BOM_10 = "http://cyclonedx.org/schema/bom/1.0";
public static final String NS_BOM_11 = "http://cyclonedx.org/schema/bom/1.1";
public static final String NS_BOM_12 = "http://cyclonedx.org/schema/bom/1.2";
public static final String NS_BOM_13 = "http://cyclonedx.org/schema/bom/1.3";
public static final String NS_BOM_14 = "http://cyclonedx.org/schema/bom/1.4";
public static final String NS_BOM_15 = "http://cyclonedx.org/schema/bom/1.5";
public static final String NS_BOM_16 = "http://cyclonedx.org/schema/bom/1.6";
public static final String NS_BOM_17 = "http://cyclonedx.org/schema/bom/1.7";
@Deprecated
public static final String NS_DEPENDENCY_GRAPH_10 = "http://cyclonedx.org/schema/ext/dependency-graph/1.0";
public static final String NS_BOM_LATEST = NS_BOM_17;
public static final Version VERSION_LATEST = Version.VERSION_17;
public static final List<Version> ALL_VERSIONS = Arrays.asList(Version.values());
/**
* Returns the CycloneDX JsonSchema for the specified schema version.
*
* @param schemaVersion The version to return the schema for
* @param mapper is to provide a Jackson ObjectMapper
* @return a Schema
* @throws IOException when errors are encountered
* @since 6.0.0
*/
public JsonSchema getJsonSchema(Version schemaVersion, final ObjectMapper mapper)
throws IOException
{
final InputStream spdxInstream = getJsonSchemaAsStream(schemaVersion);
final SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setPreloadJsonSchema(false);
final Map<String, String> offlineMappings = new HashMap<>();
offlineMappings.put("http://cyclonedx.org/schema/spdx.schema.json",
getClass().getClassLoader().getResource("spdx.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/jsf-0.82.schema.json",
getClass().getClassLoader().getResource("jsf-0.82.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.2.schema.json",
getClass().getClassLoader().getResource("bom-1.2-strict.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.3.schema.json",
getClass().getClassLoader().getResource("bom-1.3-strict.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.4.schema.json",
getClass().getClassLoader().getResource("bom-1.4.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.5.schema.json",
getClass().getClassLoader().getResource("bom-1.5.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.6.schema.json",
getClass().getClassLoader().getResource("bom-1.6.schema.json").toExternalForm());
offlineMappings.put("http://cyclonedx.org/schema/bom-1.7.schema.json",
getClass().getClassLoader().getResource("bom-1.7.schema.json").toExternalForm());
JsonNode schemaNode = mapper.readTree(spdxInstream);
final MapSchemaMapper offlineSchemaMapper = new MapSchemaMapper(offlineMappings);
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersionDetector.detect(schemaNode)))
.jsonMapper(mapper)
.schemaMappers(s -> s.add(offlineSchemaMapper))
.build();
return factory.getSchema(schemaNode, config);
}
private InputStream getJsonSchemaAsStream(final Version schemaVersion) {
if (Version.VERSION_12 == schemaVersion) {
return this.getClass().getClassLoader().getResourceAsStream("bom-1.2-strict.schema.json");
}
else if (Version.VERSION_13 == schemaVersion) {
return this.getClass().getClassLoader().getResourceAsStream("bom-1.3-strict.schema.json");
}
else if (Version.VERSION_14 == schemaVersion) {
return this.getClass().getClassLoader().getResourceAsStream("bom-1.4.schema.json");
}
else if(Version.VERSION_15 == schemaVersion){
return this.getClass().getClassLoader().getResourceAsStream("bom-1.5.schema.json");
}
else if(Version.VERSION_16 == schemaVersion){
return this.getClass().getClassLoader().getResourceAsStream("bom-1.6.schema.json");
}
else {
return this.getClass().getClassLoader().getResourceAsStream("bom-1.7.schema.json");
}
}
/**
* Returns the CycloneDX XML Schema for the specified schema version.
*
* @param schemaVersion The version to return the schema for
* @return a Schema
* @throws SAXException a SAXException
* @since 2.0.0
*/
public Schema getXmlSchema(Version schemaVersion) throws SAXException {
if (Version.VERSION_10 == schemaVersion) {
return getXmlSchema10();
}
else if (Version.VERSION_11 == schemaVersion) {
return getXmlSchema11();
}
else if (Version.VERSION_12 == schemaVersion) {
return getXmlSchema12();
}
else if (Version.VERSION_13 == schemaVersion) {
return getXmlSchema13();
}
else if (Version.VERSION_14 == schemaVersion) {
return getXmlSchema14();
}
else if (Version.VERSION_15 == schemaVersion) {
return getXmlSchema15();
}
else if (Version.VERSION_16 == schemaVersion) {
return getXmlSchema16();
}
else {
return getXmlSchema17();
}
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 1.1.0
*/
private Schema getXmlSchema10() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.0.xsd")
);
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 2.0.0
*/
private Schema getXmlSchema11() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.1.xsd")
);
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 2.8.0
*/
private Schema getXmlSchema12() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.2.xsd")
);
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 5.0.0
*/
private Schema getXmlSchema13() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.3.xsd")
);
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 5.1.0
*/
private Schema getXmlSchema14() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.4.xsd")
);
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 8.0.1
*/
private Schema getXmlSchema15() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.5.xsd")
);
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 8.1.0
*/
private Schema getXmlSchema16() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.6.xsd")
);
}
/**
* Returns the CycloneDX XML Schema from the specifications XSD.
*
* @return a Schema
* @throws SAXException a SAXException
* @since 10.0.0
*/
private Schema getXmlSchema17() throws SAXException {
// Use local copies of schemas rather than resolving from the net. It's faster, and less prone to errors.
return getXmlSchema(
this.getClass().getClassLoader().getResourceAsStream("spdx.xsd"),
this.getClass().getClassLoader().getResourceAsStream("bom-1.7.xsd")
);
}
public Schema getXmlSchema(InputStream... inputStreams) throws SAXException {
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
schemaFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
final Source[] schemaFiles = new Source[inputStreams.length];
for (int i = 0; i < inputStreams.length; i++) {
schemaFiles[i] = new StreamSource(inputStreams[i]);
}
return schemaFactory.newSchema(schemaFiles);
}
}