1 29 30 package com.caucho.jsp.java; 31 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.jsp.TagInstance; 34 import com.caucho.vfs.WriteStream; 35 import com.caucho.xml.QName; 36 import com.caucho.xml.XmlChar; 37 38 import java.io.IOException ; 39 40 43 public class JspAttribute extends JspFragmentNode { 44 private static final QName NAME = new QName("name"); 45 private static final QName TRIM = new QName("trim"); 46 47 private QName _name; 48 private boolean _trim = true; 49 private TagInstance _tag; 50 51 private boolean _oldScriptingInvalid; 52 53 public JspAttribute() 54 { 55 } 56 57 60 public QName getName() 61 { 62 return _name; 63 } 64 65 68 public boolean isTrim() 69 { 70 return _trim; 71 } 72 73 76 public void addAttribute(QName name, String value) 77 throws JspParseException 78 { 79 if (NAME.equals(name)) 80 _name = _gen.getParseState().getQName(value); 81 else if (TRIM.equals(name)) 82 _trim = value.equals("true"); 83 else 84 throw error(L.l("`{0}' is an unknown attribute for jsp:attribute.", 85 name)); 86 } 87 88 91 public void endAttributes() 92 throws JspParseException 93 { 94 _oldScriptingInvalid = _parseState.isScriptingInvalid(); 95 98 super.endAttributes(); 99 } 100 101 104 public JspNode addText(String text) 105 throws JspParseException 106 { 107 JspNode node = new StaticText(_gen, text, this); 108 109 addChild(node); 110 111 return node; 112 } 113 114 117 public void endElement() 118 throws JspParseException 119 { 120 _parseState.setScriptingInvalid(_oldScriptingInvalid); 121 122 if (_name == null) 123 throw error(L.l("jsp:attribute needs a `name' attribute.")); 124 125 if (_trim) { 126 prefix_loop: 127 while (_children.size() > 0) { 128 JspNode node = (JspNode) _children.get(0); 129 130 if (! (node instanceof StaticText)) 131 break; 132 133 StaticText textNode = (StaticText) node; 134 135 String text = textNode.getText(); 136 137 for (int i = 0; i < text.length(); i++) { 138 if (! XmlChar.isWhitespace(text.charAt(i))) { 139 textNode.setText(text.substring(i)); 140 break prefix_loop; 141 } 142 } 143 144 _children.remove(0); 145 } 146 147 suffix_loop: 148 while (_children.size() > 0) { 149 JspNode node = _children.get(_children.size() - 1); 150 151 if (! (node instanceof StaticText)) 152 break; 153 154 StaticText textNode = (StaticText) node; 155 156 String text = textNode.getText(); 157 158 for (int i = text.length() - 1; i >= 0; i--) { 159 if (! XmlChar.isWhitespace(text.charAt(i))) { 160 textNode.setText(text.substring(0, i + 1)); 161 break suffix_loop; 162 } 163 } 164 165 _children.remove(_children.size() - 1); 166 } 167 } 168 } 169 170 173 public TagInstance getTag() 174 { 175 if (_tag == null) 176 _tag = new TagInstance(_gen.getTagManager()); 177 178 return _tag; 179 } 180 181 184 public boolean isStatic() 185 { 186 return isChildrenStatic(); 187 } 188 189 194 public void printXml(WriteStream os) 195 throws IOException 196 { 197 os.print("<jsp:attribute name=\"" + _name + "\">"); 198 printXmlChildren(os); 199 os.print("</jsp:attribute>"); 200 } 201 202 205 public void generatePrologue(JspJavaWriter out) 206 throws Exception 207 { 208 JspNode parent = getParent(); 209 210 if (! isJspFragment()) { 211 generatePrologueChildren(out); 212 return; 213 } 214 215 TagInstance parentTag = getParent().getTag(); 216 217 if (parentTag == null || parentTag.getId() == null) { 218 } 219 else if (parentTag.isSimpleTag()) 220 getTag().setId(TagInstance.FRAGMENT_WITH_SIMPLE_PARENT); 221 else 222 getTag().setId(TagInstance.FRAGMENT_WITH_TAG_PARENT); 223 224 super.generatePrologue(out); 225 } 226 227 230 String generateValue(Class type) 231 throws Exception 232 { 233 if (isStatic()) { 234 String text = getStaticText(); 235 236 if (_trim) 237 text = text.trim(); 238 239 return stringToValue(type, '"' + escapeJavaString(text) + '"'); 240 } 241 else { 242 return stringToValue(type, generateValue()); 243 } 244 } 245 246 249 protected String generateValue() 250 throws Exception 251 { 252 if (! isStatic()) 253 return super.generateValue(); 254 else if (_trim) 255 return '"' + escapeJavaString(getStaticText().trim()) + '"'; 256 else 257 return '"' + escapeJavaString(getStaticText()) + '"'; 258 } 259 } 260 | Popular Tags |