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: import com.caucho.xml.QName;
034:
035: import java.io.IOException;
036: import java.util.ArrayList;
037:
038: /**
039: * Represents a Java scriptlet.
040: */
041: public class JspInclude extends JspNode {
042: private static final QName PAGE = new QName("page");
043: private static final QName FLUSH = new QName("flush");
044:
045: private String _page;
046: private boolean _flush = false; // jsp/15m4
047:
048: private String _text;
049:
050: private ArrayList<JspParam> _params;
051:
052: /**
053: * Adds an attribute.
054: */
055: public void addAttribute(QName name, String value)
056: throws JspParseException {
057: if (PAGE.equals(name))
058: _page = value;
059: else if (FLUSH.equals(name))
060: _flush = value.equals("true");
061: else
062: throw error(L.l(
063: "`{0}' is an invalid attribute in <jsp:include>",
064: name.getName()));
065: }
066:
067: /**
068: * True if the node has scripting
069: */
070: public boolean hasScripting() {
071: if (_params == null)
072: return false;
073:
074: for (int i = 0; i < _params.size(); i++) {
075: if (_params.get(i).hasScripting())
076: return true;
077: }
078:
079: return false;
080: }
081:
082: /**
083: * True if the jsf-parent setting is required.
084: */
085: @Override
086: public boolean isJsfParentRequired() {
087: return true;
088: }
089:
090: /**
091: * Adds text to the scriptlet.
092: */
093: public JspNode addText(String text) {
094: _text = text;
095:
096: return null;
097: }
098:
099: /**
100: * Adds a parameter.
101: */
102: public void addChild(JspNode node) throws JspParseException {
103: if (node instanceof JspParam) {
104: JspParam param = (JspParam) node;
105:
106: if (_params == null)
107: _params = new ArrayList<JspParam>();
108:
109: _params.add(param);
110: } else {
111: super .addChild(node);
112: }
113: }
114:
115: /**
116: * Generates the XML text representation for the tag validation.
117: *
118: * @param os write stream to the generated XML.
119: */
120: public void printXml(WriteStream os) throws IOException {
121: os.print("<jsp:include");
122:
123: printXmlAttribute(os, "page", _page);
124:
125: os.print("></jsp:include>");
126: }
127:
128: /**
129: * Generates the code for the scriptlet
130: *
131: * @param out the output writer for the generated java.
132: */
133: public void generate(JspJavaWriter out) throws Exception {
134: boolean hasQuery = false;
135:
136: if (_page == null)
137: throw error(L
138: .l("<jsp:include> expects a `page' attribute. `page' specifies the path to include."));
139:
140: if (hasRuntimeAttribute(_page)) {
141: out.print("pageContext.include(");
142: out.print(getRuntimeAttribute(_page));
143: } else {
144: String page = _page;
145:
146: out.print("pageContext.include(");
147: out.print(generateParameterValue(String.class, _page));
148: }
149:
150: if (_params != null) {
151: out.print(", ");
152: generateIncludeParams(out, _params);
153: }
154:
155: out.print(", " + _flush);
156:
157: out.println(");");
158: }
159: }
|