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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.java;
031:
032: import com.caucho.jsp.JspParseException;
033: import com.caucho.vfs.WriteStream;
034:
035: import java.io.IOException;
036:
037: /**
038: * Represents a Java scriptlet.
039: */
040: public class JspScriptlet extends JspNode {
041: private String _text;
042:
043: /**
044: * Adds text to the scriptlet.
045: */
046: public JspNode addText(String text) {
047: if (_text == null)
048: _text = text;
049: else
050: _text += text;
051:
052: return null;
053: }
054:
055: /**
056: * Completes the scriptlet.
057: */
058: public void endElement() throws JspParseException {
059: if (_parseState.isScriptingInvalid())
060: throw error(L
061: .l("Scriptlets 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."));
062: }
063:
064: /**
065: * True if the node has scripting
066: */
067: public boolean hasScripting() {
068: return true;
069: }
070:
071: /**
072: * True if the node has scripting element (i.e. not counting rtexpr values)
073: */
074: @Override
075: public boolean hasScriptingElement() {
076: return true;
077: }
078:
079: /**
080: * Generates the XML text representation for the tag validation.
081: *
082: * @param os write stream to the generated XML.
083: */
084: public void printXml(WriteStream os) throws IOException {
085: os.print("<jsp:scriptlet");
086: printJspId(os);
087: os.print(">");
088:
089: printXmlText(os, _text);
090: os.print("</jsp:scriptlet>");
091: }
092:
093: /**
094: * Generates the code for the scriptlet
095: *
096: * @param out the output writer for the generated java.
097: */
098: public void generate(JspJavaWriter out) throws Exception {
099: int length = _text.length();
100:
101: for (int i = 0; i < length; i++) {
102: char ch = _text.charAt(i);
103:
104: if (ch == '\r' && i + 1 < length
105: && _text.charAt(i + 1) != '\n')
106: ch = '\n';
107:
108: out.print(ch);
109: }
110:
111: // jsp/150l
112: out.setLocation(_filename, _endLine);
113: // _endLine = -1;
114:
115: out.println();
116: }
117: }
|