Skip to content

Commit fa79757

Browse files
#24 - Added methods for obtaining purl strings containing coordinates only. Deprecated isBaseEquals(). Version bump.
1 parent d1a2001 commit fa79757

3 files changed

Lines changed: 59 additions & 13 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.github.package-url</groupId>
66
<artifactId>packageurl-java</artifactId>
7-
<version>1.3.2-SNAPSHOT</version>
7+
<version>1.4.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>Package URL</name>

src/main/java/com/github/packageurl/PackageURL.java

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ public String toString() {
378378
* @since 1.0.0
379379
*/
380380
public String canonicalize() {
381+
return canonicalize(false);
382+
}
383+
384+
/**
385+
* Returns the canonicalized representation of the purl.
386+
*
387+
* @return the canonicalized representation of the purl
388+
* @since 1.3.2
389+
*/
390+
private String canonicalize(boolean coordinatesOnly) {
381391
if (canonicalizedForm != null) {
382392
return canonicalizedForm;
383393
}
@@ -397,18 +407,20 @@ public String canonicalize() {
397407
if (version != null) {
398408
purl.append("@").append(percentEncode(version));
399409
}
400-
if (qualifiers != null && qualifiers.size() > 0) {
401-
purl.append("?");
402-
qualifiers.entrySet().stream().forEachOrdered((entry) -> {
403-
purl.append(entry.getKey().toLowerCase());
404-
purl.append("=");
405-
purl.append(percentEncode(entry.getValue()));
406-
purl.append("&");
407-
});
408-
purl.setLength(purl.length() - 1);
409-
}
410-
if (subpath != null) {
411-
purl.append("#").append(encodePath(subpath));
410+
if (! coordinatesOnly) {
411+
if (qualifiers != null && qualifiers.size() > 0) {
412+
purl.append("?");
413+
qualifiers.entrySet().stream().forEachOrdered((entry) -> {
414+
purl.append(entry.getKey().toLowerCase());
415+
purl.append("=");
416+
purl.append(percentEncode(entry.getValue()));
417+
purl.append("&");
418+
});
419+
purl.setLength(purl.length() - 1);
420+
}
421+
if (subpath != null) {
422+
purl.append("#").append(encodePath(subpath));
423+
}
412424
}
413425
canonicalizedForm = purl.toString();
414426
return canonicalizedForm;
@@ -594,19 +606,47 @@ private String encodePath(final String path) {
594606
* Evaluates if the specified Package URL has the same values up to, but excluding
595607
* the qualifier (querystring). This includes equivalence of: scheme, type, namespace,
596608
* name, and version, but excludes qualifier and subpath from evaluation.
609+
* @deprecated
610+
* This method is no longer recommended and will be removed from a future release.
611+
* <p> Use {@link PackageURL#isCoordinatesEquals} instead.</p>
597612
*
598613
* @param purl the Package URL to evaluate
599614
* @return true if equivalence passes, false if not
600615
* @since 1.2.0
601616
*/
617+
//@Deprecated(since = "1.4.0", forRemoval = true)
618+
@Deprecated
602619
public boolean isBaseEquals(final PackageURL purl) {
620+
return isCoordinatesEquals(purl);
621+
}
622+
623+
/**
624+
* Evaluates if the specified Package URL has the same values up to, but excluding
625+
* the qualifier (querystring). This includes equivalence of: scheme, type, namespace,
626+
* name, and version, but excludes qualifier and subpath from evaluation.
627+
*
628+
* @param purl the Package URL to evaluate
629+
* @return true if equivalence passes, false if not
630+
* @since 1.4.0
631+
*/
632+
public boolean isCoordinatesEquals(final PackageURL purl) {
603633
return Objects.equals(scheme, purl.scheme) &&
604634
Objects.equals(type, purl.type) &&
605635
Objects.equals(namespace, purl.namespace) &&
606636
Objects.equals(name, purl.name) &&
607637
Objects.equals(version, purl.version);
608638
}
609639

640+
/**
641+
* Returns only the canonicalized coordinates of the Package URL which includes the type, namespace, name,
642+
* and version, and which omits the qualifier and subpath.
643+
* @return A canonicalized PackageURL String excluding the qualifier and subpath.
644+
* @since 1.4.0
645+
*/
646+
public String getCoordinates() {
647+
return canonicalize(true);
648+
}
649+
610650
/**
611651
* Evaluates if the specified Package URL has the same canonical value. This method
612652
* canonicalizes the Package URLs being evaluated and performs an equivalence on the

src/test/java/com/github/packageurl/PackageURLTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,10 @@ public void testCanonicalEquals() throws Exception {
297297
PackageURL p2 = new PackageURL("pkg:generic/acme/example-component@1.0.0?key2=value2&key1=value1");
298298
Assert.assertTrue(p1.isCanonicalEquals(p2));
299299
}
300+
301+
@Test
302+
public void testGetCoordinates() throws Exception {
303+
PackageURL purl = new PackageURL("pkg:generic/acme/example-component@1.0.0?key1=value1&key2=value2");
304+
Assert.assertEquals("pkg:generic/acme/example-component@1.0.0", purl.getCoordinates());
305+
}
300306
}

0 commit comments

Comments
 (0)