1 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 ; 37 import java.lang.reflect.Method ; 38 39 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 _name; 47 private String _property; 48 49 private String _text; 50 51 54 public void addAttribute(QName name, String 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 69 public JspNode addText(String text) 70 { 71 _text = text; 72 73 return null; 74 } 75 76 81 public void printXml(WriteStream os) 82 throws IOException 83 { 84 os.print("<jsp:getProperty name=\"" + _name + "\""); 85 os.print(" property=\"" + _property + "\"/>"); 86 } 87 88 93 public void generate(JspJavaWriter out) 94 throws Exception 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 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 beanClass = cl; 107 Method 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 |