1 19 20 package org.apache.jasper.compiler; 21 22 import java.lang.reflect.Field ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import org.apache.jasper.JasperException; 26 import org.apache.jasper.JspCompilationContext; 27 import org.openide.ErrorManager; 28 29 33 public class CompilerHacks { 34 35 private Compiler comp; 36 protected JspCompilationContext ctxt; 37 38 private static Field pageInfoF; 39 private static Field errDispatcherF; 40 41 static { 42 initMethodsAndFields(); 43 } 44 45 46 public CompilerHacks(JspCompilationContext ctxt) { 47 this.ctxt = ctxt; 48 } 49 50 static void initMethodsAndFields() { 51 try { 52 pageInfoF = Compiler .class.getDeclaredField("pageInfo"); 54 pageInfoF.setAccessible(true); 55 errDispatcherF = Compiler .class.getDeclaredField("errDispatcher"); 57 errDispatcherF.setAccessible(true); 58 } 59 catch (NoSuchFieldException e) { 60 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 61 } 62 } 63 64 private void setupCompiler() throws JasperException { 65 if (comp == null) { 66 comp = ctxt.createCompiler(); 67 setErrDispatcherInCompiler(comp, new ErrorDispatcher(false)); 68 setPageInfoInCompiler(comp, new HackPageInfo(new BeanRepository( 69 ctxt.getClassLoader(), comp.getErrorDispatcher()), "")); 70 } 71 } 72 73 Compiler getCompiler() throws JasperException { 74 setupCompiler(); 75 return comp; 76 } 77 78 private static void setPageInfoInCompiler(Compiler c, PageInfo pageInfo) throws JasperException { 79 try { 80 pageInfoF.set(c, pageInfo); 81 } 82 catch (IllegalAccessException e) { 83 throw new JasperException(e); 84 } 85 } 86 87 private static void setErrDispatcherInCompiler(Compiler c, ErrorDispatcher errDispatcher) throws JasperException { 88 try { 89 errDispatcherF.set(c, errDispatcher); 90 } 91 catch (IllegalAccessException e) { 92 throw new JasperException(e); 93 } 94 } 95 96 98 class HackPageInfo extends PageInfo { 99 100 101 private Map approxXmlPrefixMapper; 102 103 HackPageInfo(BeanRepository beanRepository, String jspFile) { 104 super(beanRepository, jspFile); 105 approxXmlPrefixMapper = new HashMap (); 106 } 107 108 public void pushPrefixMapping(String prefix, String uri) { 109 super.pushPrefixMapping(prefix, uri); 110 if (uri != null) { 111 approxXmlPrefixMapper.put(prefix, uri); 112 } 113 } 114 115 Map getApproxXmlPrefixMapper() { 116 return approxXmlPrefixMapper; 117 } 118 } 119 120 } 121 | Popular Tags |