1 19 20 package org.netbeans.modules.web.jspparser; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.security.AllPermission ; 30 import java.security.CodeSource ; 31 import java.security.PermissionCollection ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI; 35 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI.JspOpenInfo; 36 37 import org.openide.ErrorManager; 38 import org.openide.filesystems.FileObject; 39 import org.openide.modules.InstalledFileLocator; 40 import org.openide.util.NbBundle; 41 42 45 48 public class JspParserImpl implements JspParserAPI { 49 50 private HashMap <JspParserImpl.WAParseSupportKey, WebAppParseProxy> parseSupports; 51 52 private static Constructor webAppParserImplConstructor; 53 54 private static final JspParserAPI.JspOpenInfo DEFAULT_OPENINFO = new JspParserAPI.JspOpenInfo(false, "ISO-8859-1"); 55 56 58 public JspParserImpl() { 59 initializeLogger(); 60 parseSupports = new HashMap <JspParserImpl.WAParseSupportKey, WebAppParseProxy>(); 63 } 64 65 private static void initReflection() { 66 if (webAppParserImplConstructor == null) { 67 File files[] = new File [4]; 68 files[0] = InstalledFileLocator.getDefault().locate("ant/lib/ant.jar", null, false); 69 files[1] = InstalledFileLocator.getDefault().locate("modules/ext/glassfish-jspparser.jar", null, false); 70 files[2] = InstalledFileLocator.getDefault().locate("modules/ext/jsp-parser-ext.jar", null, false); 72 files[3] = InstalledFileLocator.getDefault().locate("modules/ext/servlet2.5-jsp2.1-api.jar", null, false); 73 74 try { 75 URL urls[] = new URL [files.length]; 76 for (int i = 0; i < files.length; i++) { 77 urls[i] = files[i].toURI().toURL(); 78 } 79 ExtClassLoader urlCL = new ExtClassLoader(urls, JspParserImpl.class.getClassLoader()); 80 Class <?> cl = urlCL.loadClass("org.netbeans.modules.web.jspparser_ext.WebAppParseSupport"); 81 82 webAppParserImplConstructor = cl.getDeclaredConstructor(new Class [] {WebModule.class}); 83 } catch (NoSuchMethodException e) { 84 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 85 } catch (MalformedURLException e) { 86 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 87 } catch (ClassNotFoundException e) { 88 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 89 } 90 } 91 } 92 93 94 95 private static boolean loggerInitialized = false; 96 97 private static synchronized void initializeLogger() { 98 105 } 106 107 public JspParserAPI.JspOpenInfo getJspOpenInfo(FileObject jspFile, WebModule wm, boolean useEditor) { 108 110 FastOpenInfoParser fastOIP = FastOpenInfoParser.get(wm); 112 if(fastOIP != null) { 113 JspParserAPI.JspOpenInfo jspOI = fastOIP.getJspOpenInfo(jspFile, useEditor); 114 if(jspOI != null) return jspOI; 115 } 116 117 if (wm != null) { 119 FileObject wmRoot = wm.getDocumentBase(); 120 if (wmRoot != null) { 121 WebAppParseProxy pp = getParseProxy(wm); 122 if (pp != null) return pp.getJspOpenInfo(jspFile, useEditor); 123 } 124 } 125 126 return DEFAULT_OPENINFO; 127 } 128 129 public JspParserAPI.ParseResult analyzePage(FileObject jspFile, WebModule wm, int errorReportingMode) { 130 if (wm ==null) 131 return getNoWebModuleResult(jspFile, null); 132 FileObject wmRoot = wm.getDocumentBase(); 133 if (wmRoot == null) { 134 return getNoWebModuleResult(jspFile, wm); 135 } 136 WebAppParseProxy pp = getParseProxy(wm); 137 if (pp == null) 138 return getNoWebModuleResult(jspFile, wm); 139 return pp.analyzePage(jspFile, errorReportingMode); 140 } 141 142 152 public Map getTaglibMap(WebModule wm) throws IOException { 153 FileObject wmRoot = wm.getDocumentBase(); 154 if (wmRoot == null) { 155 throw new IOException (); 156 } 157 WebAppParseProxy pp = getParseProxy(wm); 158 return pp.getTaglibMap(true); 159 } 160 161 private synchronized WebAppParseProxy getParseProxy(WebModule wm) { 162 JspParserImpl.WAParseSupportKey key = new JspParserImpl.WAParseSupportKey(wm); 163 WebAppParseProxy pp = parseSupports.get(key); 164 if (pp == null) { 165 pp = createParseProxy(wm); 166 parseSupports.put(key, pp); 167 } 168 return pp; 169 } 170 171 private WebAppParseProxy createParseProxy(WebModule wm) { 172 try { 174 initReflection(); 175 return (WebAppParseProxy)webAppParserImplConstructor.newInstance(new Object [] {wm}); 176 } catch (IllegalAccessException e) { 177 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 178 e.printStackTrace(); 179 } catch (InstantiationException e) { 180 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 181 e.printStackTrace(); 182 } catch (InvocationTargetException e) { 183 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 184 e.printStackTrace(); 185 } 186 return null; 187 } 189 190 public URLClassLoader getModuleClassLoader(WebModule wm) { 191 WebAppParseProxy pp = getParseProxy(wm); 192 return pp.getWAClassLoader(); 193 } 194 195 private JspParserAPI.ParseResult getNoWebModuleResult(FileObject jspFile, WebModule wm) { 196 JspParserAPI.ErrorDescriptor error = new JspParserAPI.ErrorDescriptor(null, jspFile, -1, -1, 197 NbBundle.getMessage(JspParserImpl.class, "MSG_webModuleNotFound", jspFile.getNameExt()), ""); return new JspParserAPI.ParseResult(new JspParserAPI.ErrorDescriptor[] {error}); 199 } 200 201 202 208 209 210 212 215 216 223 244 245 246 private static class WAParseSupportKey { 247 248 WebModule wm; 249 FileObject wmRoot; 250 251 WAParseSupportKey(WebModule wm) { 252 this.wm = wm; 253 this.wmRoot = wm.getDocumentBase(); 254 } 255 256 public boolean equals(Object o) { 257 258 if (o instanceof WAParseSupportKey) { 259 WAParseSupportKey k = (WAParseSupportKey)o; 260 return wmRoot.isValid() && k.wmRoot.isValid() && wmRoot.getPath().equals(k.wmRoot.getPath()); 263 } 264 return false; 265 } 266 267 public int hashCode() { 268 return 0; 271 } 272 } 273 274 private static class ExtClassLoader extends URLClassLoader { 275 276 private static final AllPermission ALL_PERM = new AllPermission (); 277 278 public ExtClassLoader(URL [] classLoadingURLs, ClassLoader parent) { 279 super(classLoadingURLs, parent); 280 } 281 282 protected PermissionCollection getPermissions(CodeSource codesource) { 283 PermissionCollection perms = super.getPermissions(codesource); 284 perms.add(ALL_PERM); 285 return perms; 286 } 287 } 288 } 289 | Popular Tags |