Skip to content

Commit df46822

Browse files
authored
Merge pull request #216 from jkrae-metaeffekt/issue215
A canonical way to create ExtractedLicenseInfo (Helper method and documentation, Issue #215)
2 parents 0f80417 + fbfd9ac commit df46822

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

src/main/java/org/spdx/library/model/ModelObject.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@
4040
import org.spdx.library.model.enumerations.ReferenceCategory;
4141
import org.spdx.library.model.enumerations.RelationshipType;
4242
import org.spdx.library.model.enumerations.SpdxEnumFactory;
43-
import org.spdx.library.model.license.AnyLicenseInfo;
44-
import org.spdx.library.model.license.ConjunctiveLicenseSet;
43+
import org.spdx.library.model.license.*;
4544
import org.spdx.library.model.license.CrossRef.CrossRefBuilder;
46-
import org.spdx.library.model.license.DisjunctiveLicenseSet;
47-
import org.spdx.library.model.license.ListedLicenses;
48-
import org.spdx.library.model.license.SpdxNoAssertionLicense;
49-
import org.spdx.library.model.license.SpdxNoneLicense;
5045
import org.spdx.library.model.pointer.ByteOffsetPointer;
5146
import org.spdx.library.model.pointer.LineCharPointer;
5247
import org.spdx.library.model.pointer.SinglePointer;
@@ -1418,6 +1413,24 @@ public CrossRefBuilder createCrossRef(String url) throws InvalidSPDXAnalysisExce
14181413
return new CrossRefBuilder(this.modelStore, this.documentUri,
14191414
this.modelStore.getNextId(IdType.Anonymous, this.documentUri), this.copyManager, url);
14201415
}
1416+
1417+
/**
1418+
* Constructs {@link ExtractedLicenseInfo} with text set.
1419+
*
1420+
* <p> Note that object construction has side-effects relating to a document and modelStore,
1421+
* requiring document context.
1422+
* This may bind usage of {@link ExtractedLicenseInfo} instances to the document that they were created by!
1423+
*
1424+
* @param id id that the text relates to
1425+
* @param text license text corresponding to the id
1426+
* @return returns a constructed object
1427+
* @throws InvalidSPDXAnalysisException
1428+
*/
1429+
public ExtractedLicenseInfo createExtractedLicense(String id, String text) throws InvalidSPDXAnalysisException {
1430+
ExtractedLicenseInfo eli = new ExtractedLicenseInfo(modelStore, documentUri, id, copyManager, true);
1431+
eli.setExtractedText(text);
1432+
return eli;
1433+
}
14211434

14221435
@Override
14231436
public String toString() {

src/main/java/org/spdx/library/model/SpdxDocument.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,15 @@ public Collection<ExternalDocumentRef> getExternalDocumentRefs() throws InvalidS
184184
public Collection<ExtractedLicenseInfo> getExtractedLicenseInfos() throws InvalidSPDXAnalysisException {
185185
return this.extractedLicenseInfos;
186186
}
187-
187+
188188
/**
189-
* Add a license info to the collection of extracted license infos
190-
* @param licenseInfo
191-
* @return
189+
* Add a license info to the collection of extracted license infos.
190+
*
191+
* <p> Useful for adding license texts for licenseRefs.
192+
*
193+
* @param licenseInfo object containing license information
194+
* @return true if the underlying collection changed due to this call
195+
* @see SpdxDocument#createExtractedLicense(String, String)
192196
*/
193197
public boolean addExtractedLicenseInfos(ExtractedLicenseInfo licenseInfo) {
194198
Objects.requireNonNull(licenseInfo, "License info can not be null");
@@ -197,8 +201,9 @@ public boolean addExtractedLicenseInfos(ExtractedLicenseInfo licenseInfo) {
197201

198202
/**
199203
* Clear the extractedLicenseInfos and add all elements from extractedLicenseInfos
200-
* @param extractedLicenseInfos
201-
* @return this to enable chaining of sets
204+
*
205+
* @param extractedLicenseInfos the new list of license infos
206+
* @return this to enable chaining of setter calls
202207
*/
203208
public SpdxDocument setExtractedLicenseInfos(List<ExtractedLicenseInfo> extractedLicenseInfos) {
204209
Objects.requireNonNull(extractedLicenseInfos, "Extracted license infos can not be null");

src/main/java/org/spdx/library/model/license/ExtractedLicenseInfo.java

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,72 @@
4444
*
4545
*/
4646
public class ExtractedLicenseInfo extends AbstractExtractedLicenseInfo {
47-
47+
48+
/**
49+
* Create a new ExtractedLicenseInfo object
50+
*
51+
* <p> Users of the library should not call this constructor directly but use
52+
* {@link org.spdx.library.model.SpdxDocument#createExtractedLicense SpdxDocument#createExtractedLicense}.
53+
* This ensures correct behaviour between that document and its {@link ExtractedLicenseInfo} instance.
54+
*
55+
* <p> Otherwise, the object may misbehave, such as with
56+
* {@link org.spdx.library.model.SpdxDocument#addExtractedLicenseInfos SpdxDocument#addExtractedLicenseInfos}.
57+
*
58+
* @throws InvalidSPDXAnalysisException
59+
*/
4860
public ExtractedLicenseInfo() throws InvalidSPDXAnalysisException {
4961
super(DefaultModelStore.getDefaultModelStore().getNextId(IdType.LicenseRef, DefaultModelStore.getDefaultDocumentUri()));
5062
}
51-
63+
64+
/**
65+
* Create a new ExtractedLicenseInfo object
66+
*
67+
* <p> Users of the library should not call this constructor directly but use
68+
* {@link org.spdx.library.model.SpdxDocument#createExtractedLicense SpdxDocument#createExtractedLicense}.
69+
* This ensures correct behaviour between that document and its {@link ExtractedLicenseInfo} instance.
70+
*
71+
* <p> Otherwise, the object may misbehave, such as with
72+
* {@link org.spdx.library.model.SpdxDocument#addExtractedLicenseInfos SpdxDocument#addExtractedLicenseInfos}.
73+
*
74+
* @param id identifier for the license
75+
* @throws InvalidSPDXAnalysisException
76+
*/
5277
public ExtractedLicenseInfo(String id) throws InvalidSPDXAnalysisException {
5378
super(id);
5479
}
5580

5681
/**
5782
* Create a new ExtractedLicenseInfo object
58-
* @param modelStore container which includes the license
83+
*
84+
* <p> Users of the library should prefer
85+
* {@link org.spdx.library.model.SpdxDocument#createExtractedLicense SpdxDocument#createExtractedLicense}.
86+
*
87+
* @param modelStore container which includes the license
5988
* @param documentUri URI for the SPDX document containing the license
60-
* @param id identifier for the license
89+
* @param id identifier for the license
6190
* @param copyManager if non-null, allows for copying of any properties set which use other model stores or document URI's
62-
* @param create if true, create the license if it does not exist
63-
* @throws InvalidSPDXAnalysisException
91+
* @param create if true, create the license if it does not exist
92+
* @throws InvalidSPDXAnalysisException
6493
*/
6594
public ExtractedLicenseInfo(IModelStore modelStore, String documentUri, String id,
6695
@Nullable ModelCopyManager copyManager, boolean create)
6796
throws InvalidSPDXAnalysisException {
6897
super(modelStore, documentUri, id, copyManager, create);
6998
}
70-
99+
71100
/**
72101
* Create a new ExtractedLicenseInfo using the ID and text
73-
* @param id
74-
* @param text
75-
* @throws InvalidSPDXAnalysisException
102+
*
103+
* <p> Users of the library should not call this constructor directly but use
104+
* {@link org.spdx.library.model.SpdxDocument#createExtractedLicense SpdxDocument#createExtractedLicense}.
105+
* This ensures correct behaviour between that document and its {@link ExtractedLicenseInfo} instance.
106+
*
107+
* <p> Otherwise, the object may misbehave, such as with
108+
* {@link org.spdx.library.model.SpdxDocument#addExtractedLicenseInfos SpdxDocument#addExtractedLicenseInfos}.
109+
*
110+
* @param id identifier for the license
111+
* @param text text to associate with the license
112+
* @throws InvalidSPDXAnalysisException
76113
*/
77114
public ExtractedLicenseInfo(String id, String text) throws InvalidSPDXAnalysisException {
78115
super(id);
@@ -101,7 +138,9 @@ public String getExtractedText() throws InvalidSPDXAnalysisException {
101138
}
102139

103140
/**
104-
* @param text the text to set
141+
* Sets the license text.
142+
* <p> Affects both this object and the underlying store.
143+
* @param text text to associate with the license
105144
* @throws InvalidSPDXAnalysisException
106145
*/
107146
public void setExtractedText(String text) throws InvalidSPDXAnalysisException {

0 commit comments

Comments
 (0)