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