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 declaration.
039: */
040: public class JspDeclaration extends JspNode {
041: private String _text;
042:
043: /**
044: * Adds text to the directive.
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("Script declarations 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: _gen.addDeclaration(this );
064: }
065:
066: /**
067: * Returns true for a scripting element.
068: */
069: public boolean hasScripting() {
070: return true;
071: }
072:
073: /**
074: * True if the node has scripting element (i.e. not counting rtexpr values)
075: */
076: @Override
077: public boolean hasScriptingElement() {
078: return true;
079: }
080:
081: /**
082: * Generates the XML text representation for the tag validation.
083: *
084: * @param os write stream to the generated XML.
085: */
086: public void printXml(WriteStream os) throws IOException {
087: os.print("<jsp:declaration");
088: printJspId(os);
089: os.print(">");
090:
091: printXmlText(os, _text);
092: os.print("</jsp:declaration>");
093: }
094:
095: /**
096: * Generates the code for the scriptlet
097: *
098: * @param out the output writer for the generated java.
099: */
100: public void generateDeclaration(JspJavaWriter out)
101: throws IOException {
102: int length = _text.length();
103:
104: out.setLocation(getFilename(), getStartLine());
105:
106: for (int i = 0; i < length; i++) {
107: char ch = _text.charAt(i);
108:
109: if (ch == '\r' && i + 1 < length
110: && _text.charAt(i + 1) != '\n')
111: ch = '\n';
112:
113: if (ch == '\n')
114: out.println();
115: else
116: out.print(ch);
117: }
118:
119: out.setLocation(_filename, _endLine);
120:
121: out.println();
122: }
123:
124: /**
125: * No general code.
126: *
127: * @param out the output writer for the generated java.
128: */
129: public void generate(JspJavaWriter out) throws Exception {
130: }
131: }
|