forked from apache/tomcat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
1798 lines (1621 loc) · 66.6 KB
/
Parser.java
File metadata and controls
1798 lines (1621 loc) · 66.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.compiler;
import java.io.CharArrayWriter;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.jsp.tagext.TagAttributeInfo;
import javax.servlet.jsp.tagext.TagFileInfo;
import javax.servlet.jsp.tagext.TagInfo;
import javax.servlet.jsp.tagext.TagLibraryInfo;
import org.apache.jasper.JasperException;
import org.apache.jasper.JspCompilationContext;
import org.apache.jasper.util.UniqueAttributesImpl;
import org.apache.tomcat.util.descriptor.tld.TldResourcePath;
import org.apache.tomcat.util.scan.Jar;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
/**
* This class implements a parser for a JSP page (non-xml view). JSP page
* grammar is included here for reference. The token '#' that appears in the
* production indicates the current input token location in the production.
*
* @author Kin-man Chung
* @author Shawn Bayern
* @author Mark Roth
*/
class Parser implements TagConstants {
private final ParserController parserController;
private final JspCompilationContext ctxt;
private final JspReader reader;
private Mark start;
private final ErrorDispatcher err;
private int scriptlessCount;
private final boolean isTagFile;
private final boolean directivesOnly;
private final Jar jar;
private final PageInfo pageInfo;
// Virtual body content types, to make parsing a little easier.
// These are not accessible from outside the parser.
private static final String JAVAX_BODY_CONTENT_PARAM =
"JAVAX_BODY_CONTENT_PARAM";
private static final String JAVAX_BODY_CONTENT_PLUGIN =
"JAVAX_BODY_CONTENT_PLUGIN";
private static final String JAVAX_BODY_CONTENT_TEMPLATE_TEXT =
"JAVAX_BODY_CONTENT_TEMPLATE_TEXT";
/* System property that controls if the strict white space rules are
* applied.
*/
private static final boolean STRICT_WHITESPACE = Boolean.valueOf(
System.getProperty(
"org.apache.jasper.compiler.Parser.STRICT_WHITESPACE",
"true")).booleanValue();
/**
* The constructor
*/
private Parser(ParserController pc, JspReader reader, boolean isTagFile,
boolean directivesOnly, Jar jar) {
this.parserController = pc;
this.ctxt = pc.getJspCompilationContext();
this.pageInfo = pc.getCompiler().getPageInfo();
this.err = pc.getCompiler().getErrorDispatcher();
this.reader = reader;
this.scriptlessCount = 0;
this.isTagFile = isTagFile;
this.directivesOnly = directivesOnly;
this.jar = jar;
start = reader.mark();
}
/**
* The main entry for Parser
*
* @param pc
* The ParseController, use for getting other objects in compiler
* and for parsing included pages
* @param reader
* To read the page
* @param parent
* The parent node to this page, null for top level page
* @return list of nodes representing the parsed page
*/
public static Node.Nodes parse(ParserController pc, JspReader reader,
Node parent, boolean isTagFile, boolean directivesOnly,
Jar jar, String pageEnc, String jspConfigPageEnc,
boolean isDefaultPageEncoding, boolean isBomPresent)
throws JasperException {
Parser parser = new Parser(pc, reader, isTagFile, directivesOnly, jar);
Node.Root root = new Node.Root(reader.mark(), parent, false);
root.setPageEncoding(pageEnc);
root.setJspConfigPageEncoding(jspConfigPageEnc);
root.setIsDefaultPageEncoding(isDefaultPageEncoding);
root.setIsBomPresent(isBomPresent);
// For the Top level page, add include-prelude and include-coda
PageInfo pageInfo = pc.getCompiler().getPageInfo();
if (parent == null && !isTagFile) {
parser.addInclude(root, pageInfo.getIncludePrelude());
}
if (directivesOnly) {
parser.parseFileDirectives(root);
} else {
while (reader.hasMoreInput()) {
parser.parseElements(root);
}
}
if (parent == null && !isTagFile) {
parser.addInclude(root, pageInfo.getIncludeCoda());
}
Node.Nodes page = new Node.Nodes(root);
return page;
}
/**
* Attributes ::= (S Attribute)* S?
*/
Attributes parseAttributes() throws JasperException {
return parseAttributes(false);
}
Attributes parseAttributes(boolean pageDirective) throws JasperException {
UniqueAttributesImpl attrs = new UniqueAttributesImpl(pageDirective);
reader.skipSpaces();
int ws = 1;
try {
while (parseAttribute(attrs)) {
if (ws == 0 && STRICT_WHITESPACE) {
err.jspError(reader.mark(),
"jsp.error.attribute.nowhitespace");
}
ws = reader.skipSpaces();
}
} catch (IllegalArgumentException iae) {
// Duplicate attribute
err.jspError(reader.mark(), "jsp.error.attribute.duplicate");
}
return attrs;
}
/**
* Parse Attributes for a reader, provided for external use
*/
public static Attributes parseAttributes(ParserController pc,
JspReader reader) throws JasperException {
Parser tmpParser = new Parser(pc, reader, false, false, null);
return tmpParser.parseAttributes(true);
}
/**
* Attribute ::= Name S? Eq S? ( '"<%=' RTAttributeValueDouble | '"'
* AttributeValueDouble | "'<%=" RTAttributeValueSingle | "'"
* AttributeValueSingle } Note: JSP and XML spec does not allow while spaces
* around Eq. It is added to be backward compatible with Tomcat, and with
* other xml parsers.
*/
private boolean parseAttribute(AttributesImpl attrs)
throws JasperException {
// Get the qualified name
String qName = parseName();
if (qName == null)
return false;
// Determine prefix and local name components
String localName = qName;
String uri = "";
int index = qName.indexOf(':');
if (index != -1) {
String prefix = qName.substring(0, index);
uri = pageInfo.getURI(prefix);
if (uri == null) {
err.jspError(reader.mark(),
"jsp.error.attribute.invalidPrefix", prefix);
}
localName = qName.substring(index + 1);
}
reader.skipSpaces();
if (!reader.matches("="))
err.jspError(reader.mark(), "jsp.error.attribute.noequal");
reader.skipSpaces();
char quote = (char) reader.nextChar();
if (quote != '\'' && quote != '"')
err.jspError(reader.mark(), "jsp.error.attribute.noquote");
String watchString = "";
if (reader.matches("<%="))
watchString = "%>";
watchString = watchString + quote;
String attrValue = parseAttributeValue(watchString);
attrs.addAttribute(uri, localName, qName, "CDATA", attrValue);
return true;
}
/**
* Name ::= (Letter | '_' | ':') (Letter | Digit | '.' | '_' | '-' | ':')*
*/
private String parseName() {
char ch = (char) reader.peekChar();
if (Character.isLetter(ch) || ch == '_' || ch == ':') {
StringBuilder buf = new StringBuilder();
buf.append(ch);
reader.nextChar();
ch = (char) reader.peekChar();
while (Character.isLetter(ch) || Character.isDigit(ch) || ch == '.'
|| ch == '_' || ch == '-' || ch == ':') {
buf.append(ch);
reader.nextChar();
ch = (char) reader.peekChar();
}
return buf.toString();
}
return null;
}
/**
* AttributeValueDouble ::= (QuotedChar - '"')* ('"' | <TRANSLATION_ERROR>)
* RTAttributeValueDouble ::= ((QuotedChar - '"')* - ((QuotedChar-'"')'%>"')
* ('%>"' | TRANSLATION_ERROR)
*/
private String parseAttributeValue(String watch) throws JasperException {
Mark start = reader.mark();
Mark stop = reader.skipUntilIgnoreEsc(watch);
if (stop == null) {
err.jspError(start, "jsp.error.attribute.unterminated", watch);
}
String ret = null;
try {
char quote = watch.charAt(watch.length() - 1);
// If watch is longer than 1 character this is a scripting
// expression and EL is always ignored
boolean isElIgnored =
pageInfo.isELIgnored() || watch.length() > 1;
ret = AttributeParser.getUnquoted(reader.getText(start, stop),
quote, isElIgnored,
pageInfo.isDeferredSyntaxAllowedAsLiteral());
} catch (IllegalArgumentException iae) {
err.jspError(start, iae.getMessage());
}
if (watch.length() == 1) // quote
return ret;
// Put back delimiter '<%=' and '%>', since they are needed if the
// attribute does not allow RTexpression.
return "<%=" + ret + "%>";
}
private String parseScriptText(String tx) {
CharArrayWriter cw = new CharArrayWriter();
int size = tx.length();
int i = 0;
while (i < size) {
char ch = tx.charAt(i);
if (i + 2 < size && ch == '%' && tx.charAt(i + 1) == '\\'
&& tx.charAt(i + 2) == '>') {
cw.write('%');
cw.write('>');
i += 3;
} else {
cw.write(ch);
++i;
}
}
cw.close();
return cw.toString();
}
/*
* Invokes parserController to parse the included page
*/
private void processIncludeDirective(String file, Node parent)
throws JasperException {
if (file == null) {
return;
}
try {
parserController.parse(file, parent, jar);
} catch (FileNotFoundException ex) {
err.jspError(start, "jsp.error.file.not.found", file);
} catch (Exception ex) {
err.jspError(start, ex.getMessage());
}
}
/*
* Parses a page directive with the following syntax: PageDirective ::= ( S
* Attribute)*
*/
private void parsePageDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes(true);
Node.PageDirective n = new Node.PageDirective(attrs, start, parent);
/*
* A page directive may contain multiple 'import' attributes, each of
* which consists of a comma-separated list of package names. Store each
* list with the node, where it is parsed.
*/
for (int i = 0; i < attrs.getLength(); i++) {
if ("import".equals(attrs.getQName(i))) {
n.addImport(attrs.getValue(i));
}
}
}
/*
* Parses an include directive with the following syntax: IncludeDirective
* ::= ( S Attribute)*
*/
private void parseIncludeDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
// Included file expanded here
Node includeNode = new Node.IncludeDirective(attrs, start, parent);
processIncludeDirective(attrs.getValue("file"), includeNode);
}
/**
* Add a list of files. This is used for implementing include-prelude and
* include-coda of jsp-config element in web.xml
*/
private void addInclude(Node parent, Collection<String> files) throws JasperException {
if (files != null) {
Iterator<String> iter = files.iterator();
while (iter.hasNext()) {
String file = iter.next();
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "file", "file", "CDATA", file);
// Create a dummy Include directive node
Node includeNode = new Node.IncludeDirective(attrs, reader
.mark(), parent);
processIncludeDirective(file, includeNode);
}
}
}
/*
* Parses a taglib directive with the following syntax: Directive ::= ( S
* Attribute)*
*/
private void parseTaglibDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
String uri = attrs.getValue("uri");
String prefix = attrs.getValue("prefix");
if (prefix != null) {
Mark prevMark = pageInfo.getNonCustomTagPrefix(prefix);
if (prevMark != null) {
err.jspError(reader.mark(), "jsp.error.prefix.use_before_dcl",
prefix, prevMark.getFile(), ""
+ prevMark.getLineNumber());
}
if (uri != null) {
String uriPrev = pageInfo.getURI(prefix);
if (uriPrev != null && !uriPrev.equals(uri)) {
err.jspError(reader.mark(), "jsp.error.prefix.refined",
prefix, uri, uriPrev);
}
if (pageInfo.getTaglib(uri) == null) {
TagLibraryInfoImpl impl = null;
if (ctxt.getOptions().isCaching()) {
impl = (TagLibraryInfoImpl) ctxt.getOptions()
.getCache().get(uri);
}
if (impl == null) {
TldResourcePath tldResourcePath = ctxt.getTldResourcePath(uri);
impl = new TagLibraryInfoImpl(ctxt, parserController,
pageInfo, prefix, uri, tldResourcePath, err);
if (ctxt.getOptions().isCaching()) {
ctxt.getOptions().getCache().put(uri, impl);
}
}
pageInfo.addTaglib(uri, impl);
}
pageInfo.addPrefixMapping(prefix, uri);
} else {
String tagdir = attrs.getValue("tagdir");
if (tagdir != null) {
String urnTagdir = URN_JSPTAGDIR + tagdir;
if (pageInfo.getTaglib(urnTagdir) == null) {
pageInfo.addTaglib(urnTagdir,
new ImplicitTagLibraryInfo(ctxt,
parserController, pageInfo, prefix,
tagdir, err));
}
pageInfo.addPrefixMapping(prefix, urnTagdir);
}
}
}
@SuppressWarnings("unused")
Node unused = new Node.TaglibDirective(attrs, start, parent);
}
/*
* Parses a directive with the following syntax: Directive ::= S? ( 'page'
* PageDirective | 'include' IncludeDirective | 'taglib' TagLibDirective) S?
* '%>'
*
* TagDirective ::= S? ('tag' PageDirective | 'include' IncludeDirective |
* 'taglib' TagLibDirective) | 'attribute AttributeDirective | 'variable
* VariableDirective S? '%>'
*/
private void parseDirective(Node parent) throws JasperException {
reader.skipSpaces();
String directive = null;
if (reader.matches("page")) {
directive = "<%@ page";
if (isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.istagfile",
directive);
}
parsePageDirective(parent);
} else if (reader.matches("include")) {
directive = "<%@ include";
parseIncludeDirective(parent);
} else if (reader.matches("taglib")) {
if (directivesOnly) {
// No need to get the tagLibInfo objects. This alos suppresses
// parsing of any tag files used in this tag file.
return;
}
directive = "<%@ taglib";
parseTaglibDirective(parent);
} else if (reader.matches("tag")) {
directive = "<%@ tag";
if (!isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.isnottagfile",
directive);
}
parseTagDirective(parent);
} else if (reader.matches("attribute")) {
directive = "<%@ attribute";
if (!isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.isnottagfile",
directive);
}
parseAttributeDirective(parent);
} else if (reader.matches("variable")) {
directive = "<%@ variable";
if (!isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.isnottagfile",
directive);
}
parseVariableDirective(parent);
} else {
err.jspError(reader.mark(), "jsp.error.invalid.directive");
}
reader.skipSpaces();
if (!reader.matches("%>")) {
err.jspError(start, "jsp.error.unterminated", directive);
}
}
/*
* Parses a directive with the following syntax:
*
* XMLJSPDirectiveBody ::= S? ( ( 'page' PageDirectiveAttrList S? ( '/>' | (
* '>' S? ETag ) ) | ( 'include' IncludeDirectiveAttrList S? ( '/>' | ( '>'
* S? ETag ) ) | <TRANSLATION_ERROR>
*
* XMLTagDefDirectiveBody ::= ( ( 'tag' TagDirectiveAttrList S? ( '/>' | (
* '>' S? ETag ) ) | ( 'include' IncludeDirectiveAttrList S? ( '/>' | ( '>'
* S? ETag ) ) | ( 'attribute' AttributeDirectiveAttrList S? ( '/>' | ( '>'
* S? ETag ) ) | ( 'variable' VariableDirectiveAttrList S? ( '/>' | ( '>' S?
* ETag ) ) ) | <TRANSLATION_ERROR>
*/
private void parseXMLDirective(Node parent) throws JasperException {
reader.skipSpaces();
String eTag = null;
if (reader.matches("page")) {
eTag = "jsp:directive.page";
if (isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.istagfile",
"<" + eTag);
}
parsePageDirective(parent);
} else if (reader.matches("include")) {
eTag = "jsp:directive.include";
parseIncludeDirective(parent);
} else if (reader.matches("tag")) {
eTag = "jsp:directive.tag";
if (!isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.isnottagfile",
"<" + eTag);
}
parseTagDirective(parent);
} else if (reader.matches("attribute")) {
eTag = "jsp:directive.attribute";
if (!isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.isnottagfile",
"<" + eTag);
}
parseAttributeDirective(parent);
} else if (reader.matches("variable")) {
eTag = "jsp:directive.variable";
if (!isTagFile) {
err.jspError(reader.mark(), "jsp.error.directive.isnottagfile",
"<" + eTag);
}
parseVariableDirective(parent);
} else {
err.jspError(reader.mark(), "jsp.error.invalid.directive");
}
reader.skipSpaces();
if (reader.matches(">")) {
reader.skipSpaces();
if (!reader.matchesETag(eTag)) {
err.jspError(start, "jsp.error.unterminated", "<" + eTag);
}
} else if (!reader.matches("/>")) {
err.jspError(start, "jsp.error.unterminated", "<" + eTag);
}
}
/*
* Parses a tag directive with the following syntax: PageDirective ::= ( S
* Attribute)*
*/
private void parseTagDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes(true);
Node.TagDirective n = new Node.TagDirective(attrs, start, parent);
/*
* A page directive may contain multiple 'import' attributes, each of
* which consists of a comma-separated list of package names. Store each
* list with the node, where it is parsed.
*/
for (int i = 0; i < attrs.getLength(); i++) {
if ("import".equals(attrs.getQName(i))) {
n.addImport(attrs.getValue(i));
}
}
}
/*
* Parses a attribute directive with the following syntax:
* AttributeDirective ::= ( S Attribute)*
*/
private void parseAttributeDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
@SuppressWarnings("unused")
Node unused = new Node.AttributeDirective(attrs, start, parent);
}
/*
* Parses a variable directive with the following syntax:
* PageDirective ::= ( S Attribute)*
*/
private void parseVariableDirective(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
@SuppressWarnings("unused")
Node unused = new Node.VariableDirective(attrs, start, parent);
}
/*
* JSPCommentBody ::= (Char* - (Char* '--%>')) '--%>'
*/
private void parseComment(Node parent) throws JasperException {
start = reader.mark();
Mark stop = reader.skipUntil("--%>");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated", "<%--");
}
@SuppressWarnings("unused")
Node unused =
new Node.Comment(reader.getText(start, stop), start, parent);
}
/*
* DeclarationBody ::= (Char* - (char* '%>')) '%>'
*/
private void parseDeclaration(Node parent) throws JasperException {
start = reader.mark();
Mark stop = reader.skipUntil("%>");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated", "<%!");
}
@SuppressWarnings("unused")
Node unused = new Node.Declaration(
parseScriptText(reader.getText(start, stop)), start, parent);
}
/*
* XMLDeclarationBody ::= ( S? '/>' ) | ( S? '>' (Char* - (char* '<'))
* CDSect?)* ETag | <TRANSLATION_ERROR> CDSect ::= CDStart CData CDEnd
* CDStart ::= '<![CDATA[' CData ::= (Char* - (Char* ']]>' Char*)) CDEnd
* ::= ']]>'
*/
private void parseXMLDeclaration(Node parent) throws JasperException {
reader.skipSpaces();
if (!reader.matches("/>")) {
if (!reader.matches(">")) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:declaration>");
}
Mark stop;
String text;
while (true) {
start = reader.mark();
stop = reader.skipUntil("<");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:declaration>");
}
text = parseScriptText(reader.getText(start, stop));
@SuppressWarnings("unused")
Node unused = new Node.Declaration(text, start, parent);
if (reader.matches("![CDATA[")) {
start = reader.mark();
stop = reader.skipUntil("]]>");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated", "CDATA");
}
text = parseScriptText(reader.getText(start, stop));
@SuppressWarnings("unused")
Node unused2 = new Node.Declaration(text, start, parent);
} else {
break;
}
}
if (!reader.matchesETagWithoutLessThan("jsp:declaration")) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:declaration>");
}
}
}
/*
* ExpressionBody ::= (Char* - (char* '%>')) '%>'
*/
private void parseExpression(Node parent) throws JasperException {
start = reader.mark();
Mark stop = reader.skipUntil("%>");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated", "<%=");
}
@SuppressWarnings("unused")
Node unused = new Node.Expression(
parseScriptText(reader.getText(start, stop)), start, parent);
}
/*
* XMLExpressionBody ::= ( S? '/>' ) | ( S? '>' (Char* - (char* '<'))
* CDSect?)* ETag ) | <TRANSLATION_ERROR>
*/
private void parseXMLExpression(Node parent) throws JasperException {
reader.skipSpaces();
if (!reader.matches("/>")) {
if (!reader.matches(">")) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:expression>");
}
Mark stop;
String text;
while (true) {
start = reader.mark();
stop = reader.skipUntil("<");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:expression>");
}
text = parseScriptText(reader.getText(start, stop));
@SuppressWarnings("unused")
Node unused = new Node.Expression(text, start, parent);
if (reader.matches("![CDATA[")) {
start = reader.mark();
stop = reader.skipUntil("]]>");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated", "CDATA");
}
text = parseScriptText(reader.getText(start, stop));
@SuppressWarnings("unused")
Node unused2 = new Node.Expression(text, start, parent);
} else {
break;
}
}
if (!reader.matchesETagWithoutLessThan("jsp:expression")) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:expression>");
}
}
}
/*
* ELExpressionBody. Starts with "#{" or "${". Ends with "}".
* See JspReader.skipELExpression().
*/
private void parseELExpression(Node parent, char type)
throws JasperException {
start = reader.mark();
Mark last = reader.skipELExpression();
if (last == null) {
err.jspError(start, "jsp.error.unterminated", type + "{");
}
@SuppressWarnings("unused")
Node unused = new Node.ELExpression(type, reader.getText(start, last),
start, parent);
}
/*
* ScriptletBody ::= (Char* - (char* '%>')) '%>'
*/
private void parseScriptlet(Node parent) throws JasperException {
start = reader.mark();
Mark stop = reader.skipUntil("%>");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated", "<%");
}
@SuppressWarnings("unused")
Node unused = new Node.Scriptlet(
parseScriptText(reader.getText(start, stop)), start, parent);
}
/*
* XMLScriptletBody ::= ( S? '/>' ) | ( S? '>' (Char* - (char* '<'))
* CDSect?)* ETag ) | <TRANSLATION_ERROR>
*/
private void parseXMLScriptlet(Node parent) throws JasperException {
reader.skipSpaces();
if (!reader.matches("/>")) {
if (!reader.matches(">")) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:scriptlet>");
}
Mark stop;
String text;
while (true) {
start = reader.mark();
stop = reader.skipUntil("<");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:scriptlet>");
}
text = parseScriptText(reader.getText(start, stop));
@SuppressWarnings("unused")
Node unused = new Node.Scriptlet(text, start, parent);
if (reader.matches("![CDATA[")) {
start = reader.mark();
stop = reader.skipUntil("]]>");
if (stop == null) {
err.jspError(start, "jsp.error.unterminated", "CDATA");
}
text = parseScriptText(reader.getText(start, stop));
@SuppressWarnings("unused")
Node unused2 = new Node.Scriptlet(text, start, parent);
} else {
break;
}
}
if (!reader.matchesETagWithoutLessThan("jsp:scriptlet")) {
err.jspError(start, "jsp.error.unterminated",
"<jsp:scriptlet>");
}
}
}
/**
* Param ::= '<jsp:param' S Attributes S? EmptyBody S?
*/
private void parseParam(Node parent) throws JasperException {
if (!reader.matches("<jsp:param")) {
err.jspError(reader.mark(), "jsp.error.paramexpected");
}
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node paramActionNode = new Node.ParamAction(attrs, start, parent);
parseEmptyBody(paramActionNode, "jsp:param");
reader.skipSpaces();
}
/*
* For Include: StdActionContent ::= Attributes ParamBody
*
* ParamBody ::= EmptyBody | ( '>' S? ( '<jsp:attribute' NamedAttributes )? '<jsp:body'
* (JspBodyParam | <TRANSLATION_ERROR> ) S? ETag ) | ( '>' S? Param* ETag )
*
* EmptyBody ::= '/>' | ( '>' ETag ) | ( '>' S? '<jsp:attribute'
* NamedAttributes ETag )
*
* JspBodyParam ::= S? '>' Param* '</jsp:body>'
*/
private void parseInclude(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node includeNode = new Node.IncludeAction(attrs, start, parent);
parseOptionalBody(includeNode, "jsp:include", JAVAX_BODY_CONTENT_PARAM);
}
/*
* For Forward: StdActionContent ::= Attributes ParamBody
*/
private void parseForward(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node forwardNode = new Node.ForwardAction(attrs, start, parent);
parseOptionalBody(forwardNode, "jsp:forward", JAVAX_BODY_CONTENT_PARAM);
}
private void parseInvoke(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node invokeNode = new Node.InvokeAction(attrs, start, parent);
parseEmptyBody(invokeNode, "jsp:invoke");
}
private void parseDoBody(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node doBodyNode = new Node.DoBodyAction(attrs, start, parent);
parseEmptyBody(doBodyNode, "jsp:doBody");
}
private void parseElement(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node elementNode = new Node.JspElement(attrs, start, parent);
parseOptionalBody(elementNode, "jsp:element", TagInfo.BODY_CONTENT_JSP);
}
/*
* For GetProperty: StdActionContent ::= Attributes EmptyBody
*/
private void parseGetProperty(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node getPropertyNode = new Node.GetProperty(attrs, start, parent);
parseOptionalBody(getPropertyNode, "jsp:getProperty",
TagInfo.BODY_CONTENT_EMPTY);
}
/*
* For SetProperty: StdActionContent ::= Attributes EmptyBody
*/
private void parseSetProperty(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node setPropertyNode = new Node.SetProperty(attrs, start, parent);
parseOptionalBody(setPropertyNode, "jsp:setProperty",
TagInfo.BODY_CONTENT_EMPTY);
}
/*
* EmptyBody ::= '/>' | ( '>' ETag ) | ( '>' S? '<jsp:attribute'
* NamedAttributes ETag )
*/
private void parseEmptyBody(Node parent, String tag) throws JasperException {
if (reader.matches("/>")) {
// Done
} else if (reader.matches(">")) {
if (reader.matchesETag(tag)) {
// Done
} else if (reader.matchesOptionalSpacesFollowedBy("<jsp:attribute")) {
// Parse the one or more named attribute nodes
parseNamedAttributes(parent);
if (!reader.matchesETag(tag)) {
// Body not allowed
err.jspError(reader.mark(),
"jsp.error.jspbody.emptybody.only", "<" + tag);
}
} else {
err.jspError(reader.mark(), "jsp.error.jspbody.emptybody.only",
"<" + tag);
}
} else {
err.jspError(reader.mark(), "jsp.error.unterminated", "<" + tag);
}
}
/*
* For UseBean: StdActionContent ::= Attributes OptionalBody
*/
private void parseUseBean(Node parent) throws JasperException {
Attributes attrs = parseAttributes();
reader.skipSpaces();
Node useBeanNode = new Node.UseBean(attrs, start, parent);
parseOptionalBody(useBeanNode, "jsp:useBean", TagInfo.BODY_CONTENT_JSP);
}
/*
* Parses OptionalBody, but also reused to parse bodies for plugin and param
* since the syntax is identical (the only thing that differs substantially
* is how to process the body, and thus we accept the body type as a
* parameter).
*
* OptionalBody ::= EmptyBody | ActionBody
*
* ScriptlessOptionalBody ::= EmptyBody | ScriptlessActionBody
*
* TagDependentOptionalBody ::= EmptyBody | TagDependentActionBody
*
* EmptyBody ::= '/>' | ( '>' ETag ) | ( '>' S? '<jsp:attribute'
* NamedAttributes ETag )
*
* ActionBody ::= JspAttributeAndBody | ( '>' Body ETag )
*
* ScriptlessActionBody ::= JspAttributeAndBody | ( '>' ScriptlessBody ETag )
*
* TagDependentActionBody ::= JspAttributeAndBody | ( '>' TagDependentBody
* ETag )
*
*/
private void parseOptionalBody(Node parent, String tag, String bodyType)
throws JasperException {
if (reader.matches("/>")) {
// EmptyBody
return;
}
if (!reader.matches(">")) {
err.jspError(reader.mark(), "jsp.error.unterminated", "<" + tag);
}
if (reader.matchesETag(tag)) {
// EmptyBody
return;
}
if (!parseJspAttributeAndBody(parent, tag, bodyType)) {
// Must be ( '>' # Body ETag )
parseBody(parent, tag, bodyType);
}
}
/**