Skip to content

Commit 3657a4b

Browse files
author
Bob Garner
committed
Fixed issue with prompt returning null instead of empty string. Also added "path" support for "is" filter so a template can check if a string is a path. Also enhanced ECStringUtil test case.
1 parent d9790cd commit 3657a4b

File tree

5 files changed

+86
-50
lines changed

5 files changed

+86
-50
lines changed

src/main/java/org/entityc/compiler/cmdline/command/CLBuild.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.entityc.compiler.repository.RepositoryFile;
2121
import org.entityc.compiler.util.ECLog;
2222

23+
import java.io.File;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -42,6 +43,9 @@ public void run(String[] args) {
4243
}
4344

4445
String configurationName = args[0];
46+
if (configurationName.contains(File.separator)) {
47+
ECLog.logFatal("The specified configuration name is not a valid configuration name: " + configurationName);
48+
}
4549
List<String> defines = new ArrayList<>();
4650
boolean quietMode = false;
4751

src/main/java/org/entityc/compiler/transform/template/tree/FTPrompt.java

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.entityc.compiler.model.entity.MTNativeType;
2020
import org.entityc.compiler.transform.template.TemplateLexer;
2121
import org.entityc.compiler.transform.template.formatter.TemplateFormatController;
22-
import org.entityc.compiler.util.ECLog;
2322

2423
import java.text.DateFormat;
2524
import java.text.ParseException;
@@ -63,6 +62,42 @@ public FTPrompt(ParserRuleContext ctx, FTContainerNode parent,
6362
this.nativeType = nativeType;
6463
}
6564

65+
public static boolean isInteger(String strNum) {
66+
if (strNum == null) {
67+
return false;
68+
}
69+
try {
70+
double d = Integer.parseInt(strNum);
71+
} catch (NumberFormatException nfe) {
72+
return false;
73+
}
74+
return true;
75+
}
76+
77+
public static boolean isFloat(String strNum) {
78+
if (strNum == null) {
79+
return false;
80+
}
81+
try {
82+
double d = Double.parseDouble(strNum);
83+
} catch (NumberFormatException nfe) {
84+
return false;
85+
}
86+
return true;
87+
}
88+
89+
public static boolean isDouble(String strNum) {
90+
if (strNum == null) {
91+
return false;
92+
}
93+
try {
94+
double d = Double.parseDouble(strNum);
95+
} catch (NumberFormatException nfe) {
96+
return false;
97+
}
98+
return true;
99+
}
100+
66101
@Override
67102
public FTBody getBody() {
68103
return body;
@@ -89,7 +124,8 @@ public boolean format(TemplateFormatController formatController, int indentLevel
89124
formatController.addInstructionInside(InstructionArgument, variableName, this.getStartLineNumber());
90125
if (nativeType != null) {
91126
formatController.addInstructionInside(InstructionArgumentDelim, ":", this.getStartLineNumber());
92-
formatController.addInstructionInside(InstructionArgument, nativeType.getDataType().getName(), this.getStartLineNumber());
127+
formatController.addInstructionInside(InstructionArgument, nativeType.getDataType().getName(),
128+
this.getStartLineNumber());
93129
}
94130
}
95131
formatController.addInstructionEnd(this);
@@ -103,42 +139,6 @@ public boolean hasOwnBody() {
103139
return true;
104140
}
105141

106-
public static boolean isInteger(String strNum) {
107-
if (strNum == null) {
108-
return false;
109-
}
110-
try {
111-
double d = Integer.parseInt(strNum);
112-
} catch (NumberFormatException nfe) {
113-
return false;
114-
}
115-
return true;
116-
}
117-
118-
public static boolean isFloat(String strNum) {
119-
if (strNum == null) {
120-
return false;
121-
}
122-
try {
123-
double d = Double.parseDouble(strNum);
124-
} catch (NumberFormatException nfe) {
125-
return false;
126-
}
127-
return true;
128-
}
129-
130-
public static boolean isDouble(String strNum) {
131-
if (strNum == null) {
132-
return false;
133-
}
134-
try {
135-
double d = Double.parseDouble(strNum);
136-
} catch (NumberFormatException nfe) {
137-
return false;
138-
}
139-
return true;
140-
}
141-
142142
public void prompt(FTTransformSession session) {
143143
String text = body.getText();
144144
boolean asking = true;
@@ -148,7 +148,11 @@ public void prompt(FTTransformSession session) {
148148
Scanner scanner = new Scanner(System.in);
149149
String value = scanner.nextLine();
150150
if (value.trim().length() == 0) {
151-
value = session.getValue(variableName).toString();
151+
if (session.getValue(variableName) == null) {
152+
value = "";
153+
} else {
154+
value = session.getValue(variableName).toString();
155+
}
152156
}
153157
try {
154158
switch (nativeType.getDataType()) {
@@ -175,7 +179,8 @@ public void prompt(FTTransformSession session) {
175179
session.setValue(variableName, Boolean.parseBoolean(value));
176180
} else {
177181
System.err.println(
178-
"ERROR: Invalid boolean value \"" + value + "\" must be \"true\", \"false\", \"yes\", or \"no\".");
182+
"ERROR: Invalid boolean value \"" + value
183+
+ "\" must be \"true\", \"false\", \"yes\", or \"no\".");
179184
asking = true;
180185
}
181186
break;

src/main/java/org/entityc/compiler/transform/template/tree/filter/FTIsFilter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public FTIsFilter() {
3434
private enum KindOfThing {
3535
IDENTIFIER("identifier"),
3636
NAMESPACE("namespace"),
37+
PATH("path"),
3738
CAPITALIZED("capitalized"),
3839
UNCAPITALIZED("uncapitalized"),
3940
;
@@ -89,6 +90,8 @@ public Object filter(ParserRuleContext ctx, FTTransformSession session, Object i
8990
return ECStringUtil.IsIdentifier(stringValue);
9091
case NAMESPACE:
9192
return ECStringUtil.IsNamespace(stringValue);
93+
case PATH:
94+
return ECStringUtil.IsPath(stringValue);
9295
case CAPITALIZED:
9396
return ECStringUtil.IsCapitalized(stringValue);
9497
case UNCAPITALIZED:

src/main/java/org/entityc/compiler/util/ECStringUtil.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static String ProcessParserString(String inputString) {
105105
* When reading in text with double quotes, before they can be written out they need to first
106106
* be double escaped so the end result is a single escaped double quote - just like the very original
107107
* input text that was parsed.
108+
*
108109
* @param inputString The input string with double quotes.
109110
* @return Double escaped double quoted string.
110111
*/
@@ -306,6 +307,24 @@ public static String GetClassNameFromFullName(String fullTypeName) {
306307
return shortName;
307308
}
308309

310+
public static boolean IsNamespace(String text) {
311+
if (text == null || text.length() == 0) {
312+
return false;
313+
}
314+
for (int i = 1; i < text.length(); i++) {
315+
char ch = text.charAt(i);
316+
if (!(Character.isLetterOrDigit(ch) || ch == '.')) {
317+
return false;
318+
}
319+
}
320+
for (String seg : text.split("\\.")) {
321+
if (!IsIdentifier(seg)) {
322+
return false;
323+
}
324+
}
325+
return true;
326+
}
327+
309328
public static boolean IsIdentifier(String text) {
310329
//ECLog.logFatal("Checking if identifier: \"" + text + "\"...");
311330
if (text == null || text.length() == 0) {
@@ -325,18 +344,13 @@ public static boolean IsIdentifier(String text) {
325344
return true;
326345
}
327346

328-
public static boolean IsNamespace(String text) {
347+
public static boolean IsPath(String text) {
329348
if (text == null || text.length() == 0) {
330-
return false;
349+
return true;
331350
}
332-
for (int i = 1; i < text.length(); i++) {
351+
for (int i = 0; i < text.length(); i++) {
333352
char ch = text.charAt(i);
334-
if (!(Character.isLetterOrDigit(ch) || ch == '.')) {
335-
return false;
336-
}
337-
}
338-
for (String seg : text.split("\\.")) {
339-
if (!IsIdentifier(seg)) {
353+
if (!(Character.isLetterOrDigit(ch) || ch == '-' || ch == '/')) {
340354
return false;
341355
}
342356
}
@@ -359,7 +373,7 @@ public static boolean IsUncapitalized(String text) {
359373

360374
public static String RepeatString(String str, int times) {
361375
StringBuilder sb = new StringBuilder();
362-
for (int i=0;i<times;i++) {
376+
for (int i = 0; i < times; i++) {
363377
sb.append(str);
364378
}
365379
return sb.toString();

test/java/org/entityc/compiler/util/ECStringUtilTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ void stripQuotes() {
3535
void doubleEscapeDoubleQuotes() {
3636
assertEquals("\\\\\"DoubleQuotes\\\\\"", ECStringUtil.DoubleEscapeDoubleQuotes("\"DoubleQuotes\""));
3737
}
38+
39+
@Test
40+
void isPath() {
41+
assertEquals(true, ECStringUtil.IsPath(""));
42+
assertEquals(true, ECStringUtil.IsPath("this-is-ok"));
43+
assertEquals(true, ECStringUtil.IsPath("this/is-ok"));
44+
assertEquals(true, ECStringUtil.IsPath("this/is/ok"));
45+
assertEquals(false, ECStringUtil.IsPath("this-os&bad"));
46+
assertEquals(false, ECStringUtil.IsPath("this/is+bad"));
47+
}
3848
@Test
3949
void stripLineEnd() {
4050
assertEquals(ECStringUtil.StripLineEnd("WithoutCR\n"), "WithoutCR");

0 commit comments

Comments
 (0)