Skip to content

Commit 3b3afd1

Browse files
committed
Remove init(modelSTore...)
Also adds documentation to the GETTING-STARTED.md file
1 parent f01d6a5 commit 3b3afd1

File tree

2 files changed

+78
-37
lines changed

2 files changed

+78
-37
lines changed

GETTING-STARTED.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Getting Started
22

3-
## SPDX Version 3
4-
5-
### Programmatically Creating SPDX Data
3+
## Initialization
64

75
Before executing any of the model class methods, the model versions need to be initialized. This is done by calling:
86

@@ -19,6 +17,28 @@ InMemSpdxStore modelStore = new InMemSpdxStore();
1917
IModelCopyManager copyManager = new ModelCopyManager();
2018
```
2119

20+
Many factory and helper methods in the library make use of a DefaultModelStore
21+
if no model store or copy manager is specified.
22+
23+
The `SpdxModelFactory.init()` will create defaults for this purpose.
24+
25+
If you would like to use a different default model store and/or copy manager, you can call:
26+
27+
```java
28+
DefaultModelStore.initialize(IModelStore newModelStore, String newDefaultDocumentUri,
29+
IModelCopyManager newDefaultCopyManager);
30+
```
31+
32+
The `newDefaultDocumentUri` is a default document URI used for SPDX Spec version 2 model objects.
33+
34+
IMPORTANT NOTE: The call to `DefaultModelStore.initialize` must be made prior to or immediately after the call
35+
to `SpdxModelFactory.init()`. Otherwise, any data stored in the previous default model object will be lost.
36+
The `SpdxModelFactory.init()` will not overwrite an already initialized default model store.
37+
38+
## SPDX Spec Version 3
39+
40+
### Programmatically Creating SPDX Data
41+
2242
All SPDX elements are required to have a unique SPDX ID which is an Object URI. In the SPDX Java libraries, this is commonly referred to as the `objectUri` to avoid confusion with the SPDX 2.X version short SPDX IDs.
2343

2444
A good practice is to create a common prefix to use for your programmatic session. The prefix should be unique to the session. There are convenience methods in the library to append identifiers unique to the model store.
@@ -29,7 +49,7 @@ In these examples, we'll use:
2949
String prefix = "https://org.spdx.spdxdata/899b1918-f72a-4755-9215-6262b3c346df/";
3050
```
3151

32-
Since SPDX 3.0 requires creation info on every element, the easiest way to start is to use the SPDX 3 model convenience method `SpdxModelClassFactory.createCreationInfo(...)` which will create the `Agent` and `CreationInfo` classes which can be added to all of the subsequent elements.
52+
Since SPDX 3.0 requires creation info on every element, the easiest way to start is to use the SPDX 3 model convenience method `SpdxModelClassFactory.createCreationInfo(...)` which will create the `Agent` and `CreationInfo` classes which can be added to all the subsequent elements.
3353

3454
For example:
3555

@@ -68,3 +88,56 @@ sbom.getElements().add(
6888
```
6989

7090
The model store, creation info, copy manager, and prefix information will all be copied from the sbom allowing you to focus just on the properties you need to add.
91+
92+
## SPDX Spec Version 2
93+
94+
### Programmatically Creating SPDX Data
95+
96+
SPDX Spec version 2 stores all date within an SPDX document. SPDX documents have a single document URI which is a
97+
prefix for all IDs within the document.
98+
99+
The SPDX document MUST be unique.
100+
101+
Below is an example:
102+
103+
```java
104+
String newDocumentUri = "http://spdx.org/spdxdocs/spdx-example2-444504E0-4F89-41D3-9A0C-0305E82CCCCC";
105+
```
106+
107+
We then need to create the SPDX document:
108+
109+
```java
110+
SpdxDocument myDoc = new SpdxDocument(
111+
inMemStore, // here we'll just use a very simple in memory store which doesn't support serialization
112+
newDocumentUri, // the URI of the document - must be globally unique
113+
null, // an optional copy manager can be provided if working with more than one store
114+
true); // this time, we want to create it
115+
```
116+
117+
We now need to add the required fields to make this a valid SPDX document:
118+
119+
```java
120+
myDoc.setCreationInfo( // Set the required creationInfo
121+
myDoc.createCreationInfo( // All model objects have a set of convenience methods to create
122+
// other model objects using the same model store and document URI
123+
Arrays.asList(new String[] {"Tool: Sample App"}), // creators
124+
new SimpleDateFormat(SpdxConstants.SPDX_DATE_FORMAT).format(new Date())));
125+
// creation date - note that SpdxConstants has several useful constant values
126+
myDoc.setSpecVersion(Version.CURRENT_SPDX_VERSION); // the Version class has constants defined for all supported SPDX spec versions
127+
myDoc.setName("My Document");
128+
// The LicenseInfoFactory contains some convenient static methods to manage licenses including
129+
// a license parser. Note that we have to pass in the model store and document URI so that the
130+
// license is created in the same store.
131+
myDoc.setDataLicense(LicenseInfoFactory.parseSPDXLicenseString("CC0-1.0", inMemStore, newDocumentUri, null));
132+
// We need something for the document to describe, we'll create an SPDX file
133+
AnyLicenseInfo apacheLicense = LicenseInfoFactory.parseSPDXLicenseString("Apache-2.0", inMemStore, newDocumentUri, null);
134+
SpdxFile file = myDoc.createSpdxFile(
135+
SpdxConstants.SPDX_ELEMENT_REF_PRENUM + "44",
136+
"./myfile/name",
137+
apacheLicense,
138+
Arrays.asList(new AnyLicenseInfo[] {apacheLicense}),
139+
"Copyright me, 2023",
140+
myDoc.createChecksum(ChecksumAlgorithm.SHA1, "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"))
141+
.build(); // The more complex model objects follows a builder pattern
142+
myDoc.getDocumentDescribes().add(file);
143+
```

src/main/java/org/spdx/library/SpdxModelFactory.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -125,38 +125,6 @@ public static void init() {
125125
}
126126
}
127127

128-
/**
129-
* This static method is a convenience to load this class and initialize the supported model versions and
130-
* initialize the DefaultModelStore with the parameter values
131-
* <p>
132-
* It should be called before using any other functionality from the library
133-
* @param modelStore Model store to use as a default
134-
* @param defaultDocumentUri Document URI to use as a default
135-
* @param defaultCopyManager Copy manager to use as a default
136-
*/
137-
public static void init(IModelStore modelStore, String defaultDocumentUri,
138-
IModelCopyManager defaultCopyManager) {
139-
Objects.requireNonNull(modelStore, "Model store can not be null");
140-
Objects.requireNonNull(defaultDocumentUri, "Document URI can not be null");
141-
Objects.requireNonNull(defaultCopyManager, "Copy manager can not be null");
142-
synchronized (INIT_LOCK) {
143-
if (!DefaultModelStore.isInitialized()) {
144-
DefaultModelStore.initialize(modelStore, defaultDocumentUri, defaultCopyManager);
145-
} else {
146-
try {
147-
if (!(Objects.equals(modelStore, DefaultModelStore.getDefaultModelStore()) &&
148-
Objects.equals(defaultDocumentUri, DefaultModelStore.getDefaultDocumentUri()) &&
149-
Objects.equals(defaultCopyManager, DefaultModelStore.getDefaultCopyManager()))) {
150-
logger.warn("Ignoring second call to initialize the model store");
151-
}
152-
} catch (DefaultStoreNotInitialized e) {
153-
logger.error("Unexpected store not initialized during init", e);
154-
}
155-
}
156-
}
157-
}
158-
159-
160128
/**
161129
* If the object exists in the model store, it will be "inflated" back to the Java object.
162130
* If the object does not exist AND the create parameter is true, a new object will be created and
@@ -252,7 +220,7 @@ public static Object getExternalElement(IModelStore store, String uri,
252220
* @param typeFilter type to filter on
253221
* @param objectUriPrefixFilter only return objects with URI's starting with this string
254222
* @param idPrefix optional prefix used for any new object URI's created in support of this model object
255-
* @return stream of objects stored in the model store - an object being any non primitive type
223+
* @return stream of objects stored in the model store - an object being any non-primitive type
256224
* @throws InvalidSPDXAnalysisException on SPDX parsing errors
257225
*/
258226
public static Stream<?> getSpdxObjects(IModelStore store, @Nullable IModelCopyManager copyManager,

0 commit comments

Comments
 (0)