KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.BeanUtil;
33 import com.caucho.vfs.WriteStream;
34 import com.caucho.xml.QName;
35
36 import java.io.IOException JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38
39 /**
40  * Represents a Java scriptlet.
41  */

42 public class JspGetProperty extends JspContainerNode {
43   private static final QName NAME = new QName("name");
44   private static final QName PROPERTY = new QName("property");
45   
46   private String JavaDoc _name;
47   private String JavaDoc _property;
48   
49   private String JavaDoc _text;
50
51   /**
52    * Adds an attribute.
53    */

54   public void addAttribute(QName name, String JavaDoc value)
55     throws JspParseException
56   {
57     if (NAME.equals(name))
58       _name = value;
59     else if (PROPERTY.equals(name))
60       _property = value;
61     else
62       throw error(L.l("`{0}' is an invalid attribute in <jsp:getProperty>",
63                       name.getName()));
64   }
65
66   /**
67    * Adds text to the scriptlet.
68    */

69   public JspNode addText(String JavaDoc text)
70   {
71     _text = text;
72
73     return null;
74   }
75
76   /**
77    * Generates the XML text representation for the tag validation.
78    *
79    * @param os write stream to the generated XML.
80    */

81   public void printXml(WriteStream os)
82     throws IOException JavaDoc
83   {
84     os.print("<jsp:getProperty name=\"" + _name + "\"");
85     os.print(" property=\"" + _property + "\"/>");
86   }
87
88   /**
89    * Generates the code for the scriptlet
90    *
91    * @param out the output writer for the generated java.
92    */

93   public void generate(JspJavaWriter out)
94     throws Exception JavaDoc
95   {
96     if (_name == null)
97       throw error(L.l("<jsp:getProperty> expects attribute `name'. name specifies the variable name of the bean."));
98
99     if (_property == null)
100       throw error(L.l("<jsp:getProperty> expects attribute `property'. property specifies the bean's get method."));
101
102     Class JavaDoc cl = _gen.getClass(_name);
103     if (cl == null)
104       throw error(L.l("`{0}' is not a declared bean. Beans used in <jsp:getProperty> must be declared in a <jsp:useBean>.", _name));
105
106     Class JavaDoc beanClass = cl;
107     Method JavaDoc reader = BeanUtil.getGetMethod(cl, _property);
108     if (reader == null)
109       throw error(L.l("`{0}' has no get method for `{1}'",
110                       _name, _property));
111
112     out.print(" out.print(((");
113     out.printClass(beanClass);
114     out.print(") ");
115     out.print("pageContext.findAttribute(\"" + _name + "\"))");
116     out.println("." + reader.getName() + "());");
117   }
118 }
119
Popular Tags