KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > PageInfo


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jasper.compiler;
18
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.apache.el.ExpressionFactoryImpl;
27 import org.apache.jasper.Constants;
28 import org.apache.jasper.JasperException;
29
30 import javax.el.ExpressionFactory;
31 import javax.servlet.jsp.tagext.TagLibraryInfo JavaDoc;
32
33 /**
34  * A repository for various info about the translation unit under compilation.
35  *
36  * @author Kin-man Chung
37  */

38
39 class PageInfo {
40
41     private Vector JavaDoc imports;
42     private Vector JavaDoc dependants;
43
44     private BeanRepository beanRepository;
45     private HashMap JavaDoc taglibsMap;
46     private HashMap JavaDoc jspPrefixMapper;
47     private HashMap JavaDoc xmlPrefixMapper;
48     private HashMap JavaDoc nonCustomTagPrefixMap;
49     private String JavaDoc jspFile;
50     private String JavaDoc defaultLanguage = "java";
51     private String JavaDoc language;
52     private String JavaDoc defaultExtends = Constants.JSP_SERVLET_BASE;
53     private String JavaDoc xtends;
54     private String JavaDoc contentType = null;
55     private String JavaDoc session;
56     private boolean isSession = true;
57     private String JavaDoc bufferValue;
58     private int buffer = 8*1024; // XXX confirm
59
private String JavaDoc autoFlush;
60     private boolean isAutoFlush = true;
61     private String JavaDoc isThreadSafeValue;
62     private boolean isThreadSafe = true;
63     private String JavaDoc isErrorPageValue;
64     private boolean isErrorPage = false;
65     private String JavaDoc errorPage = null;
66     private String JavaDoc info;
67
68     private boolean scriptless = false;
69     private boolean scriptingInvalid = false;
70     
71     private String JavaDoc isELIgnoredValue;
72     private boolean isELIgnored = false;
73     
74     // JSP 2.1
75
private String JavaDoc deferredSyntaxAllowedAsLiteralValue;
76     private boolean deferredSyntaxAllowedAsLiteral = false;
77     private ExpressionFactory expressionFactory = new ExpressionFactoryImpl();
78     private String JavaDoc trimDirectiveWhitespacesValue;
79     private boolean trimDirectiveWhitespaces = false;
80     
81     private String JavaDoc omitXmlDecl = null;
82     private String JavaDoc doctypeName = null;
83     private String JavaDoc doctypePublic = null;
84     private String JavaDoc doctypeSystem = null;
85
86     private boolean isJspPrefixHijacked;
87
88     // Set of all element and attribute prefixes used in this translation unit
89
private HashSet JavaDoc prefixes;
90
91     private boolean hasJspRoot = false;
92     private Vector JavaDoc includePrelude;
93     private Vector JavaDoc includeCoda;
94     private Vector JavaDoc pluginDcls; // Id's for tagplugin declarations
95

96
97     PageInfo(BeanRepository beanRepository, String JavaDoc jspFile) {
98
99         this.jspFile = jspFile;
100         this.beanRepository = beanRepository;
101         this.taglibsMap = new HashMap JavaDoc();
102         this.jspPrefixMapper = new HashMap JavaDoc();
103         this.xmlPrefixMapper = new HashMap JavaDoc();
104         this.nonCustomTagPrefixMap = new HashMap JavaDoc();
105         this.imports = new Vector JavaDoc();
106         this.dependants = new Vector JavaDoc();
107         this.includePrelude = new Vector JavaDoc();
108         this.includeCoda = new Vector JavaDoc();
109         this.pluginDcls = new Vector JavaDoc();
110         this.prefixes = new HashSet JavaDoc();
111
112         // Enter standard imports
113
for(int i = 0; i < Constants.STANDARD_IMPORTS.length; i++)
114             imports.add(Constants.STANDARD_IMPORTS[i]);
115     }
116
117     /**
118      * Check if the plugin ID has been previously declared. Make a not
119      * that this Id is now declared.
120      * @return true if Id has been declared.
121      */

122     public boolean isPluginDeclared(String JavaDoc id) {
123         if (pluginDcls.contains(id))
124             return true;
125         pluginDcls.add(id);
126         return false;
127     }
128
129     public void addImports(List JavaDoc imports) {
130         this.imports.addAll(imports);
131     }
132
133     public void addImport(String JavaDoc imp) {
134         this.imports.add(imp);
135     }
136
137     public List JavaDoc getImports() {
138         return imports;
139     }
140
141     public String JavaDoc getJspFile() {
142         return jspFile;
143     }
144
145     public void addDependant(String JavaDoc d) {
146         if (!dependants.contains(d) && !jspFile.equals(d))
147                 dependants.add(d);
148     }
149
150     public List JavaDoc getDependants() {
151         return dependants;
152     }
153
154     public BeanRepository getBeanRepository() {
155         return beanRepository;
156     }
157
158     public void setScriptless(boolean s) {
159         scriptless = s;
160     }
161
162     public boolean isScriptless() {
163         return scriptless;
164     }
165
166     public void setScriptingInvalid(boolean s) {
167         scriptingInvalid = s;
168     }
169
170     public boolean isScriptingInvalid() {
171         return scriptingInvalid;
172     }
173
174     public List JavaDoc getIncludePrelude() {
175         return includePrelude;
176     }
177
178     public void setIncludePrelude(Vector JavaDoc prelude) {
179         includePrelude = prelude;
180     }
181
182     public List JavaDoc getIncludeCoda() {
183         return includeCoda;
184     }
185
186     public void setIncludeCoda(Vector JavaDoc coda) {
187         includeCoda = coda;
188     }
189
190     public void setHasJspRoot(boolean s) {
191         hasJspRoot = s;
192     }
193
194     public boolean hasJspRoot() {
195         return hasJspRoot;
196     }
197
198     public String JavaDoc getOmitXmlDecl() {
199         return omitXmlDecl;
200     }
201
202     public void setOmitXmlDecl(String JavaDoc omit) {
203         omitXmlDecl = omit;
204     }
205
206     public String JavaDoc getDoctypeName() {
207         return doctypeName;
208     }
209
210     public void setDoctypeName(String JavaDoc doctypeName) {
211         this.doctypeName = doctypeName;
212     }
213
214     public String JavaDoc getDoctypeSystem() {
215         return doctypeSystem;
216     }
217
218     public void setDoctypeSystem(String JavaDoc doctypeSystem) {
219         this.doctypeSystem = doctypeSystem;
220     }
221
222     public String JavaDoc getDoctypePublic() {
223         return doctypePublic;
224     }
225
226     public void setDoctypePublic(String JavaDoc doctypePublic) {
227         this.doctypePublic = doctypePublic;
228     }
229
230     /* Tag library and XML namespace management methods */
231
232     public void setIsJspPrefixHijacked(boolean isHijacked) {
233         isJspPrefixHijacked = isHijacked;
234     }
235
236     public boolean isJspPrefixHijacked() {
237         return isJspPrefixHijacked;
238     }
239
240     /*
241      * Adds the given prefix to the set of prefixes of this translation unit.
242      *
243      * @param prefix The prefix to add
244      */

245     public void addPrefix(String JavaDoc prefix) {
246         prefixes.add(prefix);
247     }
248
249     /*
250      * Checks to see if this translation unit contains the given prefix.
251      *
252      * @param prefix The prefix to check
253      *
254      * @return true if this translation unit contains the given prefix, false
255      * otherwise
256      */

257     public boolean containsPrefix(String JavaDoc prefix) {
258         return prefixes.contains(prefix);
259     }
260
261     /*
262      * Maps the given URI to the given tag library.
263      *
264      * @param uri The URI to map
265      * @param info The tag library to be associated with the given URI
266      */

267     public void addTaglib(String JavaDoc uri, TagLibraryInfo JavaDoc info) {
268         taglibsMap.put(uri, info);
269     }
270
271     /*
272      * Gets the tag library corresponding to the given URI.
273      *
274      * @return Tag library corresponding to the given URI
275      */

276     public TagLibraryInfo JavaDoc getTaglib(String JavaDoc uri) {
277         return (TagLibraryInfo JavaDoc) taglibsMap.get(uri);
278     }
279
280     /*
281      * Gets the collection of tag libraries that are associated with a URI
282      *
283      * @return Collection of tag libraries that are associated with a URI
284      */

285     public Collection JavaDoc getTaglibs() {
286         return taglibsMap.values();
287     }
288
289     /*
290      * Checks to see if the given URI is mapped to a tag library.
291      *
292      * @param uri The URI to map
293      *
294      * @return true if the given URI is mapped to a tag library, false
295      * otherwise
296      */

297     public boolean hasTaglib(String JavaDoc uri) {
298         return taglibsMap.containsKey(uri);
299     }
300
301     /*
302      * Maps the given prefix to the given URI.
303      *
304      * @param prefix The prefix to map
305      * @param uri The URI to be associated with the given prefix
306      */

307     public void addPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
308         jspPrefixMapper.put(prefix, uri);
309     }
310
311     /*
312      * Pushes the given URI onto the stack of URIs to which the given prefix
313      * is mapped.
314      *
315      * @param prefix The prefix whose stack of URIs is to be pushed
316      * @param uri The URI to be pushed onto the stack
317      */

318     public void pushPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
319         LinkedList JavaDoc stack = (LinkedList JavaDoc) xmlPrefixMapper.get(prefix);
320         if (stack == null) {
321             stack = new LinkedList JavaDoc();
322             xmlPrefixMapper.put(prefix, stack);
323         }
324         stack.addFirst(uri);
325     }
326
327     /*
328      * Removes the URI at the top of the stack of URIs to which the given
329      * prefix is mapped.
330      *
331      * @param prefix The prefix whose stack of URIs is to be popped
332      */

333     public void popPrefixMapping(String JavaDoc prefix) {
334         LinkedList JavaDoc stack = (LinkedList JavaDoc) xmlPrefixMapper.get(prefix);
335         if (stack == null || stack.size() == 0) {
336             // XXX throw new Exception("XXX");
337
}
338         stack.removeFirst();
339     }
340
341     /*
342      * Returns the URI to which the given prefix maps.
343      *
344      * @param prefix The prefix whose URI is sought
345      *
346      * @return The URI to which the given prefix maps
347      */

348     public String JavaDoc getURI(String JavaDoc prefix) {
349
350         String JavaDoc uri = null;
351
352         LinkedList JavaDoc stack = (LinkedList JavaDoc) xmlPrefixMapper.get(prefix);
353         if (stack == null || stack.size() == 0) {
354             uri = (String JavaDoc) jspPrefixMapper.get(prefix);
355         } else {
356             uri = (String JavaDoc) stack.getFirst();
357         }
358
359         return uri;
360     }
361
362
363     /* Page/Tag directive attributes */
364
365     /*
366      * language
367      */

368     public void setLanguage(String JavaDoc value, Node n, ErrorDispatcher err,
369                 boolean pagedir)
370         throws JasperException {
371
372         if (!"java".equalsIgnoreCase(value)) {
373             if (pagedir)
374                 err.jspError(n, "jsp.error.page.language.nonjava");
375             else
376                 err.jspError(n, "jsp.error.tag.language.nonjava");
377         }
378
379         language = value;
380     }
381
382     public String JavaDoc getLanguage(boolean useDefault) {
383         return (language == null && useDefault ? defaultLanguage : language);
384     }
385
386     public String JavaDoc getLanguage() {
387         return getLanguage(true);
388     }
389
390
391     /*
392      * extends
393      */

394     public void setExtends(String JavaDoc value, Node.PageDirective n) {
395
396         xtends = value;
397
398         /*
399          * If page superclass is top level class (i.e. not in a package)
400          * explicitly import it. If this is not done, the compiler will assume
401          * the extended class is in the same pkg as the generated servlet.
402          */

403         if (value.indexOf('.') < 0)
404             n.addImport(value);
405     }
406
407     /**
408      * Gets the value of the 'extends' page directive attribute.
409      *
410      * @param useDefault TRUE if the default
411      * (org.apache.jasper.runtime.HttpJspBase) should be returned if this
412      * attribute has not been set, FALSE otherwise
413      *
414      * @return The value of the 'extends' page directive attribute, or the
415      * default (org.apache.jasper.runtime.HttpJspBase) if this attribute has
416      * not been set and useDefault is TRUE
417      */

418     public String JavaDoc getExtends(boolean useDefault) {
419         return (xtends == null && useDefault ? defaultExtends : xtends);
420     }
421
422     /**
423      * Gets the value of the 'extends' page directive attribute.
424      *
425      * @return The value of the 'extends' page directive attribute, or the
426      * default (org.apache.jasper.runtime.HttpJspBase) if this attribute has
427      * not been set
428      */

429     public String JavaDoc getExtends() {
430         return getExtends(true);
431     }
432
433
434     /*
435      * contentType
436      */

437     public void setContentType(String JavaDoc value) {
438         contentType = value;
439     }
440
441     public String JavaDoc getContentType() {
442         return contentType;
443     }
444
445
446     /*
447      * buffer
448      */

449     public void setBufferValue(String JavaDoc value, Node n, ErrorDispatcher err)
450         throws JasperException {
451
452         if ("none".equalsIgnoreCase(value))
453             buffer = 0;
454         else {
455             if (value == null || !value.endsWith("kb"))
456                 err.jspError(n, "jsp.error.page.invalid.buffer");
457             try {
458                 Integer JavaDoc k = new Integer JavaDoc(value.substring(0, value.length()-2));
459                 buffer = k.intValue() * 1024;
460             } catch (NumberFormatException JavaDoc e) {
461                 err.jspError(n, "jsp.error.page.invalid.buffer");
462             }
463         }
464
465         bufferValue = value;
466     }
467
468     public String JavaDoc getBufferValue() {
469         return bufferValue;
470     }
471
472     public int getBuffer() {
473         return buffer;
474     }
475
476
477     /*
478      * session
479      */

480     public void setSession(String JavaDoc value, Node n, ErrorDispatcher err)
481         throws JasperException {
482
483         if ("true".equalsIgnoreCase(value))
484             isSession = true;
485         else if ("false".equalsIgnoreCase(value))
486             isSession = false;
487         else
488             err.jspError(n, "jsp.error.page.invalid.session");
489
490         session = value;
491     }
492
493     public String JavaDoc getSession() {
494         return session;
495     }
496
497     public boolean isSession() {
498         return isSession;
499     }
500
501
502     /*
503      * autoFlush
504      */

505     public void setAutoFlush(String JavaDoc value, Node n, ErrorDispatcher err)
506         throws JasperException {
507
508         if ("true".equalsIgnoreCase(value))
509             isAutoFlush = true;
510         else if ("false".equalsIgnoreCase(value))
511             isAutoFlush = false;
512         else
513             err.jspError(n, "jsp.error.autoFlush.invalid");
514
515         autoFlush = value;
516     }
517
518     public String JavaDoc getAutoFlush() {
519         return autoFlush;
520     }
521
522     public boolean isAutoFlush() {
523         return isAutoFlush;
524     }
525
526
527     /*
528      * isThreadSafe
529      */

530     public void setIsThreadSafe(String JavaDoc value, Node n, ErrorDispatcher err)
531         throws JasperException {
532
533         if ("true".equalsIgnoreCase(value))
534             isThreadSafe = true;
535         else if ("false".equalsIgnoreCase(value))
536             isThreadSafe = false;
537         else
538             err.jspError(n, "jsp.error.page.invalid.isthreadsafe");
539
540         isThreadSafeValue = value;
541     }
542
543     public String JavaDoc getIsThreadSafe() {
544         return isThreadSafeValue;
545     }
546
547     public boolean isThreadSafe() {
548         return isThreadSafe;
549     }
550
551
552     /*
553      * info
554      */

555     public void setInfo(String JavaDoc value) {
556         info = value;
557     }
558
559     public String JavaDoc getInfo() {
560         return info;
561     }
562
563
564     /*
565      * errorPage
566      */

567     public void setErrorPage(String JavaDoc value) {
568         errorPage = value;
569     }
570
571     public String JavaDoc getErrorPage() {
572         return errorPage;
573     }
574
575
576     /*
577      * isErrorPage
578      */

579     public void setIsErrorPage(String JavaDoc value, Node n, ErrorDispatcher err)
580         throws JasperException {
581
582         if ("true".equalsIgnoreCase(value))
583             isErrorPage = true;
584         else if ("false".equalsIgnoreCase(value))
585             isErrorPage = false;
586         else
587             err.jspError(n, "jsp.error.page.invalid.iserrorpage");
588
589         isErrorPageValue = value;
590     }
591
592     public String JavaDoc getIsErrorPage() {
593         return isErrorPageValue;
594     }
595
596     public boolean isErrorPage() {
597         return isErrorPage;
598     }
599
600
601     /*
602      * isELIgnored
603      */

604     public void setIsELIgnored(String JavaDoc value, Node n, ErrorDispatcher err,
605                    boolean pagedir)
606         throws JasperException {
607
608         if ("true".equalsIgnoreCase(value))
609             isELIgnored = true;
610         else if ("false".equalsIgnoreCase(value))
611             isELIgnored = false;
612         else {
613             if (pagedir)
614                 err.jspError(n, "jsp.error.page.invalid.iselignored");
615             else
616                 err.jspError(n, "jsp.error.tag.invalid.iselignored");
617         }
618
619         isELIgnoredValue = value;
620     }
621     
622     /*
623      * deferredSyntaxAllowedAsLiteral
624      */

625     public void setDeferredSyntaxAllowedAsLiteral(String JavaDoc value, Node n, ErrorDispatcher err,
626                    boolean pagedir)
627         throws JasperException {
628
629         if ("true".equalsIgnoreCase(value))
630             deferredSyntaxAllowedAsLiteral = true;
631         else if ("false".equalsIgnoreCase(value))
632             deferredSyntaxAllowedAsLiteral = false;
633         else {
634             if (pagedir)
635                 err.jspError(n, "jsp.error.page.invalid.deferredsyntaxallowedasliteral");
636             else
637                 err.jspError(n, "jsp.error.tag.invalid.deferredsyntaxallowedasliteral");
638         }
639
640         deferredSyntaxAllowedAsLiteralValue = value;
641     }
642     
643     /*
644      * trimDirectiveWhitespaces
645      */

646     public void setTrimDirectiveWhitespaces(String JavaDoc value, Node n, ErrorDispatcher err,
647                    boolean pagedir)
648         throws JasperException {
649
650         if ("true".equalsIgnoreCase(value))
651             trimDirectiveWhitespaces = true;
652         else if ("false".equalsIgnoreCase(value))
653             trimDirectiveWhitespaces = false;
654         else {
655             if (pagedir)
656                 err.jspError(n, "jsp.error.page.invalid.trimdirectivewhitespaces");
657             else
658                 err.jspError(n, "jsp.error.tag.invalid.trimdirectivewhitespaces");
659         }
660
661         trimDirectiveWhitespacesValue = value;
662     }
663
664     public void setELIgnored(boolean s) {
665         isELIgnored = s;
666     }
667
668     public String JavaDoc getIsELIgnored() {
669         return isELIgnoredValue;
670     }
671
672     public boolean isELIgnored() {
673         return isELIgnored;
674     }
675
676     public void putNonCustomTagPrefix(String JavaDoc prefix, Mark where) {
677         nonCustomTagPrefixMap.put(prefix, where);
678     }
679
680     public Mark getNonCustomTagPrefix(String JavaDoc prefix) {
681         return (Mark) nonCustomTagPrefixMap.get(prefix);
682     }
683     
684     public String JavaDoc getDeferredSyntaxAllowedAsLiteral() {
685         return deferredSyntaxAllowedAsLiteralValue;
686     }
687
688     public boolean isDeferredSyntaxAllowedAsLiteral() {
689         return deferredSyntaxAllowedAsLiteral;
690     }
691
692     public void setDeferredSyntaxAllowedAsLiteral(boolean isELDeferred) {
693         this.deferredSyntaxAllowedAsLiteral = isELDeferred;
694     }
695
696     public ExpressionFactory getExpressionFactory() {
697         return expressionFactory;
698     }
699
700     public String JavaDoc getTrimDirectiveWhitespaces() {
701         return trimDirectiveWhitespacesValue;
702     }
703
704     public boolean isTrimDirectiveWhitespaces() {
705         return trimDirectiveWhitespaces;
706     }
707
708     public void setTrimDirectiveWhitespaces(boolean trimDirectiveWhitespaces) {
709         this.trimDirectiveWhitespaces = trimDirectiveWhitespaces;
710     }
711 }
712
Popular Tags