Skip to content

Commit 4933193

Browse files
committed
Prefix number for generated ID's
Fixes #33 Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent 86faccb commit 4933193

6 files changed

Lines changed: 35 additions & 33 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<dependency>
9292
<groupId>org.spdx</groupId>
9393
<artifactId>java-spdx-library</artifactId>
94-
<version>1.1.0</version>
94+
<version>1.1.1</version>
9595
</dependency>
9696
<dependency>
9797
<groupId>org.apache.jena</groupId>

src/main/java/org/spdx/spdxRdfStore/RdfSpdxDocumentModelManager.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class RdfSpdxDocumentModelManager implements IModelStoreLock {
101101
static final Set<String> LISTED_LICENSE_CLASSES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(SpdxConstants.LISTED_LICENSE_URI_CLASSES)));
102102

103103
private static final String HTTPS_LISTED_LICENSE_NAMESPACE_PREFIX = SpdxConstants.LISTED_LICENSE_NAMESPACE_PREFIX.replaceAll("http:", "https:");
104-
104+
105105
/**
106106
* subset of the listed license namespace to be used for matching
107107
*/
@@ -336,17 +336,17 @@ private void checkAddNewId(RDFNode node) {
336336
logger.warn("Possibly ambiguous ID being introduced. "+previous+" is being replaced by "+id);
337337
}
338338
}
339-
Matcher licenseRefMatcher = SpdxConstants.LICENSE_ID_PATTERN_NUMERIC.matcher(id);
339+
Matcher licenseRefMatcher = RdfStore.LICENSE_ID_PATTERN_GENERATED.matcher(id);
340340
if (licenseRefMatcher.matches()) {
341341
checkUpdateLicenseId(licenseRefMatcher);
342342
return;
343343
}
344-
Matcher documentIdMatcher = RdfStore.DOCUMENT_ID_PATTERN_NUMERIC.matcher(id);
344+
Matcher documentIdMatcher = RdfStore.DOCUMENT_ID_PATTERN_GENERATED.matcher(id);
345345
if (documentIdMatcher.matches()) {
346346
checkUpdateNextDocumentId(documentIdMatcher);
347347
return;
348348
}
349-
Matcher spdxIdMatcher = RdfStore.SPDX_ID_PATTERN_NUMERIC.matcher(id);
349+
Matcher spdxIdMatcher = RdfStore.SPDX_ID_PATTERN_GENERATED.matcher(id);
350350
if (spdxIdMatcher.matches()) {
351351
checkUpdateNextSpdxId(spdxIdMatcher);
352352
return;
@@ -818,9 +818,9 @@ private String resourceToId(Resource resource) throws SpdxRdfException {
818818
public String getNextId(IdType idType) throws InvalidSPDXAnalysisException {
819819
switch (idType) {
820820
case Anonymous: return RdfStore.ANON_PREFIX+String.valueOf(model.createResource().getId());
821-
case LicenseRef: return SpdxConstants.NON_STD_LICENSE_ID_PRENUM+String.valueOf(getNextLicenseId());
822-
case DocumentRef: return SpdxConstants.EXTERNAL_DOC_REF_PRENUM+String.valueOf(getNextDocumentId());
823-
case SpdxId: return SpdxConstants.SPDX_ELEMENT_REF_PRENUM+String.valueOf(getNextSpdxId());
821+
case LicenseRef: return SpdxConstants.NON_STD_LICENSE_ID_PRENUM+RdfStore.GENERATED+String.valueOf(getNextLicenseId());
822+
case DocumentRef: return SpdxConstants.EXTERNAL_DOC_REF_PRENUM+RdfStore.GENERATED+String.valueOf(getNextDocumentId());
823+
case SpdxId: return SpdxConstants.SPDX_ELEMENT_REF_PRENUM+RdfStore.GENERATED+String.valueOf(getNextSpdxId());
824824
case ListedLicense: {
825825
logger.error("Can not generate a license ID for a Listed License");
826826
throw new InvalidSPDXAnalysisException("Can not generate a license ID for a Listed License");

src/main/java/org/spdx/spdxRdfStore/RdfStore.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ public class RdfStore implements IModelStore, ISerializableModelStore {
6363

6464
static final Logger logger = LoggerFactory.getLogger(RdfStore.class.getName());
6565

66-
static Pattern DOCUMENT_ID_PATTERN_NUMERIC = Pattern.compile(SpdxConstants.EXTERNAL_DOC_REF_PRENUM+"(\\d+)$");
67-
static Pattern SPDX_ID_PATTERN_NUMERIC = Pattern.compile(SpdxConstants.SPDX_ELEMENT_REF_PRENUM+"(\\d+)$");
66+
static final String GENERATED = "gnrtd";
67+
static Pattern DOCUMENT_ID_PATTERN_GENERATED = Pattern.compile(SpdxConstants.EXTERNAL_DOC_REF_PRENUM+GENERATED+"(\\d+)$");
68+
static Pattern SPDX_ID_PATTERN_GENERATED = Pattern.compile(SpdxConstants.SPDX_ELEMENT_REF_PRENUM+GENERATED+"(\\d+)$");
69+
static Pattern LICENSE_ID_PATTERN_GENERATED = Pattern.compile(SpdxConstants.NON_STD_LICENSE_ID_PRENUM+GENERATED+"(\\d+)$");
6870
static final String ANON_PREFIX = "__anon__";
6971
static Pattern ANON_ID_PATTERN = Pattern.compile(ANON_PREFIX+"(.+)$");
7072
private static final Set<String> LITERAL_VALUE_SET = new HashSet<String>(Arrays.asList(SpdxConstants.LITERAL_VALUES));

src/test/java/org/spdx/library/model/ExternalDocumentRefTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void testGetExternalDocRefByDocNamespace() throws InvalidSPDXAnalysisExce
198198
result = ExternalDocumentRef.getExternalDocRefByDocNamespace(gmo.getModelStore(), gmo.getDocumentUri(),
199199
DOCUMENT_URI1, gmo.getCopyManager());
200200
assertTrue(result.isPresent());
201-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "1", result.get().getId());
201+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd1", result.get().getId());
202202
// test non matching
203203
result = ExternalDocumentRef.getExternalDocRefByDocNamespace(gmo.getModelStore(), gmo.getDocumentUri(),
204204
DOCUMENT_URI2, null);
@@ -207,16 +207,16 @@ public void testGetExternalDocRefByDocNamespace() throws InvalidSPDXAnalysisExce
207207
result = ExternalDocumentRef.getExternalDocRefByDocNamespace(gmo.getModelStore(), gmo.getDocumentUri(),
208208
DOCUMENT_URI2, gmo.getCopyManager());
209209
assertTrue(result.isPresent());
210-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "2", result.get().getId());
210+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd2", result.get().getId());
211211
// test match
212212
result = ExternalDocumentRef.getExternalDocRefByDocNamespace(gmo.getModelStore(), gmo.getDocumentUri(),
213213
DOCUMENT_URI1, null);
214214
assertTrue(result.isPresent());
215-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "1", result.get().getId());
215+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd1", result.get().getId());
216216
result = ExternalDocumentRef.getExternalDocRefByDocNamespace(gmo.getModelStore(), gmo.getDocumentUri(),
217217
DOCUMENT_URI2, gmo.getCopyManager());
218218
assertTrue(result.isPresent());
219-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "2", result.get().getId());
219+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd2", result.get().getId());
220220
}
221221

222222
}

src/test/java/org/spdx/library/model/ExternalSpdxElementTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void testUriToExternalSpdxElementId() throws InvalidSPDXAnalysisException
122122
gmo.getDocumentUri(), null);
123123
assertEquals(expected, result);
124124
uri = DOCURI2 + "#" + SPDXID2;
125-
String generatedDocId = "DocumentRef-1";
125+
String generatedDocId = "DocumentRef-gnrtd1";
126126
expected = generatedDocId + ":" + SPDXID2;
127127
try {
128128
result = ExternalSpdxElement.uriToExternalSpdxElementId(uri, gmo.getModelStore(),

src/test/java/org/spdx/spdxRdfStore/RdfSpdxDocumentModelManagerTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ public void testUpdateNextIds() throws InvalidSPDXAnalysisException {
6767
RdfSpdxDocumentModelManager store = new RdfSpdxDocumentModelManager(TEST_DOCUMENT_URI1, model);
6868
// License ID's
6969
String nextId = store.getNextId(IdType.LicenseRef);
70-
assertEquals("LicenseRef-1", nextId);
71-
store.getOrCreate("LicenseRef-33", SpdxConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO);
70+
assertEquals("LicenseRef-gnrtd1", nextId);
71+
store.getOrCreate("LicenseRef-gnrtd33", SpdxConstants.CLASS_SPDX_EXTRACTED_LICENSING_INFO);
7272
nextId = store.getNextId(IdType.LicenseRef);
73-
assertEquals("LicenseRef-34", nextId);
73+
assertEquals("LicenseRef-gnrtd34", nextId);
7474

7575
// SPDX ID's
7676
nextId = store.getNextId(IdType.SpdxId);
77-
assertEquals("SPDXRef-1", nextId);
78-
store.getOrCreate("SPDXRef-33", SpdxConstants.CLASS_SPDX_FILE);
77+
assertEquals("SPDXRef-gnrtd1", nextId);
78+
store.getOrCreate("SPDXRef-gnrtd33", SpdxConstants.CLASS_SPDX_FILE);
7979
nextId = store.getNextId(IdType.SpdxId);
80-
assertEquals("SPDXRef-34", nextId);
80+
assertEquals("SPDXRef-gnrtd34", nextId);
8181

8282
// Anonymous ID's
8383
nextId = store.getNextId(IdType.Anonymous);
@@ -88,22 +88,22 @@ public void testUpdateNextIds() throws InvalidSPDXAnalysisException {
8888

8989
// Document ID
9090
nextId = store.getNextId(IdType.DocumentRef);
91-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "1", nextId);
92-
store.getOrCreate(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "33", SpdxConstants.CLASS_EXTERNAL_DOC_REF);
91+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd1", nextId);
92+
store.getOrCreate(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd33", SpdxConstants.CLASS_EXTERNAL_DOC_REF);
9393
nextId = store.getNextId(IdType.DocumentRef);
94-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "34", nextId);
94+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd34", nextId);
9595

9696
// test initialization of the next ID's
9797
RdfSpdxDocumentModelManager store2 = new RdfSpdxDocumentModelManager(TEST_DOCUMENT_URI1, model);
9898
nextId = store2.getNextId(IdType.LicenseRef);
99-
assertEquals("LicenseRef-34", nextId);
99+
assertEquals("LicenseRef-gnrtd34", nextId);
100100
nextId = store2.getNextId(IdType.SpdxId);
101-
assertEquals("SPDXRef-34", nextId);
101+
assertEquals("SPDXRef-gnrtd34", nextId);
102102
nextId = store2.getNextId(IdType.Anonymous);
103103
assertTrue(nextId.startsWith(RdfStore.ANON_PREFIX));
104104
assertFalse(nextId.equals(nextNextId));
105105
nextId = store2.getNextId(IdType.DocumentRef);
106-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "34", nextId);
106+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd34", nextId);
107107
}
108108

109109

@@ -236,21 +236,21 @@ public void testGetNextId() throws InvalidSPDXAnalysisException {
236236
RdfSpdxDocumentModelManager store = new RdfSpdxDocumentModelManager(TEST_DOCUMENT_URI1, model);
237237
// License ID's
238238
String nextId = store.getNextId(IdType.LicenseRef);
239-
assertEquals("LicenseRef-1", nextId);
239+
assertEquals("LicenseRef-gnrtd1", nextId);
240240
nextId = store.getNextId(IdType.LicenseRef);
241-
assertEquals("LicenseRef-2", nextId);
241+
assertEquals("LicenseRef-gnrtd2", nextId);
242242

243243
// SPDX ID's
244244
nextId = store.getNextId(IdType.SpdxId);
245-
assertEquals("SPDXRef-1", nextId);
245+
assertEquals("SPDXRef-gnrtd1", nextId);
246246
nextId = store.getNextId(IdType.SpdxId);
247-
assertEquals("SPDXRef-2", nextId);
247+
assertEquals("SPDXRef-gnrtd2", nextId);
248248

249249
// Document ID
250250
nextId = store.getNextId(IdType.DocumentRef);
251-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "1", nextId);
251+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd1", nextId);
252252
nextId = store.getNextId(IdType.DocumentRef);
253-
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "2", nextId);
253+
assertEquals(SpdxConstants.EXTERNAL_DOC_REF_PRENUM + "gnrtd2", nextId);
254254
}
255255

256256
/**

0 commit comments

Comments
 (0)