KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > debug > util > Utils


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.debug.util;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.*;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
47  *
48  * @author Martin Grebac
49  */

50 public class Utils {
51     
52     /** Logger for web.debug module. */
53     private static ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.web.debug"); // NOI18N
54

55     public static ErrorManager getEM () {
56         return err;
57     }
58     
59     public static FileObject getFileObjectFromUrl(String JavaDoc url) {
60         
61         FileObject fo = null;
62         
63         try {
64             fo = URLMapper.findFileObject(new URL(url));
65         } catch (MalformedURLException e) {
66             //noop
67
}
68
69         return fo;
70     }
71     
72     public static boolean isJsp(FileObject fo) {
73         return fo != null && "text/x-jsp".equals(fo.getMIMEType()); //NOI18N
74
}
75     
76     public static boolean isJsp(String JavaDoc 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()); //NOI18N
83
}
84     
85     public static boolean isTag(String JavaDoc url) {
86         FileObject fo = getFileObjectFromUrl(url);
87         return isTag(fo);
88     }
89
90     public static String JavaDoc 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 JavaDoc serverID = mp.getServerID();
97                     return serverID;
98                 }
99             }
100         }
101         return null;
102     }
103     
104     public static String JavaDoc getJspName(String JavaDoc 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 JavaDoc getJspPath(String JavaDoc url) {
114        
115         FileObject fo = getFileObjectFromUrl(url);
116         String JavaDoc 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 JavaDoc getServletClass(String JavaDoc 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 JavaDoc jspRelativePath = FileUtil.getRelativePath(wm.getDocumentBase(), fo);
138         String JavaDoc contextPath = wm.getContextPath();
139
140         String JavaDoc servletPath = finder.getServletResourcePath(jspRelativePath);
141         if (servletPath == null) // we don't have class name, so assume we are debugging tomcat or appsrv
142
servletPath = JspNameUtil.getServletResourcePath(contextPath, jspRelativePath);
143         if (servletPath != null) {
144             servletPath = servletPath.substring(0, servletPath.length()-5); // length of ".java"
145
servletPath = servletPath.replace('/', '.'); //NOI18N
146
}
147         Utils.getEM().log("servlet class: " + servletPath);
148         return servletPath;
149     }
150
151     public static String JavaDoc getClassFilter(String JavaDoc url) {
152         String JavaDoc filter = getServletClass(url);
153         if (filter != null) {
154             // get package only
155
filter = filter.substring(0, filter.lastIndexOf('.')) + ".*"; //NOI18N
156
if (filter.startsWith("org.apache.jsp"))
157                 filter = "org.apache.jsp.*";
158         }
159         return filter;
160     }
161     
162 // public static String getCompoundClassFilter(String url) {
163
//
164
// FileObject wmfo = getJspFileObjectFromUrl(url);
165
// if (wmfo == null) {
166
// return null;
167
// }
168
// WebModule wm = WebModule.getWebModule(wmfo);
169
// Enumeration files = wm.getDocumentBase().getChildren(true);
170
//
171
// String filter = null; //NOI18N
172
// while (files.hasMoreElements()) {
173
// FileObject fo = (FileObject)files.nextElement();
174
// if (!fo.isFolder() && "text/x-jsp".equals(fo.getMIMEType()) && (fo != null)) {
175
// String jspRelPath = FileUtil.getRelativePath(wm.getDocumentBase(), fo);
176
// JSPServletFinder finder = JSPServletFinder.findJSPServletFinder(fo);
177
// String servletPath = finder.getServletResourcePath(jspRelPath);
178
// if ((servletPath != null) && !servletPath.equals("")) {
179
// servletPath = servletPath.substring(0, servletPath.length()-5); // length of ".java"
180
// servletPath = servletPath.substring(0, servletPath.lastIndexOf('/')); // get package only
181
// if (filter == null) {
182
// filter = servletPath;
183
// } else {
184
// if (!(servletPath.startsWith(filter))) {
185
// while (!servletPath.startsWith(filter)) {
186
// filter = filter.substring(0, filter.lastIndexOf('/'));
187
// }
188
// }
189
// }
190
// }
191
// }
192
// }
193
//
194
// filter = filter.replace('/', '.') + "."; //NOI18N
195
// Utils.getEM().log("compound filter: " + filter);
196
// return filter;
197
// }
198

199     public static String JavaDoc getContextPath(String JavaDoc 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     /**
212      * Returns current editor component instance.
213      *
214      * @return current editor component instance
215      */

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     /**
235      * Returns current editor component instance.
236      *
237      * @return current editor component instance
238      */

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 JavaDoc getJavaIdentifier(StyledDocument doc, JEditorPane ep, int offset) {
249         String JavaDoc 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 JavaDoc 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 JavaDoc e) {
285             return null;
286         }
287     }
288
289     public static boolean isScriptlet(StyledDocument doc, JEditorPane ep, int offset) {
290         String JavaDoc 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 JavaDoc 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 JavaDoc e) {
323         }
324         return false;
325     }
326     
327     public static String JavaDoc getELIdentifier(StyledDocument doc, JEditorPane ep, int offset) {
328         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
374             return null;
375         }
376     }
377     
378     public static String JavaDoc 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 // public static String getELIdentifier () {
395
// EditorCookie e = getCurrentEditorCookie ();
396
// if (e == null) {
397
// return null;
398
// }
399
// JEditorPane ep = getCurrentEditor (e);
400
// if (ep == null) {
401
// return null;
402
// }
403
// return getELIdentifier (
404
// e.getDocument (),
405
// ep,
406
// ep.getCaret ().getDot ()
407
// );
408
// }
409

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