1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.parse; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.Iterator; |
21 |
| import java.util.List; |
22 |
| import java.util.Map; |
23 |
| |
24 |
| import org.apache.hivemind.ApplicationRuntimeException; |
25 |
| import org.apache.hivemind.Location; |
26 |
| import org.apache.hivemind.Resource; |
27 |
| import org.apache.hivemind.impl.LocationImpl; |
28 |
| import org.apache.oro.text.regex.MalformedPatternException; |
29 |
| import org.apache.oro.text.regex.MatchResult; |
30 |
| import org.apache.oro.text.regex.Pattern; |
31 |
| import org.apache.oro.text.regex.PatternMatcher; |
32 |
| import org.apache.oro.text.regex.Perl5Compiler; |
33 |
| import org.apache.oro.text.regex.Perl5Matcher; |
34 |
| import org.apache.tapestry.util.IdAllocator; |
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 |
| public class TemplateParser implements ITemplateParser |
92 |
| { |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| private static final String REMOVE_ID = "$remove$"; |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| private static final String CONTENT_ID = "$content$"; |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| public static final String LOCALIZATION_KEY_ATTRIBUTE_NAME = "key"; |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| public static final String RAW_ATTRIBUTE_NAME = "raw"; |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| private String _componentAttributeName; |
134 |
| |
135 |
| private static final String PROPERTY_NAME_PATTERN = "_?[a-zA-Z]\\w*"; |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| public static final String SIMPLE_ID_PATTERN = "^(" + PROPERTY_NAME_PATTERN + ")$"; |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| public static final String IMPLICIT_ID_PATTERN = "^(" + PROPERTY_NAME_PATTERN + ")?@(((" |
155 |
| + PROPERTY_NAME_PATTERN + "):)?(" + PROPERTY_NAME_PATTERN + "))$"; |
156 |
| |
157 |
| private static final int IMPLICIT_ID_PATTERN_ID_GROUP = 1; |
158 |
| |
159 |
| private static final int IMPLICIT_ID_PATTERN_TYPE_GROUP = 2; |
160 |
| |
161 |
| private static final int IMPLICIT_ID_PATTERN_LIBRARY_ID_GROUP = 4; |
162 |
| |
163 |
| private static final int IMPLICIT_ID_PATTERN_SIMPLE_TYPE_GROUP = 5; |
164 |
| |
165 |
| private Pattern _simpleIdPattern; |
166 |
| |
167 |
| private Pattern _implicitIdPattern; |
168 |
| |
169 |
| private PatternMatcher _patternMatcher; |
170 |
| |
171 |
| private IdAllocator _idAllocator = new IdAllocator(); |
172 |
| |
173 |
| private ITemplateParserDelegate _delegate; |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| private Resource _resourceLocation; |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| private Location _templateLocation; |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| private Location _currentLocation; |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| private char[] _templateData; |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| private List _stack = new ArrayList(); |
204 |
| |
205 |
| private static class Tag |
206 |
| { |
207 |
| |
208 |
| String _tagName; |
209 |
| |
210 |
| |
211 |
| boolean _component; |
212 |
| |
213 |
| |
214 |
| |
215 |
| boolean _ignoringBody; |
216 |
| |
217 |
| |
218 |
| boolean _removeTag; |
219 |
| |
220 |
| |
221 |
| |
222 |
| boolean _mustBalance; |
223 |
| |
224 |
| |
225 |
| int _line; |
226 |
| |
227 |
| |
228 |
| boolean _content; |
229 |
| |
230 |
1388
| Tag(String tagName, int line)
|
231 |
| { |
232 |
1388
| _tagName = tagName;
|
233 |
1388
| _line = line;
|
234 |
| } |
235 |
| |
236 |
1306
| boolean match(String matchTagName)
|
237 |
| { |
238 |
1306
| return _tagName.equalsIgnoreCase(matchTagName);
|
239 |
| } |
240 |
| } |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| private List _tokens = new ArrayList(); |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| private int _cursor; |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| private int _blockStart; |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| private int _line; |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| private boolean _ignoring; |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
| private Map _attributes = new HashMap(); |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| private TemplateTokenFactory _factory; |
286 |
| |
287 |
125
| public TemplateParser()
|
288 |
| { |
289 |
125
| Perl5Compiler compiler = new Perl5Compiler();
|
290 |
| |
291 |
125
| try
|
292 |
| { |
293 |
125
| _simpleIdPattern = compiler.compile(SIMPLE_ID_PATTERN);
|
294 |
125
| _implicitIdPattern = compiler.compile(IMPLICIT_ID_PATTERN);
|
295 |
| } |
296 |
| catch (MalformedPatternException ex) |
297 |
| { |
298 |
0
| throw new ApplicationRuntimeException(ex);
|
299 |
| } |
300 |
| |
301 |
125
| _patternMatcher = new Perl5Matcher();
|
302 |
| } |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
189
| public TemplateToken[] parse(char[] templateData, ITemplateParserDelegate delegate,
|
319 |
| Resource resourceLocation) throws TemplateParseException |
320 |
| { |
321 |
189
| try
|
322 |
| { |
323 |
189
| beforeParse(templateData, delegate, resourceLocation);
|
324 |
| |
325 |
189
| parse();
|
326 |
| |
327 |
177
| return (TemplateToken[]) _tokens.toArray(new TemplateToken[_tokens.size()]);
|
328 |
| } |
329 |
| finally |
330 |
| { |
331 |
189
| afterParse();
|
332 |
| } |
333 |
| } |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
189
| protected void beforeParse(char[] templateData, ITemplateParserDelegate delegate,
|
340 |
| Resource resourceLocation) |
341 |
| { |
342 |
189
| _templateData = templateData;
|
343 |
189
| _resourceLocation = resourceLocation;
|
344 |
189
| _templateLocation = new LocationImpl(resourceLocation);
|
345 |
189
| _delegate = delegate;
|
346 |
189
| _ignoring = false;
|
347 |
189
| _line = 1;
|
348 |
189
| _componentAttributeName = delegate.getComponentAttributeName();
|
349 |
| } |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
189
| protected void afterParse()
|
356 |
| { |
357 |
189
| _delegate = null;
|
358 |
189
| _templateData = null;
|
359 |
189
| _resourceLocation = null;
|
360 |
189
| _templateLocation = null;
|
361 |
189
| _currentLocation = null;
|
362 |
189
| _stack.clear();
|
363 |
189
| _tokens.clear();
|
364 |
189
| _attributes.clear();
|
365 |
189
| _idAllocator.clear();
|
366 |
| } |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
| |
386 |
| |
387 |
12
| protected void templateParseProblem(String message, Location location, int line, int cursor)
|
388 |
| throws TemplateParseException |
389 |
| { |
390 |
12
| throw new TemplateParseException(message, location);
|
391 |
| } |
392 |
| |
393 |
| |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
| |
399 |
| |
400 |
| |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
0
| protected void templateParseProblem(ApplicationRuntimeException exception, int line, int cursor)
|
411 |
| throws ApplicationRuntimeException |
412 |
| { |
413 |
0
| throw exception;
|
414 |
| } |
415 |
| |
416 |
| |
417 |
| |
418 |
| |
419 |
0
| protected List getTokens()
|
420 |
| { |
421 |
0
| if (_tokens == null)
|
422 |
0
| return Collections.EMPTY_LIST;
|
423 |
| |
424 |
0
| return _tokens;
|
425 |
| } |
426 |
| |
427 |
| |
428 |
| |
429 |
| |
430 |
| |
431 |
16461
| private boolean lookahead(char[] match)
|
432 |
| { |
433 |
16461
| try
|
434 |
| { |
435 |
16461
| for (int i = 0; i < match.length; i++)
|
436 |
| { |
437 |
21915
| if (_templateData[_cursor + i] != match[i])
|
438 |
15067
| return false;
|
439 |
| } |
440 |
| |
441 |
| |
442 |
| |
443 |
1394
| return true;
|
444 |
| } |
445 |
| catch (IndexOutOfBoundsException ex) |
446 |
| { |
447 |
0
| return false;
|
448 |
| } |
449 |
| } |
450 |
| |
451 |
| private static final char[] COMMENT_START = new char[] |
452 |
| { '<', '!', '-', '-' }; |
453 |
| |
454 |
| private static final char[] COMMENT_END = new char[] |
455 |
| { '-', '-', '>' }; |
456 |
| |
457 |
| private static final char[] CLOSE_TAG = new char[] |
458 |
| { '<', '/' }; |
459 |
| |
460 |
189
| protected void parse() throws TemplateParseException
|
461 |
| { |
462 |
189
| _cursor = 0;
|
463 |
189
| _blockStart = -1;
|
464 |
189
| int length = _templateData.length;
|
465 |
| |
466 |
189
| while (_cursor < length)
|
467 |
| { |
468 |
21727
| if (_templateData[_cursor] != '<')
|
469 |
| { |
470 |
18686
| if (_blockStart < 0 && !_ignoring)
|
471 |
1153
| _blockStart = _cursor;
|
472 |
| |
473 |
18686
| advance();
|
474 |
18686
| continue;
|
475 |
| } |
476 |
| |
477 |
| |
478 |
| |
479 |
3041
| if (lookahead(CLOSE_TAG))
|
480 |
| { |
481 |
1235
| closeTag();
|
482 |
1232
| continue;
|
483 |
| } |
484 |
| |
485 |
1806
| if (lookahead(COMMENT_START))
|
486 |
| { |
487 |
80
| skipComment();
|
488 |
79
| continue;
|
489 |
| } |
490 |
| |
491 |
| |
492 |
| |
493 |
1726
| startTag();
|
494 |
| } |
495 |
| |
496 |
| |
497 |
| |
498 |
| |
499 |
| |
500 |
| |
501 |
177
| addTextToken(_templateData.length - 1);
|
502 |
| } |
503 |
| |
504 |
| |
505 |
| |
506 |
| |
507 |
| |
508 |
| |
509 |
80
| private void skipComment() throws TemplateParseException
|
510 |
| { |
511 |
80
| int length = _templateData.length;
|
512 |
80
| int startLine = _line;
|
513 |
| |
514 |
80
| if (_blockStart < 0 && !_ignoring)
|
515 |
19
| _blockStart = _cursor;
|
516 |
| |
517 |
80
| while (true)
|
518 |
| { |
519 |
11615
| if (_cursor >= length)
|
520 |
1
| templateParseProblem(ParseMessages.commentNotEnded(startLine), new LocationImpl(
|
521 |
| _resourceLocation, startLine), startLine, _cursor); |
522 |
| |
523 |
11614
| if (lookahead(COMMENT_END))
|
524 |
79
| break;
|
525 |
| |
526 |
| |
527 |
| |
528 |
11535
| advance();
|
529 |
| } |
530 |
| |
531 |
79
| _cursor += COMMENT_END.length;
|
532 |
79
| advanceOverWhitespace();
|
533 |
| } |
534 |
| |
535 |
1682
| private void addTextToken(int end)
|
536 |
| { |
537 |
| |
538 |
| |
539 |
1682
| if (_blockStart < 0)
|
540 |
419
| return;
|
541 |
| |
542 |
1263
| if (_blockStart <= end)
|
543 |
| { |
544 |
| |
545 |
| |
546 |
| |
547 |
1263
| TemplateToken token = _factory.createTextToken(
|
548 |
| _templateData, |
549 |
| _blockStart, |
550 |
| end, |
551 |
| _templateLocation); |
552 |
| |
553 |
1263
| _tokens.add(token);
|
554 |
| } |
555 |
| |
556 |
1263
| _blockStart = -1;
|
557 |
| } |
558 |
| |
559 |
| private static final int WAIT_FOR_ATTRIBUTE_NAME = 0; |
560 |
| |
561 |
| private static final int COLLECT_ATTRIBUTE_NAME = 1; |
562 |
| |
563 |
| private static final int ADVANCE_PAST_EQUALS = 2; |
564 |
| |
565 |
| private static final int WAIT_FOR_ATTRIBUTE_VALUE = 3; |
566 |
| |
567 |
| private static final int COLLECT_QUOTED_VALUE = 4; |
568 |
| |
569 |
| private static final int COLLECT_UNQUOTED_VALUE = 5; |
570 |
| |
571 |
1726
| private void startTag() throws TemplateParseException
|
572 |
| { |
573 |
1726
| int cursorStart = _cursor;
|
574 |
1726
| int length = _templateData.length;
|
575 |
1726
| String tagName = null;
|
576 |
1726
| boolean endOfTag = false;
|
577 |
1726
| boolean emptyTag = false;
|
578 |
1726
| int startLine = _line;
|
579 |
1726
| Location startLocation = new LocationImpl(_resourceLocation, startLine);
|
580 |
| |
581 |
1726
| tagBeginEvent(startLine, _cursor);
|
582 |
| |
583 |
1726
| advance();
|
584 |
| |
585 |
| |
586 |
| |
587 |
7359
| while (_cursor < length)
|
588 |
| { |
589 |
7359
| char ch = _templateData[_cursor];
|
590 |
| |
591 |
7359
| if (ch == '/' || ch == '>' || Character.isWhitespace(ch))
|
592 |
| { |
593 |
1726
| tagName = new String(_templateData, cursorStart + 1, _cursor - cursorStart - 1);
|
594 |
| |
595 |
1726
| break;
|
596 |
| } |
597 |
| |
598 |
5633
| advance();
|
599 |
| } |
600 |
| |
601 |
1726
| String attributeName = null;
|
602 |
1726
| int attributeNameStart = -1;
|
603 |
1726
| int attributeValueStart = -1;
|
604 |
1726
| int state = WAIT_FOR_ATTRIBUTE_NAME;
|
605 |
1726
| char quoteChar = 0;
|
606 |
| |
607 |
1726
| _attributes.clear();
|
608 |
| |
609 |
| |
610 |
| |
611 |
1726
| while (!endOfTag)
|
612 |
| { |
613 |
44344
| if (_cursor >= length)
|
614 |
| { |
615 |
1
| String message = (tagName == null) ? ParseMessages.unclosedUnknownTag(startLine)
|
616 |
| : ParseMessages.unclosedTag(tagName, startLine); |
617 |
| |
618 |
1
| templateParseProblem(message, startLocation, startLine, cursorStart);
|
619 |
| } |
620 |
| |
621 |
44343
| char ch = _templateData[_cursor];
|
622 |
| |
623 |
44343
| switch (state)
|
624 |
| { |
625 |
6358
| case WAIT_FOR_ATTRIBUTE_NAME:
|
626 |
| |
627 |
| |
628 |
| |
629 |
| |
630 |
6358
| if (ch == '/')
|
631 |
| { |
632 |
495
| emptyTag = true;
|
633 |
495
| advance();
|
634 |
495
| break;
|
635 |
| } |
636 |
| |
637 |
5863
| if (ch == '>')
|
638 |
| { |
639 |
1724
| endOfTag = true;
|
640 |
1724
| break;
|
641 |
| } |
642 |
| |
643 |
4139
| if (Character.isWhitespace(ch))
|
644 |
| { |
645 |
1988
| advance();
|
646 |
1988
| break;
|
647 |
| } |
648 |
| |
649 |
| |
650 |
| |
651 |
| |
652 |
2151
| attributeNameStart = _cursor;
|
653 |
2151
| state = COLLECT_ATTRIBUTE_NAME;
|
654 |
2151
| advance();
|
655 |
2151
| break;
|
656 |
| |
657 |
11518
| case COLLECT_ATTRIBUTE_NAME:
|
658 |
| |
659 |
| |
660 |
| |
661 |
11518
| if (ch == '=' || ch == '/' || ch == '>' || Character.isWhitespace(ch))
|
662 |
| { |
663 |
2151
| attributeName = new String(_templateData, attributeNameStart, _cursor
|
664 |
| - attributeNameStart); |
665 |
| |
666 |
2151
| state = ADVANCE_PAST_EQUALS;
|
667 |
2151
| break;
|
668 |
| } |
669 |
| |
670 |
| |
671 |
| |
672 |
9367
| advance();
|
673 |
9367
| break;
|
674 |
| |
675 |
2294
| case ADVANCE_PAST_EQUALS:
|
676 |
| |
677 |
| |
678 |
| |
679 |
| |
680 |
| |
681 |
2294
| if (ch == '/' || ch == '>')
|
682 |
| { |
683 |
| |
684 |
| |
685 |
| |
686 |
145
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
687 |
145
| break;
|
688 |
| } |
689 |
| |
690 |
2149
| if (Character.isWhitespace(ch))
|
691 |
| { |
692 |
143
| advance();
|
693 |
143
| break;
|
694 |
| } |
695 |
| |
696 |
2006
| if (ch == '=')
|
697 |
| { |
698 |
1916
| state = WAIT_FOR_ATTRIBUTE_VALUE;
|
699 |
1916
| quoteChar = 0;
|
700 |
1916
| attributeValueStart = -1;
|
701 |
1916
| advance();
|
702 |
1916
| break;
|
703 |
| } |
704 |
| |
705 |
| |
706 |
| |
707 |
| |
708 |
| |
709 |
90
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
710 |
90
| break;
|
711 |
| |
712 |
1919
| case WAIT_FOR_ATTRIBUTE_VALUE:
|
713 |
| |
714 |
1919
| if (ch == '/' || ch == '>')
|
715 |
1
| templateParseProblem(ParseMessages.missingAttributeValue(
|
716 |
| tagName, |
717 |
| _line, |
718 |
| attributeName), getCurrentLocation(), _line, _cursor); |
719 |
| |
720 |
| |
721 |
| |
722 |
| |
723 |
1918
| if (Character.isWhitespace(ch))
|
724 |
| { |
725 |
3
| advance();
|
726 |
3
| break;
|
727 |
| } |
728 |
| |
729 |
1915
| if (ch == '\'' || ch == '"')
|
730 |
| { |
731 |
1886
| quoteChar = ch;
|
732 |
| |
733 |
1886
| state = COLLECT_QUOTED_VALUE;
|
734 |
1886
| advance();
|
735 |
1886
| attributeValueStart = _cursor;
|
736 |
1886
| attributeBeginEvent(attributeName, _line, attributeValueStart);
|
737 |
1886
| break;
|
738 |
| } |
739 |
| |
740 |
| |
741 |
| |
742 |
29
| state = COLLECT_UNQUOTED_VALUE;
|
743 |
29
| attributeValueStart = _cursor;
|
744 |
29
| attributeBeginEvent(attributeName, _line, attributeValueStart);
|
745 |
29
| break;
|
746 |
| |
747 |
22192
| case COLLECT_QUOTED_VALUE:
|
748 |
| |
749 |
| |
750 |
| |
751 |
| |
752 |
| |
753 |
22192
| if (ch == quoteChar)
|
754 |
| { |
755 |
1886
| String attributeValue = new String(_templateData, attributeValueStart,
|
756 |
| _cursor - attributeValueStart); |
757 |
| |
758 |
1886
| _attributes.put(attributeName, attributeValue);
|
759 |
1886
| attributeEndEvent(_cursor);
|
760 |
| |
761 |
| |
762 |
1886
| advance();
|
763 |
1886
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
764 |
1886
| break;
|
765 |
| } |
766 |
| |
767 |
20306
| advance();
|
768 |
20306
| break;
|
769 |
| |
770 |
62
| case COLLECT_UNQUOTED_VALUE:
|
771 |
| |
772 |
| |
773 |
| |
774 |
| |
775 |
62
| if (ch == '/' || ch == '>' || Character.isWhitespace(ch))
|
776 |
| { |
777 |
29
| String attributeValue = new String(_templateData, attributeValueStart,
|
778 |
| _cursor - attributeValueStart); |
779 |
| |
780 |
29
| _attributes.put(attributeName, attributeValue);
|
781 |
29
| attributeEndEvent(_cursor);
|
782 |
| |
783 |
29
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
784 |
29
| break;
|
785 |
| } |
786 |
| |
787 |
33
| advance();
|
788 |
33
| break;
|
789 |
| } |
790 |
| } |
791 |
| |
792 |
1724
| tagEndEvent(_cursor);
|
793 |
| |
794 |
| |
795 |
| |
796 |
1724
| String localizationKey = findValueCaselessly(LOCALIZATION_KEY_ATTRIBUTE_NAME, _attributes);
|
797 |
1724
| String jwcId = findValueCaselessly(_componentAttributeName, _attributes);
|
798 |
| |
799 |
1724
| if (localizationKey != null && tagName.equalsIgnoreCase("span") && jwcId == null)
|
800 |
| { |
801 |
16
| if (_ignoring)
|
802 |
1
| templateParseProblem(
|
803 |
| ParseMessages.componentMayNotBeIgnored(tagName, startLine), |
804 |
| startLocation, |
805 |
| startLine, |
806 |
| cursorStart); |
807 |
| |
808 |
| |
809 |
| |
810 |
| |
811 |
15
| if (!emptyTag)
|
812 |
| { |
813 |
3
| Tag tag = new Tag(tagName, startLine);
|
814 |
| |
815 |
3
| tag._component = false;
|
816 |
3
| tag._removeTag = true;
|
817 |
3
| tag._ignoringBody = true;
|
818 |
3
| tag._mustBalance = true;
|
819 |
| |
820 |
3
| _stack.add(tag);
|
821 |
| |
822 |
| |
823 |
| |
824 |
3
| _ignoring = true;
|
825 |
| } |
826 |
| else |
827 |
| { |
828 |
| |
829 |
12
| advance();
|
830 |
12
| advanceOverWhitespace();
|
831 |
| } |
832 |
| |
833 |
| |
834 |
| |
835 |
15
| addTextToken(cursorStart - 1);
|
836 |
| |
837 |
15
| boolean raw = checkBoolean(RAW_ATTRIBUTE_NAME, _attributes);
|
838 |
| |
839 |
15
| Map attributes = filter(_attributes, new String[]
|
840 |
| { LOCALIZATION_KEY_ATTRIBUTE_NAME, RAW_ATTRIBUTE_NAME }); |
841 |
| |
842 |
15
| TemplateToken token = _factory.createLocalizationToken(
|
843 |
| tagName, |
844 |
| localizationKey, |
845 |
| raw, |
846 |
| attributes, |
847 |
| startLocation); |
848 |
| |
849 |
15
| _tokens.add(token);
|
850 |
| |
851 |
15
| return;
|
852 |
| } |
853 |
| |
854 |
1708
| if (jwcId != null)
|
855 |
| { |
856 |
964
| processComponentStart(tagName, jwcId, emptyTag, startLine, cursorStart, startLocation);
|
857 |
959
| return;
|
858 |
| } |
859 |
| |
860 |
| |
861 |
| |
862 |
| |
863 |
744
| if (!emptyTag)
|
864 |
| { |
865 |
669
| Tag tag = new Tag(tagName, startLine);
|
866 |
669
| _stack.add(tag);
|
867 |
| } |
868 |
| |
869 |
| |
870 |
| |
871 |
744
| if (_blockStart < 0 && !_ignoring)
|
872 |
44
| _blockStart = cursorStart;
|
873 |
| |
874 |
744
| advance();
|
875 |
| } |
876 |
| |
877 |
| |
878 |
| |
879 |
| |
880 |
| |
881 |
| |
882 |
| |
883 |
| |
884 |
| |
885 |
| |
886 |
| |
887 |
1726
| protected void tagBeginEvent(int startLine, int cursorPosition)
|
888 |
| { |
889 |
| } |
890 |
| |
891 |
| |
892 |
| |
893 |
| |
894 |
| |
895 |
| |
896 |
1724
| protected void tagEndEvent(int cursorPosition)
|
897 |
| { |
898 |
| } |
899 |
| |
900 |
| |
901 |
| |
902 |
| |
903 |
| |
904 |
| |
905 |
1915
| protected void attributeBeginEvent(String attributeName, int startLine, int cursorPosition)
|
906 |
| { |
907 |
| } |
908 |
| |
909 |
| |
910 |
| |
911 |
| |
912 |
| |
913 |
| |
914 |
1915
| protected void attributeEndEvent(int cursorPosition)
|
915 |
| { |
916 |
| } |
917 |
| |
918 |
964
| private void processComponentStart(String tagName, String jwcId, boolean emptyTag,
|
919 |
| int startLine, int cursorStart, Location startLocation) throws TemplateParseException |
920 |
| { |
921 |
964
| if (jwcId.equalsIgnoreCase(CONTENT_ID))
|
922 |
| { |
923 |
78
| processContentTag(tagName, startLine, cursorStart, emptyTag);
|
924 |
| |
925 |
77
| return;
|
926 |
| } |
927 |
| |
928 |
886
| boolean isRemoveId = jwcId.equalsIgnoreCase(REMOVE_ID);
|
929 |
| |
930 |
886
| if (_ignoring && !isRemoveId)
|
931 |
2
| templateParseProblem(
|
932 |
| ParseMessages.componentMayNotBeIgnored(tagName, startLine), |
933 |
| startLocation, |
934 |
| startLine, |
935 |
| cursorStart); |
936 |
| |
937 |
884
| String type = null;
|
938 |
884
| boolean allowBody = false;
|
939 |
| |
940 |
884
| if (_patternMatcher.matches(jwcId, _implicitIdPattern))
|
941 |
| { |
942 |
498
| MatchResult match = _patternMatcher.getMatch();
|
943 |
| |
944 |
498
| jwcId = match.group(IMPLICIT_ID_PATTERN_ID_GROUP);
|
945 |
498
| type = match.group(IMPLICIT_ID_PATTERN_TYPE_GROUP);
|
946 |
| |
947 |
498
| String libraryId = match.group(IMPLICIT_ID_PATTERN_LIBRARY_ID_GROUP);
|
948 |
498
| String simpleType = match.group(IMPLICIT_ID_PATTERN_SIMPLE_TYPE_GROUP);
|
949 |
| |
950 |
| |
951 |
| |
952 |
| |
953 |
| |
954 |
| |
955 |
| |
956 |
| |
957 |
| |
958 |
498
| if (jwcId == null)
|
959 |
432
| jwcId = _idAllocator.allocateId("$" + simpleType);
|
960 |
| |
961 |
498
| try
|
962 |
| { |
963 |
498
| allowBody = _delegate.getAllowBody(libraryId, simpleType, startLocation);
|
964 |
| } |
965 |
| catch (ApplicationRuntimeException e) |
966 |
| { |
967 |
| |
968 |
0
| templateParseProblem(e, startLine, cursorStart);
|
969 |
| } |
970 |
| |
971 |
| } |
972 |
| else |
973 |
| { |
974 |
386
| if (!isRemoveId)
|
975 |
| { |
976 |
282
| if (!_patternMatcher.matches(jwcId, _simpleIdPattern))
|
977 |
0
| templateParseProblem(
|
978 |
| ParseMessages.componentIdInvalid(tagName, startLine, jwcId), |
979 |
| startLocation, |
980 |
| startLine, |
981 |
| cursorStart); |
982 |
| |
983 |
282
| if (!_delegate.getKnownComponent(jwcId))
|
984 |
1
| templateParseProblem(
|
985 |
| ParseMessages.unknownComponentId(tagName, startLine, jwcId), |
986 |
| startLocation, |
987 |
| startLine, |
988 |
| cursorStart); |
989 |
| |
990 |
281
| try
|
991 |
| { |
992 |
281
| allowBody = _delegate.getAllowBody(jwcId, startLocation);
|
993 |
| } |
994 |
| catch (ApplicationRuntimeException e) |
995 |
| { |
996 |
| |
997 |
0
| templateParseProblem(e, startLine, cursorStart);
|
998 |
| } |
999 |
| } |
1000 |
| } |
1001 |
| |
1002 |
| |
1003 |
| |
1004 |
| |
1005 |
| |
1006 |
883
| boolean ignoreBody = !emptyTag && (isRemoveId || !allowBody);
|
1007 |
| |
1008 |
883
| if (_ignoring && ignoreBody)
|
1009 |
1
| templateParseProblem(ParseMessages.nestedIgnore(tagName, startLine), new LocationImpl(
|
1010 |
| _resourceLocation, startLine), startLine, cursorStart); |
1011 |
| |
1012 |
882
| if (!emptyTag)
|
1013 |
639
| pushNewTag(tagName, startLine, isRemoveId, ignoreBody);
|
1014 |
| |
1015 |
| |
1016 |
| |
1017 |
882
| addTextToken(cursorStart - 1);
|
1018 |
| |
1019 |
882
| if (!isRemoveId)
|
1020 |
| { |
1021 |
779
| addOpenToken(tagName, jwcId, type, startLocation);
|
1022 |
| |
1023 |
779
| if (emptyTag)
|
1024 |
243
| _tokens.add(_factory.createCloseToken(tagName, getCurrentLocation()));
|
1025 |
| } |
1026 |
| |
1027 |
882
| advance();
|
1028 |
| } |
1029 |
| |
1030 |
639
| private void pushNewTag(String tagName, int startLine, boolean isRemoveId, boolean ignoreBody)
|
1031 |
| { |
1032 |
639
| Tag tag = new Tag(tagName, startLine);
|
1033 |
| |
1034 |
639
| tag._component = !isRemoveId;
|
1035 |
639
| tag._removeTag = isRemoveId;
|
1036 |
| |
1037 |
639
| tag._ignoringBody = ignoreBody;
|
1038 |
| |
1039 |
639
| _ignoring = tag._ignoringBody;
|
1040 |
| |
1041 |
639
| tag._mustBalance = true;
|
1042 |
| |
1043 |
639
| _stack.add(tag);
|
1044 |
| } |
1045 |
| |
1046 |
78
| private void processContentTag(String tagName, int startLine, int cursorStart, boolean emptyTag)
|
1047 |
| throws TemplateParseException |
1048 |
| { |
1049 |
78
| if (_ignoring)
|
1050 |
1
| templateParseProblem(
|
1051 |
| ParseMessages.contentBlockMayNotBeIgnored(tagName, startLine), |
1052 |
| new LocationImpl(_resourceLocation, startLine), |
1053 |
| startLine, |
1054 |
| cursorStart); |
1055 |
| |
1056 |
77
| if (emptyTag)
|
1057 |
0
| templateParseProblem(
|
1058 |
| ParseMessages.contentBlockMayNotBeEmpty(tagName, startLine), |
1059 |
| new LocationImpl(_resourceLocation, startLine), |
1060 |
| startLine, |
1061 |
| cursorStart); |
1062 |
| |
1063 |
77
| _tokens.clear();
|
1064 |
77
| _blockStart = -1;
|
1065 |
| |
1066 |
77
| Tag tag = new Tag(tagName, startLine);
|
1067 |
| |
1068 |
77
| tag._mustBalance = true;
|
1069 |
77
| tag._content = true;
|
1070 |
| |
1071 |
77
| _stack.clear();
|
1072 |
77
| _stack.add(tag);
|
1073 |
| |
1074 |
77
| advance();
|
1075 |
| } |
1076 |
| |
1077 |
779
| private void addOpenToken(String tagName, String jwcId, String type, Location location)
|
1078 |
| { |
1079 |
779
| OpenToken token = _factory.createOpenToken(tagName, jwcId, type, location);
|
1080 |
779
| _tokens.add(token);
|
1081 |
| |
1082 |
779
| if (_attributes.isEmpty())
|
1083 |
0
| return;
|
1084 |
| |
1085 |
779
| Iterator i = _attributes.entrySet().iterator();
|
1086 |
779
| while (i.hasNext())
|
1087 |
| { |
1088 |
1382
| Map.Entry entry = (Map.Entry) i.next();
|
1089 |
| |
1090 |
1382
| String key = (String) entry.getKey();
|
1091 |
| |
1092 |
1382
| if (key.equalsIgnoreCase(_componentAttributeName))
|
1093 |
779
| continue;
|
1094 |
| |
1095 |
603
| String value = (String) entry.getValue();
|
1096 |
| |
1097 |
603
| addAttributeToToken(token, key, value);
|
1098 |
| } |
1099 |
| } |
1100 |
| |
1101 |
| |
1102 |
| |
1103 |
| |
1104 |
| |
1105 |
| |
1106 |
| |
1107 |
603
| private void addAttributeToToken(OpenToken token, String name, String attributeValue)
|
1108 |
| { |
1109 |
603
| token.addAttribute(name, convertEntitiesToPlain(attributeValue));
|
1110 |
| } |
1111 |
| |
1112 |
| |
1113 |
| |
1114 |
| |
1115 |
| |
1116 |
| |
1117 |
| |
1118 |
| |
1119 |
| |
1120 |
| |
1121 |
| |
1122 |
| |
1123 |
| |
1124 |
| |
1125 |
| |
1126 |
1235
| private void closeTag() throws TemplateParseException
|
1127 |
| { |
1128 |
1235
| int cursorStart = _cursor;
|
1129 |
1235
| int length = _templateData.length;
|
1130 |
1235
| int startLine = _line;
|
1131 |
| |
1132 |
1235
| Location startLocation = getCurrentLocation();
|
1133 |
| |
1134 |
1235
| _cursor += CLOSE_TAG.length;
|
1135 |
| |
1136 |
1235
| int tagStart = _cursor;
|
1137 |
| |
1138 |
1235
| while (true)
|
1139 |
| { |
1140 |
4994
| if (_cursor >= length)
|
1141 |
1
| templateParseProblem(
|
1142 |
| ParseMessages.incompleteCloseTag(startLine), |
1143 |
| startLocation, |
1144 |
| startLine, |
1145 |
| cursorStart); |
1146 |
| |
1147 |
4993
| char ch = _templateData[_cursor];
|
1148 |
| |
1149 |
4993
| if (ch == '>')
|
1150 |
1234
| break;
|
1151 |
| |
1152 |
3759
| advance();
|
1153 |
| } |
1154 |
| |
1155 |
1234
| String tagName = new String(_templateData, tagStart, _cursor - tagStart);
|
1156 |
| |
1157 |
1234
| int stackPos = _stack.size() - 1;
|
1158 |
1234
| Tag tag = null;
|
1159 |
| |
1160 |
1234
| while (stackPos >= 0)
|
1161 |
| { |
1162 |
1306
| tag = (Tag) _stack.get(stackPos);
|
1163 |
| |
1164 |
1306
| if (tag.match(tagName))
|
1165 |
1232
| break;
|
1166 |
| |
1167 |
74
| if (tag._mustBalance)
|
1168 |
1
| templateParseProblem(ParseMessages.improperlyNestedCloseTag(
|
1169 |
| tagName, |
1170 |
| startLine, |
1171 |
| tag._tagName, |
1172 |
| tag._line), startLocation, startLine, cursorStart); |
1173 |
| |
1174 |
73
| stackPos--;
|
1175 |
| } |
1176 |
| |
1177 |
1233
| if (stackPos < 0)
|
1178 |
1
| templateParseProblem(
|
1179 |
| ParseMessages.unmatchedCloseTag(tagName, startLine), |
1180 |
| startLocation, |
1181 |
| startLine, |
1182 |
| cursorStart); |
1183 |
| |
1184 |
| |
1185 |
| |
1186 |
1232
| if (tag._content)
|
1187 |
| { |
1188 |
76
| addTextToken(cursorStart - 1);
|
1189 |
| |
1190 |
| |
1191 |
| |
1192 |
76
| _cursor = length;
|
1193 |
76
| _stack.clear();
|
1194 |
76
| return;
|
1195 |
| } |
1196 |
| |
1197 |
| |
1198 |
1156
| if (tag._component)
|
1199 |
| { |
1200 |
532
| addTextToken(cursorStart - 1);
|
1201 |
| |
1202 |
532
| _tokens.add(_factory.createCloseToken(tagName, getCurrentLocation()));
|
1203 |
| } |
1204 |
| else |
1205 |
| { |
1206 |
| |
1207 |
| |
1208 |
| |
1209 |
624
| if (_blockStart < 0 && !tag._removeTag && !_ignoring)
|
1210 |
102
| _blockStart = cursorStart;
|
1211 |
| } |
1212 |
| |
1213 |
| |
1214 |
| |
1215 |
1156
| for (int i = _stack.size() - 1; i >= stackPos; i--)
|
1216 |
1212
| _stack.remove(i);
|
1217 |
| |
1218 |
| |
1219 |
| |
1220 |
1156
| advance();
|
1221 |
| |
1222 |
| |
1223 |
| |
1224 |
| |
1225 |
| |
1226 |
1156
| if (tag._removeTag)
|
1227 |
101
| advanceOverWhitespace();
|
1228 |
| |
1229 |
| |
1230 |
| |
1231 |
| |
1232 |
1156
| if (tag._ignoringBody)
|
1233 |
222
| _ignoring = false;
|
1234 |
| } |
1235 |
| |
1236 |
| |
1237 |
| |
1238 |
| |
1239 |
| |
1240 |
| |
1241 |
85126
| private void advance()
|
1242 |
| { |
1243 |
85126
| int length = _templateData.length;
|
1244 |
| |
1245 |
85126
| if (_cursor >= length)
|
1246 |
0
| return;
|
1247 |
| |
1248 |
85126
| char ch = _templateData[_cursor];
|
1249 |
| |
1250 |
85126
| _cursor++;
|
1251 |
| |
1252 |
85126
| if (ch == '\n')
|
1253 |
| { |
1254 |
269
| _line++;
|
1255 |
269
| _currentLocation = null;
|
1256 |
269
| return;
|
1257 |
| } |
1258 |
| |
1259 |
| |
1260 |
| |
1261 |
84857
| if (ch == '\r')
|
1262 |
| { |
1263 |
2982
| _line++;
|
1264 |
2982
| _currentLocation = null;
|
1265 |
| |
1266 |
2982
| if (_cursor < length && _templateData[_cursor] == '\n')
|
1267 |
2981
| _cursor++;
|
1268 |
| |
1269 |
2982
| return;
|
1270 |
| } |
1271 |
| |
1272 |
| |
1273 |
| |
1274 |
| } |
1275 |
| |
1276 |
192
| private void advanceOverWhitespace()
|
1277 |
| { |
1278 |
192
| int length = _templateData.length;
|
1279 |
| |
1280 |
192
| while (_cursor < length)
|
1281 |
| { |
1282 |
929
| char ch = _templateData[_cursor];
|
1283 |
929
| if (!Character.isWhitespace(ch))
|
1284 |
187
| return;
|
1285 |
| |
1286 |
742
| advance();
|
1287 |
| } |
1288 |
| } |
1289 |
| |
1290 |
| |
1291 |
| |
1292 |
| |
1293 |
| |
1294 |
| |
1295 |
| |
1296 |
15
| private Map filter(Map input, String[] removeKeys)
|
1297 |
| { |
1298 |
15
| if (input == null || input.isEmpty())
|
1299 |
0
| return null;
|
1300 |
| |
1301 |
15
| Map result = null;
|
1302 |
| |
1303 |
15
| Iterator i = input.entrySet().iterator();
|
1304 |
| |
1305 |
15
| nextkey: while (i.hasNext())
|
1306 |
| { |
1307 |
20
| Map.Entry entry = (Map.Entry) i.next();
|
1308 |
| |
1309 |
20
| String key = (String) entry.getKey();
|
1310 |
| |
1311 |
20
| for (int j = 0; j < removeKeys.length; j++)
|
1312 |
| { |
1313 |
25
| if (key.equalsIgnoreCase(removeKeys[j]))
|
1314 |
17
| continue nextkey;
|
1315 |
| } |
1316 |
| |
1317 |
3
| if (result == null)
|
1318 |
2
| result = new HashMap(input.size());
|
1319 |
| |
1320 |
3
| result.put(key, entry.getValue());
|
1321 |
| } |
1322 |
| |
1323 |
15
| return result;
|
1324 |
| } |
1325 |
| |
1326 |
| |
1327 |
| |
1328 |
| |
1329 |
| |
1330 |
| |
1331 |
| |
1332 |
3463
| protected String findValueCaselessly(String key, Map map)
|
1333 |
| { |
1334 |
3463
| String result = (String) map.get(key);
|
1335 |
| |
1336 |
3463
| if (result != null)
|
1337 |
979
| return result;
|
1338 |
| |
1339 |
2484
| Iterator i = map.entrySet().iterator();
|
1340 |
2484
| while (i.hasNext())
|
1341 |
| { |
1342 |
2218
| Map.Entry entry = (Map.Entry) i.next();
|
1343 |
| |
1344 |
2218
| String entryKey = (String) entry.getKey();
|
1345 |
| |
1346 |
2218
| if (entryKey.equalsIgnoreCase(key))
|
1347 |
3
| return (String) entry.getValue();
|
1348 |
| } |
1349 |
| |
1350 |
2481
| return null;
|
1351 |
| } |
1352 |
| |
1353 |
| |
1354 |
| |
1355 |
| |
1356 |
| |
1357 |
| private static final String[] CONVERSIONS = |
1358 |
| { "<", "<", ">", ">", """, "\"", "&", "&" }; |
1359 |
| |
1360 |
| |
1361 |
| |
1362 |
| |
1363 |
| |
1364 |
| |
1365 |
| |
1366 |
| |
1367 |
603
| private String convertEntitiesToPlain(String input)
|
1368 |
| { |
1369 |
603
| int inputLength = input.length();
|
1370 |
| |
1371 |
603
| StringBuffer buffer = new StringBuffer(inputLength);
|
1372 |
| |
1373 |
603
| int cursor = 0;
|
1374 |
| |
1375 |
603
| outer: while (cursor < inputLength)
|
1376 |
| { |
1377 |
8680
| for (int i = 0; i < CONVERSIONS.length; i += 2)
|
1378 |
| { |
1379 |
34703
| String entity = CONVERSIONS[i];
|
1380 |
34703
| int entityLength = entity.length();
|
1381 |
34703
| String value = CONVERSIONS[i + 1];
|
1382 |
| |
1383 |
34703
| if (cursor + entityLength > inputLength)
|
1384 |
8521
| continue;
|
1385 |
| |
1386 |
26182
| if (input.substring(cursor, cursor + entityLength).equals(entity))
|
1387 |
| { |
1388 |
15
| buffer.append(value);
|
1389 |
15
| cursor += entityLength;
|
1390 |
15
| continue outer;
|
1391 |
| } |
1392 |
| } |
1393 |
| |
1394 |
8665
| buffer.append(input.charAt(cursor));
|
1395 |
8665
| cursor++;
|
1396 |
| } |
1397 |
| |
1398 |
603
| return buffer.toString().trim();
|
1399 |
| } |
1400 |
| |
1401 |
| |
1402 |
| |
1403 |
| |
1404 |
| |
1405 |
| |
1406 |
15
| private boolean checkBoolean(String key, Map map)
|
1407 |
| { |
1408 |
15
| String value = findValueCaselessly(key, map);
|
1409 |
| |
1410 |
15
| if (value == null)
|
1411 |
13
| return false;
|
1412 |
| |
1413 |
2
| return value.equalsIgnoreCase("true");
|
1414 |
| } |
1415 |
| |
1416 |
| |
1417 |
| |
1418 |
| |
1419 |
| |
1420 |
| |
1421 |
| |
1422 |
| |
1423 |
2011
| protected Location getCurrentLocation()
|
1424 |
| { |
1425 |
2011
| if (_currentLocation == null)
|
1426 |
1330
| _currentLocation = new LocationImpl(_resourceLocation, _line);
|
1427 |
| |
1428 |
2011
| return _currentLocation;
|
1429 |
| } |
1430 |
| |
1431 |
125
| public void setFactory(TemplateTokenFactory factory)
|
1432 |
| { |
1433 |
125
| _factory = factory;
|
1434 |
| } |
1435 |
| |
1436 |
| } |