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.util;
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.io.FilenameUtils;
27  import org.apache.commons.lang3.StringUtils;
28  
29  import com.google.common.base.CaseFormat;
30  
31  import io.wcm.qa.glnm.maven.freemarker.pojo.SelectorPojo;
32  import io.wcm.qa.glnm.maven.freemarker.pojo.SpecPojo;
33  
34  /**
35   * Utility methods to convert between different casings.
36   *
37   * @since 1.0.0
38   */
39  public final class FormatUtil {
40  
41    private static final String FILE_ENDING_GSPEC = ".gspec";
42    private static final String REGEX_NAME_CLEANING = "\\.";
43  
44    private FormatUtil() {
45      // do not instantiate
46    }
47  
48    /**
49     * <p>getClassName.</p>
50     *
51     * @param file to extract class name from
52     * @return class name build from file name
53     */
54    public static String getClassName(File file) {
55      return getClassName(FilenameUtils.getBaseName(file.getPath()));
56    }
57  
58    /**
59     * <p>getClassName.</p>
60     *
61     * @param selector to extract class name from
62     * @return class name from selectors element name
63     */
64    public static String getClassName(SelectorPojo selector) {
65      String elementName = selector.elementName();
66      String relativeElementName = getRelativeElementName(elementName);
67      String cleanElementName = getCleanElementName(relativeElementName);
68      return kebapToUpperCamel(cleanElementName);
69    }
70  
71    /**
72     * <p>getFullyQualifiedWebElementClassName.</p>
73     *
74     * @param packageName name of package
75     * @param selector to extract class name from
76     * @return class name from selectors element name
77     */
78    public static String getFullyQualifiedWebElementClassName(String packageName, SelectorPojo selector) {
79      return getFullyQualifiedClassName(packageName, selector, "Gwe");
80    }
81  
82    /**
83     * <p>getFullyQualifiedSelectorClassName.</p>
84     *
85     * @param selector to extract class name from
86     * @return class name from selectors element name
87     * @param packageName a {@link java.lang.String} object.
88     */
89    public static String getFullyQualifiedSelectorClassName(String packageName, SelectorPojo selector) {
90      return getFullyQualifiedClassName(packageName, selector, "");
91    }
92  
93    private static String getFullyQualifiedClassName(String packageName, SelectorPojo selector, String classNamePostfix) {
94      List<String> classNames = new ArrayList<String>();
95      String[] elementNameParts = selector.elementName().split("\\.");
96      for (String namePart : elementNameParts) {
97        String cleanName = getCleanElementName(namePart);
98        String className = kebapToUpperCamel(cleanName);
99        classNames.add(className);
100     }
101     String joinedClassNames = StringUtils.join(classNames, classNamePostfix
102         + ".");
103     return packageName + "." + joinedClassNames + classNamePostfix;
104   }
105 
106   /**
107    * <p>getClassName.</p>
108    *
109    * @param specPojo to extract name from
110    * @return class name from spec file
111    */
112   public static String getClassName(SpecPojo specPojo) {
113     return kebapToUpperCamel(specPojo.getBaseName());
114   }
115 
116   /**
117    * <p>getClassName.</p>
118    *
119    * @param string kebap cased string
120    * @return class name formatted string from kebap string
121    */
122   public static String getClassName(String string) {
123     return kebapToUpperCamel(string);
124   }
125 
126   /**
127    * <p>getConstantName.</p>
128    *
129    * @param selector to extract name from
130    * @return selectors element name formatted for use as constant name
131    */
132   public static String getConstantName(SelectorPojo selector) {
133     String elementName = selector.elementName();
134     String relativeElementName = getRelativeElementName(elementName);
135     String cleanElementName = getCleanElementName(relativeElementName);
136     return kebapToConstant(cleanElementName);
137   }
138 
139   /**
140    * <p>getSelectorsPackageName.</p>
141    *
142    * @param packageRoot root package to prepend
143    * @param spec to extract package name from
144    * @return absolute package name built from package root and spec
145    */
146   public static String getSelectorsPackageName(String packageRoot, SpecPojo spec) {
147     StringBuilder stringBuilder = new StringBuilder();
148     stringBuilder.append(packageRoot);
149     String relativePackageName = getPackageName(spec);
150     if (!StringUtils.startsWith(relativePackageName, ".")) {
151       stringBuilder.append(".");
152     }
153     stringBuilder.append(relativePackageName);
154     return stringBuilder.toString();
155   }
156 
157   private static String getCleanElementName(String elementName) {
158     return elementName.replaceAll(REGEX_NAME_CLEANING, "__");
159   }
160 
161   private static String getPackageName(SpecPojo spec) {
162     String relativePath = spec.getRelativeFilePath();
163     String relativePathWithoutExtension = StringUtils.removeEnd(relativePath, FILE_ENDING_GSPEC);
164     String lowerCasePath = relativePathWithoutExtension.toLowerCase().replaceAll("[^a-z0-9/]", "");
165     return lowerCasePath.replaceAll("/", ".");
166   }
167 
168   /**
169    * Format as constant name.
170    * @param input lower hyphen formatted string
171    * @return upper case with underscores version of input string
172    */
173   private static String kebapToConstant(String input) {
174     return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, input);
175   }
176 
177   /**
178    * Format as class name.
179    * @param input lower hyphen formatted string
180    * @return upper camel case version of input string
181    */
182   private static String kebapToUpperCamel(String input) {
183     return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, input);
184   }
185 
186   protected static String getRelativeElementName(String elementName) {
187     return elementName.replaceFirst("^.*\\.", "");
188   }
189 
190 }