001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.java;
030:
031: import com.caucho.jsp.JspParseException;
032: import com.caucho.vfs.WriteStream;
033:
034: import java.io.IOException;
035:
036: /**
037: * Represents a Java scriptlet.
038: */
039: public class JspExpression extends JspNode {
040: private String _text;
041:
042: /**
043: * Adds text to the expression.
044: */
045: public JspNode addText(String text) {
046: _text = text;
047:
048: return null;
049: }
050:
051: /**
052: * Completes the expression.
053: */
054: public void endElement() throws JspParseException {
055: if (_parseState.isScriptingInvalid())
056: throw error(L
057: .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."));
058: }
059:
060: /**
061: * True if the node has scripting
062: */
063: public boolean hasScripting() {
064: return true;
065: }
066:
067: /**
068: * True if the node has scripting element (i.e. not counting rtexpr values)
069: */
070: @Override
071: public boolean hasScriptingElement() {
072: return true;
073: }
074:
075: /**
076: * Generates the XML text representation for the tag validation.
077: *
078: * @param os write stream to the generated XML.
079: */
080: public void printXml(WriteStream os) throws IOException {
081: os.print("<jsp:expression");
082: printJspId(os);
083: os.print(">");
084:
085: printXmlText(os, _text);
086: os.print("</jsp:expression>");
087: }
088:
089: /**
090: * Generates the code for the scriptlet
091: *
092: * @param out the output writer for the generated java.
093: */
094: public void generate(JspJavaWriter out) throws Exception {
095: int length = _text.length();
096:
097: out.print("out.print((");
098:
099: // when an expression ends with '// comment', the code needs a
100: // newline so following code doesn't become part of the comment
101: boolean hasSlashes = false;
102:
103: for (int i = 0; i < length; i++) {
104: char ch = _text.charAt(i);
105:
106: if (ch == '/' && i + 1 < length
107: && _text.charAt(i + 1) == '/')
108: hasSlashes = true;
109:
110: // destLine needs incrementing for newlines
111: if (ch == '\n') {
112: hasSlashes = false;
113: out.println();
114: } else
115: out.print(ch);
116: }
117: if (hasSlashes)
118: out.println();
119:
120: // jsp/150l
121: out.setLocation(_filename, _endLine);
122: // _endLine = -1;
123:
124: out.println("));");
125: }
126: }
|