1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.config.types.Signature; 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.jsp.cfg.TldAttribute; 34 import com.caucho.util.L10N; 35 import com.caucho.vfs.WriteStream; 36 import com.caucho.xml.QName; 37 38 import javax.servlet.jsp.tagext.JspFragment ; 39 import java.io.IOException ; 40 41 public class JspDirectiveAttribute extends JspNode { 42 static L10N L = new L10N(JspDirectiveAttribute.class); 43 44 private static final QName NAME = new QName("name"); 45 private static final QName REQUIRED = new QName("required"); 46 private static final QName FRAGMENT = new QName("fragment"); 47 private static final QName RTEXPRVALUE = new QName("rtexprvalue"); 48 private static final QName TYPE = new QName("type"); 49 private static final QName DESCRIPTION = new QName("description"); 50 private static final QName DEFERRED_VALUE 51 = new QName("deferredValue"); 52 private static final QName DEFERRED_VALUE_TYPE 53 = new QName("deferredValueType"); 54 private static final QName DEFERRED_METHOD 55 = new QName("deferredMethod"); 56 private static final QName DEFERRED_METHOD_SIGNATURE 57 = new QName("deferredMethodSignature"); 58 59 private String _name; 60 private String _type; 61 private boolean _isRequired; 62 private boolean _isFragment; 63 private Boolean _isRtexprvalue; 64 private String _description; 65 66 private boolean _deferredValue; 67 private String _deferredValueType; 68 private boolean _deferredMethod; 69 private String _deferredMethodSignature; 70 71 77 public void addAttribute(QName name, String value) 78 throws JspParseException 79 { 80 if (! _gen.getParseState().isTag()) 81 throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.", 82 getTagName())); 83 84 JavaTagGenerator gen = (JavaTagGenerator) _gen; 85 86 if (NAME.equals(name)) { 87 if (gen.findVariable(value) != null) { 88 throw error(L.l("@attribute name '{0}' is already used by a variable.", 89 value)); 90 } 91 else if (gen.findAttribute(value) != null) { 92 throw error(L.l("@attribute name '{0}' is already used by another attribute.", 93 value)); 94 } 95 96 _name = value; 97 } 98 else if (TYPE.equals(name)) 99 _type = value; 100 else if (REQUIRED.equals(name)) 101 _isRequired = attributeToBoolean(name.getName(), value); 102 else if (FRAGMENT.equals(name)) 103 _isFragment = attributeToBoolean(name.getName(), value); 104 else if (RTEXPRVALUE.equals(name)) 105 _isRtexprvalue = attributeToBoolean(name.getName(), value); 106 else if (DESCRIPTION.equals(name)) 107 _description = value; 108 else if (DEFERRED_VALUE.equals(name)) { 109 _deferredValue = attributeToBoolean(name.getName(), value); 110 if (_deferredValue) 111 _type = "javax.el.ValueExpression"; 112 } 113 else if (DEFERRED_VALUE_TYPE.equals(name)) { 114 _type = "javax.el.ValueExpression"; 115 _deferredValueType = value; 116 } 117 else if (DEFERRED_METHOD.equals(name)) { 118 _deferredMethod = attributeToBoolean(name.getName(), value); 119 if (_deferredMethod) 120 _type = "javax.el.MethodExpression"; 121 } 122 else if (DEFERRED_METHOD_SIGNATURE.equals(name)) { 123 try { 124 new Signature(value); 125 } catch (Exception e) { 126 throw error(e.getMessage()); 127 } 128 129 _type = "javax.el.MethodExpression"; 130 _deferredMethodSignature = value; 131 } 132 else { 133 throw error(L.l("'{0}' is an unknown JSP attribute directive attributes. The valid attributes are: deferredMethod, deferredMethodSignature, deferredValue, deferredValueType, description, fragment, name, rtexprvalue, type.", 134 name.getName())); 135 } 136 } 137 138 141 public void endElement() 142 throws JspParseException 143 { 144 if (! _gen.getParseState().isTag()) 145 throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.", 146 getTagName())); 147 148 if (_name == null) 149 throw error(L.l("<{0}> needs a 'name' attribute.", 150 getTagName())); 151 152 JavaTagGenerator tagGen = (JavaTagGenerator) _gen; 153 154 TldAttribute attr = new TldAttribute(); 155 attr.setName(_name); 156 157 if (_type != null) { 158 Class type = loadClass(_type); 159 160 if (type == null) 161 throw error(L.l("type '{0}' is an unknown class for tag attribute {1}.", 162 _type, _name)); 163 164 if (type.isPrimitive()) 165 throw error(L.l("attribute type '{0}' cannot be a Java primitive for {1}.", 166 _type, _name)); 167 168 attr.setType(type); 169 } 170 171 attr.setRequired(_isRequired); 172 173 if (_isFragment && _isRtexprvalue != null) 174 throw error(L.l("@attribute rtexprvalue cannot be set when fragment is true.")); 175 176 if (_isFragment && _type != null) 177 throw error(L.l("@attribute type cannot be set when fragment is true.")); 178 179 if (_isRtexprvalue == null || Boolean.TRUE.equals(_isRtexprvalue)) 180 attr.setRtexprvalue(Boolean.TRUE); 181 182 if (_isFragment) 183 attr.setType(JspFragment .class); 184 185 if (_deferredValue && _deferredValueType != null) 186 throw error(L.l("@attribute deferredValue and deferredValueType may not both be specified")); 187 188 if (_deferredMethod && _deferredMethodSignature != null) 189 throw error(L.l("@attribute deferredMethod and deferredMethodSignature may not both be specified")); 190 191 if ((_deferredValue || _deferredValueType != null) 192 && (_deferredMethod || _deferredMethodSignature != null)) 193 throw error(L.l("@attribute deferredValue and deferredMethod may not both be specified")); 194 195 if (_deferredValue || _deferredValueType != null) { 196 attr.setDeferredValue(new TldAttribute.DeferredValue()); 197 198 if (_deferredValueType != null) 199 attr.getDeferredValue().setType(_deferredValueType); 200 } 201 202 if (_deferredMethod || _deferredMethodSignature != null) { 203 attr.setDeferredMethod(new TldAttribute.DeferredMethod()); 204 205 if (_deferredMethodSignature != null) 206 attr.getDeferredMethod().setMethodSignature(new Signature(_deferredMethodSignature)); 207 } 208 209 tagGen.addAttribute(attr); 210 } 211 212 215 public boolean isStatic() 216 { 217 return true; 218 } 219 220 225 public void printXml(WriteStream os) 226 throws IOException 227 { 228 os.print("<jsp:directive.attribute"); 229 os.print(" jsp:id=\"" + _gen.generateJspId() + "\""); 230 os.print(" name=\"" + _name + "\""); 231 232 if (_type != null) 233 os.print(" type=\"" + _type + "\""); 234 235 os.println("/>"); 236 } 237 238 243 public void generate(JspJavaWriter out) 244 throws Exception 245 { 246 } 247 } 248 | Popular Tags |