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 JspForward extends JspNode {
042: private static final QName PAGE = new QName("page");
043:
044: private String _page;
045:
046: private ArrayList<JspParam> _params;
047:
048: /**
049: * Adds an attribute.
050: */
051: public void addAttribute(QName name, String value)
052: throws JspParseException {
053: if (PAGE.equals(name))
054: _page = value;
055: else
056: throw error(L.l(
057: "`{0}' is an invalid attribute in <jsp:forward>",
058: name.getName()));
059: }
060:
061: /**
062: * Adds a parameter.
063: */
064: public void addChild(JspNode node) throws JspParseException {
065: if (node instanceof JspParam) {
066: JspParam param = (JspParam) node;
067:
068: if (_params == null)
069: _params = new ArrayList<JspParam>();
070:
071: _params.add(param);
072: } else {
073: super .addChild(node);
074: }
075: }
076:
077: /**
078: * Generates the XML text representation for the tag validation.
079: *
080: * @param os write stream to the generated XML.
081: */
082: public void printXml(WriteStream os) throws IOException {
083: os.print("<jsp:forward");
084:
085: printXmlAttribute(os, "page", _page);
086:
087: os.print("></jsp:forward>");
088: }
089:
090: /**
091: * Generates the code for the scriptlet
092: *
093: * @param out the output writer for the generated java.
094: */
095: public void generate(JspJavaWriter out) throws Exception {
096: boolean hasQuery = false;
097:
098: if (_page == null)
099: throw error(L
100: .l("<jsp:forward> expects a `page' attribute. `page' specifies the path to forward."));
101:
102: if (hasRuntimeAttribute(_page)) {
103: out.print("pageContext.forward(");
104: out.print(getRuntimeAttribute(_page));
105: } else {
106: out.print("pageContext.forward(");
107: out.print(generateParameterValue(String.class, _page));
108: }
109:
110: if (_params != null) {
111: out.print(", ");
112: generateIncludeParams(out, _params);
113: }
114:
115: out.println(");");
116: if (_gen.isTag() || isInFragment())
117: out.println("if (true) throw new SkipPageException();");
118: else
119: out.println("if (true) return;");
120: }
121: }
|