1 29 30 package com.caucho.jsp.java; 31 32 import com.caucho.jsp.JspParseException; 33 import com.caucho.util.L10N; 34 import com.caucho.vfs.WriteStream; 35 import com.caucho.xml.XmlChar; 36 37 import java.io.IOException ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 44 public class JspTop extends JspContainerNode implements JspSegmentNode { 45 private static final L10N L = new L10N(JspTop.class); 46 47 private boolean _hasRoot; 48 private int _maxFragmentIndex = -1; 49 private int _maxStaticFragmentIndex = -1; 50 51 private HashMap <String ,String > _namespaceMap = new HashMap <String ,String >(); 52 53 56 public JspNode addText(String text) 57 throws JspParseException 58 { 59 if (true || ! _hasRoot) { 61 JspNode node = new StaticText(_gen, text, this); 62 63 addChild(node); 64 65 return node; 66 } 67 else { 68 for (int i = 0; i < text.length(); i++) 69 if (! XmlChar.isWhitespace(text.charAt(i))) 70 throw error(L.l("JSP pages with <jsp:root> must not have text outside the <jsp:root>.")); 71 72 return null; 73 } 74 } 75 76 79 public void addChild(JspNode child) 80 throws JspParseException 81 { 82 if (child instanceof JspRoot) { 83 _hasRoot = true; 84 _gen.setOmitXmlDeclaration(true); 85 } 86 87 super.addChild(child); 88 } 89 90 93 public boolean isStatic() 94 { 95 for (int i = 0; i < _children.size(); i++) { 96 JspNode node = _children.get(i); 97 98 if (! node.isStatic()) 99 return false; 100 } 101 102 return true; 103 } 104 105 108 public JspSegmentNode getSegment() 109 { 110 return this; 111 } 112 113 116 public int getMaxFragmentIndex() 117 { 118 return _maxFragmentIndex; 119 } 120 121 124 public void setMaxFragmentIndex(int index) 125 { 126 if (_maxFragmentIndex < index) 127 _maxFragmentIndex = index; 128 } 129 130 133 public int getMaxStaticFragmentIndex() 134 { 135 return _maxStaticFragmentIndex; 136 } 137 138 141 public void setMaxStaticFragmentIndex(int index) 142 { 143 if (_maxStaticFragmentIndex < index) 144 _maxStaticFragmentIndex = index; 145 } 146 147 150 public void addNamespaceRec(String prefix, String value) 151 { 152 _namespaceMap.put(prefix, value); 153 } 154 155 160 public void printXml(WriteStream os) 161 throws IOException 162 { 163 if (_hasRoot) { 164 printXmlChildren(os); 165 return; 166 } 167 168 173 os.print("<jsp:root"); 174 printJspId(os); 175 os.print(" version=\"2.0\""); 176 os.print(" xmlns:jsp=\"http://java.sun.com/JSP/Page\""); 177 178 for (Map.Entry entry : _namespaceMap.entrySet()) { 179 os.print(" xmlns:" + entry.getKey() + "=\"" + entry.getValue() + "\""); 180 } 181 os.print(">"); 182 printXmlChildren(os); 183 os.print("</jsp:root>"); 184 } 185 186 189 public boolean hasNamespace(String prefix, String uri) 190 { 191 if ("".equals(uri) && ("".equals(prefix) || prefix == null)) 192 return true; 193 else 194 return false; 195 } 196 197 202 public void generate(JspJavaWriter out) 203 throws Exception 204 { 205 if (! _hasRoot) { 206 if (! _gen.isOmitXmlDeclaration()) { 207 String encoding = _gen.getCharacterEncoding(); 208 209 if (encoding == null) 210 encoding = "UTF-8"; 211 212 out.addText("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); 213 } 214 215 if (_gen.getDoctypeSystem() != null) { 216 out.addText("<!DOCTYPE "); 217 out.addText(_gen.getDoctypeRootElement()); 218 219 if (_gen.getDoctypePublic() != null) { 220 out.addText(" PUBLIC \""); 221 out.addText(_gen.getDoctypePublic()); 222 out.addText("\" \""); 223 out.addText(_gen.getDoctypeSystem()); 224 out.addText("\""); 225 } 226 else { 227 out.addText(" SYSTEM \""); 228 out.addText(_gen.getDoctypeSystem()); 229 out.addText("\""); 230 } 231 232 out.addText(">\n"); 233 } 234 } 235 236 generateChildren(out); 237 } 238 } 239 | Popular Tags |