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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.fo.expr;
/**
* Class to tokenize XSL FO property expression.
* This class is heavily based on the epxression tokenizer in James Clark's
* XT, an XSLT processor.
*/
class PropertyTokenizer {
static final int TOK_EOF = 0;
static final int TOK_NCNAME = 1;
static final int TOK_MULTIPLY = TOK_NCNAME + 1;
static final int TOK_LPAR = TOK_MULTIPLY + 1;
static final int TOK_RPAR = TOK_LPAR + 1;
static final int TOK_LITERAL = TOK_RPAR + 1;
static final int TOK_NUMBER = TOK_LITERAL + 1;
static final int TOK_FUNCTION_LPAR = TOK_NUMBER + 1;
static final int TOK_PLUS = TOK_FUNCTION_LPAR + 1;
static final int TOK_MINUS = TOK_PLUS + 1;
static final int TOK_MOD = TOK_MINUS + 1;
static final int TOK_DIV = TOK_MOD + 1;
static final int TOK_NUMERIC = TOK_DIV + 1;
static final int TOK_COMMA = TOK_NUMERIC + 1;
static final int TOK_PERCENT = TOK_COMMA + 1;
static final int TOK_COLORSPEC = TOK_PERCENT + 1;
static final int TOK_FLOAT = TOK_COLORSPEC + 1;
static final int TOK_INTEGER = TOK_FLOAT + 1;
protected int currentToken = TOK_EOF;
protected String currentTokenValue = null;
protected int currentUnitLength = 0;
private int currentTokenStartIndex = 0;
private /* final */ String expr;
private int exprIndex = 0;
private int exprLength;
/**
* Construct a new PropertyTokenizer object to tokenize the passed
* String.
* @param s The Property expressio to tokenize.
*/
PropertyTokenizer(String s) {
this.expr = s;
this.exprLength = s.length();
}
/**
* Parse the next token in the expression string.
* This sets the following package visible variables:
* currentToken An enumerated value identifying the recognized token
* currentTokenValue A String containing the token contents
* currentUnitLength If currentToken = TOK_NUMERIC, the number of
* characters in the unit name.
* @throws PropertyException If un unrecognized token is encountered.
*/
void next() throws PropertyException {
currentTokenValue = null;
currentTokenStartIndex = exprIndex;
boolean bSawDecimal;
while ( true ) {
if (exprIndex >= exprLength) {
currentToken = TOK_EOF;
return;
}
char c = expr.charAt(exprIndex++);
switch (c) {
case ' ':
case '\t':
case '\r':
case '\n':
currentTokenStartIndex = exprIndex;
break;
case ',':
currentToken = TOK_COMMA;
return;
case '+':
currentToken = TOK_PLUS;
return;
case '-':
currentToken = TOK_MINUS;
return;
case '(':
currentToken = TOK_LPAR;
return;
case ')':
currentToken = TOK_RPAR;
return;
case '"':
case '\'':
exprIndex = expr.indexOf(c, exprIndex);
if (exprIndex < 0) {
exprIndex = currentTokenStartIndex + 1;
throw new PropertyException("missing quote");
}
currentTokenValue = expr.substring(currentTokenStartIndex
+ 1, exprIndex++);
currentToken = TOK_LITERAL;
return;
case '*':
/*
* if (currentMaybeOperator) {
* recognizeOperator = false;
*/
currentToken = TOK_MULTIPLY;
/*
* }
* else
* throw new PropertyException("illegal operator *");
*/
return;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
scanDigits();
if (exprIndex < exprLength && expr.charAt(exprIndex) == '.') {
exprIndex++;
bSawDecimal = true;
if (exprIndex < exprLength
&& isDigit(expr.charAt(exprIndex))) {
exprIndex++;
scanDigits();
}
} else {
bSawDecimal = false;
}
if (exprIndex < exprLength && expr.charAt(exprIndex) == '%') {
exprIndex++;
currentToken = TOK_PERCENT;
} else {
// Check for possible unit name following number
currentUnitLength = exprIndex;
scanName();
currentUnitLength = exprIndex - currentUnitLength;
currentToken = (currentUnitLength > 0) ? TOK_NUMERIC
: (bSawDecimal ? TOK_FLOAT : TOK_INTEGER);
}
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
return;
case '.':
nextDecimalPoint();
return;
case '#': // Start of color value
nextColor();
return;
default:
--exprIndex;
scanName();
if (exprIndex == currentTokenStartIndex) {
throw new PropertyException("illegal character");
}
currentTokenValue = expr.substring(currentTokenStartIndex, exprIndex);
if (currentTokenValue.equals("mod")) {
currentToken = TOK_MOD;
return;
} else if (currentTokenValue.equals("div")) {
currentToken = TOK_DIV;
return;
}
if (followingParen()) {
currentToken = TOK_FUNCTION_LPAR;
} else {
currentToken = TOK_NCNAME;
}
return;
}
}
}
private void nextDecimalPoint() throws PropertyException {
if (exprIndex < exprLength
&& isDigit(expr.charAt(exprIndex))) {
++exprIndex;
scanDigits();
if (exprIndex < exprLength
&& expr.charAt(exprIndex) == '%') {
exprIndex++;
currentToken = TOK_PERCENT;
} else {
// Check for possible unit name following number
currentUnitLength = exprIndex;
scanName();
currentUnitLength = exprIndex - currentUnitLength;
currentToken = (currentUnitLength > 0) ? TOK_NUMERIC
: TOK_FLOAT;
}
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
return;
}
throw new PropertyException("illegal character '.'");
}
private void nextColor() throws PropertyException {
if (exprIndex < exprLength) {
++exprIndex;
scanHexDigits();
int len = exprIndex - currentTokenStartIndex - 1;
if (len % 3 == 0) {
currentToken = TOK_COLORSPEC;
} else {
//Actually not a color at all, but an NCNAME starting with "#"
scanRestOfName();
currentToken = TOK_NCNAME;
}
currentTokenValue = expr.substring(currentTokenStartIndex,
exprIndex);
return;
} else {
throw new PropertyException("illegal character '#'");
}
}
/**
* Attempt to recognize a valid NAME token in the input expression.
*/
private void scanName() {
if (exprIndex < exprLength && isNameStartChar(expr.charAt(exprIndex))) {
scanRestOfName();
}
}
private void scanRestOfName() {
while ( ++exprIndex < exprLength ) {
if ( !isNameChar ( expr.charAt ( exprIndex ) ) ) {
break;
}
}
}
/**
* Attempt to recognize a valid sequence of decimal DIGITS in the
* input expression.
*/
private void scanDigits() {
while (exprIndex < exprLength && isDigit(expr.charAt(exprIndex))) {
exprIndex++;
}
}
/**
* Attempt to recognize a valid sequence of hexadecimal DIGITS in the
* input expression.
*/
private void scanHexDigits() {
while (exprIndex < exprLength && isHexDigit(expr.charAt(exprIndex))) {
exprIndex++;
}
}
/**
* Return a boolean value indicating whether the following non-whitespace
* character is an opening parenthesis.
*/
private boolean followingParen() {
for (int i = exprIndex; i < exprLength; i++) {
switch (expr.charAt(i)) {
case '(':
exprIndex = i + 1;
return true;
case ' ':
case '\r':
case '\n':
case '\t':
break;
default:
return false;
}
}
return false;
}
private static final String NAME_START_CHARS
= "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String NAME_CHARS = ".-0123456789";
private static final String DIGITS = "0123456789";
private static final String HEX_CHARS = DIGITS + "abcdefABCDEF";
/**
* Return a boolean value indicating whether the argument is a
* decimal digit (0-9).
* @param c The character to check
*/
private static boolean isDigit(char c) {
return DIGITS.indexOf(c) >= 0;
}
/**
* Return a boolean value indicating whether the argument is a
* hexadecimal digit (0-9, A-F, a-f).
* @param c The character to check
*/
private static boolean isHexDigit(char c) {
return HEX_CHARS.indexOf(c) >= 0;
}
/**
* Return a boolean value indicating whether the argument is whitespace
* as defined by XSL (space, newline, CR, tab).
* @param c The character to check
*/
private static boolean isSpace(char c) {
switch (c) {
case ' ':
case '\r':
case '\n':
case '\t':
return true;
default:
return false;
}
}
/**
* Return a boolean value indicating whether the argument is a valid name
* start character, ie. can start a NAME as defined by XSL.
* @param c The character to check
*/
private static boolean isNameStartChar(char c) {
return NAME_START_CHARS.indexOf(c) >= 0 || c >= 0x80;
}
/**
* Return a boolean value indicating whether the argument is a valid name
* character, ie. can occur in a NAME as defined by XSL.
* @param c The character to check
*/
private static boolean isNameChar(char c) {
return NAME_START_CHARS.indexOf(c) >= 0 || NAME_CHARS.indexOf(c) >= 0
|| c >= 0x80;
}
}
|