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
33
34
35 public static final String NN_ASPECT_RATIOS = "aspectRatios";
36
37 static final String NN_MEDIA_FORMAT = "wcmio:mediaFormat";
38 static final String NN_CONFIG = "wcmio:config";
39
40 @SuppressWarnings("java:S1075")
41 private static final Pattern PATH_PATTERN = Pattern.compile(
42 "^" + IPEConfigResourceProvider.IPECONFIG_OVERLAY_ROOTPATH + "((/[^/]+)+)"
43 + "/" + NN_MEDIA_FORMAT + "((/[^/]+)+)"
44 + "/" + NN_CONFIG + "(/.*)?$");
45
46 private static final Pattern PLUGINS_CROP_PATH_PATTERN = Pattern.compile(
47 "^.*/plugins/crop(/" + NN_ASPECT_RATIOS + "(/([^/]+))?)?$");
48
49 private String componentContentPath;
50 private String relativeConfigPath;
51 private SortedSet<String> mediaFormatNames;
52
53 private boolean pluginsCropNode;
54 private boolean aspectRatiosNode;
55 private String aspectRatioItemName;
56
57 PathParser(String path) {
58 Matcher matcher = PATH_PATTERN.matcher(path);
59 if (matcher.matches()) {
60 this.componentContentPath = matcher.group(1);
61 String[] names = StringUtils.split(matcher.group(3), "/");
62 this.mediaFormatNames = new TreeSet<>(Arrays.asList(names));
63 this.relativeConfigPath = matcher.group(5);
64
65
66 if (StringUtils.isNotEmpty(this.relativeConfigPath)) {
67 Matcher pluginsCropPathMatcher = PLUGINS_CROP_PATH_PATTERN.matcher(this.relativeConfigPath);
68 if (pluginsCropPathMatcher.matches()) {
69 if (StringUtils.isEmpty(pluginsCropPathMatcher.group(1))) {
70 pluginsCropNode = true;
71 }
72 else if (StringUtils.isEmpty(pluginsCropPathMatcher.group(2))) {
73 aspectRatiosNode = true;
74 }
75 else {
76 aspectRatioItemName = pluginsCropPathMatcher.group(3);
77 }
78 }
79 }
80 }
81 }
82
83 public String getComponentContentPath() {
84 return this.componentContentPath;
85 }
86
87 public String getRelativeConfigPath() {
88 return this.relativeConfigPath;
89 }
90
91 public SortedSet<String> getMediaFormatNames() {
92 return this.mediaFormatNames;
93 }
94
95 public boolean isValid() {
96 return StringUtils.isNotEmpty(this.componentContentPath);
97 }
98
99 public boolean isPluginsCropNode() {
100 return pluginsCropNode;
101 }
102
103 public boolean isAspectRatiosNode() {
104 return aspectRatiosNode;
105 }
106
107 public boolean isAspectRatioItem() {
108 return StringUtils.isNotEmpty(this.aspectRatioItemName);
109 }
110
111 public String getAspectRatioItemName() {
112 return this.aspectRatioItemName;
113 }
114
115 }