KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.net.URL JavaDoc;
37 import javax.servlet.RequestDispatcher JavaDoc;
38 import javax.servlet.Servlet JavaDoc;
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.swing.text.EditorKit JavaDoc;
42 import javax.swing.text.StyledDocument JavaDoc;
43
44 import org.openide.filesystems.FileObject;
45
46 import org.openide.ErrorManager;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.filesystems.URLMapper;
49 import org.openide.util.NbBundle;
50
51 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
52 import org.openide.text.CloneableEditorSupport;
53
54 /**
55  * Simple <code>ServletContext</code> implementation without
56  * HTTP-specific methods.
57  *
58  * @author Peter Rossbach (pr@webapp.de)
59  */

60
61 public class ParserServletContext implements ServletContext JavaDoc {
62
63
64     // ----------------------------------------------------- Instance Variables
65

66
67     /**
68      * Servlet context attributes.
69      */

70     protected Hashtable JavaDoc<String JavaDoc, Object JavaDoc> myAttributes;
71
72
73     /**
74      * The base FileObject (document root) for this context.
75      */

76     protected FileObject wmRoot;
77     
78     
79     protected JspParserAPI.WebModule myWm;
80     
81     /** If true, takes the data from the editor; otherwise
82      * from the disk.
83      */

84     protected boolean useEditorVersion;
85     
86     
87     // ----------------------------------------------------------- Constructors
88

89
90     /**
91      * Create a new instance of this ServletContext implementation.
92      *
93      * @param wmRoot Resource base FileObject
94      * @param wm JspParserAPI.WebModule in which we are parsing the file - this is used to
95      * find the editor for objects which are open in the editor
96      */

97     public ParserServletContext(FileObject wmRoot, JspParserAPI.WebModule wm, boolean useEditor) {
98
99         myAttributes = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
100         this.wmRoot = wmRoot;
101         this.myWm = wm;
102         this.useEditorVersion = useEditor;
103     }
104
105
106     // --------------------------------------------------------- Public Methods
107

108
109     /**
110      * Return the specified context attribute, if any.
111      *
112      * @param name Name of the requested attribute
113      */

114     public Object JavaDoc getAttribute(String JavaDoc name) {
115         return myAttributes.get(name);
116     }
117
118
119     /**
120      * Return an enumeration of context attribute names.
121      */

122     public Enumeration JavaDoc<String JavaDoc> getAttributeNames() {
123
124         return myAttributes.keys();
125
126     }
127
128
129     /**
130      * Return the servlet context for the specified path.
131      *
132      * @param uripath Server-relative path starting with '/'
133      */

134     public ServletContext JavaDoc getContext(String JavaDoc uripath) {
135
136         return (null);
137
138     }
139
140
141     /**
142      * Return the specified context initialization parameter.
143      *
144      * @param name Name of the requested parameter
145      */

146     public String JavaDoc getInitParameter(String JavaDoc name) {
147
148         return (null);
149
150     }
151
152
153     /**
154      * Return an enumeration of the names of context initialization
155      * parameters.
156      */

157     public Enumeration JavaDoc getInitParameterNames() {
158
159         return (new Vector JavaDoc().elements());
160
161     }
162
163
164     /**
165      * Return the Servlet API major version number.
166      */

167     public int getMajorVersion() {
168
169         return (2);
170
171     }
172
173
174     /**
175      * Return the MIME type for the specified filename.
176      *
177      * @param file Filename whose MIME type is requested
178      */

179     public String JavaDoc getMimeType(String JavaDoc file) {
180
181         return (null);
182
183     }
184
185
186     /**
187      * Return the Servlet API minor version number.
188      */

189     public int getMinorVersion() {
190
191         return (3);
192
193     }
194
195
196     /**
197      * Return a request dispatcher for the specified servlet name.
198      *
199      * @param name Name of the requested servlet
200      */

201     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc name) {
202
203         return (null);
204
205     }
206
207     /** Returns a FileObject representation of the specified context-relative
208      * virtual path.
209      */

210     protected FileObject getResourceAsObject(String JavaDoc path) {
211         return ContextUtil.findRelativeFileObject(wmRoot, path);
212     }
213     
214
215     /**
216      * Return the real path for the specified context-relative
217      * virtual path.
218      *
219      * @param path The context-relative virtual path to resolve
220      */

221     public String JavaDoc getRealPath(String JavaDoc path) {
222         
223         if (!path.startsWith("/")) {
224             return (null);
225         }
226         FileObject fo = getResourceAsObject(path);
227         if (fo != null) {
228             File JavaDoc ff = FileUtil.toFile(fo);
229             if (ff != null) {
230                 return ff.getAbsolutePath();
231             }
232         }
233         
234         return null;
235     }
236             
237             
238     /**
239      * Return a request dispatcher for the specified context-relative path.
240      *
241      * @param path Context-relative path for which to acquire a dispatcher
242      */

243     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
244
245         return (null);
246
247     }
248
249
250     /**
251      * Return a URL object of a resource that is mapped to the
252      * specified context-relative path.
253      *
254      * @param path Context-relative path of the desired resource
255      *
256      * @exception MalformedURLException if the resource path is
257      * not properly formed
258      */

259     public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc {
260
261         if (!path.startsWith("/"))
262             throw new MalformedURLException JavaDoc(NbBundle.getMessage(ParserServletContext.class,
263                 "EXC_PathMustStartWithSlash", path));
264         
265         FileObject fo = getResourceAsObject(path);
266         if (fo == null) {
267             return null;
268         }
269         return URLMapper.findURL(fo, URLMapper.EXTERNAL);
270
271     }
272
273
274     /**
275      * Return an InputStream allowing access to the resource at the
276      * specified context-relative path.
277      *
278      * @param path Context-relative path of the desired resource
279      */

280     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
281
282         // first try from the opened editor - if fails read from file
283
if (myWm != null) {
284             FileObject fo = getResourceAsObject(path);
285             if ((fo != null) && (useEditorVersion)) {
286                 // reading from the editor
287
InputStream JavaDoc result = myWm.getEditorInputStream (fo);
288                 if (result != null) {
289                     return result;
290                 }
291             }
292         }
293         
294         // read from the file by default
295
try {
296             URL JavaDoc url = getResource(path);
297             if (url == null) {
298                 return null;
299             }
300             else {
301                 return url.openStream();
302             }
303         } catch (Throwable JavaDoc t) {
304             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, t);
305             return (null);
306         }
307
308     }
309     
310
311     /**
312      * Return the set of resource paths for the "directory" at the
313      * specified context path.
314      *
315      * @param path Context-relative base path
316      */

317     public Set JavaDoc<String JavaDoc> getResourcePaths(String JavaDoc path) {
318
319         Set JavaDoc<String JavaDoc> thePaths = new HashSet JavaDoc<String JavaDoc>();
320         if (!path.endsWith("/"))
321             path += "/";
322         String JavaDoc basePath = getRealPath(path);
323         if (basePath == null)
324             return (thePaths);
325         File JavaDoc theBaseDir = new File JavaDoc(basePath);
326         if (!theBaseDir.exists() || !theBaseDir.isDirectory())
327             return (thePaths);
328         String JavaDoc theFiles[] = theBaseDir.list();
329         for (int i = 0; i < theFiles.length; i++) {
330             File JavaDoc testFile = new File JavaDoc(basePath + File.separator + theFiles[i]);
331             if (testFile.isFile())
332                 thePaths.add(path + theFiles[i]);
333             else if (testFile.isDirectory())
334                 thePaths.add(path + theFiles[i] + "/");
335         }
336         return thePaths;
337
338     }
339
340
341     /**
342      * Return descriptive information about this server.
343      */

344     public String JavaDoc getServerInfo() {
345
346         return ("NB.ParserServletContext/1.0");
347
348     }
349
350
351     /**
352      * Return a null reference for the specified servlet name.
353      *
354      * @param name Name of the requested servlet
355      *
356      * @deprecated This method has been deprecated with no replacement
357      */

358     public Servlet JavaDoc getServlet(String JavaDoc name) throws ServletException JavaDoc {
359
360         return (null);
361
362     }
363
364
365     /**
366      * Return the name of this servlet context.
367      */

368     public String JavaDoc getServletContextName() {
369
370         return (getServerInfo());
371
372     }
373
374
375     /**
376      * Return an empty enumeration of servlet names.
377      *
378      * @deprecated This method has been deprecated with no replacement
379      */

380     public Enumeration JavaDoc getServletNames() {
381
382         return (new Vector JavaDoc().elements());
383
384     }
385
386
387     /**
388      * Return an empty enumeration of servlets.
389      *
390      * @deprecated This method has been deprecated with no replacement
391      */

392     public Enumeration JavaDoc getServlets() {
393
394         return (new Vector JavaDoc().elements());
395
396     }
397
398
399     /**
400      * Log the specified message.
401      *
402      * @param message The message to be logged
403      */

404     public void log(String JavaDoc message) {
405
406         ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message);
407
408     }
409
410
411     /**
412      * Log the specified message and exception.
413      *
414      * @param exception The exception to be logged
415      * @param message The message to be logged
416      *
417      * @deprecated Use log(String,Throwable) instead
418      */

419     public void log(Exception JavaDoc exception, String JavaDoc message) {
420
421         log(message, exception);
422
423     }
424
425
426     /**
427      * Log the specified message and exception.
428      *
429      * @param message The message to be logged
430      * @param exception The exception to be logged
431      */

432     public void log(String JavaDoc message, Throwable JavaDoc exception) {
433
434         ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message);
435         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
436         
437     }
438
439
440     /**
441      * Remove the specified context attribute.
442      *
443      * @param name Name of the attribute to remove
444      */

445     public void removeAttribute(String JavaDoc name) {
446
447         myAttributes.remove(name);
448
449     }
450
451
452     /**
453      * Set or replace the specified context attribute.
454      *
455      * @param name Name of the context attribute to set
456      * @param value Corresponding attribute value
457      */

458     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
459
460         myAttributes.put(name, value);
461
462     }
463
464
465     public String JavaDoc getContextPath(){
466         return "";
467     }
468
469 }
470
Popular Tags