1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.el.Expr; 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.jsp.el.JspELParser; 34 import com.caucho.vfs.WriteStream; 35 import com.caucho.xml.QName; 36 37 import java.io.IOException ; 38 39 public class JstlCoreOut extends JstlNode { 40 private static final QName VALUE = new QName("value"); 41 private static final QName ESCAPE_XML = new QName("escapeXml"); 42 private static final QName DEFAULT = new QName("default"); 43 44 private String _value; 45 private String _escapeXml = "true"; 46 private String _default; 47 48 private JspAttribute _valueAttr; 49 private JspAttribute _escapeXmlAttr; 50 private JspAttribute _defaultAttr; 51 52 55 public void addAttribute(QName name, String value) 56 throws JspParseException 57 { 58 if (VALUE.equals(name)) 59 _value = value; 60 else if (ESCAPE_XML.equals(name)) 61 _escapeXml = value; 62 else if (DEFAULT.equals(name)) 63 _default = value; 64 else 65 throw error(L.l("`{0}' is an unknown attribute for <{1}>.", 66 name.getName(), getTagName())); 67 } 68 69 72 public void addAttribute(QName name, JspAttribute value) 73 throws JspParseException 74 { 75 if (VALUE.equals(name)) 76 _valueAttr = value; 77 else if (ESCAPE_XML.equals(name)) 78 _escapeXmlAttr = value; 79 else if (DEFAULT.equals(name)) 80 _defaultAttr = value; 81 else 82 throw error(L.l("`{0}' is an unknown attribute for <{1}>.", 83 name.getName(), getTagName())); 84 } 85 86 91 public void printXml(WriteStream os) 92 throws IOException 93 { 94 os.print("<c:out"); 95 96 if (_value != null) { 97 os.print(" value=\""); 98 printXmlText(os, _value); 99 os.print("\""); 100 } 101 os.print(">"); 102 103 printXmlChildren(os); 104 105 os.print("</c:out>"); 106 } 107 108 111 public void generate(JspJavaWriter out) 112 throws Exception 113 { 114 if (_value == null && _valueAttr == null) 115 throw error(L.l("required attribute `value' missing from <{0}>", 116 getTagName())); 117 118 String escapeXml = "true"; 119 120 if (_escapeXml != null) 121 escapeXml = generateValue(boolean.class, _escapeXml); 122 else if (_escapeXmlAttr != null) 123 escapeXml = _escapeXmlAttr.generateValue(String .class); 124 125 if (_escapeXml != null && (_default != null || _defaultAttr != null)) { 126 String var = "_jsp_var_" + _gen.uniqueId(); 127 128 out.println("boolean " + var + " = " + escapeXml + ";"); 129 escapeXml = var; 130 } 131 132 if (_default != null || _defaultAttr != null || hasChildren()) 133 out.print("if ("); 134 135 if (_valueAttr != null) { 136 out.print("com.caucho.el.Expr.toStream(out, "); 137 out.print(_valueAttr.generateValue(String .class)); 138 out.print(", " + escapeXml + ")"); 139 } 140 else if (hasRuntimeAttribute(_value)) { 141 out.print("com.caucho.el.Expr.toStream(out, "); 142 out.print(generateRTValue(String .class, _value)); 143 out.print(", " + escapeXml + ")"); 144 } 145 else { 146 int index = _gen.addExpr(_value); 147 148 out.print("_caucho_expr_" + index + 149 ".print(out, _jsp_env, " + escapeXml + ")"); 150 } 151 152 if (_default != null || _defaultAttr != null) { 153 out.println(") {"); 154 out.pushDepth(); 155 156 if (_defaultAttr != null) { 157 out.print("com.caucho.el.Expr.toStream(out, "); 158 out.print(_defaultAttr.generateValue(String .class)); 159 out.println(", " + escapeXml + ");"); 160 } 161 else if (hasRuntimeAttribute(_default)) { 162 out.print("com.caucho.el.Expr.toStream(out, "); 163 out.print(generateRTValue(String .class, _default)); 164 out.println(", " + escapeXml + ");"); 165 } 166 else { 167 Expr defaultExpr = new JspELParser(_gen.getELContext(), 168 _default).parse(); 169 170 if (defaultExpr.isConstant() && escapeXml.equals("true")) { 171 String string = defaultExpr.evalString(null); 172 173 if (string != null && ! string.equals("")) { 174 out.print("com.caucho.el.Expr.toStreamEscaped(out, \""); 175 out.printJavaString(string); 176 out.println("\");"); 177 } 178 } 179 else if (defaultExpr.isConstant() && escapeXml.equals("false")) { 180 String string = defaultExpr.evalString(null); 181 182 if (string != null && ! string.equals("")) { 183 out.addText(string); 184 } 185 } 186 else { 187 int defaultIndex = _gen.addExpr(defaultExpr); 188 out.println("_caucho_expr_" + defaultIndex + 189 ".print(out, _jsp_env, " + escapeXml + ");"); 190 } 191 } 192 193 out.popDepth(); 194 out.println("}"); 195 } 196 else if (! hasChildren()) { 197 out.println(";"); 198 } 199 else if (isChildrenStatic()) { 200 out.println(") {"); 201 out.pushDepth(); 202 203 out.print("com.caucho.el.Expr.toStream(out, "); 204 out.print("\"" + escapeJavaString(getStaticText().trim()) + "\""); 205 out.println(", " + escapeXml + ");"); 206 207 out.popDepth(); 208 out.println("}"); 209 } 210 else { 211 out.println(") {"); 212 out.pushDepth(); 213 214 out.println("out = pageContext.pushBody();"); 215 216 generateChildren(out); 217 218 out.println("pageContext.printBody((com.caucho.jsp.BodyContentImpl) out, " + escapeXml + ");"); 219 220 out.println("out = pageContext.popAndReleaseBody();"); 221 222 out.popDepth(); 223 out.println("}"); 224 } 225 } 226 } 227 | Popular Tags |