1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.handler.media.impl.ipeconfig;
21
22 import java.util.Arrays;
23 import java.util.SortedSet;
24 import java.util.TreeSet;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.apache.commons.lang3.StringUtils;
29
30 class PathParser {
31
32 public static final String NN_ASPECT_RATIOS = "aspectRatios";
33 static final String NN_MEDIA_FORMAT = "wcmio:mediaFormat";
34 static final String NN_CONFIG = "wcmio:config";
35
36 @SuppressWarnings("java:S1075")
37 private static final Pattern PATH_PATTERN = Pattern.compile(
38 "^" + IPEConfigResourceProvider.IPECONFIG_OVERLAY_ROOTPATH + "((/[^/]+)+)"
39 + "/" + NN_MEDIA_FORMAT + "((/[^/]+)+)"
40 + "/" + NN_CONFIG + "(/.*)?$");
41
42 private static final Pattern PLUGINS_CROP_PATH_PATTERN = Pattern.compile(
43 "^.*/plugins/crop(/" + NN_ASPECT_RATIOS + "(/([^/]+))?)?$");
44
45 private String componentContentPath;
46 private String relativeConfigPath;
47 private SortedSet<String> mediaFormatNames;
48
49 private boolean pluginsCropNode;
50 private boolean aspectRatiosNode;
51 private String aspectRatioItemName;
52
53 PathParser(String path) {
54 Matcher matcher = PATH_PATTERN.matcher(path);
55 if (matcher.matches()) {
56 this.componentContentPath = matcher.group(1);
57 String[] names = StringUtils.split(matcher.group(3), "/");
58 this.mediaFormatNames = new TreeSet<>(Arrays.asList(names));
59 this.relativeConfigPath = matcher.group(5);
60
61
62 if (StringUtils.isNotEmpty(this.relativeConfigPath)) {
63 Matcher pluginsCropPathMatcher = PLUGINS_CROP_PATH_PATTERN.matcher(this.relativeConfigPath);
64 if (pluginsCropPathMatcher.matches()) {
65 if (StringUtils.isEmpty(pluginsCropPathMatcher.group(1))) {
66 pluginsCropNode = true;
67 }
68 else if (StringUtils.isEmpty(pluginsCropPathMatcher.group(2))) {
69 aspectRatiosNode = true;
70 }
71 else {
72 aspectRatioItemName = pluginsCropPathMatcher.group(3);
73 }
74 }
75 }
76 }
77 }
78
79 public String getComponentContentPath() {
80 return this.componentContentPath;
81 }
82
83 public String getRelativeConfigPath() {
84 return this.relativeConfigPath;
85 }
86
87 public SortedSet<String> getMediaFormatNames() {
88 return this.mediaFormatNames;
89 }
90
91 public boolean isValid() {
92 return StringUtils.isNotEmpty(this.componentContentPath);
93 }
94
95 public boolean isPluginsCropNode() {
96 return pluginsCropNode;
97 }
98
99 public boolean isAspectRatiosNode() {
100 return aspectRatiosNode;
101 }
102
103 public boolean isAspectRatioItem() {
104 return StringUtils.isNotEmpty(this.aspectRatioItemName);
105 }
106
107 public String getAspectRatioItemName() {
108 return this.aspectRatioItemName;
109 }
110
111 }