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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
package bjc.utils.ioutils.format;
import bjc.utils.esodata.Tape;
import java.util.ArrayList;
import java.util.IllegalFormatConversionException;
import java.util.List;
import java.util.regex.Matcher;
class ConditionalDirective implements Directive {
@Override
public void format(StringBuffer sb, Object item, CLModifiers mods, CLParameters arrParams,
Tape<Object> formatParams, Matcher dirMatcher, CLFormatter fmt) {
StringBuffer condBody = new StringBuffer();
List<String> clauses = new ArrayList<>();
String defClause = null;
boolean isDefault = false;
while(dirMatcher.find()) {
/* Process a list of clauses. */
String dirName = dirMatcher.group("name");
String dirMods = dirMatcher.group("modifiers");
if(dirName != null) {
/* Append everything up to this directive. */
dirMatcher.appendReplacement(condBody, "");
if(dirName.equals("]")) {
/* End the conditional. */
String clause = condBody.toString();
if(isDefault) {
defClause = clause;
} else {
clauses.add(clause);
}
break;
} else if(dirName.equals(";")) {
/* End the clause. */
String clause = condBody.toString();
if(isDefault) {
defClause = clause;
} else {
clauses.add(clause);
}
/*
* Mark the next clause as the default.
*/
if(dirMods.contains(":")) {
isDefault = true;
}
} else {
/* Not a special directive. */
condBody.append(dirMatcher.group());
}
}
}
Object par = formatParams.item();
if(mods.colonMod) {
formatParams.right();
if(par == null) {
throw new IllegalArgumentException("No parameter provided for [ directive.");
} else if(!(par instanceof Boolean)) {
throw new IllegalFormatConversionException('[', par.getClass());
}
boolean res = (Boolean) par;
String frmt;
if(res)
frmt = clauses.get(1);
else
frmt = clauses.get(0);
fmt.doFormatString(frmt, sb, formatParams);
} else if(mods.atMod) {
if(par == null) {
throw new IllegalArgumentException("No parameter provided for [ directive.");
} else if(!(par instanceof Boolean)) {
throw new IllegalFormatConversionException('[', par.getClass());
}
boolean res = (Boolean) par;
if(res) {
fmt.doFormatString(clauses.get(0), sb, formatParams);
} else {
formatParams.right();
}
} else {
int res;
if(arrParams.length() > 1) {
res = arrParams.getInt(0, "conditional choice", '[');
} else {
if(par == null) {
throw new IllegalArgumentException("No parameter provided for [ directive.");
} else if(!(par instanceof Number)) {
throw new IllegalFormatConversionException('[', par.getClass());
}
res = ((Number) par).intValue();
formatParams.right();
}
if(res < 0 || res > clauses.size()) {
if(defClause != null) fmt.doFormatString(defClause, sb, formatParams);
} else {
fmt.doFormatString(clauses.get(res), sb, formatParams);
}
}
return;
}
}
|