1 19 20 package org.apache.jasper.compiler; 21 22 import java.lang.reflect.Field ; 23 import java.lang.reflect.Method ; 24 import java.lang.reflect.InvocationTargetException ; 25 26 import javax.servlet.jsp.tagext.PageData ; 27 28 import org.openide.ErrorManager; 29 30 import org.apache.jasper.JasperException; 31 32 37 public class NbValidator { 38 39 private static Method validateXmlViewM; 40 private static Field bufF; 41 42 static { 43 initReflection(); 44 } 45 46 private static void initReflection() { 47 try { 48 validateXmlViewM = Validator.class.getDeclaredMethod("validateXmlView", new Class [] {PageData .class, Compiler .class}); 49 validateXmlViewM.setAccessible(true); 50 bufF = PageDataImpl.class.getDeclaredField("buf"); 51 bufF.setAccessible(true); 52 } 53 catch (NoSuchMethodException e) { 54 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 55 } 56 catch (NoSuchFieldException e) { 57 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 58 } 59 } 60 61 63 public static String validate(Compiler compiler, 64 Node.Nodes page) throws JasperException { 65 66 70 page.visit(new Validator.DirectiveVisitor(compiler)); 71 72 PageInfo pageInfo = compiler.getPageInfo(); 74 String contentType = pageInfo.getContentType(); 75 76 if (contentType == null || contentType.indexOf("charset=") < 0) { 77 boolean isXml = page.getRoot().isXmlSyntax(); 78 String defaultType; 79 if (contentType == null) { 80 defaultType = isXml? "text/xml": "text/html"; 81 } else { 82 defaultType = contentType; 83 } 84 85 String charset = null; 86 if (isXml) { 87 charset = "UTF-8"; 88 } else { 89 if (!page.getRoot().isDefaultPageEncoding()) { 90 charset = page.getRoot().getPageEncoding(); 91 } 92 } 93 94 if (charset != null) { 95 pageInfo.setContentType(defaultType + ";charset=" + charset); 96 } else { 97 pageInfo.setContentType(defaultType); 98 } 99 } 100 101 107 page.visit(new Validator.ValidateVisitor(compiler)); 108 109 113 try { 115 PageDataImpl pdi = new PageDataImpl(page, compiler); 116 117 validateXmlViewM.invoke(null, new Object [] {pdi, compiler}); 118 119 123 page.visit(new Validator.TagExtraInfoVisitor(compiler)); 124 125 StringBuffer buf = (StringBuffer )bufF.get(pdi); 126 return buf.toString(); 127 } 128 catch (IllegalAccessException e) { 129 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 130 throw new JasperException(e.getMessage()); 131 } 132 catch (InvocationTargetException e) { 133 Throwable target = e.getTargetException(); 134 if (target instanceof JasperException) { 135 throw (JasperException)target; 136 } 137 else { 138 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 139 throw new JasperException(e.getMessage()); 140 } 141 } 142 143 } 144 145 146 147 } 148 | Popular Tags |