KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jspparser > JspParserImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.jspparser;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.security.AllPermission JavaDoc;
30 import java.security.CodeSource JavaDoc;
31 import java.security.PermissionCollection JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
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 // PENDING - need to call reinitOptions when something changes (taglib, jar, web.xml)
43
// PENDING - separate to two classes, have a per-application instance of one of them
44

45 /**
46  * @author Petr Jiricka
47  */

48 public class JspParserImpl implements JspParserAPI {
49     
50     private HashMap JavaDoc<JspParserImpl.WAParseSupportKey, WebAppParseProxy> parseSupports;
51     
52     private static Constructor JavaDoc webAppParserImplConstructor;
53     
54     private static final JspParserAPI.JspOpenInfo DEFAULT_OPENINFO = new JspParserAPI.JspOpenInfo(false, "ISO-8859-1");
55     
56     /** Constructs a new Parser API implementation.
57      */

58     public JspParserImpl() {
59         initializeLogger();
60         // PENDING - we are preventing the garbage collection of
61
// Project-s and FileObject-s (wmRoots)
62
parseSupports = new HashMap JavaDoc<JspParserImpl.WAParseSupportKey, WebAppParseProxy>();
63     }
64     
65     private static void initReflection() {
66         if (webAppParserImplConstructor == null) {
67             File JavaDoc files[] = new File JavaDoc[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/glassfish-logging.jar", null, false);
71
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 JavaDoc urls[] = new URL JavaDoc[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 JavaDoc<?> cl = urlCL.loadClass("org.netbeans.modules.web.jspparser_ext.WebAppParseSupport");
81                 
82                 webAppParserImplConstructor = cl.getDeclaredConstructor(new Class JavaDoc[] {WebModule.class});
83             } catch (NoSuchMethodException JavaDoc e) {
84                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
85             } catch (MalformedURLException JavaDoc e) {
86                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
87             } catch (ClassNotFoundException JavaDoc 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 /* if (!loggerInitialized) {
99             Logger l = new DefaultLogger(null);
100             l.setDefaultSink(new NullWriter());
101             l.setName("JASPER_LOG"); // NOI18N
102             //Logger.putLogger();
103             loggerInitialized = true;
104         }*/

105     }
106     
107     public JspParserAPI.JspOpenInfo getJspOpenInfo(FileObject jspFile, WebModule wm, boolean useEditor) {
108         //try to fastly create openinfo
109

110         //detects encoding even if there is not webmodule (null) or deployment descriptor doesn't exist
111
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         //no encoding found in the file or the deployment descriptor contains encoding declarations
118
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     /**
143      * Returns the mapping of the 'global' tag library URI to the location (resource
144      * path) of the TLD associated with that tag library.
145      * @param wmRoot the web module for which to return the map
146      * @return Map which maps global tag library URI to the location
147      * (resource path) of its tld. The location is
148      * returned as a String array:
149      * [0] The location
150      * [1] If the location is a jar file, this is the location of the tld.
151      */

152     public Map JavaDoc getTaglibMap(WebModule wm) throws IOException JavaDoc {
153         FileObject wmRoot = wm.getDocumentBase();
154         if (wmRoot == null) {
155             throw new IOException JavaDoc();
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         // PENDING - do caching for individual JSPs
173
try {
174             initReflection();
175             return (WebAppParseProxy)webAppParserImplConstructor.newInstance(new Object JavaDoc[] {wm});
176         } catch (IllegalAccessException JavaDoc e) {
177             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
178             e.printStackTrace();
179         } catch (InstantiationException JavaDoc e) {
180             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
181             e.printStackTrace();
182         } catch (InvocationTargetException JavaDoc e) {
183             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
184             e.printStackTrace();
185         }
186         return null;
187         //return new WebAppParseSupport(wm);
188
}
189     
190     public URLClassLoader JavaDoc 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()), ""); // NOI18N
198
return new JspParserAPI.ParseResult(new JspParserAPI.ErrorDescriptor[] {error});
199     }
200     
201     
202     /*
203     public TagLibParseSupport.TagLibData createTagLibData(JspInfo.TagLibraryData info, FileSystem fs) {
204         // PENDING - we are not using the cached stuff
205         return new TagLibDataImpl(info);
206     }
207      */

208     
209     
210     /** For use from instancedataobject in filesystem layer
211      */

212     /*public static JspParserImpl getParserImpl(FileObject fo, String str1) {
213         return new JspParserImpl();
214     }*/

215     
216     /** Returns the mapping of 'global' tag library URI to the location
217      * (resource path) of the TLD associated with that tag library.
218      * The location is returned as a String array:
219      * [0] The location
220      * [1] If the location is a jar file, this is the location
221      * of the tld.
222      */

223 /* public Map getTagLibraryMappings(FileObject webModuleRoot) throws IOException {
224         OptionsImpl options = new OptionsImpl(webModuleRoot);
225         TldLocationsCache cache = options.getTldLocationsCache();
226         TreeMap result = new TreeMap();
227         try {
228             Field ff = TldLocationsCache.class.getDeclaredField("mappings"); // NOI18N
229             ff.setAccessible(true);
230             Hashtable mappings = (Hashtable)ff.get(cache);
231             for (Iterator it = mappings.keySet().iterator(); it.hasNext(); ) {
232                 Object elem = it.next();
233                 result.put(elem, mappings.get(elem));
234             }
235         }
236         catch (NoSuchFieldException e) {
237             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
238         }
239         catch (IllegalAccessException e) {
240             ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, e);
241         }
242         return result;
243     }*/

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 JavaDoc o) {
257             
258             if (o instanceof WAParseSupportKey) {
259                 WAParseSupportKey k = (WAParseSupportKey)o;
260                 //Two keys are the same only if the document roots are same.
261
//&& the document roots fileobjects are valid (#67785)
262
return wmRoot.isValid() && k.wmRoot.isValid() && wmRoot.getPath().equals(k.wmRoot.getPath());
263             }
264             return false;
265         }
266         
267         public int hashCode() {
268             //return wm.hashCode() + wmRoot.hashCode();
269
// Two keys are the same only if the document roots are same.
270
return 0;
271         }
272     }
273     
274     private static class ExtClassLoader extends URLClassLoader JavaDoc {
275         
276         private static final AllPermission JavaDoc ALL_PERM = new AllPermission JavaDoc();
277         
278         public ExtClassLoader(URL JavaDoc[] classLoadingURLs, ClassLoader JavaDoc parent) {
279             super(classLoadingURLs, parent);
280         }
281         
282         protected PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource) {
283             PermissionCollection JavaDoc perms = super.getPermissions(codesource);
284             perms.add(ALL_PERM);
285             return perms;
286         }
287     }
288 }
289
Popular Tags