KickJava   Java API By Example, From Geeks To Geeks.

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


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 JspInclude extends JspNode {
42   private static final QName PAGE = new QName("page");
43   private static final QName FLUSH = new QName("flush");
44   
45   private String JavaDoc _page;
46   private boolean _flush = false; // jsp/15m4
47

48   private String JavaDoc _text;
49   
50   private ArrayList JavaDoc<JspParam> _params;
51
52   /**
53    * Adds an attribute.
54    */

55   public void addAttribute(QName name, String JavaDoc value)
56     throws JspParseException
57   {
58     if (PAGE.equals(name))
59       _page = value;
60     else if (FLUSH.equals(name))
61       _flush = value.equals("true");
62     else
63       throw error(L.l("`{0}' is an invalid attribute in <jsp:include>",
64                       name.getName()));
65   }
66   
67   /**
68    * True if the node has scripting
69    */

70   public boolean hasScripting()
71   {
72     if (_params == null)
73       return false;
74     
75     for (int i = 0; i < _params.size(); i++) {
76       if (_params.get(i).hasScripting())
77     return true;
78     }
79
80     return false;
81   }
82
83   /**
84    * Adds text to the scriptlet.
85    */

86   public JspNode addText(String JavaDoc text)
87   {
88     _text = text;
89
90     return null;
91   }
92
93   /**
94    * Adds a parameter.
95    */

96   public void addChild(JspNode node)
97     throws JspParseException
98   {
99     if (node instanceof JspParam) {
100       JspParam param = (JspParam) node;
101
102       if (_params == null)
103         _params = new ArrayList JavaDoc<JspParam>();
104
105       _params.add(param);
106     }
107     else {
108       super.addChild(node);
109     }
110   }
111
112   /**
113    * Generates the XML text representation for the tag validation.
114    *
115    * @param os write stream to the generated XML.
116    */

117   public void printXml(WriteStream os)
118     throws IOException JavaDoc
119   {
120     os.print("<jsp:include page=\"" + _page + "\">");
121
122     os.print("</jsp:include>");
123   }
124
125   /**
126    * Generates the code for the scriptlet
127    *
128    * @param out the output writer for the generated java.
129    */

130   public void generate(JspJavaWriter out)
131     throws Exception JavaDoc
132   {
133     boolean hasQuery = false;
134
135     if (_page == null)
136       throw error(L.l("<jsp:include> expects a `page' attribute. `page' specifies the path to include."));
137
138     if (hasRuntimeAttribute(_page)) {
139       out.print("pageContext.include(");
140       out.print(getRuntimeAttribute(_page));
141     }
142     else {
143       String JavaDoc page = _page;
144
145       out.print("pageContext.include(");
146       out.print(generateParameterValue(String JavaDoc.class, _page));
147     }
148
149     if (_params != null) {
150       out.print(", ");
151       generateIncludeParams(out, _params);
152     }
153
154     out.print(", " + _flush);
155     
156     out.println(");");
157   }
158 }
159
Popular Tags