-
Notifications
You must be signed in to change notification settings - Fork 188
/
AxiomAntlrLiterals.java
80 lines (64 loc) · 3.15 KB
/
AxiomAntlrLiterals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.evolveum.axiom.lang.antlr;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.BooleanValueContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.DoubleQuoteStringContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.FloatValueContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.IntValueContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.LiteralValueContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.MultilineStringContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.NullValueContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.SingleQuoteStringContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.StringLiteralContext;
import com.evolveum.axiom.lang.antlr.query.AxiomQueryParser.StringValueContext;
public class AxiomAntlrLiterals {
public static String convertSingleQuote(String text) {
int stop = text.length();
return text.substring(1, stop - 1);
}
public static String convertDoubleQuote(String text) {
int stop = text.length();
return text.substring(1, stop - 1);
}
public static String convertMultiline(String text) {
return text.replace("\"\"\"", "");
}
public static Object convert(LiteralValueContext value) {
if (value instanceof StringValueContext) {
return convertString((StringValueContext) value);
} else if(value instanceof IntValueContext) {
return convertInteger((IntValueContext) value);
} else if (value instanceof BooleanValueContext) {
return convertBoolean((BooleanValueContext) value);
} else if (value instanceof NullValueContext) {
return convertNull((NullValueContext) value);
} else if (value instanceof FloatValueContext) {
return convertFloat((FloatValueContext) value);
}
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unknown type of literal" + value.getClass());
}
private static Number convertFloat(FloatValueContext value) {
return Double.parseDouble(value.getText());
}
private static String convertString(StringValueContext value) {
return convertString(value.stringLiteral());
}
private static String convertString(StringLiteralContext string) {
if(string instanceof SingleQuoteStringContext) {
return convertSingleQuote(string.getText());
} else if (string instanceof MultilineStringContext) {
return convertMultiline(string.getText());
} else if (string instanceof DoubleQuoteStringContext) {
return convertDoubleQuote(string.getText());
}
throw new UnsupportedOperationException("Unknown String type" + string.getClass());
}
private static Void convertNull(NullValueContext value) {
return null;
}
private static Boolean convertBoolean(BooleanValueContext value) {
return Boolean.parseBoolean(value.getText());
}
private static Number convertInteger(IntValueContext value) {
return Integer.parseInt(value.getText());
}
}