1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.vfs.WriteStream; 33 import com.caucho.xml.QName; 34 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 38 public class JstlFmtMessage extends JstlNode { 39 private static final QName KEY = new QName("key"); 40 private static final QName BUNDLE = new QName("bundle"); 41 private static final QName VAR = new QName("var"); 42 private static final QName SCOPE = new QName("scope"); 43 44 private String _key; 45 private JspAttribute _keyAttr; 46 47 private String _bundle; 48 private JspAttribute _bundleAttr; 49 50 private String _var; 51 private String _scope; 52 53 private ArrayList <JstlFmtParam> _params = new ArrayList <JstlFmtParam>(); 54 55 58 public void addAttribute(QName name, String value) 59 throws JspParseException 60 { 61 if (KEY.equals(name)) 62 _key = value; 63 else if (BUNDLE.equals(name)) 64 _bundle = value; 65 else if (VAR.equals(name)) 66 _var = value; 67 else if (SCOPE.equals(name)) 68 _scope = value; 69 else 70 throw error(L.l("`{0}' is an unknown attribute for <{1}>.", 71 name.getName(), getTagName())); 72 } 73 74 77 public void addAttribute(QName name, JspAttribute value) 78 throws JspParseException 79 { 80 if (KEY.equals(name)) 81 _keyAttr = value; 82 else if (BUNDLE.equals(name)) 83 _bundleAttr = value; 84 else 85 throw error(L.l("`{0}' is an unsupported jsp:attribute for <{1}>.", 86 name.getName(), getTagName())); 87 } 88 89 92 public void addChild(JspNode node) 93 throws JspParseException 94 { 95 if (node instanceof JstlFmtParam) 96 _params.add((JstlFmtParam) node); 97 else 98 super.addChild(node); 99 } 100 101 106 public void printXml(WriteStream os) 107 throws IOException 108 { 109 os.print("<fmt:message"); 110 111 if (_key != null) { 112 os.print(" key=\"" + xmlText(_key) + "\""); 113 } 114 115 if (_bundle != null) 116 os.print(" bundle=\"" + xmlText(_bundle) + "\""); 117 118 if (_var != null) 119 os.print(" var=\"" + _var + "\""); 120 121 if (_scope != null) 122 os.print(" scope=\"" + _scope + "\""); 123 124 os.print(">"); 125 126 for (int i = 0; i < _params.size(); i++) 127 _params.get(i).printXml(os); 128 129 printXmlChildren(os); 130 131 os.print("</fmt:message>"); 132 } 133 134 137 public void generatePrologue(JspJavaWriter out) 138 throws Exception 139 { 140 for (int i = 0; i < _params.size(); i++) { 141 _params.get(i).generatePrologue(out); 142 } 143 } 144 145 148 public void generate(JspJavaWriter out) 149 throws Exception 150 { 151 String keyExpr = null; 152 153 if (_key != null) { 154 keyExpr = generateValue(String .class, _key); 155 } 156 else if (_keyAttr != null) { 157 keyExpr = _keyAttr.generateValue(); 158 } 159 else if (isChildrenStatic()) { 160 keyExpr = '"' + escapeJavaString(getStaticText().trim()) + '"'; 161 } 162 else { 163 out.println("out = pageContext.pushBody();"); 164 165 generateChildren(out); 166 167 keyExpr = "_caucho_var_" + _gen.uniqueId(); 168 169 out.println("String " + keyExpr + " = ((com.caucho.jsp.BodyContentImpl) out).getTrimString();"); 170 171 out.println("out = pageContext.popBody();"); 172 } 173 174 176 String prefix = ""; 177 JspNode node; 178 179 188 189 String paramVar = "null"; 190 191 if (_params.size() > 0) { 192 paramVar = "_caucho_param_" + _gen.uniqueId(); 193 194 out.println("Object []" + paramVar + " = new Object[" + _params.size() + "];"); 195 196 for (int i = 0; i < _params.size(); i++) { 197 JstlFmtParam param = _params.get(i); 198 199 param.generateSet(out, paramVar + "[" + i + "]"); 200 201 219 } 220 } 221 222 if (_var != null) { 223 String value; 224 225 if (_bundleAttr != null) { 226 value = ("pageContext.getLocalizedMessage(" + 227 _bundleAttr.generateValue() + ", " + 228 prefix + keyExpr + ", " + paramVar + ", null)"); 229 } 230 else if (_bundle != null) { 231 String bundleExpr = generateValue(Object .class, _bundle); 232 233 value = ("pageContext.getLocalizedMessage(" + bundleExpr + ", " + 234 prefix + keyExpr + ", " + paramVar + ", null)"); 235 } 236 else 237 value = ("pageContext.getLocalizedMessage(null, " + prefix + keyExpr + ", " + 238 paramVar + ", null)"); 239 240 generateSetOrRemove(out, _var, _scope, value); 241 } 242 else { 243 if (_bundleAttr != null) { 244 String bundleExpr = _bundleAttr.generateValue(); 245 246 out.println("out.print(pageContext.getLocalizedMessage(" + bundleExpr + ", " + prefix + keyExpr + ", " + paramVar + ", null));"); 247 248 } 249 else if (_bundle != null) { 250 String bundleExpr = generateValue(Object .class, _bundle); 251 252 out.println("out.print(pageContext.getLocalizedMessage(" + bundleExpr + ", " + prefix + keyExpr + ", " + paramVar + ", null));"); 253 254 } 255 else 256 out.println("out.print(pageContext.getLocalizedMessage(null, " + prefix + keyExpr + ", " + paramVar + ", null));"); 257 } 258 } 259 } 260 | Popular Tags |