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.maven.freemarker.pojo;
21  
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  
26  import org.apache.commons.io.FilenameUtils;
27  
28  import io.wcm.qa.glnm.galen.specs.ParsingUtil;
29  import io.wcm.qa.glnm.maven.freemarker.util.FormatUtil;
30  import io.wcm.qa.glnm.selectors.base.NestedSelector;
31  
32  /**
33   * Holds data from Galen spec to use in generation.
34   *
35   * @since 1.0.0
36   */
37  public class SpecPojo {
38  
39    private Collection<SelectorPojo> selectors;
40    private String specPath;
41  
42    /**
43     * <p>Constructor for SpecPojo.</p>
44     *
45     * @param specPath to retrieve data from
46     */
47    public SpecPojo(String specPath) {
48      setSpecPath(specPath);
49    }
50  
51    /**
52     * <p>getBaseName.</p>
53     *
54     * @return a {@link java.lang.String} object.
55     */
56    public String getBaseName() {
57      return FilenameUtils.getBaseName(getFilename());
58    }
59  
60    /**
61     * <p>getClassName.</p>
62     *
63     * @return class name extracted from spec file
64     */
65    public String getClassName() {
66      return FormatUtil.getClassName(getFilename());
67    }
68  
69    /**
70     * <p>getFilename.</p>
71     *
72     * @return spec file name
73     */
74    public String getFilename() {
75      return FilenameUtils.getBaseName(getSpecPath());
76    }
77  
78    /**
79     * <p>getPackageNamePart.</p>
80     *
81     * @return package name from spec file path
82     */
83    public String getPackageNamePart() {
84      String baseName = getBaseName();
85      return baseName.toLowerCase().replaceAll("[^a-z0-9]", "");
86    }
87  
88    /**
89     * <p>getRelativeFilePath.</p>
90     *
91     * @return relative path to Galen spec file
92     */
93    public String getRelativeFilePath() {
94      return getUnixStyleFilePath(getSpecPath());
95    }
96  
97    /**
98     * <p>getRootSelectors.</p>
99     *
100    * @return root objects from Galen spec
101    */
102   public Collection<SelectorPojo> getRootSelectors() {
103     Collection<SelectorPojo> rootSelectors = new ArrayList<>();
104     for (NestedSelector selector : getSelectors()) {
105       if (!selector.hasParent()) {
106         rootSelectors.add(new SelectorPojo(this, selector));
107       }
108     }
109     return rootSelectors;
110   }
111 
112   /**
113    * <p>Getter for the field <code>selectors</code>.</p>
114    *
115    * @return all selectors from spec
116    */
117   public Collection<SelectorPojo> getSelectors() {
118     if (selectors == null) {
119       selectors = new ArrayList<SelectorPojo>();
120       for (NestedSelector selector : ParsingUtil.getSelectorsFromSpec(getSpecPath())) {
121         selectors.add(new SelectorPojo(this, selector));
122       }
123     }
124     return selectors;
125   }
126 
127   /**
128    * <p>Getter for the field <code>specFile</code>.</p>
129    *
130    * @return a {@link java.io.File} object.
131    */
132   public String getSpecPath() {
133     return specPath;
134   }
135 
136   /**
137    * <p>Getter for the field <code>tags</code>.</p>
138    *
139    * @return tags used in Galen spec
140    */
141   public Collection<String> getTags() {
142     return ParsingUtil.getTags(getSpecPath());
143   }
144 
145   private void setSpecPath(String specPath) {
146     this.specPath = specPath;
147   }
148 
149   private static String getUnixStyleFilePath(String path) {
150     return FilenameUtils.normalize(path, true);
151   }
152 
153 }