View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2019 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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") // not a file path
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        // check if related config path is around the "aspectRatios" node of crop plugin
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 }