View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 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.qa.glnm.galen.specs;
21  
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.commons.collections4.Bag;
26  import org.apache.commons.collections4.bag.HashBag;
27  import org.apache.commons.lang3.StringUtils;
28  
29  import io.wcm.qa.glnm.galen.validation.GalenValidation;
30  import io.wcm.qa.glnm.selectors.base.NestedSelector;
31  
32  /**
33   * Utility methods for parsing Galen specs.
34   *
35   * @since 1.0.0
36   */
37  public final class ParsingUtil {
38  
39    private static final String PATTERN_LINE_WITH_TAGS_IN_SPEC = "@on";
40  
41    private ParsingUtil() {
42      // do not instantiate
43    }
44  
45    /**
46     * Extracts all selectors defined in spec.
47     * @param specPath to parse
48     * @return all defined objects as selectors
49     */
50    public static Collection<NestedSelector> getSelectorsFromSpec(String specPath) {
51      GalenSpec galenSpec = GalenValidation.readSpec(specPath);
52      return galenSpec.getObjects();
53    }
54  
55    /**
56     * <p>
57     * getTags.
58     * </p>
59     * @param specPath to parse
60     * @return all tags used in spec
61     */
62    public static Collection<String> getTags(String specPath) {
63      Bag<String> tags = new HashBag<>();
64      for (String line : GalenParsing.getSourceLines(specPath)) {
65        String trimmedLine = line.trim();
66        if (StringUtils.startsWith(trimmedLine, PATTERN_LINE_WITH_TAGS_IN_SPEC)) {
67          String tagsAsString = StringUtils.removeStart(trimmedLine, PATTERN_LINE_WITH_TAGS_IN_SPEC);
68          String[] tagsAsArray = tagsAsString.split(",");
69          for (String tag : tagsAsArray) {
70            if (StringUtils.isNotBlank(tag)) {
71              String cleanedTag = tag.trim().replaceAll("[^A-Za-z0-9]*", "");
72              if (StringUtils.isNotBlank(cleanedTag)) {
73                tags.add(cleanedTag);
74              }
75            }
76          }
77        }
78      }
79      return tags.uniqueSet();
80    }
81  
82  }