|
32 | 32 | import java.util.Collections;
|
33 | 33 | import java.util.Map;
|
34 | 34 | import java.util.Objects;
|
| 35 | +import java.util.StringJoiner; |
35 | 36 | import java.util.TreeMap;
|
36 | 37 | import java.util.function.IntPredicate;
|
37 | 38 | import java.util.stream.Collectors;
|
@@ -421,26 +422,27 @@ private static void validateValue(final String key, final @Nullable String value
|
421 | 422 | return validatePath(value.split("/"), true);
|
422 | 423 | }
|
423 | 424 |
|
424 |
| - private static @Nullable String validatePath(final String[] segments, final boolean isSubPath) throws MalformedPackageURLException { |
| 425 | + private static @Nullable String validatePath(final String[] segments, final boolean isSubpath) throws MalformedPackageURLException { |
425 | 426 | if (segments.length == 0) {
|
426 | 427 | return null;
|
427 | 428 | }
|
428 |
| - try { |
429 |
| - return Arrays.stream(segments) |
430 |
| - .peek(segment -> { |
431 |
| - if (isSubPath && ("..".equals(segment) || ".".equals(segment))) { |
432 |
| - throw new ValidationException("Segments in the subpath may not be a period ('.') or repeated period ('..')"); |
433 |
| - } else if (segment.contains("/")) { |
434 |
| - throw new ValidationException("Segments in the namespace and subpath may not contain a forward slash ('/')"); |
435 |
| - } else if (segment.isEmpty()) { |
436 |
| - throw new ValidationException("Segments in the namespace and subpath may not be empty"); |
437 |
| - } |
438 |
| - }).collect(Collectors.joining("/")); |
439 |
| - } catch (ValidationException e) { |
440 |
| - throw new MalformedPackageURLException(e); |
| 429 | + |
| 430 | + StringJoiner joiner = new StringJoiner("/"); |
| 431 | + |
| 432 | + for (String segment : segments) { |
| 433 | + if (".".equals(segment) || "..".equals(segment)) { |
| 434 | + if (!isSubpath) { |
| 435 | + throw new MalformedPackageURLException("Segments in the namespace must not be a period ('.') or repeated period ('..'): '" + segment + "'"); |
| 436 | + } |
| 437 | + } else if (segment.isEmpty() || segment.contains("/")) { |
| 438 | + throw new MalformedPackageURLException("Segments in the namespace and subpath must not contain a '/' and must not be empty: '" + segment + "'"); |
| 439 | + } else { |
| 440 | + joiner.add(segment); |
| 441 | + } |
441 | 442 | }
|
442 |
| - } |
443 | 443 |
|
| 444 | + return joiner.toString(); |
| 445 | + } |
444 | 446 | /**
|
445 | 447 | * Returns the canonicalized representation of the purl.
|
446 | 448 | *
|
|
0 commit comments