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 |
38
| protected void addToParent(RuleDirectedParser parser, IScriptToken token)
|
38 |
| { |
39 |
38
| IScriptToken parent = (IScriptToken) parser.peek();
|
40 |
| |
41 |
38
| parent.addToken(token);
|
42 |
| } |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
44
| public void content(RuleDirectedParser parser, String content)
|
51 |
| { |
52 |
44
| IScriptToken token = (IScriptToken) parser.peek();
|
53 |
| |
54 |
44
| 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 |
44
| protected void addTextTokens(IScriptToken token, String text, Location location)
|
65 |
| { |
66 |
44
| char[] buffer = text.toCharArray();
|
67 |
44
| int state = STATE_START;
|
68 |
44
| int blockStart = 0;
|
69 |
44
| int blockLength = 0;
|
70 |
44
| int expressionStart = -1;
|
71 |
44
| int expressionLength = 0;
|
72 |
44
| int i = 0;
|
73 |
44
| int braceDepth = 0;
|
74 |
| |
75 |
44
| while (i < buffer.length)
|
76 |
| { |
77 |
580
| char ch = buffer[i];
|
78 |
| |
79 |
580
| switch (state)
|
80 |
| { |
81 |
430
| case STATE_START :
|
82 |
| |
83 |
430
| if (ch == '$')
|
84 |
| { |
85 |
16
| state = STATE_DOLLAR;
|
86 |
16
| i++;
|
87 |
16
| continue;
|
88 |
| } |
89 |
| |
90 |
414
| blockLength++;
|
91 |
414
| i++;
|
92 |
414
| continue;
|
93 |
| |
94 |
14
| case STATE_DOLLAR :
|
95 |
| |
96 |
14
| if (ch == '{')
|
97 |
| { |
98 |
12
| state = STATE_COLLECT_EXPRESSION;
|
99 |
12
| i++;
|
100 |
| |
101 |
12
| expressionStart = i;
|
102 |
12
| expressionLength = 0;
|
103 |
12
| braceDepth = 1;
|
104 |
| |
105 |
12
| continue;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
2
| blockLength++;
|
112 |
| |
113 |
2
| state = STATE_START;
|
114 |
2
| continue;
|
115 |
| |
116 |
136
| case STATE_COLLECT_EXPRESSION :
|
117 |
| |
118 |
136
| if (ch != '}')
|
119 |
| { |
120 |
124
| if (ch == '{')
|
121 |
2
| braceDepth++;
|
122 |
| |
123 |
124
| i++;
|
124 |
124
| expressionLength++;
|
125 |
124
| continue;
|
126 |
| } |
127 |
| |
128 |
12
| braceDepth--;
|
129 |
| |
130 |
12
| if (braceDepth > 0)
|
131 |
| { |
132 |
2
| i++;
|
133 |
2
| expressionLength++;
|
134 |
2
| continue;
|
135 |
| } |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
10
| if (expressionLength == 0)
|
142 |
2
| blockLength += 3;
|
143 |
| |
144 |
10
| if (blockLength > 0)
|
145 |
10
| token.addToken(constructStatic(text, blockStart, blockLength, location));
|
146 |
| |
147 |
10
| if (expressionLength > 0)
|
148 |
| { |
149 |
8
| String expression =
|
150 |
| text.substring(expressionStart, expressionStart + expressionLength); |
151 |
| |
152 |
8
| token.addToken(new InsertToken(expression, location));
|
153 |
| } |
154 |
| |
155 |
10
| i++;
|
156 |
10
| blockStart = i;
|
157 |
10
| blockLength = 0;
|
158 |
| |
159 |
| |
160 |
| |
161 |
10
| state = STATE_START;
|
162 |
| |
163 |
10
| continue;
|
164 |
| } |
165 |
| |
166 |
| } |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
44
| if (state == STATE_DOLLAR)
|
172 |
2
| blockLength++;
|
173 |
| |
174 |
44
| if (state == STATE_COLLECT_EXPRESSION)
|
175 |
2
| blockLength += expressionLength + 2;
|
176 |
| |
177 |
44
| if (blockLength > 0)
|
178 |
42
| token.addToken(constructStatic(text, blockStart, blockLength, location));
|
179 |
| } |
180 |
| |
181 |
52
| private IScriptToken constructStatic(
|
182 |
| String text, |
183 |
| int blockStart, |
184 |
| int blockLength, |
185 |
| Location location) |
186 |
| { |
187 |
52
| String literal = text.substring(blockStart, blockStart + blockLength);
|
188 |
| |
189 |
52
| return new StaticToken(literal, location);
|
190 |
| } |
191 |
| } |