Skip to content

Commit 85b21d5

Browse files
committed
package-urlGH-188: Implement package type providers
1 parent 8925c06 commit 85b21d5

37 files changed

+1408
-11
lines changed

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.function.IntPredicate;
3737
import java.util.stream.Collectors;
3838
import java.util.stream.IntStream;
39+
40+
import com.github.packageurl.type.PackageTypeFactory;
3941
import org.jspecify.annotations.Nullable;
4042

4143
/**
@@ -99,7 +101,7 @@ public PackageURL(final String type, final String name) throws MalformedPackageU
99101
* @deprecated use {@link #PackageURL(String, String, String, String, Map, String)} instead
100102
*/
101103
@Deprecated
102-
public PackageURL(final String type, final @Nullable String namespace, final String name, final @Nullable String version,
104+
public PackageURL(final @Nullable String type, final @Nullable String namespace, final @Nullable String name, final @Nullable String version,
103105
final @Nullable TreeMap<String, String> qualifiers, final @Nullable String subpath)
104106
throws MalformedPackageURLException {
105107
this.type = toLowerCase(validateType(requireNonNull(type, "type")));
@@ -108,7 +110,8 @@ public PackageURL(final String type, final @Nullable String namespace, final Str
108110
this.version = validateVersion(type, version);
109111
this.qualifiers = parseQualifiers(qualifiers);
110112
this.subpath = validateSubpath(subpath);
111-
verifyTypeConstraints(this.type, this.namespace, this.name);
113+
//verifyTypeConstraints(this.type, this.namespace, this.name);
114+
PackageTypeFactory.getInstance().validateComponents(type, namespace, name, version, qualifiers, subpath);
112115
}
113116

114117
/**
@@ -124,7 +127,7 @@ public PackageURL(final String type, final @Nullable String namespace, final Str
124127
* @throws NullPointerException if {@code type} or {@code name} are {@code null}
125128
* @since 1.6.0
126129
*/
127-
public PackageURL(final String type, final @Nullable String namespace, final String name, final @Nullable String version,
130+
public PackageURL(final @Nullable String type, final @Nullable String namespace, final @Nullable String name, final @Nullable String version,
128131
final @Nullable Map<String, @Nullable String> qualifiers, final @Nullable String subpath)
129132
throws MalformedPackageURLException {
130133
this(type, namespace, name, version, (qualifiers != null) ? new TreeMap<>(qualifiers) : null, subpath);
@@ -279,11 +282,11 @@ private static String validateType(final String value) throws MalformedPackageUR
279282
return value;
280283
}
281284

282-
private static boolean isValidCharForType(int c) {
285+
public static boolean isValidCharForType(int c) {
283286
return (isAlphaNumeric(c) || c == '.' || c == '+' || c == '-');
284287
}
285288

286-
private static boolean isValidCharForKey(int c) {
289+
public static boolean isValidCharForKey(int c) {
287290
return (isAlphaNumeric(c) || c == '.' || c == '_' || c == '-');
288291
}
289292

@@ -441,6 +444,10 @@ private static void validateValue(final String key, final @Nullable String value
441444
}
442445
}
443446

447+
public PackageURL normalize() throws MalformedPackageURLException {
448+
return PackageTypeFactory.getInstance().normalizeComponents(type, namespace, name, version, qualifiers, subpath);
449+
}
450+
444451
/**
445452
* Returns the canonicalized representation of the purl.
446453
*
@@ -469,6 +476,17 @@ public String canonicalize() {
469476
* @since 1.3.2
470477
*/
471478
private String canonicalize(boolean coordinatesOnly) {
479+
try {
480+
PackageURL packageURL = normalize();
481+
namespace = packageURL.getNamespace();
482+
name = packageURL.getName();
483+
version = packageURL.getVersion();
484+
qualifiers = packageURL.getQualifiers();
485+
subpath = packageURL.getSubpath();
486+
} catch (MalformedPackageURLException e) {
487+
throw new ValidationException("Normalization failed", e);
488+
}
489+
472490
final StringBuilder purl = new StringBuilder();
473491
purl.append(SCHEME_PART).append(type).append("/");
474492
if (namespace != null) {
@@ -505,18 +523,22 @@ private static boolean shouldEncode(int c) {
505523
return !isUnreserved(c);
506524
}
507525

508-
private static boolean isAlpha(int c) {
526+
public static boolean isAlpha(int c) {
509527
return (isLowerCase(c) || isUpperCase(c));
510528
}
511529

512530
private static boolean isDigit(int c) {
513531
return (c >= '0' && c <= '9');
514532
}
515533

516-
private static boolean isAlphaNumeric(int c) {
534+
public static boolean isAlphaNumeric(int c) {
517535
return (isDigit(c) || isAlpha(c));
518536
}
519537

538+
public static boolean isWhitespace(int c) {
539+
return (c == ' ' || c == '\t' || c == '\r' || c == '\n');
540+
}
541+
520542
private static boolean isUpperCase(int c) {
521543
return (c >= 'A' && c <= 'Z');
522544
}
@@ -541,7 +563,7 @@ private static int toLowerCase(int c) {
541563
return (c ^ 0x20);
542564
}
543565

544-
private static String toLowerCase(String s) {
566+
public static String toLowerCase(String s) {
545567
int pos = indexOfFirstUpperCaseChar(s);
546568

547569
if (pos == -1) {
@@ -770,7 +792,7 @@ private void parse(final String purl) throws MalformedPackageURLException {
770792
remainder = remainder.substring(0, index);
771793
this.namespace = validateNamespace(parsePath(remainder.substring(start), false));
772794
}
773-
verifyTypeConstraints(this.type, this.namespace, this.name);
795+
//verifyTypeConstraints(this.type, this.namespace, this.name);
774796
} catch (URISyntaxException e) {
775797
throw new MalformedPackageURLException("Invalid purl: " + e.getMessage(), e);
776798
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ class ValidationException extends RuntimeException {
3838
ValidationException(String msg) {
3939
super(msg);
4040
}
41+
42+
ValidationException(String msg, Throwable cause) {
43+
super(msg, cause);
44+
}
4145
}

src/main/java/com/github/packageurl/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
@NullMarked
2727
package com.github.packageurl;
2828

29-
import org.jspecify.annotations.NullMarked;
29+
import org.jspecify.annotations.NullMarked;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.github.packageurl.type;
23+
24+
public class ApkPackageTypeProvider extends LowercaseNamespaceAndNameTypeProvider {
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.github.packageurl.type;
23+
24+
public class BitbucketPackageTypeProvider extends LowercaseNamespaceAndNameTypeProvider {
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.github.packageurl.type;
23+
24+
public class BitnamiPackageTypeProvider extends LowercaseNamespacePackageTypeProvider {
25+
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.github.packageurl.type;
23+
24+
import com.github.packageurl.MalformedPackageURLException;
25+
import com.github.packageurl.PackageURL;
26+
import org.jspecify.annotations.NonNull;
27+
import org.jspecify.annotations.Nullable;
28+
29+
import java.util.Map;
30+
31+
public class CocoapodsPackageTypeProvider implements PackageTypeProvider {
32+
@Override
33+
public void validateComponents(@NonNull String type, @Nullable String namespace, @NonNull String name, @Nullable String version, @Nullable Map<String, String> qualifiers, @Nullable String subpath) throws MalformedPackageURLException {
34+
if (namespace != null && !namespace.isEmpty()) {
35+
throw new MalformedPackageURLException("invalid cocoapods purl cannot have a namespace");
36+
}
37+
38+
if (name.chars().anyMatch(PackageURL::isWhitespace) || name.startsWith(".") || name.contains("+")) {
39+
throw new MalformedPackageURLException("invalid cocoapods purl invalid name");
40+
}
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.github.packageurl.type;
23+
24+
public class ComposerPackageTypeProvider extends LowercaseNamespaceAndNameTypeProvider {
25+
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.github.packageurl.type;
23+
24+
import com.github.packageurl.MalformedPackageURLException;
25+
import org.jspecify.annotations.NonNull;
26+
import org.jspecify.annotations.Nullable;
27+
28+
import java.util.Map;
29+
30+
public class ConanPackageTypeProvider implements PackageTypeProvider {
31+
@Override
32+
public void validateComponents(@NonNull String type, @Nullable String namespace, @NonNull String name, @Nullable String version, @Nullable Map<String, String> qualifiers, @Nullable String subpath) throws MalformedPackageURLException {
33+
boolean hasChannel = (qualifiers != null && !qualifiers.isEmpty());
34+
35+
if ((namespace != null && !namespace.isEmpty()) && !hasChannel) {
36+
throw new MalformedPackageURLException("invalid conan purl only namespace");
37+
} else if ((namespace == null || namespace.isEmpty()) && hasChannel) {
38+
throw new MalformedPackageURLException("invalid conan purl only channel qualifier");
39+
}
40+
}
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package com.github.packageurl.type;
23+
24+
import com.github.packageurl.MalformedPackageURLException;
25+
import org.jspecify.annotations.NonNull;
26+
import org.jspecify.annotations.Nullable;
27+
28+
import java.util.Map;
29+
30+
public class CpanPackageTypeProvider implements PackageTypeProvider {
31+
@Override
32+
public void validateComponents(@NonNull String type, @Nullable String namespace, @NonNull String name, @Nullable String version, @Nullable Map<String, String> qualifiers, @Nullable String subpath) throws MalformedPackageURLException {
33+
if ((namespace == null || namespace.isEmpty()) && name.indexOf('-') != -1) {
34+
throw new MalformedPackageURLException("cpan module name like distribution name");
35+
} else if ((namespace != null && !namespace.isEmpty()) && name.contains("::")) {
36+
throw new MalformedPackageURLException("cpan distribution name like module name");
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)