KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JspForward


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.java;
30
31 import com.caucho.jsp.JspParseException;
32 import com.caucho.vfs.WriteStream;
33 import com.caucho.xml.QName;
34
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * Represents a Java scriptlet.
40  */

41 public class JspForward extends JspNode {
42   private static final QName PAGE = new QName("page");
43   
44   private String JavaDoc _page;
45   
46   private ArrayList JavaDoc<JspParam> _params;
47   
48   /**
49    * Adds an attribute.
50    */

51   public void addAttribute(QName name, String JavaDoc value)
52     throws JspParseException
53   {
54     if (PAGE.equals(name))
55       _page = value;
56     else
57       throw error(L.l("`{0}' is an invalid attribute in <jsp:forward>",
58                       name.getName()));
59   }
60
61   /**
62    * Adds a parameter.
63    */

64   public void addChild(JspNode node)
65     throws JspParseException
66   {
67     if (node instanceof JspParam) {
68       JspParam param = (JspParam) node;
69
70       if (_params == null)
71         _params = new ArrayList JavaDoc<JspParam>();
72
73       _params.add(param);
74     }
75     else {
76       super.addChild(node);
77     }
78   }
79
80   /**
81    * Generates the XML text representation for the tag validation.
82    *
83    * @param os write stream to the generated XML.
84    */

85   public void printXml(WriteStream os)
86     throws IOException JavaDoc
87   {
88     os.print("<jsp:forward page=\"" + _page + "\">");
89
90     os.print("</jsp:forward>");
91   }
92
93   /**
94    * Generates the code for the scriptlet
95    *
96    * @param out the output writer for the generated java.
97    */

98   public void generate(JspJavaWriter out)
99     throws Exception JavaDoc
100   {
101     boolean hasQuery = false;
102
103     if (_page == null)
104       throw error(L.l("<jsp:forward> expects a `page' attribute. `page' specifies the path to forward."));
105
106     if (hasRuntimeAttribute(_page)) {
107       out.print("pageContext.forward(");
108       out.print(getRuntimeAttribute(_page));
109     }
110     else {
111       out.print("pageContext.forward(\"");
112       out.print(_page);
113       out.print("\"");
114     }
115
116     if (_params != null) {
117       out.print(", ");
118       generateIncludeParams(out, _params);
119     }
120     
121     out.println(");");
122     if (_gen.isTag() || isInFragment())
123       out.println("if (true) throw new SkipPageException();");
124     else
125       out.println("if (true) return;");
126   }
127 }
128
Popular Tags