1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.script; |
16 |
| |
17 |
| import org.apache.hivemind.Location; |
18 |
| import org.apache.tapestry.util.xml.BaseRule; |
19 |
| import org.apache.tapestry.util.xml.RuleDirectedParser; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| abstract class AbstractTokenRule extends BaseRule |
32 |
| { |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
19
| protected void addToParent(RuleDirectedParser parser, IScriptToken token)
|
38 |
| { |
39 |
19
| IScriptToken parent = (IScriptToken) parser.peek();
|
40 |
| |
41 |
19
| parent.addToken(token);
|
42 |
| } |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
22
| public void content(RuleDirectedParser parser, String content)
|
51 |
| { |
52 |
22
| IScriptToken token = (IScriptToken) parser.peek();
|
53 |
| |
54 |
22
| addTextTokens(token, content, parser.getLocation());
|
55 |
| } |
56 |
| |
57 |
| private static final int STATE_START = 0; |
58 |
| private static final int STATE_DOLLAR = 1; |
59 |
| private static final int STATE_COLLECT_EXPRESSION = 2; |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
22
| protected void addTextTokens(IScriptToken token, String text, Location location)
|
65 |
| { |
66 |
22
| char[] buffer = text.toCharArray();
|
67 |
22
| int state = STATE_START;
|
68 |
22
| int blockStart = 0;
|
69 |
22
| int blockLength = 0;
|
70 |
22
| int expressionStart = -1;
|
71 |
22
| int expressionLength = 0;
|
72 |
22
| int i = 0;
|
73 |
22
| int braceDepth = 0;
|
74 |
| |
75 |
22
| while (i < buffer.length)
|
76 |
| { |
77 |
290
| char ch = buffer[i];
|
78 |
| |
79 |
290
| switch (state)
|
80 |
| { |
81 |
215
| case STATE_START :
|
82 |
| |
83 |
215
| if (ch == '$')
|
84 |
| { |
85 |
8
| state = STATE_DOLLAR;
|
86 |
8
| i++;
|
87 |
8
| continue;
|
88 |
| } |
89 |
| |
90 |
207
| blockLength++;
|
91 |
207
| i++;
|
92 |
207
| continue;
|
93 |
| |
94 |
7
| case STATE_DOLLAR :
|
95 |
| |
96 |
7
| if (ch == '{')
|
97 |
| { |
98 |
6
| state = STATE_COLLECT_EXPRESSION;
|
99 |
6
| i++;
|
100 |
| |
101 |
6
| expressionStart = i;
|
102 |
6
| expressionLength = 0;
|
103 |
6
| braceDepth = 1;
|
104 |
| |
105 |
6
| continue;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
1
| blockLength++;
|
112 |
| |
113 |
1
| state = STATE_START;
|
114 |
1
| continue;
|
115 |
| |
116 |
68
| case STATE_COLLECT_EXPRESSION :
|
117 |
| |
118 |
68
| if (ch != '}')
|
119 |
| { |
120 |
62
| if (ch == '{')
|
121 |
1
| braceDepth++;
|
122 |
| |
123 |
62
| i++;
|
124 |
62
| expressionLength++;
|
125 |
62
| continue;
|
126 |
| } |
127 |
| |
128 |
6
| braceDepth--;
|
129 |
| |
130 |
6
| if (braceDepth > 0)
|
131 |
| { |
132 |
1
| i++;
|
133 |
1
| expressionLength++;
|
134 |
1
| continue;
|
135 |
| } |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
5
| if (expressionLength == 0)
|
142 |
1
| blockLength += 3;
|
143 |
| |
144 |
5
| if (blockLength > 0)
|
145 |
5
| token.addToken(constructStatic(text, blockStart, blockLength, location));
|
146 |
| |
147 |
5
| if (expressionLength > 0)
|
148 |
| { |
149 |
4
| String expression =
|
150 |
| text.substring(expressionStart, expressionStart + expressionLength); |
151 |
| |
152 |
4
| token.addToken(new InsertToken(expression, location));
|
153 |
| } |
154 |
| |
155 |
5
| i++;
|
156 |
5
| blockStart = i;
|
157 |
5
| blockLength = 0;
|
158 |
| |
159 |
| |
160 |
| |
161 |
5
| state = STATE_START;
|
162 |
| |
163 |
5
| continue;
|
164 |
| } |
165 |
| |
166 |
| } |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
22
| if (state == STATE_DOLLAR)
|
172 |
1
| blockLength++;
|
173 |
| |
174 |
22
| if (state == STATE_COLLECT_EXPRESSION)
|
175 |
1
| blockLength += expressionLength + 2;
|
176 |
| |
177 |
22
| if (blockLength > 0)
|
178 |
21
| token.addToken(constructStatic(text, blockStart, blockLength, location));
|
179 |
| } |
180 |
| |
181 |
26
| private IScriptToken constructStatic(
|
182 |
| String text, |
183 |
| int blockStart, |
184 |
| int blockLength, |
185 |
| Location location) |
186 |
| { |
187 |
26
| String literal = text.substring(blockStart, blockStart + blockLength);
|
188 |
| |
189 |
26
| return new StaticToken(literal, location);
|
190 |
| } |
191 |
| } |