KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > ParseState


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp;
31
32 import com.caucho.java.LineMap;
33 import com.caucho.jsp.cfg.JspPropertyGroup;
34 import com.caucho.log.Log;
35 import com.caucho.server.webapp.Application;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.util.CharScanner;
38 import com.caucho.util.L10N;
39 import com.caucho.util.StringCharCursor;
40 import com.caucho.vfs.Depend;
41 import com.caucho.vfs.Path;
42 import com.caucho.xml.QName;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Represents the current state of the parser.
49  */

50 public class ParseState {
51   private static final L10N L = new L10N(ParseState.class);
52   static final Logger JavaDoc log = Log.open(ParseState.class);
53
54   private Application _application;
55
56   private JspPropertyGroup _jspPropertyGroup = new JspPropertyGroup();
57
58   private boolean _isELIgnored = false;
59   private boolean _isELIgnoredSet = false;
60   private boolean _isELIgnoredDefault = true;
61   
62   private boolean _isScriptingInvalid = false;
63   
64   private boolean _isVelocityEnabled;
65
66   private boolean _isSession = true;
67   private boolean _isOptionalSession = false;
68   private boolean _isSessionSet = false;
69   
70   private boolean _isErrorPage = false;
71   private boolean _isErrorPageSet = false;
72   
73   private boolean _isAutoFlush = true;
74   private boolean _isAutoFlushSet = false;
75   
76   private boolean _isThreadSafe = true;
77   private boolean _isThreadSafeSet = false;
78
79   private boolean _isTag = false;
80   private boolean _isXml = false;
81   private boolean _isForbidXml = false;
82
83   private int _buffer = 8192;
84   private boolean _isBufferSet = false;
85
86   private String JavaDoc _info;
87   private String JavaDoc _errorPage;
88   private String JavaDoc _contentType;
89   private String JavaDoc _charEncoding;
90   private String JavaDoc _pageEncoding;
91   private Class JavaDoc _extends;
92
93   private boolean _recycleTags = true;
94   private boolean _isTrimWhitespace;
95   private boolean _isDeferredSyntaxAllowedAsLiteral;
96
97   private JspResourceManager _resourceManager;
98
99   private JspBuilder _jspBuilder;
100
101   private ArrayList JavaDoc<String JavaDoc> _importList = new ArrayList JavaDoc<String JavaDoc>();
102
103   private String JavaDoc _uriPwd;
104   
105   private ArrayList JavaDoc<Depend> _depends = new ArrayList JavaDoc<Depend>();
106   private LineMap _lineMap;
107   
108   private Namespace _namespaces;
109
110   /**
111    * Create a new parse state instance.
112    */

113   public ParseState()
114   {
115   }
116
117   /**
118    * Sets the JSP property group.
119    */

120   public void setJspPropertyGroup(JspPropertyGroup group)
121   {
122     _jspPropertyGroup = group;
123   }
124
125   /**
126    * Gets the JSP property group.
127    */

128   public JspPropertyGroup getJspPropertyGroup()
129   {
130     return _jspPropertyGroup;
131   }
132
133   /**
134    * Returns true if JSP EL is ignored.
135    */

136   public boolean isELIgnored()
137   {
138     return _isELIgnored;
139   }
140
141   /**
142    * Set if JSP EL is ignored.
143    */

144   public boolean setELIgnored(boolean isELIgnored)
145   {
146     boolean oldELIgnored = _isELIgnored;
147     
148     _isELIgnored = isELIgnored;
149     _isELIgnoredDefault = false;
150     
151     return (oldELIgnored == isELIgnored || ! _isELIgnoredSet);
152   }
153
154   /**
155    * Mark the el-ignored attribute as set.
156    */

157   public void markELIgnoredSet()
158   {
159     _isELIgnoredSet = true;
160   }
161
162   /**
163    * Set if JSP EL is ignored.
164    */

165   public void setELIgnoredDefault(boolean isELIgnored)
166   {
167     if (_isELIgnoredDefault)
168       _isELIgnored = isELIgnored;
169   }
170
171   /**
172    * Returns true if JSP scripting is invalidn.
173    */

174   public boolean isScriptingInvalid()
175   {
176     return _isScriptingInvalid;
177   }
178
179   /**
180    * Set if JSP scripting is ignored.
181    */

182   public void setScriptingInvalid(boolean isScriptingInvalid)
183   {
184     _isScriptingInvalid = isScriptingInvalid;
185   }
186
187   /**
188    * Set if velocity statements are enabled.
189    */

190   public void setVelocityEnabled(boolean isVelocity)
191   {
192     _isVelocityEnabled = isVelocity;
193   }
194
195   /**
196    * Returns true if Velocity statements are enabled.
197    */

198   public boolean isVelocityEnabled()
199   {
200     return _isVelocityEnabled;
201   }
202
203   /**
204    * Returns true if the session is enabled.
205    */

206   public boolean isSession()
207   {
208     return _isSession;
209   }
210
211   /**
212    * Returns true if the optional session is enabled.
213    */

214   public boolean isOptionalSession()
215   {
216     return _isOptionalSession;
217   }
218   
219   /**
220    * Set if the session is enabled.
221    */

222   public boolean setSession(boolean session)
223   {
224     boolean isSession = _isSession;
225     
226     _isSession = session;
227     _isOptionalSession = session;
228
229     return (session == isSession || ! _isSessionSet);
230   }
231
232   /**
233    * Mark the thread safe attribute as set.
234    */

235   public void markSessionSet()
236   {
237     _isSessionSet = true;
238   }
239
240   /**
241    * Returns true if the autoFlush is enabled.
242    */

243   public boolean isAutoFlush()
244   {
245     return _isAutoFlush;
246   }
247   
248   /**
249    * Set if the autoFlush is enabled.
250    */

251   public boolean setAutoFlush(boolean autoFlush)
252   {
253     boolean isAutoFlush = _isAutoFlush;
254     
255     _isAutoFlush = autoFlush;
256
257     return (autoFlush == isAutoFlush || ! _isAutoFlushSet);
258   }
259
260   /**
261    * Mark the thread safe attribute as set.
262    */

263   public void markAutoFlushSet()
264   {
265     _isAutoFlushSet = true;
266   }
267
268   /**
269    * Returns true if the threadSafe is enabled.
270    */

271   public boolean isThreadSafe()
272   {
273     return _isThreadSafe;
274   }
275
276   /**
277    * Set if the threadSafe is enabled.
278    */

279   public boolean setThreadSafe(boolean threadSafe)
280   {
281     boolean isThreadSafe = _isThreadSafe;
282     
283     _isThreadSafe = threadSafe;
284
285     return (threadSafe == isThreadSafe || ! _isThreadSafeSet);
286   }
287
288   /**
289    * Mark the thread safe attribute as set.
290    */

291   public void markThreadSafeSet()
292   {
293     _isThreadSafeSet = true;
294   }
295
296   /**
297    * Set if the errorPage is enabled.
298    */

299   public boolean setErrorPage(boolean errorPage)
300   {
301     boolean isErrorPage = _isErrorPage;
302     
303     _isErrorPage = errorPage;
304
305     return (errorPage == isErrorPage || ! _isErrorPageSet);
306   }
307
308   /**
309    * Returns true if the errorPage is enabled.
310    */

311   public boolean isErrorPage()
312   {
313     return _isErrorPage;
314   }
315
316   /**
317    * Mark the error page attribute as set.
318    */

319   public void markErrorPage()
320   {
321     _isErrorPageSet = true;
322   }
323
324   /**
325    * Returns the buffer size in bytes.
326    */

327   public int getBuffer()
328   {
329     return _buffer;
330   }
331   
332   /**
333    * Set the buffer size.
334    */

335   public boolean setBuffer(int buffer)
336   {
337     int oldBuffer = _buffer;
338     
339     _buffer = buffer;
340
341     return (buffer == oldBuffer || ! _isBufferSet);
342   }
343
344   /**
345    * Mark the buffer attribute as set.
346    */

347   public void markBufferSet()
348   {
349     _isBufferSet = true;
350   }
351
352   /**
353    * Sets the JSP's error page
354    */

355   public void setErrorPage(String JavaDoc errorPage)
356   {
357     _errorPage = errorPage;
358   }
359
360   /**
361    * Gets the JSP's error page
362    */

363   public String JavaDoc getErrorPage()
364   {
365     return _errorPage;
366   }
367
368   /**
369    * Sets the JSP's content type
370    */

371   public void setContentType(String JavaDoc contentType)
372   {
373     _contentType = contentType;
374   }
375
376   /**
377    * Gets the JSP's content type
378    */

379   public String JavaDoc getContentType()
380   {
381     return _contentType;
382   }
383
384   /**
385    * Sets the JSP's character encoding
386    */

387   public void setCharEncoding(String JavaDoc charEncoding)
388     throws JspParseException
389   {
390     /*
391     if (_charEncoding != null &&
392     ! _charEncoding.equalsIgnoreCase(charEncoding))
393       throw new JspParseException(L.l("Cannot change character encoding to '{0}' (old value '{1}'). The character encoding may only be set once.",
394                       charEncoding, _charEncoding));
395     */

396     
397     _charEncoding = charEncoding;
398   }
399
400   /**
401    * Gets the JSP's character encoding
402    */

403   public String JavaDoc getCharEncoding()
404   {
405     return _charEncoding;
406   }
407
408   /**
409    * Sets the JSP's page encoding
410    */

411   public void setPageEncoding(String JavaDoc pageEncoding)
412     throws JspParseException
413   {
414     if (pageEncoding == null)
415       return;
416
417     if (_pageEncoding == null
418     || _pageEncoding.equalsIgnoreCase(pageEncoding)) {
419       _pageEncoding = pageEncoding;
420     }
421     else if ("UTF-16".equalsIgnoreCase(_pageEncoding)
422          && ("UTF-16LE".equalsIgnoreCase(pageEncoding)
423          || "UTF-16BE".equalsIgnoreCase(pageEncoding))) {
424       _pageEncoding = pageEncoding;
425     }
426     else if ("UTF-16".equalsIgnoreCase(pageEncoding)
427          && ("UTF-16LE".equalsIgnoreCase(_pageEncoding)
428          || "UTF-16BE".equalsIgnoreCase(_pageEncoding))) {
429     }
430     else {
431       String JavaDoc oldPageEncoding = _pageEncoding;
432       
433       _pageEncoding = pageEncoding;
434       
435       throw new JspParseException(L.l("Cannot change page encoding to '{0}' (old value '{1}'). The page encoding may only be set once.",
436                       pageEncoding, oldPageEncoding));
437     }
438     
439   }
440
441   /**
442    * Gets the JSP's character encoding
443    */

444   public String JavaDoc getPageEncoding()
445   {
446     return _pageEncoding;
447   }
448
449   /**
450    * Returns the JSP's info string.
451    */

452   public String JavaDoc getInfo()
453   {
454     return _info;
455   }
456
457   /**
458    * Sets the JSP's info string
459    */

460   public void setInfo(String JavaDoc info)
461   {
462     _info = info;
463   }
464
465   /**
466    * Returns the JSP's extends
467    */

468   public Class JavaDoc getExtends()
469   {
470     return _extends;
471   }
472
473   /**
474    * Sets the JSP's extends
475    */

476   public void setExtends(Class JavaDoc extendsValue)
477   {
478     _extends = extendsValue;
479   }
480
481   /**
482    * Returns true if parsing is a tag
483    */

484   public boolean isTag()
485   {
486     return _isTag;
487   }
488
489   /**
490    * Set if parsing a tag
491    */

492   public void setTag(boolean isTag)
493   {
494     _isTag = isTag;
495   }
496
497   /**
498    * Returns true if parsing is XML
499    */

500   public boolean isXml()
501   {
502     return _isXml;
503   }
504
505   /**
506    * Set if parsing is xml
507    */

508   public void setXml(boolean isXml)
509   {
510     _isXml = isXml;
511   }
512
513   /**
514    * Returns true if parsing forbids XML
515    */

516   public boolean isForbidXml()
517   {
518     return _isForbidXml;
519   }
520
521   /**
522    * Set if parsing forbids xml
523    */

524   public void setForbidXml(boolean isForbidXml)
525   {
526     _isForbidXml = isForbidXml;
527   }
528
529   /**
530    * Returns true if the print-null-as-blank is enabled.
531    */

532   public boolean isPrintNullAsBlank()
533   {
534     return _jspPropertyGroup.isPrintNullAsBlank();
535   }
536
537   /**
538    * Returns true if JSP whitespace is trimmed.
539    */

540   public boolean isTrimWhitespace()
541   {
542     return _isTrimWhitespace;
543   }
544
545   /**
546    * Set true if JSP whitespace is trimmed.
547    */

548   public void setTrimWhitespace(boolean trim)
549   {
550     _isTrimWhitespace = trim;
551   }
552
553   /**
554    * Returns true if JSP whitespace is trimmed.
555    */

556   public boolean isDeferredSyntaxAllowedAsLiteral()
557   {
558     return _isDeferredSyntaxAllowedAsLiteral;
559   }
560
561   /**
562    * Set true if JSP whitespace is trimmed.
563    */

564   public void setDeferredSyntaxAllowedAsLiteral(boolean trim)
565   {
566     _isDeferredSyntaxAllowedAsLiteral = trim;
567   }
568   
569   /**
570    * Gets the resource manager.
571    */

572   public JspResourceManager getResourceManager()
573   {
574     return _resourceManager;
575   }
576   
577   /**
578    * Sets the resource manager.
579    */

580   public void setResourceManager(JspResourceManager resourceManager)
581   {
582     _resourceManager = resourceManager;
583   }
584   
585   /**
586    * Gets the builder
587    */

588   public JspBuilder getBuilder()
589   {
590     return _jspBuilder;
591   }
592   
593   /**
594    * Sets the builder
595    */

596   public void setBuilder(JspBuilder jspBuilder)
597   {
598     _jspBuilder = jspBuilder;
599   }
600
601   private static CharScanner COMMA_DELIM_SCANNER = new CharScanner(" \t\n\r,");
602   
603   /**
604    * Adds an import string.
605    */

606   public void addImport(String JavaDoc importString)
607     throws JspParseException
608   {
609     StringCharCursor cursor = new StringCharCursor(importString);
610     CharBuffer cb = new CharBuffer();
611     while (cursor.current() != cursor.DONE) {
612       char ch;
613       COMMA_DELIM_SCANNER.skip(cursor);
614
615       cb.clear();
616       ch = COMMA_DELIM_SCANNER.scan(cursor, cb);
617
618       if (cb.length() != 0) {
619         String JavaDoc value = cb.toString();
620
621         if (! _importList.contains(value))
622           _importList.add(value);
623       }
624       else if (ch != cursor.DONE)
625         throw new JspParseException(L.l("`{0}' is an illegal page import directive.",
626                                         importString));
627     }
628   }
629
630   /**
631    * Returns the import list.
632    */

633   public ArrayList JavaDoc<String JavaDoc> getImportList()
634   {
635     return _importList;
636   }
637
638   /**
639    * Sets the URI pwd
640    */

641   public void setUriPwd(String JavaDoc uriPwd)
642   {
643     _uriPwd = uriPwd;
644   }
645
646   /**
647    * Gets the URI pwd
648    */

649   public String JavaDoc getUriPwd()
650   {
651     return _uriPwd;
652   }
653
654   /**
655    * Returns the line map.
656    */

657   public LineMap getLineMap()
658   {
659     return _lineMap;
660   }
661
662   /**
663    * Add a dependency.
664    */

665   public void addDepend(Path path)
666   {
667     Depend depend = new Depend(path);
668     if (! _depends.contains(depend))
669       _depends.add(depend);
670   }
671
672   /**
673    * Returns the dependencies
674    */

675   public ArrayList JavaDoc<Depend> getDependList()
676   {
677     return _depends;
678   }
679
680   /**
681    * Resolves a path.
682    *
683    * @param uri the uri for the path
684    *
685    * @return the Path
686    */

687   public Path resolvePath(String JavaDoc uri)
688   {
689     return getResourceManager().resolvePath(uri);
690   }
691
692   /**
693    * Set if recycle-tags is enabled.
694    */

695   public void setRecycleTags(boolean recycleTags)
696   {
697     _recycleTags = recycleTags;
698   }
699
700   /**
701    * Returns true if recycle-tags is enabled.
702    */

703   public boolean isRecycleTags()
704   {
705     return _recycleTags;
706   }
707
708   /**
709    * Returns the QName for the given name.
710    */

711   public QName getQName(String JavaDoc name)
712   {
713     int p = name.indexOf(':');
714
715     if (p < 0)
716       return new QName(name);
717     else {
718       String JavaDoc prefix = name.substring(0, p);
719       String JavaDoc uri = Namespace.find(_namespaces, prefix);
720
721       if (uri != null)
722     return new QName(name, uri);
723       else
724     return new QName(name);
725     }
726   }
727
728   public Namespace getNamespaces()
729   {
730     return _namespaces;
731   }
732
733   /**
734    * Pushes a namespace.
735    */

736   public void pushNamespace(String JavaDoc prefix, String JavaDoc uri)
737   {
738     _namespaces = new Namespace(_namespaces, prefix, uri);
739   }
740
741   /**
742    * Pops a namespace.
743    */

744   public void popNamespace(String JavaDoc prefix)
745   {
746     if (_namespaces._prefix.equals(prefix))
747       _namespaces = _namespaces.getNext();
748     else
749       throw new IllegalStateException JavaDoc();
750   }
751 }
752
753
Popular Tags