1 19 20 package org.netbeans.modules.web.debug.util; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.*; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.net.*; 27 import javax.swing.*; 28 import javax.swing.text.*; 29 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 30 31 import org.openide.ErrorManager; 32 import org.openide.nodes.*; 33 import org.openide.filesystems.*; 34 import org.openide.text.*; 35 import org.openide.cookies.*; 36 import org.openide.util.Mutex; 37 import org.openide.windows.TopComponent; 38 39 import org.netbeans.modules.web.api.webmodule.*; 40 import org.netbeans.modules.j2ee.deployment.devmodules.api.JSPServletFinder; 41 42 import org.netbeans.api.project.*; 43 44 45 46 50 public class Utils { 51 52 53 private static ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.web.debug"); 55 public static ErrorManager getEM () { 56 return err; 57 } 58 59 public static FileObject getFileObjectFromUrl(String url) { 60 61 FileObject fo = null; 62 63 try { 64 fo = URLMapper.findFileObject(new URL(url)); 65 } catch (MalformedURLException e) { 66 } 68 69 return fo; 70 } 71 72 public static boolean isJsp(FileObject fo) { 73 return fo != null && "text/x-jsp".equals(fo.getMIMEType()); } 75 76 public static boolean isJsp(String url) { 77 FileObject fo = getFileObjectFromUrl(url); 78 return isJsp(fo); 79 } 80 81 public static boolean isTag(FileObject fo) { 82 return fo != null && "text/x-tag".equals(fo.getMIMEType()); } 84 85 public static boolean isTag(String url) { 86 FileObject fo = getFileObjectFromUrl(url); 87 return isTag(fo); 88 } 89 90 public static String getTargetServerID(FileObject fo) { 91 if (fo != null) { 92 Project p = FileOwnerQuery.getOwner(fo); 93 if (p != null) { 94 J2eeModuleProvider mp = (J2eeModuleProvider)p.getLookup().lookup(J2eeModuleProvider.class); 95 if (mp != null) { 96 String serverID = mp.getServerID(); 97 return serverID; 98 } 99 } 100 } 101 return null; 102 } 103 104 public static String getJspName(String url) { 105 106 FileObject fo = getFileObjectFromUrl(url); 107 if (fo != null) { 108 return fo.getNameExt(); 109 } 110 return (url == null) ? null : url.toString(); 111 } 112 113 public static String getJspPath(String url) { 114 115 FileObject fo = getFileObjectFromUrl(url); 116 String jspRelativePath = url; 117 if (fo != null) { 118 WebModule wm = WebModule.getWebModule (fo); 119 if (wm != null) 120 jspRelativePath = FileUtil.getRelativePath(wm.getDocumentBase(), fo); 121 } 122 123 return jspRelativePath; 124 125 } 126 127 public static String getServletClass(String url) { 128 FileObject fo = getFileObjectFromUrl(url); 129 if (fo == null) { 130 return null; 131 } 132 JSPServletFinder finder = JSPServletFinder.findJSPServletFinder (fo); 133 WebModule wm = WebModule.getWebModule (fo); 134 if (wm == null) 135 return null; 136 137 String jspRelativePath = FileUtil.getRelativePath(wm.getDocumentBase(), fo); 138 String contextPath = wm.getContextPath(); 139 140 String servletPath = finder.getServletResourcePath(jspRelativePath); 141 if (servletPath == null) servletPath = JspNameUtil.getServletResourcePath(contextPath, jspRelativePath); 143 if (servletPath != null) { 144 servletPath = servletPath.substring(0, servletPath.length()-5); servletPath = servletPath.replace('/', '.'); } 147 Utils.getEM().log("servlet class: " + servletPath); 148 return servletPath; 149 } 150 151 public static String getClassFilter(String url) { 152 String filter = getServletClass(url); 153 if (filter != null) { 154 filter = filter.substring(0, filter.lastIndexOf('.')) + ".*"; if (filter.startsWith("org.apache.jsp")) 157 filter = "org.apache.jsp.*"; 158 } 159 return filter; 160 } 161 162 199 public static String getContextPath(String url) { 200 FileObject wmfo = getFileObjectFromUrl(url); 201 if (wmfo == null) { 202 return null; 203 } 204 WebModule wm = WebModule.getWebModule(wmfo); 205 if (wm != null) { 206 return wm.getContextPath(); 207 } 208 return null; 209 } 210 211 216 public static EditorCookie getCurrentEditorCookie () { 217 Node[] nodes = TopComponent.getRegistry ().getCurrentNodes(); 218 if ( (nodes == null) || 219 (nodes.length != 1) ) return null; 220 Node n = nodes [0]; 221 return (EditorCookie) n.getCookie ( 222 EditorCookie.class 223 ); 224 } 225 226 public static JEditorPane getCurrentEditor () { 227 EditorCookie e = getCurrentEditorCookie (); 228 if (e == null) { 229 return null; 230 } 231 return getCurrentEditor(e); 232 } 233 234 239 public static JEditorPane getCurrentEditor (final EditorCookie e) { 240 return Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane>() { 241 public JEditorPane run() { 242 JEditorPane[] op = e.getOpenedPanes(); 243 return (op == null ? null : op[0]); 244 } 245 }); 246 } 247 248 public static String getJavaIdentifier(StyledDocument doc, JEditorPane ep, int offset) { 249 String t = null; 250 if ( (ep.getSelectionStart() <= offset) && (offset <= ep.getSelectionEnd())) { 251 t = ep.getSelectedText(); 252 } 253 if (t != null) { 254 return t; 255 } 256 int line = NbDocument.findLineNumber(doc, offset); 257 int col = NbDocument.findLineColumn(doc, offset); 258 try { 259 javax.swing.text.Element lineElem = 260 org.openide.text.NbDocument.findLineRootElement(doc). 261 getElement(line); 262 if (lineElem == null) { 263 return null; 264 } 265 int lineStartOffset = lineElem.getStartOffset(); 266 int lineLen = lineElem.getEndOffset() - lineStartOffset; 267 t = doc.getText (lineStartOffset, lineLen); 268 int identStart = col; 269 while (identStart > 0 && 270 (Character.isJavaIdentifierPart ( 271 t.charAt (identStart - 1) 272 ) || 273 (t.charAt (identStart - 1) == '.'))) { 274 identStart--; 275 } 276 int identEnd = col; 277 while (identEnd < lineLen && Character.isJavaIdentifierPart(t.charAt(identEnd))) { 278 identEnd++; 279 } 280 if (identStart == identEnd) { 281 return null; 282 } 283 return t.substring (identStart, identEnd); 284 } catch (javax.swing.text.BadLocationException e) { 285 return null; 286 } 287 } 288 289 public static boolean isScriptlet(StyledDocument doc, JEditorPane ep, int offset) { 290 String t; 291 int line = NbDocument.findLineNumber(doc, offset); 292 int col = NbDocument.findLineColumn(doc, offset); 293 try { 294 while (line > 0) { 295 javax.swing.text.Element lineElem = 296 org.openide.text.NbDocument.findLineRootElement(doc).getElement(line); 297 if (lineElem == null) { 298 continue; 299 } 300 int lineStartOffset = lineElem.getStartOffset(); 301 int lineLen = lineElem.getEndOffset() - lineStartOffset; 302 t = doc.getText (lineStartOffset, lineLen); 303 if ((t != null) && (t.length() > 1)) { 304 int identStart; 305 if (line == NbDocument.findLineNumber(doc, offset)) { 306 identStart = col; 307 } else { 308 identStart = lineLen-1; 309 } 310 while (identStart > 0) { 311 if ((t.charAt(identStart) == '%') && (t.charAt(identStart-1) == '<')) { 312 return true; 313 } 314 if ((t.charAt(identStart) == '>') && (t.charAt(identStart-1) == '%')) { 315 return false; 316 } 317 identStart--; 318 } 319 } 320 line--; 321 } 322 } catch (javax.swing.text.BadLocationException e) { 323 } 324 return false; 325 } 326 327 public static String getELIdentifier(StyledDocument doc, JEditorPane ep, int offset) { 328 String t = null; 329 if ( (ep.getSelectionStart () <= offset) && 330 (offset <= ep.getSelectionEnd ()) 331 ) t = ep.getSelectedText (); 332 if (t != null) { 333 if ((t.startsWith("$")) && (t.endsWith("}"))) { 334 return t; 335 } else { 336 return null; 337 } 338 } 339 340 int line = NbDocument.findLineNumber(doc, offset); 341 int col = NbDocument.findLineColumn(doc, offset); 342 try { 343 javax.swing.text.Element lineElem = 344 org.openide.text.NbDocument.findLineRootElement (doc). 345 getElement (line); 346 347 if (lineElem == null) { 348 return null; 349 } 350 int lineStartOffset = lineElem.getStartOffset (); 351 int lineLen = lineElem.getEndOffset() - lineStartOffset; 352 t = doc.getText (lineStartOffset, lineLen); 353 int identStart = col; 354 while (identStart > 0 && (t.charAt(identStart) != '$')) { 355 identStart--; 356 } 357 if ((identStart > 0) && (t.charAt(identStart) == '$') && (t.charAt(identStart-1) == '\\')) { 358 return null; 359 } 360 int identEnd = col; 361 while ((identEnd < lineLen) && identEnd > 0 && identEnd <= t.length() && (t.charAt(identEnd-1) != '}')) { 362 identEnd++; 363 } 364 if (identStart == identEnd) { 365 return null; 366 } 367 String outp = t.substring(identStart, identEnd); 368 if ((outp.startsWith("$")) && (outp.endsWith("}"))) { 369 return outp; 370 } else { 371 return null; 372 } 373 } catch (javax.swing.text.BadLocationException e) { 374 return null; 375 } 376 } 377 378 public static String getJavaIdentifier () { 379 EditorCookie e = getCurrentEditorCookie (); 380 if (e == null) { 381 return null; 382 } 383 JEditorPane ep = getCurrentEditor (e); 384 if (ep == null) { 385 return null; 386 } 387 return getJavaIdentifier ( 388 e.getDocument (), 389 ep, 390 ep.getCaret ().getDot () 391 ); 392 } 393 394 410 public static boolean isScriptlet() { 411 EditorCookie e = getCurrentEditorCookie (); 412 if (e == null) { 413 return false; 414 } 415 JEditorPane ep = getCurrentEditor (e); 416 if (ep == null) { 417 return false; 418 } 419 return isScriptlet( 420 e.getDocument (), 421 ep, 422 ep.getCaret ().getDot () 423 ); 424 } 425 426 427 428 } 429 | Popular Tags |