KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > providers > JavaSuggestionContext


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.tasklist.providers;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import org.openide.filesystems.FileObject;
28 import org.openide.ErrorManager;
29 import java.lang.reflect.Method JavaDoc;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataObjectNotFoundException;
32 import org.openide.util.Lookup;
33
34
35 /**
36  * Dedicated java source context that is faster than generic one.
37  *
38  * @author Petr Kuzel
39  */

40 final class JavaSuggestionContext {
41     private static boolean lookupAttempted = false;
42     private static Method JavaDoc org_netbeans_modules_java_Util_getFileEncoding;
43     private static Method JavaDoc org_netbeans_modules_java_Util_getContent;
44     
45     /**
46      * Search methods from o.n.m.java.Util
47      *
48      * @return true = methods found
49      */

50     private static boolean findMethods() {
51         if (!lookupAttempted) {
52             lookupAttempted = true;
53             ClassLoader JavaDoc systemClassLoader =
54                 (ClassLoader JavaDoc) Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
55             try {
56                 Class JavaDoc c = systemClassLoader.
57                         loadClass("org.netbeans.modules.java.Util"); // NOI18N
58
org_netbeans_modules_java_Util_getFileEncoding =
59                     c.getMethod("getFileEncoding", new Class JavaDoc[] {FileObject.class});
60                 org_netbeans_modules_java_Util_getContent =
61                     c.getMethod("getContent", new Class JavaDoc[] {FileObject.class, // NOI18N
62
Boolean.TYPE, Boolean.TYPE, String JavaDoc.class});
63             } catch (Exception JavaDoc e) {
64                 System.out.println("org.netbeans.modules.tasklist.providers" + // NOI18N
65
".JavaSuggestionContext: Methods not found"); // NOI18N
66
// ignore
67
}
68         }
69         
70         return org_netbeans_modules_java_Util_getFileEncoding != null &&
71             org_netbeans_modules_java_Util_getContent != null;
72     }
73     
74     /**
75      * @return null if the content cannot be found
76      */

77     static String JavaDoc getContent(FileObject fo) {
78         String JavaDoc result = null; // NOI18N
79
if (!findMethods()) {
80             try {
81                 char[] buf = new char[1024*64];
82                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
83                 Reader JavaDoc r = new InputStreamReader JavaDoc(new BufferedInputStream JavaDoc(fo.getInputStream()));
84                 int len;
85                 try {
86                     while (true) {
87                         len = r.read(buf);
88                         if (len == -1) break;
89                         sb.append(buf, 0, len);
90                     }
91                     result = sb.toString();
92                 } finally {
93                     r.close();
94                 }
95             } catch (IOException JavaDoc e) {
96                 ErrorManager.getDefault().notify(e);
97             }
98
99 // try {
100
// DataObject do_ = DataObject.find(fo);
101
// result = new SuggestionContext(do_).getCharSequence().toString();
102
// } catch (DataObjectNotFoundException e) {
103
// ErrorManager.getDefault().notify(e);
104
// }
105
} else {
106             try {
107                 String JavaDoc encoding = (String JavaDoc) org_netbeans_modules_java_Util_getFileEncoding.
108                     invoke(null, new Object JavaDoc[] {fo});
109
110                 char[] source = (char[]) org_netbeans_modules_java_Util_getContent.invoke(
111                     null,
112                     new Object JavaDoc[] {fo, Boolean.FALSE, Boolean.TRUE, encoding});
113                 result = new String JavaDoc(source);
114             } catch (InvocationTargetException JavaDoc e) {
115                 ErrorManager.getDefault().notify(e);
116             } catch (IllegalArgumentException JavaDoc e) {
117                 ErrorManager.getDefault().notify(e);
118             } catch (IllegalAccessException JavaDoc e) {
119                 ErrorManager.getDefault().notify(e);
120             }
121         }
122         return result;
123     }
124 }
125
Popular Tags