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.util.BeanUtil;
033: import com.caucho.vfs.WriteStream;
034: import com.caucho.xml.QName;
035:
036: import java.io.IOException;
037: import java.lang.reflect.Method;
038:
039: /**
040: * Represents a Java scriptlet.
041: */
042: public class JspGetProperty extends JspContainerNode {
043: private static final QName NAME = new QName("name");
044: private static final QName PROPERTY = new QName("property");
045:
046: private String _name;
047: private String _property;
048:
049: private String _text;
050:
051: /**
052: * Adds an attribute.
053: */
054: public void addAttribute(QName name, String value)
055: throws JspParseException {
056: if (NAME.equals(name))
057: _name = value;
058: else if (PROPERTY.equals(name))
059: _property = value;
060: else
061: throw error(L
062: .l(
063: "`{0}' is an invalid attribute in <jsp:getProperty>",
064: name.getName()));
065: }
066:
067: /**
068: * Adds text to the scriptlet.
069: */
070: public JspNode addText(String text) {
071: _text = text;
072:
073: return null;
074: }
075:
076: /**
077: * Generates the XML text representation for the tag validation.
078: *
079: * @param os write stream to the generated XML.
080: */
081: public void printXml(WriteStream os) throws IOException {
082: os.print("<jsp:getProperty name=\"" + _name + "\"");
083: os.print(" property=\"" + _property + "\"/>");
084: }
085:
086: /**
087: * Generates the code for the scriptlet
088: *
089: * @param out the output writer for the generated java.
090: */
091: public void generate(JspJavaWriter out) throws Exception {
092: if (_name == null)
093: throw error(L
094: .l("<jsp:getProperty> expects attribute `name'. name specifies the variable name of the bean."));
095:
096: if (_property == null)
097: throw error(L
098: .l("<jsp:getProperty> expects attribute `property'. property specifies the bean's get method."));
099:
100: Class cl = _gen.getClass(_name);
101: if (cl == null)
102: throw error(L
103: .l(
104: "`{0}' is not a declared bean. Beans used in <jsp:getProperty> must be declared in a <jsp:useBean>.",
105: _name));
106:
107: Class beanClass = cl;
108: Method reader = BeanUtil.getGetMethod(cl, _property);
109: if (reader == null)
110: throw error(L.l("`{0}' has no get method for `{1}'", _name,
111: _property));
112:
113: out.print(" out.print(((");
114: out.printClass(beanClass);
115: out.print(") ");
116: out.print("pageContext.findAttribute(\"" + _name + "\"))");
117: out.println("." + reader.getName() + "());");
118: }
119: }
|