1 19 20 package org.netbeans.modules.web.jspparser; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.io.PrintWriter ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Enumeration ; 32 import java.util.HashSet ; 33 import java.util.Hashtable ; 34 import java.util.Set ; 35 import java.util.Vector ; 36 import java.net.URL ; 37 import javax.servlet.RequestDispatcher ; 38 import javax.servlet.Servlet ; 39 import javax.servlet.ServletContext ; 40 import javax.servlet.ServletException ; 41 import javax.swing.text.EditorKit ; 42 import javax.swing.text.StyledDocument ; 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 60 61 public class ParserServletContext implements ServletContext { 62 63 64 66 67 70 protected Hashtable <String , Object > myAttributes; 71 72 73 76 protected FileObject wmRoot; 77 78 79 protected JspParserAPI.WebModule myWm; 80 81 84 protected boolean useEditorVersion; 85 86 87 89 90 97 public ParserServletContext(FileObject wmRoot, JspParserAPI.WebModule wm, boolean useEditor) { 98 99 myAttributes = new Hashtable <String , Object >(); 100 this.wmRoot = wmRoot; 101 this.myWm = wm; 102 this.useEditorVersion = useEditor; 103 } 104 105 106 108 109 114 public Object getAttribute(String name) { 115 return myAttributes.get(name); 116 } 117 118 119 122 public Enumeration <String > getAttributeNames() { 123 124 return myAttributes.keys(); 125 126 } 127 128 129 134 public ServletContext getContext(String uripath) { 135 136 return (null); 137 138 } 139 140 141 146 public String getInitParameter(String name) { 147 148 return (null); 149 150 } 151 152 153 157 public Enumeration getInitParameterNames() { 158 159 return (new Vector ().elements()); 160 161 } 162 163 164 167 public int getMajorVersion() { 168 169 return (2); 170 171 } 172 173 174 179 public String getMimeType(String file) { 180 181 return (null); 182 183 } 184 185 186 189 public int getMinorVersion() { 190 191 return (3); 192 193 } 194 195 196 201 public RequestDispatcher getNamedDispatcher(String name) { 202 203 return (null); 204 205 } 206 207 210 protected FileObject getResourceAsObject(String path) { 211 return ContextUtil.findRelativeFileObject(wmRoot, path); 212 } 213 214 215 221 public String getRealPath(String path) { 222 223 if (!path.startsWith("/")) { 224 return (null); 225 } 226 FileObject fo = getResourceAsObject(path); 227 if (fo != null) { 228 File ff = FileUtil.toFile(fo); 229 if (ff != null) { 230 return ff.getAbsolutePath(); 231 } 232 } 233 234 return null; 235 } 236 237 238 243 public RequestDispatcher getRequestDispatcher(String path) { 244 245 return (null); 246 247 } 248 249 250 259 public URL getResource(String path) throws MalformedURLException { 260 261 if (!path.startsWith("/")) 262 throw new MalformedURLException (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 280 public InputStream getResourceAsStream(String path) { 281 282 if (myWm != null) { 284 FileObject fo = getResourceAsObject(path); 285 if ((fo != null) && (useEditorVersion)) { 286 InputStream result = myWm.getEditorInputStream (fo); 288 if (result != null) { 289 return result; 290 } 291 } 292 } 293 294 try { 296 URL url = getResource(path); 297 if (url == null) { 298 return null; 299 } 300 else { 301 return url.openStream(); 302 } 303 } catch (Throwable t) { 304 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, t); 305 return (null); 306 } 307 308 } 309 310 311 317 public Set <String > getResourcePaths(String path) { 318 319 Set <String > thePaths = new HashSet <String >(); 320 if (!path.endsWith("/")) 321 path += "/"; 322 String basePath = getRealPath(path); 323 if (basePath == null) 324 return (thePaths); 325 File theBaseDir = new File (basePath); 326 if (!theBaseDir.exists() || !theBaseDir.isDirectory()) 327 return (thePaths); 328 String theFiles[] = theBaseDir.list(); 329 for (int i = 0; i < theFiles.length; i++) { 330 File testFile = new File (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 344 public String getServerInfo() { 345 346 return ("NB.ParserServletContext/1.0"); 347 348 } 349 350 351 358 public Servlet getServlet(String name) throws ServletException { 359 360 return (null); 361 362 } 363 364 365 368 public String getServletContextName() { 369 370 return (getServerInfo()); 371 372 } 373 374 375 380 public Enumeration getServletNames() { 381 382 return (new Vector ().elements()); 383 384 } 385 386 387 392 public Enumeration getServlets() { 393 394 return (new Vector ().elements()); 395 396 } 397 398 399 404 public void log(String message) { 405 406 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message); 407 408 } 409 410 411 419 public void log(Exception exception, String message) { 420 421 log(message, exception); 422 423 } 424 425 426 432 public void log(String message, Throwable exception) { 433 434 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message); 435 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception); 436 437 } 438 439 440 445 public void removeAttribute(String name) { 446 447 myAttributes.remove(name); 448 449 } 450 451 452 458 public void setAttribute(String name, Object value) { 459 460 myAttributes.put(name, value); 461 462 } 463 464 465 public String getContextPath(){ 466 return ""; 467 } 468 469 } 470 | Popular Tags |