|
| 1 | +package org.spdx.examples; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 6 | +import com.networknt.schema.Error; |
| 7 | +import com.networknt.schema.Schema; |
| 8 | +import com.networknt.schema.SchemaRegistry; |
| 9 | +import com.networknt.schema.SpecificationVersion; |
| 10 | +import org.spdx.core.DefaultModelStore; |
| 11 | +import org.spdx.core.IModelCopyManager; |
| 12 | +import org.spdx.library.LicenseInfoFactory; |
| 13 | +import org.spdx.library.ModelCopyManager; |
| 14 | +import org.spdx.library.SpdxModelFactory; |
| 15 | +import org.spdx.library.model.v3_0_1.SpdxModelClassFactoryV3; |
| 16 | +import org.spdx.library.model.v3_0_1.core.CreationInfo; |
| 17 | +import org.spdx.library.model.v3_0_1.core.Element; |
| 18 | +import org.spdx.library.model.v3_0_1.core.ProfileIdentifierType; |
| 19 | +import org.spdx.library.model.v3_0_1.core.SpdxDocument; |
| 20 | +import org.spdx.library.model.v3_0_1.expandedlicensing.ExtendableLicense; |
| 21 | +import org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo; |
| 22 | +import org.spdx.storage.IModelStore; |
| 23 | +import org.spdx.storage.simple.InMemSpdxStore; |
| 24 | +import org.spdx.tools.Verify; |
| 25 | +import org.spdx.v3jsonldstore.JsonLDStore; |
| 26 | + |
| 27 | +import java.io.*; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.spdx.tools.Verify.JSON_SCHEMA_RESOURCE_V3; |
| 33 | + |
| 34 | +/** |
| 35 | + * Simple example serializing a single expanded license |
| 36 | + */ |
| 37 | +public class ExpandedLicenseExampleV3 { |
| 38 | + |
| 39 | + static final ObjectMapper JSON_MAPPER = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); |
| 40 | + |
| 41 | + /** |
| 42 | + * @param args args[0] is the file path for the output serialized file |
| 43 | + */ |
| 44 | + public static void main(String[] args) throws Exception { |
| 45 | + if (args.length != 1) { |
| 46 | + usage(); |
| 47 | + System.exit(1); |
| 48 | + } |
| 49 | + File outFile = new File(args[0]); |
| 50 | + if (outFile.exists()) { |
| 51 | + System.out.printf("%s already exists.\n", args[0]); |
| 52 | + System.exit(1); |
| 53 | + } |
| 54 | + if (!outFile.createNewFile()) { |
| 55 | + System.out.printf("Unable to create file %s\n", args[0]); |
| 56 | + System.exit(1); |
| 57 | + } |
| 58 | + if (!outFile.canWrite()) { |
| 59 | + System.out.printf("Can not write to file %s\n", args[0]); |
| 60 | + System.exit(1); |
| 61 | + } |
| 62 | + SpdxModelFactory.init(); |
| 63 | + IModelCopyManager copyManager = new ModelCopyManager(); |
| 64 | + try (JsonLDStore modelStore = new JsonLDStore(new InMemSpdxStore())) { |
| 65 | + modelStore.setUseExternalListedElements(true); // setting this to false will include all the listed license details in the document |
| 66 | + String defaultDocUri = "https://spdx.github.io/spdx-spec/v3.0.1/examples/complex-license-eaa46bdcfa20"; |
| 67 | + String prefix = defaultDocUri + "#"; |
| 68 | + DefaultModelStore.initialize(modelStore, defaultDocUri, copyManager); |
| 69 | + CreationInfo creationInfo = SpdxModelClassFactoryV3.createCreationInfo( |
| 70 | + modelStore, prefix + "garyagent", "Gary O'Neall", |
| 71 | + copyManager); |
| 72 | + SpdxDocument doc = creationInfo.createSpdxDocument(prefix + "document") |
| 73 | + .setDataLicense(LicenseInfoFactory.getListedLicenseById("CC0")) |
| 74 | + .addNamespaceMap(creationInfo.createNamespaceMap(modelStore.getNextId(IModelStore.IdType.Anonymous)) |
| 75 | + .setNamespace(prefix) |
| 76 | + .setPrefix("example") |
| 77 | + .build()) |
| 78 | + .addProfileConformance(ProfileIdentifierType.CORE) |
| 79 | + .addProfileConformance(ProfileIdentifierType.SOFTWARE) |
| 80 | + .addProfileConformance(ProfileIdentifierType.EXPANDED_LICENSING) |
| 81 | + .build(); |
| 82 | + doc.setIdPrefix(prefix); |
| 83 | + AnyLicenseInfo complexLicense = doc.createConjunctiveLicenseSet(prefix + "complexlicense") |
| 84 | + // CustomLicense |
| 85 | + .addMember(doc.createCustomLicense(prefix + "LicenseRef-customlicense1") |
| 86 | + .setLicenseText("This is the license text for my custom license") |
| 87 | + .setName("Gary's Custom License") |
| 88 | + .addSeeAlso("https://example.com") |
| 89 | + .build()) |
| 90 | + // OrLaterOperator |
| 91 | + .addMember(doc.createOrLaterOperator(prefix + "complexorlater") |
| 92 | + // ListedLicense |
| 93 | + .setSubjectLicense(doc.createListedLicense("http://spdx.org/licenses/EPL-1.0") |
| 94 | + .setName("Eclipse Public License 1.0") |
| 95 | + .setLicenseText("Eclipse Public License - v 1.0\n\nTHE ACCOMPANYING PROGRAM IS PROVIDED" + |
| 96 | + " UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE (\"AGREEMENT\"). ANY USE, REPRODUCTION " + |
| 97 | + "OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENTS ACCEPTANCE OF THIS AGREEMENT.\n\n1. " + |
| 98 | + "DEFINITIONS\n\n\"Contribution\" means:\n a) in the case of the initial Contributor...") |
| 99 | + .setIsFsfLibre(true) |
| 100 | + .setComment("EPL replaced the CPL on 28 June 2005.") |
| 101 | + .addSeeAlso("https://opensource.org/licenses/EPL-1.0") |
| 102 | + .build()) |
| 103 | + .build()) |
| 104 | + // DisjunctiveLicenseSet |
| 105 | + .addMember(doc.createDisjunctiveLicenseSet(prefix + "complexdisjunctive") |
| 106 | + // WithAdditionOperator |
| 107 | + .addMember(doc.createWithAdditionOperator(prefix + "complexwith") |
| 108 | + .setSubjectExtendableLicense((ExtendableLicense) LicenseInfoFactory.parseSPDXLicenseString("GPL-2.0-or-later")) |
| 109 | + // ListedLicenseException |
| 110 | + .setSubjectAddition(doc.createListedLicenseException("http://spdx.org/licenses/Autoconf-exception-2.0") |
| 111 | + .setName("Autoconf exception 2.0") |
| 112 | + .setComment("Typically used with GPL-2.0-only or GPL-2.0-or-later") |
| 113 | + .setAdditionText("As a special exception, the Free Software Foundation gives unlimited " + |
| 114 | + "permission to copy, distribute and modify the ...") |
| 115 | + .addSeeAlso("http://ftp.gnu.org/gnu/autoconf/autoconf-2.59.tar.gz") |
| 116 | + .build()) |
| 117 | + .build()) |
| 118 | + .addMember(doc.createWithAdditionOperator(prefix + "complexwithcustomaddition") |
| 119 | + .setSubjectExtendableLicense((ExtendableLicense) LicenseInfoFactory.parseSPDXLicenseString("Apache-2.0")) |
| 120 | + // CustomLicenseAddition |
| 121 | + .setSubjectAddition(doc.createCustomLicenseAddition(prefix + "complexcustomaddition") |
| 122 | + .setName("My License Addition") |
| 123 | + .setAdditionText("Custom addition text - just for me") |
| 124 | + .addSeeAlso("https://example.com") |
| 125 | + .build()) |
| 126 | + .build()) |
| 127 | + // ExtendableLicense - Abstract |
| 128 | + // IndividualLicensingInfo - used by listed license |
| 129 | + // License - Abstract |
| 130 | + .addMember(LicenseInfoFactory.parseSPDXLicenseString("MIT")) |
| 131 | + .build()) |
| 132 | + .build(); |
| 133 | + doc.getRootElements().add(complexLicense); |
| 134 | + doc.getElements().add(complexLicense); |
| 135 | + List<String> warnings = new ArrayList<>(); |
| 136 | + Collection<Element> docElements = doc.getElements(); |
| 137 | + SpdxModelFactory.getSpdxObjects(modelStore, copyManager, null, null, prefix).forEach( |
| 138 | + modelObject -> { |
| 139 | + if (modelObject instanceof Element) { |
| 140 | + Element element = (Element)modelObject; |
| 141 | + if (!docElements.contains(element) && !element.equals(doc)) { |
| 142 | + warnings.add("Element not in the document elements: " + element.getObjectUri()); |
| 143 | + docElements.add(element); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + ); |
| 148 | + warnings.addAll(complexLicense.verify()); |
| 149 | + try (OutputStream outStream = new FileOutputStream(outFile)) { |
| 150 | + modelStore.serialize(outStream, doc); |
| 151 | + } |
| 152 | + SchemaRegistry schemaRegistry = |
| 153 | + SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12); |
| 154 | + Schema schema; |
| 155 | + try (InputStream is = Verify.class.getResourceAsStream("/" + JSON_SCHEMA_RESOURCE_V3)) { |
| 156 | + schema = schemaRegistry.getSchema(is); |
| 157 | + } |
| 158 | + JsonNode root; |
| 159 | + try (InputStream is = new FileInputStream(outFile)) { |
| 160 | + root = JSON_MAPPER.readTree(is); |
| 161 | + } |
| 162 | + List<com.networknt.schema.Error> messages = schema.validate(root); |
| 163 | + for (Error msg:messages) { |
| 164 | + warnings.add(msg.toString()); |
| 165 | + } |
| 166 | + if (!warnings.isEmpty()) { |
| 167 | + System.out.println("Generated document contains the following warnings:"); |
| 168 | + for (String warning:warnings) { |
| 169 | + System.out.print("\t"); |
| 170 | + System.out.println(warning); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static void usage() { |
| 177 | + System.out.println("Generates an SPDX JSON-LD file containing all of the supported classes."); |
| 178 | + System.out.println("Usage: FullSpdxV3Example outputfile"); |
| 179 | + } |
| 180 | +} |
0 commit comments