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    /**
33     * Aspect ratios node name
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") // not a file path
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        // check if related config path is around the "aspectRatios" node of crop plugin
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 }