1 28 29 package com.caucho.jsp.java; 30 31 import com.caucho.jsp.JspParseException; 32 import com.caucho.util.L10N; 33 import com.caucho.vfs.WriteStream; 34 import com.caucho.xml.QName; 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 JspRoot extends JspContainerNode { 45 static final L10N L = new L10N(JspRoot.class); 46 47 static final private QName VERSION = new QName("version"); 48 49 private String _version; 50 51 private HashMap <String ,String > _namespaceMap = new HashMap <String ,String >(); 52 53 56 public void setVersion(String version) 57 { 58 _version = version; 59 } 60 61 67 public void addAttribute(QName name, String value) 68 throws JspParseException 69 { 70 if (VERSION.equals(name)) { 71 if (! value.equals("2.1") 72 && ! value.equals("2.0") 73 && ! value.equals("1.2")) 74 throw error(L.l("'{0}' is an unsupported jsp:root version.", 75 value)); 76 77 _version = value; 78 } 79 else { 80 throw error(L.l("'{0}' is an unknown jsp:root attribute. 'version' is the only allowed JSP root value.", 81 name.getName())); 82 } 83 } 84 85 88 public JspNode addText(String text) 89 throws JspParseException 90 { 91 for (int i = 0; i < text.length(); i++) { 92 if (! XmlChar.isWhitespace(text.charAt(i))) { 93 JspNode node = new StaticText(_gen, text, this); 94 95 addChild(node); 96 97 return node; 98 } 99 } 100 101 return null; 102 } 103 104 107 public void endAttributes() 108 throws JspParseException 109 { 110 _gen.setOmitXmlDeclaration(true); 111 112 if (getParent() != null && ! (getParent() instanceof JspTop)) 113 throw error(L.l("jsp:root must be the root JSP element.")); 114 115 if (_version == null) 116 throw error(L.l("'version' is a required attribute of jsp:root")); 117 } 118 119 122 public void addChild(JspNode node) 123 throws JspParseException 124 { 125 super.addChild(node); 126 } 127 128 131 public void addNamespaceRec(String prefix, String value) 132 { 133 _namespaceMap.put(prefix, value); 134 } 135 136 139 public boolean isStatic() 140 { 141 for (int i = 0; i < _children.size(); i++) { 142 JspNode node = _children.get(i); 143 144 if (! node.isStatic()) 145 return false; 146 } 147 148 return true; 149 } 150 151 156 public void printXml(WriteStream os) 157 throws IOException 158 { 159 os.print("<jsp:root xmlns:jsp=\"http://java.sun.com/JSP/Page\""); 160 161 for (Map.Entry entry : _namespaceMap.entrySet()) { 162 os.print(" xmlns:" + entry.getKey() + "=\"" + entry.getValue() + "\""); 163 } 164 165 printJspId(os); 166 os.print(" version=\"2.0\""); 167 168 os.print(">"); 169 printXmlChildren(os); 170 os.print("</jsp:root>"); 171 } 172 173 178 public void generate(JspJavaWriter out) 179 throws Exception 180 { 181 if (! _gen.isOmitXmlDeclaration()) { 182 String encoding = _gen.getCharacterEncoding(); 183 184 if (encoding == null) 185 encoding = "UTF-8"; 186 187 out.addText("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); 188 } 189 190 if (_gen.getDoctypeSystem() != null) { 191 out.addText("<!DOCTYPE "); 192 out.addText(_gen.getDoctypeRootElement()); 193 194 if (_gen.getDoctypePublic() != null) { 195 out.addText(" PUBLIC \""); 196 out.addText(_gen.getDoctypePublic()); 197 out.addText("\" \""); 198 out.addText(_gen.getDoctypeSystem()); 199 out.addText("\""); 200 } 201 else { 202 out.addText(" SYSTEM \""); 203 out.addText(_gen.getDoctypeSystem()); 204 out.addText("\""); 205 } 206 207 out.addText(">\n"); 208 } 209 210 generateChildren(out); 211 } 212 } 213 | Popular Tags |