1 19 20 package org.netbeans.modules.web.jsps.parserapi; 21 22 import javax.servlet.jsp.JspException ; 23 import org.openide.ErrorManager; 24 import org.xml.sax.Attributes ; 25 26 class DumpVisitor extends Node.Visitor { 27 28 private int indent = 0; 29 30 private StringBuffer buf; 31 32 private DumpVisitor() { 33 super(); 34 buf = new StringBuffer (); 35 } 36 37 41 protected void visitCommon(Node n) throws JspException { 42 printString("\nNode [" + n.getStart() + ", " + getDisplayClassName(n.getClass().getName()) + "] "); 43 } 44 45 private String getDisplayClassName(String cn) { 46 int amp = cn.indexOf('$'); 47 return cn.substring(amp + 1); 48 } 49 50 private String getAttributes(Attributes attrs) { 51 if (attrs == null) 52 return ""; 53 54 StringBuffer buf = new StringBuffer (); 55 for (int i=0; i < attrs.getLength(); i++) { 56 buf.append(" " + attrs.getQName(i) + "=\"" 57 + attrs.getValue(i) + "\""); 58 } 59 return buf.toString(); 60 } 61 62 private void printString(String str) { 63 printIndent(); 64 buf.append(str); 65 } 66 67 private void printString(String prefix, char[] chars, String suffix) { 68 String str = null; 69 if (chars != null) { 70 str = new String (chars); 71 } 72 printString(prefix, str, suffix); 73 } 74 75 private void printString(String prefix, String str, String suffix) { 76 printIndent(); 77 if (str != null) { 78 buf.append(prefix); 79 buf.append(str); 80 buf.append(suffix); 81 } else { 82 buf.append(prefix); 83 buf.append(suffix); 84 } 85 } 86 87 private void printAttributes(String prefix, Attributes attrs, 88 String suffix) { 89 printString(prefix, getAttributes(attrs), suffix); 90 } 91 92 private void dumpBody(Node n) throws JspException { 93 Node.Nodes page = n.getBody(); 94 if (page != null) { 95 indent++; 96 page.visit(this); 97 indent--; 98 } 99 } 100 101 public void visit(Node.TagDirective n) throws JspException { 102 visitCommon(n); 103 printAttributes("<%@ tag", n.getAttributes(), "%>"); 104 } 105 106 public void visit(Node.PageDirective n) throws JspException { 107 visitCommon(n); 108 printAttributes("<%@ page", n.getAttributes(), "%>"); 109 } 110 111 public void visit(Node.TaglibDirective n) throws JspException { 112 visitCommon(n); 113 printAttributes("<%@ taglib", n.getAttributes(), "%>"); 114 } 115 116 public void visit(Node.IncludeDirective n) throws JspException { 117 visitCommon(n); 118 printAttributes("<%@ include", n.getAttributes(), "%>"); 119 dumpBody(n); 120 } 121 122 public void visit(Node.Comment n) throws JspException { 123 visitCommon(n); 124 printString("<%--", n.getText(), "--%>"); 125 } 126 127 public void visit(Node.Declaration n) throws JspException { 128 visitCommon(n); 129 printString("<%!", n.getText(), "%>"); 130 } 131 132 public void visit(Node.Expression n) throws JspException { 133 visitCommon(n); 134 printString("<%=", n.getText(), "%>"); 135 } 136 137 public void visit(Node.Scriptlet n) throws JspException { 138 visitCommon(n); 139 printString("<%", n.getText(), "%>"); 140 } 141 142 public void visit(Node.IncludeAction n) throws JspException { 143 visitCommon(n); 144 printAttributes("<jsp:include", n.getAttributes(), ">"); 145 dumpBody(n); 146 printString("</jsp:include>"); 147 } 148 149 public void visit(Node.ForwardAction n) throws JspException { 150 visitCommon(n); 151 printAttributes("<jsp:forward", n.getAttributes(), ">"); 152 dumpBody(n); 153 printString("</jsp:forward>"); 154 } 155 156 public void visit(Node.GetProperty n) throws JspException { 157 visitCommon(n); 158 printAttributes("<jsp:getProperty", n.getAttributes(), "/>"); 159 } 160 161 public void visit(Node.SetProperty n) throws JspException { 162 visitCommon(n); 163 printAttributes("<jsp:setProperty", n.getAttributes(), ">"); 164 dumpBody(n); 165 printString("</jsp:setProperty>"); 166 } 167 168 public void visit(Node.UseBean n) throws JspException { 169 visitCommon(n); 170 printAttributes("<jsp:useBean", n.getAttributes(), ">"); 171 dumpBody(n); 172 printString("</jsp:useBean>"); 173 } 174 175 public void visit(Node.PlugIn n) throws JspException { 176 visitCommon(n); 177 printAttributes("<jsp:plugin", n.getAttributes(), ">"); 178 dumpBody(n); 179 printString("</jsp:plugin>"); 180 } 181 182 public void visit(Node.ParamsAction n) throws JspException { 183 visitCommon(n); 184 printAttributes("<jsp:params", n.getAttributes(), ">"); 185 dumpBody(n); 186 printString("</jsp:params>"); 187 } 188 189 public void visit(Node.ParamAction n) throws JspException { 190 visitCommon(n); 191 printAttributes("<jsp:param", n.getAttributes(), ">"); 192 dumpBody(n); 193 printString("</jsp:param>"); 194 } 195 196 public void visit(Node.NamedAttribute n) throws JspException { 197 visitCommon(n); 198 printAttributes("<jsp:attribute", n.getAttributes(), ">"); 199 dumpBody(n); 200 printString("</jsp:attribute>"); 201 } 202 203 public void visit(Node.JspBody n) throws JspException { 204 visitCommon(n); 205 printAttributes("<jsp:body", n.getAttributes(), ">"); 206 dumpBody(n); 207 printString("</jsp:body>"); 208 } 209 210 public void visit(Node.ELExpression n) throws JspException { 211 visitCommon(n); 212 printString(n.getText()); 213 } 214 215 public void visit(Node.CustomTag n) throws JspException { 216 visitCommon(n); 217 printAttributes("<" + n.getQName(), n.getAttributes(), ">"); 218 dumpBody(n); 219 printString("</" + n.getQName() + ">"); 220 } 221 222 public void visit(Node.UninterpretedTag n) throws JspException { 223 visitCommon(n); 224 String tag = n.getQName(); 225 printAttributes("<"+tag, n.getAttributes(), ">"); 226 dumpBody(n); 227 printString("</" + tag + ">"); 228 } 229 230 public void visit(Node.InvokeAction n) throws JspException { 231 visitCommon(n); 232 printAttributes("<jsp:invoke", n.getAttributes(), ">"); 233 dumpBody(n); 234 printString("</jsp:invoke>"); 235 } 236 237 public void visit(Node.DoBodyAction n) throws JspException { 238 visitCommon(n); 239 printAttributes("<jsp:doBody", n.getAttributes(), ">"); 240 dumpBody(n); 241 printString("</jsp:doBody>"); 242 } 243 244 public void visit(Node.TemplateText n) throws JspException { 245 visitCommon(n); 246 printString(new String (n.getText())); 247 } 248 249 private void printIndent() { 250 for (int i=0; i < indent; i++) { 251 buf.append(" "); 252 } 253 } 254 255 private String getString() { 256 return buf.toString(); 257 } 258 259 public static String dump(Node n) { 260 try { 261 DumpVisitor dv = new DumpVisitor(); 262 n.accept(dv); 263 return dv.getString(); 264 } catch (JspException e) { 265 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 266 return e.getMessage(); 267 } 268 } 269 270 public static String dump(Node.Nodes page) { 271 try { 272 DumpVisitor dv = new DumpVisitor(); 273 page.visit(dv); 274 return dv.getString(); 275 } catch (JspException e) { 276 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 277 return e.getMessage(); 278 } 279 } 280 } 281 282 | Popular Tags |