001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.debug.util;
043:
044: import java.io.*;
045: import java.net.*;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.*;
049: import javax.swing.text.*;
050: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
051:
052: import org.openide.nodes.*;
053: import org.openide.filesystems.*;
054: import org.openide.text.*;
055: import org.openide.cookies.*;
056: import org.openide.util.Mutex;
057: import org.openide.windows.TopComponent;
058:
059: import org.netbeans.modules.web.api.webmodule.*;
060: import org.netbeans.modules.j2ee.deployment.devmodules.api.JSPServletFinder;
061:
062: import org.netbeans.api.project.*;
063:
064: /**
065: *
066: * @author Martin Grebac
067: */
068: public class Utils {
069:
070: /** Logger for web.debug module. */
071: private static Logger err = Logger
072: .getLogger("org.netbeans.modules.web.debug"); // NOI18N
073: private static final boolean LOG = err.isLoggable(Level.FINE);
074:
075: public static Logger getEM() {
076: return err;
077: }
078:
079: public static void log(String msg) {
080: if (LOG) {
081: err.log(Level.FINE, msg);
082: }
083: }
084:
085: public static FileObject getFileObjectFromUrl(String url) {
086:
087: FileObject fo = null;
088:
089: try {
090: fo = URLMapper.findFileObject(new URL(url));
091: } catch (MalformedURLException e) {
092: //noop
093: }
094:
095: return fo;
096: }
097:
098: public static boolean isJsp(FileObject fo) {
099: return fo != null && "text/x-jsp".equals(fo.getMIMEType()); //NOI18N
100: }
101:
102: public static boolean isJsp(String url) {
103: FileObject fo = getFileObjectFromUrl(url);
104: return isJsp(fo);
105: }
106:
107: public static boolean isTag(FileObject fo) {
108: return fo != null && "text/x-tag".equals(fo.getMIMEType()); //NOI18N
109: }
110:
111: public static boolean isTag(String url) {
112: FileObject fo = getFileObjectFromUrl(url);
113: return isTag(fo);
114: }
115:
116: public static String getTargetServerID(FileObject fo) {
117: if (fo != null) {
118: Project p = FileOwnerQuery.getOwner(fo);
119: if (p != null) {
120: J2eeModuleProvider mp = (J2eeModuleProvider) p
121: .getLookup().lookup(J2eeModuleProvider.class);
122: if (mp != null) {
123: String serverID = mp.getServerID();
124: return serverID;
125: }
126: }
127: }
128: return null;
129: }
130:
131: public static String getJspName(String url) {
132:
133: FileObject fo = getFileObjectFromUrl(url);
134: if (fo != null) {
135: return fo.getNameExt();
136: }
137: return (url == null) ? null : url.toString();
138: }
139:
140: public static String getJspPath(String url) {
141:
142: FileObject fo = getFileObjectFromUrl(url);
143: String jspRelativePath = url;
144: if (fo != null) {
145: WebModule wm = WebModule.getWebModule(fo);
146: if (wm != null)
147: jspRelativePath = FileUtil.getRelativePath(wm
148: .getDocumentBase(), fo);
149: }
150:
151: return jspRelativePath;
152:
153: }
154:
155: public static String getServletClass(String url) {
156: FileObject fo = getFileObjectFromUrl(url);
157: if (fo == null) {
158: return null;
159: }
160: JSPServletFinder finder = JSPServletFinder
161: .findJSPServletFinder(fo);
162: WebModule wm = WebModule.getWebModule(fo);
163: if (wm == null)
164: return null;
165:
166: String jspRelativePath = FileUtil.getRelativePath(wm
167: .getDocumentBase(), fo);
168: String contextPath = wm.getContextPath();
169:
170: String servletPath = finder
171: .getServletResourcePath(jspRelativePath);
172: if (servletPath == null) // we don't have class name, so assume we are debugging tomcat or appsrv
173: servletPath = JspNameUtil.getServletResourcePath(
174: contextPath, jspRelativePath);
175: if (servletPath != null) {
176: servletPath = servletPath.substring(0,
177: servletPath.length() - 5); // length of ".java"
178: servletPath = servletPath.replace('/', '.'); //NOI18N
179: }
180: Utils.log("servlet class: " + servletPath);
181: return servletPath;
182: }
183:
184: public static String getClassFilter(String url) {
185: String filter = getServletClass(url);
186: if (filter != null) {
187: // get package only
188: filter = filter.substring(0, filter.lastIndexOf('.'))
189: + ".*"; //NOI18N
190: if (filter.startsWith("org.apache.jsp"))
191: filter = "org.apache.jsp.*";
192: }
193: return filter;
194: }
195:
196: // public static String getCompoundClassFilter(String url) {
197: //
198: // FileObject wmfo = getJspFileObjectFromUrl(url);
199: // if (wmfo == null) {
200: // return null;
201: // }
202: // WebModule wm = WebModule.getWebModule(wmfo);
203: // Enumeration files = wm.getDocumentBase().getChildren(true);
204: //
205: // String filter = null; //NOI18N
206: // while (files.hasMoreElements()) {
207: // FileObject fo = (FileObject)files.nextElement();
208: // if (!fo.isFolder() && "text/x-jsp".equals(fo.getMIMEType()) && (fo != null)) {
209: // String jspRelPath = FileUtil.getRelativePath(wm.getDocumentBase(), fo);
210: // JSPServletFinder finder = JSPServletFinder.findJSPServletFinder(fo);
211: // String servletPath = finder.getServletResourcePath(jspRelPath);
212: // if ((servletPath != null) && !servletPath.equals("")) {
213: // servletPath = servletPath.substring(0, servletPath.length()-5); // length of ".java"
214: // servletPath = servletPath.substring(0, servletPath.lastIndexOf('/')); // get package only
215: // if (filter == null) {
216: // filter = servletPath;
217: // } else {
218: // if (!(servletPath.startsWith(filter))) {
219: // while (!servletPath.startsWith(filter)) {
220: // filter = filter.substring(0, filter.lastIndexOf('/'));
221: // }
222: // }
223: // }
224: // }
225: // }
226: // }
227: //
228: // filter = filter.replace('/', '.') + "."; //NOI18N
229: // Utils.getEM().log("compound filter: " + filter);
230: // return filter;
231: // }
232:
233: public static String getContextPath(String url) {
234: FileObject wmfo = getFileObjectFromUrl(url);
235: if (wmfo == null) {
236: return null;
237: }
238: WebModule wm = WebModule.getWebModule(wmfo);
239: if (wm != null) {
240: return wm.getContextPath();
241: }
242: return null;
243: }
244:
245: /**
246: * Returns current editor component instance.
247: *
248: * @return current editor component instance
249: */
250: public static EditorCookie getCurrentEditorCookie() {
251: Node[] nodes = TopComponent.getRegistry().getCurrentNodes();
252: if ((nodes == null) || (nodes.length != 1))
253: return null;
254: Node n = nodes[0];
255: return (EditorCookie) n.getCookie(EditorCookie.class);
256: }
257:
258: public static JEditorPane getCurrentEditor() {
259: EditorCookie e = getCurrentEditorCookie();
260: if (e == null) {
261: return null;
262: }
263: return getCurrentEditor(e);
264: }
265:
266: /**
267: * Returns current editor component instance.
268: *
269: * @return current editor component instance
270: */
271: public static JEditorPane getCurrentEditor(final EditorCookie e) {
272: return Mutex.EVENT.readAccess(new Mutex.Action<JEditorPane>() {
273: public JEditorPane run() {
274: JEditorPane[] op = e.getOpenedPanes();
275: return (op == null ? null : op[0]);
276: }
277: });
278: }
279:
280: public static String getJavaIdentifier(StyledDocument doc,
281: JEditorPane ep, int offset) {
282: String t = null;
283: if ((ep.getSelectionStart() <= offset)
284: && (offset <= ep.getSelectionEnd())) {
285: t = ep.getSelectedText();
286: }
287: if (t != null) {
288: return t;
289: }
290: int line = NbDocument.findLineNumber(doc, offset);
291: int col = NbDocument.findLineColumn(doc, offset);
292: try {
293: javax.swing.text.Element lineElem = org.openide.text.NbDocument
294: .findLineRootElement(doc).getElement(line);
295: if (lineElem == null) {
296: return null;
297: }
298: int lineStartOffset = lineElem.getStartOffset();
299: int lineLen = lineElem.getEndOffset() - lineStartOffset;
300: t = doc.getText(lineStartOffset, lineLen);
301: int identStart = col;
302: while (identStart > 0
303: && (Character.isJavaIdentifierPart(t
304: .charAt(identStart - 1)) || (t
305: .charAt(identStart - 1) == '.'))) {
306: identStart--;
307: }
308: int identEnd = col;
309: while (identEnd < lineLen
310: && Character.isJavaIdentifierPart(t
311: .charAt(identEnd))) {
312: identEnd++;
313: }
314: if (identStart == identEnd) {
315: return null;
316: }
317: return t.substring(identStart, identEnd);
318: } catch (javax.swing.text.BadLocationException e) {
319: return null;
320: }
321: }
322:
323: public static boolean isScriptlet(StyledDocument doc,
324: JEditorPane ep, int offset) {
325: String t;
326: int line = NbDocument.findLineNumber(doc, offset);
327: int col = NbDocument.findLineColumn(doc, offset);
328: try {
329: while (line > 0) {
330: javax.swing.text.Element lineElem = org.openide.text.NbDocument
331: .findLineRootElement(doc).getElement(line);
332: if (lineElem == null) {
333: continue;
334: }
335: int lineStartOffset = lineElem.getStartOffset();
336: int lineLen = lineElem.getEndOffset() - lineStartOffset;
337: t = doc.getText(lineStartOffset, lineLen);
338: if ((t != null) && (t.length() > 1)) {
339: int identStart;
340: if (line == NbDocument.findLineNumber(doc, offset)) {
341: identStart = col;
342: } else {
343: identStart = lineLen - 1;
344: }
345: while (identStart > 0) {
346: if ((t.charAt(identStart) == '%')
347: && (t.charAt(identStart - 1) == '<')) {
348: return true;
349: }
350: if ((t.charAt(identStart) == '>')
351: && (t.charAt(identStart - 1) == '%')) {
352: return false;
353: }
354: identStart--;
355: }
356: }
357: line--;
358: }
359: } catch (javax.swing.text.BadLocationException e) {
360: }
361: return false;
362: }
363:
364: public static String getELIdentifier(StyledDocument doc,
365: JEditorPane ep, int offset) {
366: String t = null;
367: if ((ep.getSelectionStart() <= offset)
368: && (offset <= ep.getSelectionEnd()))
369: t = ep.getSelectedText();
370: if (t != null) {
371: if ((t.startsWith("$")) && (t.endsWith("}"))) {
372: return t;
373: } else {
374: return null;
375: }
376: }
377:
378: int line = NbDocument.findLineNumber(doc, offset);
379: int col = NbDocument.findLineColumn(doc, offset);
380: try {
381: javax.swing.text.Element lineElem = org.openide.text.NbDocument
382: .findLineRootElement(doc).getElement(line);
383:
384: if (lineElem == null) {
385: return null;
386: }
387: int lineStartOffset = lineElem.getStartOffset();
388: int lineLen = lineElem.getEndOffset() - lineStartOffset;
389: t = doc.getText(lineStartOffset, lineLen);
390: int identStart = col;
391: while (identStart > 0 && (t.charAt(identStart) != '$')) {
392: identStart--;
393: }
394: if ((identStart > 0) && (t.charAt(identStart) == '$')
395: && (t.charAt(identStart - 1) == '\\')) {
396: return null;
397: }
398: int identEnd = col;
399: while ((identEnd < lineLen) && identEnd > 0
400: && identEnd <= t.length()
401: && (t.charAt(identEnd - 1) != '}')) {
402: identEnd++;
403: }
404: if (identStart == identEnd) {
405: return null;
406: }
407: String outp = t.substring(identStart, identEnd);
408: if ((outp.startsWith("$")) && (outp.endsWith("}"))) {
409: return outp;
410: } else {
411: return null;
412: }
413: } catch (javax.swing.text.BadLocationException e) {
414: return null;
415: }
416: }
417:
418: public static String getJavaIdentifier() {
419: EditorCookie e = getCurrentEditorCookie();
420: if (e == null) {
421: return null;
422: }
423: JEditorPane ep = getCurrentEditor(e);
424: if (ep == null) {
425: return null;
426: }
427: return getJavaIdentifier(e.getDocument(), ep, ep.getCaret()
428: .getDot());
429: }
430:
431: // public static String getELIdentifier () {
432: // EditorCookie e = getCurrentEditorCookie ();
433: // if (e == null) {
434: // return null;
435: // }
436: // JEditorPane ep = getCurrentEditor (e);
437: // if (ep == null) {
438: // return null;
439: // }
440: // return getELIdentifier (
441: // e.getDocument (),
442: // ep,
443: // ep.getCaret ().getDot ()
444: // );
445: // }
446:
447: public static boolean isScriptlet() {
448: EditorCookie e = getCurrentEditorCookie();
449: if (e == null) {
450: return false;
451: }
452: JEditorPane ep = getCurrentEditor(e);
453: if (ep == null) {
454: return false;
455: }
456: return isScriptlet(e.getDocument(), ep, ep.getCaret().getDot());
457: }
458:
459: }
|