Skip to content

Commit f296f57

Browse files
committed
Rename ImageReferenceParser to Regex
Rename `ImageReferenceParser` to `Regex` and remove state. The regular expressions are now used directly by the `ImageName` and `ImageReference` classes with the values accessed directly from the `Matcher`. See gh-21495
1 parent 9843888 commit f296f57

File tree

7 files changed

+228
-330
lines changed

7 files changed

+228
-330
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageName.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.buildpack.platform.docker.type;
1818

19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
1922
import org.springframework.util.Assert;
2023

2124
/**
@@ -25,11 +28,12 @@
2528
* @author Scott Frederick
2629
* @since 2.3.0
2730
* @see ImageReference
28-
* @see ImageReferenceParser
2931
* @see #of(String)
3032
*/
3133
public class ImageName {
3234

35+
private static final Pattern PATTERN = Regex.IMAGE_NAME.compile();
36+
3337
private static final String DEFAULT_DOMAIN = "docker.io";
3438

3539
private static final String OFFICIAL_REPOSITORY_NAME = "library";
@@ -42,10 +46,10 @@ public class ImageName {
4246

4347
private final String string;
4448

45-
ImageName(String domain, String name) {
46-
Assert.hasText(name, "Name must not be empty");
49+
ImageName(String domain, String path) {
50+
Assert.hasText(path, "Path must not be empty");
4751
this.domain = getDomainOrDefault(domain);
48-
this.name = getNameWithDefaultPath(this.domain, name);
52+
this.name = getNameWithDefaultPath(this.domain, path);
4953
this.string = this.domain + "/" + this.name;
5054
}
5155

@@ -128,8 +132,12 @@ private String getNameWithDefaultPath(String domain, String name) {
128132
*/
129133
public static ImageName of(String value) {
130134
Assert.hasText(value, "Value must not be empty");
131-
ImageReferenceParser parser = ImageReferenceParser.of(value);
132-
return new ImageName(parser.getDomain(), parser.getName());
135+
Matcher matcher = PATTERN.matcher(value);
136+
Assert.isTrue(matcher.matches(),
137+
() -> "Unable to parse name \"" + value + "\". "
138+
+ "Image name must be in the form '[domainHost:port/][path/]name', "
139+
+ "with 'path' and 'name' containing only [a-z0-9][.][_][-]");
140+
return new ImageName(matcher.group("domain"), matcher.group("path"));
133141
}
134142

135143
}

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageReference.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
* @author Scott Frederick
3131
* @since 2.3.0
3232
* @see ImageName
33-
* @see ImageReferenceParser
3433
*/
3534
public final class ImageReference {
3635

37-
private static final String LATEST = "latest";
36+
private static final Pattern PATTERN = Regex.IMAGE_REFERENCE.compile();
37+
38+
private static final Pattern JAR_VERSION_PATTERN = Pattern.compile("^(.*)(\\-\\d+)$");
3839

39-
private static final Pattern TRAILING_VERSION_PATTERN = Pattern.compile("^(.*)(\\-\\d+)$");
40+
private static final String LATEST = "latest";
4041

4142
private final ImageName name;
4243

@@ -182,7 +183,7 @@ public static ImageReference forJarFile(File jarFile) {
182183
}
183184
String name = filename.substring(0, firstDot);
184185
String version = filename.substring(firstDot + 1);
185-
Matcher matcher = TRAILING_VERSION_PATTERN.matcher(name);
186+
Matcher matcher = JAR_VERSION_PATTERN.matcher(name);
186187
if (matcher.matches()) {
187188
name = matcher.group(1);
188189
version = matcher.group(2).substring(1) + "." + version;
@@ -224,9 +225,13 @@ public static ImageReference random(String prefix, int randomLength) {
224225
*/
225226
public static ImageReference of(String value) {
226227
Assert.hasText(value, "Value must not be null");
227-
ImageReferenceParser parser = ImageReferenceParser.of(value);
228-
ImageName name = new ImageName(parser.getDomain(), parser.getName());
229-
return new ImageReference(name, parser.getTag(), parser.getDigest());
228+
Matcher matcher = PATTERN.matcher(value);
229+
Assert.isTrue(matcher.matches(),
230+
() -> "Unable to parse image reference \"" + value + "\". "
231+
+ "Image reference must be in the form '[domainHost:port/][path/]name[:tag][@digest]', "
232+
+ "with 'path' and 'name' containing only [a-z0-9][.][_][-]");
233+
ImageName name = new ImageName(matcher.group("domain"), matcher.group("path"));
234+
return new ImageReference(name, matcher.group("tag"), matcher.group("digest"));
230235
}
231236

232237
/**

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceParser.java

Lines changed: 0 additions & 159 deletions
This file was deleted.

0 commit comments

Comments
 (0)