KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JspExpression


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.java;
30
31 import com.caucho.jsp.JspParseException;
32 import com.caucho.vfs.WriteStream;
33
34 import java.io.IOException JavaDoc;
35
36 /**
37  * Represents a Java scriptlet.
38  */

39 public class JspExpression extends JspNode {
40   private String JavaDoc _text;
41
42   /**
43    * Adds text to the expression.
44    */

45   public JspNode addText(String JavaDoc text)
46   {
47     _text = text;
48
49     return null;
50   }
51
52   /**
53    * Completes the expression.
54    */

55   public void endElement()
56     throws JspParseException
57   {
58     if (_parseState.isScriptingInvalid())
59       throw error(L.l("Script expressions are forbidden here. Scripting has been disabled either:\n1) disabled by the web.xml scripting-invalid\n2) disabled in a tag's descriptor\n3) forbidden in <jsp:attribute> or <jsp:body> tags."));
60   }
61   
62   /**
63    * True if the node has scripting
64    */

65   public boolean hasScripting()
66   {
67     return true;
68   }
69
70   /**
71    * Generates the XML text representation for the tag validation.
72    *
73    * @param os write stream to the generated XML.
74    */

75   public void printXml(WriteStream os)
76     throws IOException JavaDoc
77   {
78     os.print("<jsp:expression");
79     printJspId(os);
80     os.print(">");
81     
82     printXmlText(os, _text);
83     os.print("</jsp:expression>");
84   }
85
86   /**
87    * Generates the code for the scriptlet
88    *
89    * @param out the output writer for the generated java.
90    */

91   public void generate(JspJavaWriter out)
92     throws Exception JavaDoc
93   {
94     int length = _text.length();
95
96     out.print("out.print((");
97
98     // when an expression ends with '// comment', the code needs a
99
// newline so following code doesn't become part of the comment
100
boolean hasSlashes = false;
101
102     for (int i = 0; i < length; i++) {
103       char ch = _text.charAt(i);
104
105       if (ch == '/' && i + 1 < length && _text.charAt(i + 1) == '/')
106     hasSlashes = true;
107
108       // destLine needs incrementing for newlines
109
if (ch == '\n') {
110     hasSlashes = false;
111         out.println();
112       }
113       else
114         out.print(ch);
115     }
116     if (hasSlashes)
117       out.println();
118     
119     // jsp/150l
120
out.setLocation(_filename, _endLine);
121     // _endLine = -1;
122

123     out.println("));");
124   }
125 }
126
Popular Tags