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 |
127
| public TemplateParser()
|
288 |
| { |
289 |
127
| Perl5Compiler compiler = new Perl5Compiler();
|
290 |
| |
291 |
127
| try
|
292 |
| { |
293 |
127
| _simpleIdPattern = compiler.compile(SIMPLE_ID_PATTERN);
|
294 |
127
| _implicitIdPattern = compiler.compile(IMPLICIT_ID_PATTERN);
|
295 |
| } |
296 |
| catch (MalformedPatternException ex) |
297 |
| { |
298 |
0
| throw new ApplicationRuntimeException(ex);
|
299 |
| } |
300 |
| |
301 |
127
| _patternMatcher = new Perl5Matcher();
|
302 |
| } |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
191
| public TemplateToken[] parse(char[] templateData, ITemplateParserDelegate delegate,
|
319 |
| Resource resourceLocation) throws TemplateParseException |
320 |
| { |
321 |
191
| try
|
322 |
| { |
323 |
191
| beforeParse(templateData, delegate, resourceLocation);
|
324 |
| |
325 |
191
| parse();
|
326 |
| |
327 |
177
| return (TemplateToken[]) _tokens.toArray(new TemplateToken[_tokens.size()]);
|
328 |
| } |
329 |
| finally |
330 |
| { |
331 |
191
| afterParse();
|
332 |
| } |
333 |
| } |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
191
| protected void beforeParse(char[] templateData, ITemplateParserDelegate delegate,
|
340 |
| Resource resourceLocation) |
341 |
| { |
342 |
191
| _templateData = templateData;
|
343 |
191
| _resourceLocation = resourceLocation;
|
344 |
191
| _templateLocation = new LocationImpl(resourceLocation);
|
345 |
191
| _delegate = delegate;
|
346 |
191
| _ignoring = false;
|
347 |
191
| _line = 1;
|
348 |
191
| _componentAttributeName = delegate.getComponentAttributeName();
|
349 |
| } |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
191
| protected void afterParse()
|
356 |
| { |
357 |
191
| _delegate = null;
|
358 |
191
| _templateData = null;
|
359 |
191
| _resourceLocation = null;
|
360 |
191
| _templateLocation = null;
|
361 |
191
| _currentLocation = null;
|
362 |
191
| _stack.clear();
|
363 |
191
| _tokens.clear();
|
364 |
191
| _attributes.clear();
|
365 |
191
| _idAllocator.clear();
|
366 |
| } |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
| |
386 |
| |
387 |
14
| protected void templateParseProblem(String message, Location location, int line, int cursor)
|
388 |
| throws TemplateParseException |
389 |
| { |
390 |
14
| 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 |
16465
| private boolean lookahead(char[] match)
|
432 |
| { |
433 |
16465
| try
|
434 |
| { |
435 |
16465
| for (int i = 0; i < match.length; i++)
|
436 |
| { |
437 |
21923
| if (_templateData[_cursor + i] != match[i])
|
438 |
15071
| 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 |
191
| protected void parse() throws TemplateParseException
|
461 |
| { |
462 |
191
| _cursor = 0;
|
463 |
191
| _blockStart = -1;
|
464 |
191
| int length = _templateData.length;
|
465 |
| |
466 |
191
| while (_cursor < length)
|
467 |
| { |
468 |
21825
| if (_templateData[_cursor] != '<')
|
469 |
| { |
470 |
18782
| if (_blockStart < 0 && !_ignoring)
|
471 |
1155
| _blockStart = _cursor;
|
472 |
| |
473 |
18782
| advance();
|
474 |
18782
| continue;
|
475 |
| } |
476 |
| |
477 |
| |
478 |
| |
479 |
3043
| if (lookahead(CLOSE_TAG))
|
480 |
| { |
481 |
1235
| closeTag();
|
482 |
1232
| continue;
|
483 |
| } |
484 |
| |
485 |
1808
| if (lookahead(COMMENT_START))
|
486 |
| { |
487 |
80
| skipComment();
|
488 |
79
| continue;
|
489 |
| } |
490 |
| |
491 |
| |
492 |
| |
493 |
1728
| 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 |
1728
| private void startTag() throws TemplateParseException
|
572 |
| { |
573 |
1728
| int cursorStart = _cursor;
|
574 |
1728
| int length = _templateData.length;
|
575 |
1728
| String tagName = null;
|
576 |
1728
| boolean endOfTag = false;
|
577 |
1728
| boolean emptyTag = false;
|
578 |
1728
| int startLine = _line;
|
579 |
1728
| Location startLocation = new LocationImpl(_resourceLocation, startLine);
|
580 |
| |
581 |
1728
| tagBeginEvent(startLine, _cursor);
|
582 |
| |
583 |
1728
| advance();
|
584 |
| |
585 |
| |
586 |
| |
587 |
7371
| while (_cursor < length)
|
588 |
| { |
589 |
7371
| char ch = _templateData[_cursor];
|
590 |
| |
591 |
7371
| if (ch == '/' || ch == '>' || Character.isWhitespace(ch))
|
592 |
| { |
593 |
1728
| tagName = new String(_templateData, cursorStart + 1, _cursor - cursorStart - 1);
|
594 |
| |
595 |
1728
| break;
|
596 |
| } |
597 |
| |
598 |
5643
| advance();
|
599 |
| } |
600 |
| |
601 |
1728
| String attributeName = null;
|
602 |
1728
| int attributeNameStart = -1;
|
603 |
1728
| int attributeValueStart = -1;
|
604 |
1728
| int state = WAIT_FOR_ATTRIBUTE_NAME;
|
605 |
1728
| char quoteChar = 0;
|
606 |
| |
607 |
1728
| _attributes.clear();
|
608 |
| |
609 |
| |
610 |
| |
611 |
1728
| while (!endOfTag)
|
612 |
| { |
613 |
44620
| 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 |
44619
| char ch = _templateData[_cursor];
|
622 |
| |
623 |
44619
| switch (state)
|
624 |
| { |
625 |
6392
| case WAIT_FOR_ATTRIBUTE_NAME:
|
626 |
| |
627 |
| |
628 |
| |
629 |
| |
630 |
6392
| if (ch == '/')
|
631 |
| { |
632 |
495
| emptyTag = true;
|
633 |
495
| advance();
|
634 |
495
| break;
|
635 |
| } |
636 |
| |
637 |
5897
| if (ch == '>')
|
638 |
| { |
639 |
1724
| endOfTag = true;
|
640 |
1724
| break;
|
641 |
| } |
642 |
| |
643 |
4173
| if (Character.isWhitespace(ch))
|
644 |
| { |
645 |
2006
| advance();
|
646 |
2006
| break;
|
647 |
| } |
648 |
| |
649 |
| |
650 |
| |
651 |
| |
652 |
2167
| attributeNameStart = _cursor;
|
653 |
2167
| state = COLLECT_ATTRIBUTE_NAME;
|
654 |
2167
| advance();
|
655 |
2167
| break;
|
656 |
| |
657 |
11594
| case COLLECT_ATTRIBUTE_NAME:
|
658 |
| |
659 |
| |
660 |
| |
661 |
11594
| if (ch == '=' || ch == '/' || ch == '>' || Character.isWhitespace(ch))
|
662 |
| { |
663 |
2167
| attributeName = new String(_templateData, attributeNameStart, _cursor
|
664 |
| - attributeNameStart); |
665 |
| |
666 |
2167
| state = ADVANCE_PAST_EQUALS;
|
667 |
2167
| break;
|
668 |
| } |
669 |
| |
670 |
| |
671 |
| |
672 |
9427
| advance();
|
673 |
9427
| break;
|
674 |
| |
675 |
2310
| case ADVANCE_PAST_EQUALS:
|
676 |
| |
677 |
| |
678 |
| |
679 |
| |
680 |
| |
681 |
2310
| if (ch == '/' || ch == '>')
|
682 |
| { |
683 |
| |
684 |
| |
685 |
| |
686 |
145
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
687 |
145
| break;
|
688 |
| } |
689 |
| |
690 |
2165
| if (Character.isWhitespace(ch))
|
691 |
| { |
692 |
143
| advance();
|
693 |
143
| break;
|
694 |
| } |
695 |
| |
696 |
2022
| if (ch == '=')
|
697 |
| { |
698 |
1932
| state = WAIT_FOR_ATTRIBUTE_VALUE;
|
699 |
1932
| quoteChar = 0;
|
700 |
1932
| attributeValueStart = -1;
|
701 |
1932
| advance();
|
702 |
1932
| break;
|
703 |
| } |
704 |
| |
705 |
| |
706 |
| |
707 |
| |
708 |
| |
709 |
90
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
710 |
90
| break;
|
711 |
| |
712 |
1935
| case WAIT_FOR_ATTRIBUTE_VALUE:
|
713 |
| |
714 |
1935
| if (ch == '/' || ch == '>')
|
715 |
1
| templateParseProblem(ParseMessages.missingAttributeValue(
|
716 |
| tagName, |
717 |
| _line, |
718 |
| attributeName), getCurrentLocation(), _line, _cursor); |
719 |
| |
720 |
| |
721 |
| |
722 |
| |
723 |
1934
| if (Character.isWhitespace(ch))
|
724 |
| { |
725 |
3
| advance();
|
726 |
3
| break;
|
727 |
| } |
728 |
| |
729 |
1931
| if (ch == '\'' || ch == '"')
|
730 |
| { |
731 |
1902
| quoteChar = ch;
|
732 |
| |
733 |
1902
| state = COLLECT_QUOTED_VALUE;
|
734 |
1902
| advance();
|
735 |
1902
| attributeValueStart = _cursor;
|
736 |
1902
| attributeBeginEvent(attributeName, _line, attributeValueStart);
|
737 |
1902
| 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 |
22326
| case COLLECT_QUOTED_VALUE:
|
748 |
| |
749 |
| |
750 |
| |
751 |
| |
752 |
| |
753 |
22326
| if (ch == quoteChar)
|
754 |
| { |
755 |
1902
| String attributeValue = new String(_templateData, attributeValueStart,
|
756 |
| _cursor - attributeValueStart); |
757 |
| |
758 |
1902
| addAttributeIfUnique(tagName, attributeName, attributeValue);
|
759 |
| |
760 |
1900
| attributeEndEvent(_cursor);
|
761 |
| |
762 |
| |
763 |
1900
| advance();
|
764 |
1900
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
765 |
1900
| break;
|
766 |
| } |
767 |
| |
768 |
20424
| advance();
|
769 |
20424
| break;
|
770 |
| |
771 |
62
| case COLLECT_UNQUOTED_VALUE:
|
772 |
| |
773 |
| |
774 |
| |
775 |
| |
776 |
62
| if (ch == '/' || ch == '>' || Character.isWhitespace(ch))
|
777 |
| { |
778 |
29
| String attributeValue = new String(_templateData, attributeValueStart,
|
779 |
| _cursor - attributeValueStart); |
780 |
| |
781 |
29
| addAttributeIfUnique(tagName, attributeName, attributeValue);
|
782 |
29
| attributeEndEvent(_cursor);
|
783 |
| |
784 |
29
| state = WAIT_FOR_ATTRIBUTE_NAME;
|
785 |
29
| break;
|
786 |
| } |
787 |
| |
788 |
33
| advance();
|
789 |
33
| break;
|
790 |
| } |
791 |
| } |
792 |
| |
793 |
1724
| tagEndEvent(_cursor);
|
794 |
| |
795 |
| |
796 |
| |
797 |
1724
| String localizationKey = findValueCaselessly(LOCALIZATION_KEY_ATTRIBUTE_NAME, _attributes);
|
798 |
1724
| String jwcId = findValueCaselessly(_componentAttributeName, _attributes);
|
799 |
| |
800 |
1724
| if (localizationKey != null && tagName.equalsIgnoreCase("span") && jwcId == null)
|
801 |
| { |
802 |
16
| if (_ignoring)
|
803 |
1
| templateParseProblem(
|
804 |
| ParseMessages.componentMayNotBeIgnored(tagName, startLine), |
805 |
| startLocation, |
806 |
| startLine, |
807 |
| cursorStart); |
808 |
| |
809 |
| |
810 |
| |
811 |
| |
812 |
15
| if (!emptyTag)
|
813 |
| { |
814 |
3
| Tag tag = new Tag(tagName, startLine);
|
815 |
| |
816 |
3
| tag._component = false;
|
817 |
3
| tag._removeTag = true;
|
818 |
3
| tag._ignoringBody = true;
|
819 |
3
| tag._mustBalance = true;
|
820 |
| |
821 |
3
| _stack.add(tag);
|
822 |
| |
823 |
| |
824 |
| |
825 |
3
| _ignoring = true;
|
826 |
| } |
827 |
| else |
828 |
| { |
829 |
| |
830 |
12
| advance();
|
831 |
12
| advanceOverWhitespace();
|
832 |
| } |
833 |
| |
834 |
| |
835 |
| |
836 |
15
| addTextToken(cursorStart - 1);
|
837 |
| |
838 |
15
| boolean raw = checkBoolean(RAW_ATTRIBUTE_NAME, _attributes);
|
839 |
| |
840 |
15
| Map attributes = filter(_attributes, new String[]
|
841 |
| { LOCALIZATION_KEY_ATTRIBUTE_NAME, RAW_ATTRIBUTE_NAME }); |
842 |
| |
843 |
15
| TemplateToken token = _factory.createLocalizationToken(
|
844 |
| tagName, |
845 |
| localizationKey, |
846 |
| raw, |
847 |
| attributes, |
848 |
| startLocation); |
849 |
| |
850 |
15
| _tokens.add(token);
|
851 |
| |
852 |
15
| return;
|
853 |
| } |
854 |
| |
855 |
1708
| if (jwcId != null)
|
856 |
| { |
857 |
964
| processComponentStart(tagName, jwcId, emptyTag, startLine, cursorStart, startLocation);
|
858 |
959
| return;
|
859 |
| } |
860 |
| |
861 |
| |
862 |
| |
863 |
| |
864 |
744
| if (!emptyTag)
|
865 |
| { |
866 |
669
| Tag tag = new Tag(tagName, startLine);
|
867 |
669
| _stack.add(tag);
|
868 |
| } |
869 |
| |
870 |
| |
871 |
| |
872 |
744
| if (_blockStart < 0 && !_ignoring)
|
873 |
44
| _blockStart = cursorStart;
|
874 |
| |
875 |
744
| advance();
|
876 |
| } |
877 |
| |
878 |
| |
879 |
| |
880 |
| |
881 |
| |
882 |
| |
883 |
1931
| private void addAttributeIfUnique(String tagName, String attributeName, String attributeValue)
|
884 |
| throws TemplateParseException |
885 |
| { |
886 |
| |
887 |
1931
| if (_attributes.containsKey(attributeName))
|
888 |
2
| templateParseProblem(
|
889 |
| ParseMessages.duplicateTagAttribute(tagName, _line, attributeName), |
890 |
| getCurrentLocation(), |
891 |
| _line, |
892 |
| _cursor); |
893 |
| |
894 |
1929
| _attributes.put(attributeName, attributeValue);
|
895 |
| } |
896 |
| |
897 |
| |
898 |
| |
899 |
| |
900 |
| |
901 |
| |
902 |
| |
903 |
| |
904 |
| |
905 |
| |
906 |
| |
907 |
1728
| protected void tagBeginEvent(int startLine, int cursorPosition)
|
908 |
| { |
909 |
| } |
910 |
| |
911 |
| |
912 |
| |
913 |
| |
914 |
| |
915 |
| |
916 |
1724
| protected void tagEndEvent(int cursorPosition)
|
917 |
| { |
918 |
| } |
919 |
| |
920 |
| |
921 |
| |
922 |
| |
923 |
| |
924 |
| |
925 |
1931
| protected void attributeBeginEvent(String attributeName, int startLine, int cursorPosition)
|
926 |
| { |
927 |
| } |
928 |
| |
929 |
| |
930 |
| |
931 |
| |
932 |
| |
933 |
| |
934 |
1929
| protected void attributeEndEvent(int cursorPosition)
|
935 |
| { |
936 |
| } |
937 |
| |
938 |
964
| private void processComponentStart(String tagName, String jwcId, boolean emptyTag,
|
939 |
| int startLine, int cursorStart, Location startLocation) throws TemplateParseException |
940 |
| { |
941 |
964
| if (jwcId.equalsIgnoreCase(CONTENT_ID))
|
942 |
| { |
943 |
78
| processContentTag(tagName, startLine, cursorStart, emptyTag);
|
944 |
| |
945 |
77
| return;
|
946 |
| } |
947 |
| |
948 |
886
| boolean isRemoveId = jwcId.equalsIgnoreCase(REMOVE_ID);
|
949 |
| |
950 |
886
| if (_ignoring && !isRemoveId)
|
951 |
2
| templateParseProblem(
|
952 |
| ParseMessages.componentMayNotBeIgnored(tagName, startLine), |
953 |
| startLocation, |
954 |
| startLine, |
955 |
| cursorStart); |
956 |
| |
957 |
884
| String type = null;
|
958 |
884
| boolean allowBody = false;
|
959 |
| |
960 |
884
| if (_patternMatcher.matches(jwcId, _implicitIdPattern))
|
961 |
| { |
962 |
498
| MatchResult match = _patternMatcher.getMatch();
|
963 |
| |
964 |
498
| jwcId = match.group(IMPLICIT_ID_PATTERN_ID_GROUP);
|
965 |
498
| type = match.group(IMPLICIT_ID_PATTERN_TYPE_GROUP);
|
966 |
| |
967 |
498
| String libraryId = match.group(IMPLICIT_ID_PATTERN_LIBRARY_ID_GROUP);
|
968 |
498
| String simpleType = match.group(IMPLICIT_ID_PATTERN_SIMPLE_TYPE_GROUP);
|
969 |
| |
970 |
| |
971 |
| |
972 |
| |
973 |
| |
974 |
| |
975 |
| |
976 |
| |
977 |
| |
978 |
498
| if (jwcId == null)
|
979 |
432
| jwcId = _idAllocator.allocateId("$" + simpleType);
|
980 |
| |
981 |
498
| try
|
982 |
| { |
983 |
498
| allowBody = _delegate.getAllowBody(libraryId, simpleType, startLocation);
|
984 |
| } |
985 |
| catch (ApplicationRuntimeException e) |
986 |
| { |
987 |
| |
988 |
0
| templateParseProblem(e, startLine, cursorStart);
|
989 |
| } |
990 |
| |
991 |
| } |
992 |
| else |
993 |
| { |
994 |
386
| if (!isRemoveId)
|
995 |
| { |
996 |
282
| if (!_patternMatcher.matches(jwcId, _simpleIdPattern))
|
997 |
0
| templateParseProblem(
|
998 |
| ParseMessages.componentIdInvalid(tagName, startLine, jwcId), |
999 |
| startLocation, |
1000 |
| startLine, |
1001 |
| cursorStart); |
1002 |
| |
1003 |
282
| if (!_delegate.getKnownComponent(jwcId))
|
1004 |
1
| templateParseProblem(
|
1005 |
| ParseMessages.unknownComponentId(tagName, startLine, jwcId), |
1006 |
| startLocation, |
1007 |
| startLine, |
1008 |
| cursorStart); |
1009 |
| |
1010 |
281
| try
|
1011 |
| { |
1012 |
281
| allowBody = _delegate.getAllowBody(jwcId, startLocation);
|
1013 |
| } |
1014 |
| catch (ApplicationRuntimeException e) |
1015 |
| { |
1016 |
| |
1017 |
0
| templateParseProblem(e, startLine, cursorStart);
|
1018 |
| } |
1019 |
| } |
1020 |
| } |
1021 |
| |
1022 |
| |
1023 |
| |
1024 |
| |
1025 |
| |
1026 |
883
| boolean ignoreBody = !emptyTag && (isRemoveId || !allowBody);
|
1027 |
| |
1028 |
883
| if (_ignoring && ignoreBody)
|
1029 |
1
| templateParseProblem(ParseMessages.nestedIgnore(tagName, startLine), new LocationImpl(
|
1030 |
| _resourceLocation, startLine), startLine, cursorStart); |
1031 |
| |
1032 |
882
| if (!emptyTag)
|
1033 |
639
| pushNewTag(tagName, startLine, isRemoveId, ignoreBody);
|
1034 |
| |
1035 |
| |
1036 |
| |
1037 |
882
| addTextToken(cursorStart - 1);
|
1038 |
| |
1039 |
882
| if (!isRemoveId)
|
1040 |
| { |
1041 |
779
| addOpenToken(tagName, jwcId, type, startLocation);
|
1042 |
| |
1043 |
779
| if (emptyTag)
|
1044 |
243
| _tokens.add(_factory.createCloseToken(tagName, getCurrentLocation()));
|
1045 |
| } |
1046 |
| |
1047 |
882
| advance();
|
1048 |
| } |
1049 |
| |
1050 |
639
| private void pushNewTag(String tagName, int startLine, boolean isRemoveId, boolean ignoreBody)
|
1051 |
| { |
1052 |
639
| Tag tag = new Tag(tagName, startLine);
|
1053 |
| |
1054 |
639
| tag._component = !isRemoveId;
|
1055 |
639
| tag._removeTag = isRemoveId;
|
1056 |
| |
1057 |
639
| tag._ignoringBody = ignoreBody;
|
1058 |
| |
1059 |
639
| _ignoring = tag._ignoringBody;
|
1060 |
| |
1061 |
639
| tag._mustBalance = true;
|
1062 |
| |
1063 |
639
| _stack.add(tag);
|
1064 |
| } |
1065 |
| |
1066 |
78
| private void processContentTag(String tagName, int startLine, int cursorStart, boolean emptyTag)
|
1067 |
| throws TemplateParseException |
1068 |
| { |
1069 |
78
| if (_ignoring)
|
1070 |
1
| templateParseProblem(
|
1071 |
| ParseMessages.contentBlockMayNotBeIgnored(tagName, startLine), |
1072 |
| new LocationImpl(_resourceLocation, startLine), |
1073 |
| startLine, |
1074 |
| cursorStart); |
1075 |
| |
1076 |
77
| if (emptyTag)
|
1077 |
0
| templateParseProblem(
|
1078 |
| ParseMessages.contentBlockMayNotBeEmpty(tagName, startLine), |
1079 |
| new LocationImpl(_resourceLocation, startLine), |
1080 |
| startLine, |
1081 |
| cursorStart); |
1082 |
| |
1083 |
77
| _tokens.clear();
|
1084 |
77
| _blockStart = -1;
|
1085 |
| |
1086 |
77
| Tag tag = new Tag(tagName, startLine);
|
1087 |
| |
1088 |
77
| tag._mustBalance = true;
|
1089 |
77
| tag._content = true;
|
1090 |
| |
1091 |
77
| _stack.clear();
|
1092 |
77
| _stack.add(tag);
|
1093 |
| |
1094 |
77
| advance();
|
1095 |
| } |
1096 |
| |
1097 |
779
| private void addOpenToken(String tagName, String jwcId, String type, Location location)
|
1098 |
| { |
1099 |
779
| OpenToken token = _factory.createOpenToken(tagName, jwcId, type, location);
|
1100 |
779
| _tokens.add(token);
|
1101 |
| |
1102 |
779
| if (_attributes.isEmpty())
|
1103 |
0
| return;
|
1104 |
| |
1105 |
779
| Iterator i = _attributes.entrySet().iterator();
|
1106 |
779
| while (i.hasNext())
|
1107 |
| { |
1108 |
1382
| Map.Entry entry = (Map.Entry) i.next();
|
1109 |
| |
1110 |
1382
| String key = (String) entry.getKey();
|
1111 |
| |
1112 |
1382
| if (key.equalsIgnoreCase(_componentAttributeName))
|
1113 |
779
| continue;
|
1114 |
| |
1115 |
603
| String value = (String) entry.getValue();
|
1116 |
| |
1117 |
603
| addAttributeToToken(token, key, value);
|
1118 |
| } |
1119 |
| } |
1120 |
| |
1121 |
| |
1122 |
| |
1123 |
| |
1124 |
| |
1125 |
| |
1126 |
| |
1127 |
603
| private void addAttributeToToken(OpenToken token, String name, String attributeValue)
|
1128 |
| { |
1129 |
603
| token.addAttribute(name, convertEntitiesToPlain(attributeValue));
|
1130 |
| } |
1131 |
| |
1132 |
| |
1133 |
| |
1134 |
| |
1135 |
| |
1136 |
| |
1137 |
| |
1138 |
| |
1139 |
| |
1140 |
| |
1141 |
| |
1142 |
| |
1143 |
| |
1144 |
| |
1145 |
| |
1146 |
1235
| private void closeTag() throws TemplateParseException
|
1147 |
| { |
1148 |
1235
| int cursorStart = _cursor;
|
1149 |
1235
| int length = _templateData.length;
|
1150 |
1235
| int startLine = _line;
|
1151 |
| |
1152 |
1235
| Location startLocation = getCurrentLocation();
|
1153 |
| |
1154 |
1235
| _cursor += CLOSE_TAG.length;
|
1155 |
| |
1156 |
1235
| int tagStart = _cursor;
|
1157 |
| |
1158 |
1235
| while (true)
|
1159 |
| { |
1160 |
4994
| if (_cursor >= length)
|
1161 |
1
| templateParseProblem(
|
1162 |
| ParseMessages.incompleteCloseTag(startLine), |
1163 |
| startLocation, |
1164 |
| startLine, |
1165 |
| cursorStart); |
1166 |
| |
1167 |
4993
| char ch = _templateData[_cursor];
|
1168 |
| |
1169 |
4993
| if (ch == '>')
|
1170 |
1234
| break;
|
1171 |
| |
1172 |
3759
| advance();
|
1173 |
| } |
1174 |
| |
1175 |
1234
| String tagName = new String(_templateData, tagStart, _cursor - tagStart);
|
1176 |
| |
1177 |
1234
| int stackPos = _stack.size() - 1;
|
1178 |
1234
| Tag tag = null;
|
1179 |
| |
1180 |
1234
| while (stackPos >= 0)
|
1181 |
| { |
1182 |
1306
| tag = (Tag) _stack.get(stackPos);
|
1183 |
| |
1184 |
1306
| if (tag.match(tagName))
|
1185 |
1232
| break;
|
1186 |
| |
1187 |
74
| if (tag._mustBalance)
|
1188 |
1
| templateParseProblem(ParseMessages.improperlyNestedCloseTag(
|
1189 |
| tagName, |
1190 |
| startLine, |
1191 |
| tag._tagName, |
1192 |
| tag._line), startLocation, startLine, cursorStart); |
1193 |
| |
1194 |
73
| stackPos--;
|
1195 |
| } |
1196 |
| |
1197 |
1233
| if (stackPos < 0)
|
1198 |
1
| templateParseProblem(
|
1199 |
| ParseMessages.unmatchedCloseTag(tagName, startLine), |
1200 |
| startLocation, |
1201 |
| startLine, |
1202 |
| cursorStart); |
1203 |
| |
1204 |
| |
1205 |
| |
1206 |
1232
| if (tag._content)
|
1207 |
| { |
1208 |
76
| addTextToken(cursorStart - 1);
|
1209 |
| |
1210 |
| |
1211 |
| |
1212 |
76
| _cursor = length;
|
1213 |
76
| _stack.clear();
|
1214 |
76
| return;
|
1215 |
| } |
1216 |
| |
1217 |
| |
1218 |
1156
| if (tag._component)
|
1219 |
| { |
1220 |
532
| addTextToken(cursorStart - 1);
|
1221 |
| |
1222 |
532
| _tokens.add(_factory.createCloseToken(tagName, getCurrentLocation()));
|
1223 |
| } |
1224 |
| else |
1225 |
| { |
1226 |
| |
1227 |
| |
1228 |
| |
1229 |
624
| if (_blockStart < 0 && !tag._removeTag && !_ignoring)
|
1230 |
102
| _blockStart = cursorStart;
|
1231 |
| } |
1232 |
| |
1233 |
| |
1234 |
| |
1235 |
1156
| for (int i = _stack.size() - 1; i >= stackPos; i--)
|
1236 |
1212
| _stack.remove(i);
|
1237 |
| |
1238 |
| |
1239 |
| |
1240 |
1156
| advance();
|
1241 |
| |
1242 |
| |
1243 |
| |
1244 |
| |
1245 |
| |
1246 |
1156
| if (tag._removeTag)
|
1247 |
101
| advanceOverWhitespace();
|
1248 |
| |
1249 |
| |
1250 |
| |
1251 |
| |
1252 |
1156
| if (tag._ignoringBody)
|
1253 |
222
| _ignoring = false;
|
1254 |
| } |
1255 |
| |
1256 |
| |
1257 |
| |
1258 |
| |
1259 |
| |
1260 |
| |
1261 |
85492
| private void advance()
|
1262 |
| { |
1263 |
85492
| int length = _templateData.length;
|
1264 |
| |
1265 |
85492
| if (_cursor >= length)
|
1266 |
0
| return;
|
1267 |
| |
1268 |
85492
| char ch = _templateData[_cursor];
|
1269 |
| |
1270 |
85492
| _cursor++;
|
1271 |
| |
1272 |
85492
| if (ch == '\n')
|
1273 |
| { |
1274 |
269
| _line++;
|
1275 |
269
| _currentLocation = null;
|
1276 |
269
| return;
|
1277 |
| } |
1278 |
| |
1279 |
| |
1280 |
| |
1281 |
85223
| if (ch == '\r')
|
1282 |
| { |
1283 |
2986
| _line++;
|
1284 |
2986
| _currentLocation = null;
|
1285 |
| |
1286 |
2986
| if (_cursor < length && _templateData[_cursor] == '\n')
|
1287 |
2985
| _cursor++;
|
1288 |
| |
1289 |
2986
| return;
|
1290 |
| } |
1291 |
| |
1292 |
| |
1293 |
| |
1294 |
| } |
1295 |
| |
1296 |
192
| private void advanceOverWhitespace()
|
1297 |
| { |
1298 |
192
| int length = _templateData.length;
|
1299 |
| |
1300 |
192
| while (_cursor < length)
|
1301 |
| { |
1302 |
929
| char ch = _templateData[_cursor];
|
1303 |
929
| if (!Character.isWhitespace(ch))
|
1304 |
187
| return;
|
1305 |
| |
1306 |
742
| advance();
|
1307 |
| } |
1308 |
| } |
1309 |
| |
1310 |
| |
1311 |
| |
1312 |
| |
1313 |
| |
1314 |
| |
1315 |
| |
1316 |
15
| private Map filter(Map input, String[] removeKeys)
|
1317 |
| { |
1318 |
15
| if (input == null || input.isEmpty())
|
1319 |
0
| return null;
|
1320 |
| |
1321 |
15
| Map result = null;
|
1322 |
| |
1323 |
15
| Iterator i = input.entrySet().iterator();
|
1324 |
| |
1325 |
15
| nextkey: while (i.hasNext())
|
1326 |
| { |
1327 |
20
| Map.Entry entry = (Map.Entry) i.next();
|
1328 |
| |
1329 |
20
| String key = (String) entry.getKey();
|
1330 |
| |
1331 |
20
| for (int j = 0; j < removeKeys.length; j++)
|
1332 |
| { |
1333 |
25
| if (key.equalsIgnoreCase(removeKeys[j]))
|
1334 |
17
| continue nextkey;
|
1335 |
| } |
1336 |
| |
1337 |
3
| if (result == null)
|
1338 |
2
| result = new HashMap(input.size());
|
1339 |
| |
1340 |
3
| result.put(key, entry.getValue());
|
1341 |
| } |
1342 |
| |
1343 |
15
| return result;
|
1344 |
| } |
1345 |
| |
1346 |
| |
1347 |
| |
1348 |
| |
1349 |
| |
1350 |
| |
1351 |
| |
1352 |
3463
| protected String findValueCaselessly(String key, Map map)
|
1353 |
| { |
1354 |
3463
| String result = (String) map.get(key);
|
1355 |
| |
1356 |
3463
| if (result != null)
|
1357 |
979
| return result;
|
1358 |
| |
1359 |
2484
| Iterator i = map.entrySet().iterator();
|
1360 |
2484
| while (i.hasNext())
|
1361 |
| { |
1362 |
2218
| Map.Entry entry = (Map.Entry) i.next();
|
1363 |
| |
1364 |
2218
| String entryKey = (String) entry.getKey();
|
1365 |
| |
1366 |
2218
| if (entryKey.equalsIgnoreCase(key))
|
1367 |
3
| return (String) entry.getValue();
|
1368 |
| } |
1369 |
| |
1370 |
2481
| return null;
|
1371 |
| } |
1372 |
| |
1373 |
| |
1374 |
| |
1375 |
| |
1376 |
| |
1377 |
| private static final String[] CONVERSIONS = |
1378 |
| { "<", "<", ">", ">", """, "\"", "&", "&" }; |
1379 |
| |
1380 |
| |
1381 |
| |
1382 |
| |
1383 |
| |
1384 |
| |
1385 |
| |
1386 |
| |
1387 |
603
| private String convertEntitiesToPlain(String input)
|
1388 |
| { |
1389 |
603
| int inputLength = input.length();
|
1390 |
| |
1391 |
603
| StringBuffer buffer = new StringBuffer(inputLength);
|
1392 |
| |
1393 |
603
| int cursor = 0;
|
1394 |
| |
1395 |
603
| outer: while (cursor < inputLength)
|
1396 |
| { |
1397 |
8680
| for (int i = 0; i < CONVERSIONS.length; i += 2)
|
1398 |
| { |
1399 |
34703
| String entity = CONVERSIONS[i];
|
1400 |
34703
| int entityLength = entity.length();
|
1401 |
34703
| String value = CONVERSIONS[i + 1];
|
1402 |
| |
1403 |
34703
| if (cursor + entityLength > inputLength)
|
1404 |
8521
| continue;
|
1405 |
| |
1406 |
26182
| if (input.substring(cursor, cursor + entityLength).equals(entity))
|
1407 |
| { |
1408 |
15
| buffer.append(value);
|
1409 |
15
| cursor += entityLength;
|
1410 |
15
| continue outer;
|
1411 |
| } |
1412 |
| } |
1413 |
| |
1414 |
8665
| buffer.append(input.charAt(cursor));
|
1415 |
8665
| cursor++;
|
1416 |
| } |
1417 |
| |
1418 |
603
| return buffer.toString().trim();
|
1419 |
| } |
1420 |
| |
1421 |
| |
1422 |
| |
1423 |
| |
1424 |
| |
1425 |
| |
1426 |
15
| private boolean checkBoolean(String key, Map map)
|
1427 |
| { |
1428 |
15
| String value = findValueCaselessly(key, map);
|
1429 |
| |
1430 |
15
| if (value == null)
|
1431 |
13
| return false;
|
1432 |
| |
1433 |
2
| return value.equalsIgnoreCase("true");
|
1434 |
| } |
1435 |
| |
1436 |
| |
1437 |
| |
1438 |
| |
1439 |
| |
1440 |
| |
1441 |
| |
1442 |
| |
1443 |
2013
| protected Location getCurrentLocation()
|
1444 |
| { |
1445 |
2013
| if (_currentLocation == null)
|
1446 |
1332
| _currentLocation = new LocationImpl(_resourceLocation, _line);
|
1447 |
| |
1448 |
2013
| return _currentLocation;
|
1449 |
| } |
1450 |
| |
1451 |
127
| public void setFactory(TemplateTokenFactory factory)
|
1452 |
| { |
1453 |
127
| _factory = factory;
|
1454 |
| } |
1455 |
| |
1456 |
| } |