1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
36
37
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
46 }
47
48
49
50
51
52
53
54 public static String getClassName(File file) {
55 return getClassName(FilenameUtils.getBaseName(file.getPath()));
56 }
57
58
59
60
61
62
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
73
74
75
76
77
78 public static String getFullyQualifiedWebElementClassName(String packageName, SelectorPojo selector) {
79 return getFullyQualifiedClassName(packageName, selector, "Gwe");
80 }
81
82
83
84
85
86
87
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
108
109
110
111
112 public static String getClassName(SpecPojo specPojo) {
113 return kebapToUpperCamel(specPojo.getBaseName());
114 }
115
116
117
118
119
120
121
122 public static String getClassName(String string) {
123 return kebapToUpperCamel(string);
124 }
125
126
127
128
129
130
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
141
142
143
144
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
170
171
172
173 private static String kebapToConstant(String input) {
174 return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, input);
175 }
176
177
178
179
180
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 }