View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2022 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.devops.conga.plugins.aem.maven.model;
21  
22  import java.util.Collections;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.lang3.StringUtils;
29  
30  /**
31   * Helper methods for {@link ModelParser}.
32   */
33  final class ParserUtil {
34  
35    private static final String PROP_VARIANTS = "variants";
36  
37    private ParserUtil() {
38      // static methods only
39    }
40  
41    @SuppressWarnings("unchecked")
42    static List<Map<String, Object>> children(Map<String, Object> data, String propertyName) {
43      List<Map<String, Object>> children = (List<Map<String, Object>>)data.get(propertyName);
44      if (children == null) {
45        return Collections.emptyList();
46      }
47      else {
48        return children;
49      }
50    }
51  
52    @SuppressWarnings("unchecked")
53    static Set<String> toStringSet(Object value) {
54      Set<String> result = new LinkedHashSet<>();
55      if (value instanceof String stringValue) {
56        String target = stringValue;
57        if (StringUtils.isNotBlank(target)) {
58          result.add(target);
59        }
60      }
61      else if (value instanceof List) {
62        result.addAll(((List<String>)value).stream()
63            .filter(StringUtils::isNotBlank)
64            .toList());
65      }
66      else {
67        throw new IllegalArgumentException("Value is neither string nor string list: " + value);
68      }
69      return result;
70    }
71  
72    @SuppressWarnings("unchecked")
73    static List<String> getVariants(Map<String, Object> roleData) {
74      List<String> variants = (List<String>)roleData.get(PROP_VARIANTS);
75      if (variants != null) {
76        return variants;
77      }
78      else {
79        return Collections.emptyList();
80      }
81    }
82  
83  }