1 29 30 package com.caucho.jsp.java; 31 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.jsp.cfg.TldAttribute; 34 import com.caucho.jsp.cfg.TldVariable; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.WriteStream; 37 import com.caucho.xml.QName; 38 39 import java.io.IOException ; 40 import java.util.ArrayList ; 41 42 public class JspDirectiveVariable extends JspNode { 43 private static final QName NAME_GIVEN = new QName("name-given"); 44 private static final QName NAME_FROM_ATTRIBUTE = 45 new QName("name-from-attribute"); 46 private static final QName ALIAS = new QName("alias"); 47 private static final QName VARIABLE_CLASS = new QName("variable-class"); 48 private static final QName DECLARE = new QName("declare"); 49 private static final QName SCOPE = new QName("scope"); 50 private static final QName DESCRIPTION = new QName("description"); 51 52 static final L10N L = new L10N(JspDirectiveVariable.class); 53 54 private String _nameGiven; 55 private String _nameFromAttribute; 56 private String _alias; 57 private String _variableClass; 58 private boolean _isDeclare = true; 59 private String _scope; 60 private String _description; 61 62 68 public void addAttribute(QName name, String value) 69 throws JspParseException 70 { 71 if (! _gen.getParseState().isTag()) 72 throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.", 73 getTagName())); 74 75 JavaTagGenerator gen = (JavaTagGenerator) _gen; 76 if (NAME_GIVEN.equals(name)) { 77 if (gen.findVariable(value) != null) { 78 throw error(L.l("@variable name-given '{0}' is already used by another variable.", 79 value)); 80 } 81 else if (gen.findAttribute(value) != null) { 82 throw error(L.l("@variable name-given '{0}' is already used by an attribute.", 83 value)); 84 } 85 else if (value.equals(gen.getDynamicAttributes())) { 86 throw error(L.l("@variable name-given '{0}' cannot be the same as the tag's dynamic-attributes.", 87 value)); 88 } 89 90 _nameGiven = value; 91 } 92 else if (NAME_FROM_ATTRIBUTE.equals(name)) { 93 if (gen.findVariable(value) != null) { 94 throw error(L.l("@variable name-from-attribute '{0}' is already used by another variable.", 95 value)); 96 } 97 103 104 _nameFromAttribute = value; 105 } 106 else if (ALIAS.equals(name)) 107 _alias = value; 108 else if (VARIABLE_CLASS.equals(name)) 109 _variableClass = value; 110 else if (DECLARE.equals(name)) 111 _isDeclare = attributeToBoolean(name.getName(), value); 112 else if (SCOPE.equals(name)) { 113 if (! "NESTED".equals(value) && 114 ! "AT_BEGIN".equals(value) && 115 ! "AT_END".equals(value)) 116 throw error(L.l("'{0}' is an illegal scope value. NESTED, AT_BEGIN, and AT_END are the only accepted values.", 117 value)); 118 119 _scope = value; 120 } 121 else if (DESCRIPTION.equals(name)) 122 _description = value; 123 else { 124 throw error(L.l("'{0}' is an unknown JSP variable directive attributes. Valid attributes are: alias, declare, description, name-from-attribute, name-given, scope, variable-class.", 125 name.getName())); 126 } 127 } 128 129 132 public void endElement() 133 throws JspParseException 134 { 135 if (! _gen.getParseState().isTag()) 136 throw error(L.l("'{0}' is only allowed in .tag files. Variable directives are not allowed in normal JSP files.", 137 getTagName())); 138 139 if (_nameGiven == null && _nameFromAttribute == null) 140 throw error(L.l("<{0}> needs a 'name-given' or 'name-from-attribute' attribute.", 141 getTagName())); 142 143 if (_nameFromAttribute != null && _alias == null) 144 throw error(L.l("<{0}> needs an 'alias' attribute. name-from-attribute requires an alias attribute.", 145 getTagName())); 146 if (_alias != null && _nameFromAttribute == null) 147 throw error(L.l("<{0}> needs an 'name-from-attribute' attribute. alias requires a name-from-attribute attribute.", 148 getTagName())); 149 150 JavaTagGenerator tagGen = (JavaTagGenerator) _gen; 151 152 TldVariable var = new TldVariable(); 153 var.setNameGiven(_nameGiven); 154 var.setNameFromAttribute(_nameFromAttribute); 155 var.setAlias(_alias); 156 157 String name = _nameGiven; 158 if (name == null) 159 name = _nameFromAttribute; 160 161 if (_variableClass != null) 162 var.setVariableClass(_variableClass); 163 164 var.setDeclare(_isDeclare); 165 if (_scope != null) 166 var.setScope(_scope); 167 168 tagGen.addVariable(var); 169 } 170 171 174 public boolean isStatic() 175 { 176 return true; 177 } 178 179 184 public void printXml(WriteStream os) 185 throws IOException 186 { 187 os.print("<jsp:directive.variable"); 188 os.print(" jsp:id=\"" + _gen.generateJspId() + "\""); 189 190 if (_nameGiven != null) 191 os.print(" name-given=\"" + _nameGiven + "\""); 192 193 if (_nameFromAttribute != null) 194 os.print(" name-from-attribute=\"" + _nameFromAttribute + "\""); 195 196 if (_variableClass != null) 197 os.print(" variable-class=\"" + _variableClass + "\""); 198 199 os.print("/>"); 200 } 201 202 207 public void generatePrologue(JspJavaWriter out) 208 throws Exception 209 { 210 JavaTagGenerator gen = (JavaTagGenerator) _gen; 211 212 if (_nameFromAttribute == null) 213 return; 214 215 ArrayList <TldAttribute> attributes = gen.getAttributes(); 216 for (int i = 0; i < attributes.size(); i++) { 217 TldAttribute attr = attributes.get(i); 218 219 if (! attr.getName().equals(_nameFromAttribute)) 220 continue; 221 222 if (! String .class.equals(attr.getType())) 223 throw error(L.l("name-from-attribute variable '{0}' needs a matching String attribute, not '{1}' . name-from-attribute requires a matching String attribute.", 224 _nameFromAttribute, attr.getType().getName())); 225 226 return; 227 } 228 229 throw error(L.l("name-from-attribute variable '{0}' needs a matching String attribute.", 230 _nameFromAttribute)); 231 } 232 233 238 public void generate(JspJavaWriter out) 239 throws Exception 240 { 241 } 242 } 243 | Popular Tags |