KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > Util


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.i18n;
21
22 import org.openide.ErrorManager;
23 import org.openide.util.*;
24 import org.netbeans.api.project.Project;
25 import java.util.*;
26 import org.openide.nodes.Node;
27 import org.netbeans.api.java.classpath.ClassPath;
28 import org.netbeans.api.java.queries.SourceForBinaryQuery;
29 import org.openide.filesystems.FileObject;
30 import org.openide.loaders.DataObject;
31 import org.netbeans.api.project.FileOwnerQuery;
32
33 /**
34  * Bundle access, ...
35  *
36  * @author Petr Kuzel
37  */

38 public class Util {
39
40     public static String JavaDoc getString(String JavaDoc key) {
41         return NbBundle.getMessage(Util.class, key);
42     }
43     
44     public static char getChar(String JavaDoc key) {
45         return getString(key).charAt(0);
46     }
47     
48     /**
49      * Write the exception into log.
50      */

51     public static void debug(Throwable JavaDoc t) {
52         ErrorManager err = ErrorManager.getDefault();
53         err.notify(err.INFORMATIONAL, t);
54     }
55
56     /**
57      * Write annotated exception into log.
58      */

59     public static void debug(String JavaDoc annotation, Throwable JavaDoc t) {
60         ErrorManager err = ErrorManager.getDefault();
61         err.annotate(t, err.INFORMATIONAL, annotation, null, null, null);
62         err.notify(err.INFORMATIONAL, t);
63     }
64     
65     public static Project getProjectFor(DataObject dobj) {
66       Project prj = null;
67       FileObject fo = dobj.getPrimaryFile();
68       return FileOwnerQuery.getOwner(fo);
69     }
70
71   public static Project getProjectFor(Node [] activatedNodes) {
72     Project project = null;
73
74     if (activatedNodes.length > 0) {
75       DataObject dataObject = (DataObject)activatedNodes[0].getCookie(DataObject.class);
76       if(dataObject != null && dataObject.getPrimaryFile() != null)
77     project = FileOwnerQuery.getOwner(dataObject.getPrimaryFile());
78     }
79     return project;
80   }
81
82     /**
83      * Gets classpath that contains the given resource bundle.
84      * In addition to the bundle file, a source must be given that
85      * will access the resource at run-time.
86      */

87     public static ClassPath getExecClassPath(FileObject srcFile, FileObject resFile) {
88         // try EXECUTE class-path first
89
ClassPath ecp = ClassPath.getClassPath( srcFile, ClassPath.EXECUTE );
90         if ((ecp != null) && (ecp.getResourceName( resFile, '.',false) != null))
91             return ecp;
92
93
94         // if not directly on EXECUTE, might be on SOURCE
95
ClassPath scp = ClassPath.getClassPath( srcFile, ClassPath.SOURCE);
96         // try to find the resource on source class path
97
if ((scp != null) && (scp.getResourceName( resFile, '.',false) != null))
98             return scp;
99
100         // now try resource owner
101
ClassPath rcp = ClassPath.getClassPath( resFile, ClassPath.SOURCE);
102         // try to find the resource on source class path
103
if ((rcp!=null) && (rcp.getResourceName( resFile, '.',false) != null))
104                 return rcp;
105         
106
107         return null;
108     }
109         
110     /**
111      * Tries to find the bundle either in sources or in execution
112      * classpath.
113      */

114     public static FileObject getResource(FileObject srcFile, String JavaDoc bundleName) {
115         // try to find it in sources of the same project
116
ClassPath scp = ClassPath.getClassPath( srcFile, ClassPath.SOURCE);
117         if (scp != null) {
118             FileObject ret = scp.findResource(bundleName);
119             if (ret != null) return ret;
120         }
121
122         // try to find in sources of execution classpath
123
ClassPath ecp = ClassPath.getClassPath( srcFile, ClassPath.EXECUTE);
124         Iterator it = ecp.entries().iterator();
125         while (it.hasNext()) {
126             ClassPath.Entry e = (ClassPath.Entry)it.next();
127             SourceForBinaryQuery.Result r = SourceForBinaryQuery.findSourceRoots(e.getURL());
128             FileObject[] sourceRoots = r.getRoots();
129             for (int i=0; i < sourceRoots.length; i++) {
130                 // try to find the bundle under this source root
131
ClassPath cp = ClassPath.getClassPath(sourceRoots[i], ClassPath.SOURCE);
132                 if (cp != null) {
133                     FileObject ret = cp.findResource(bundleName);
134                     if (ret != null)
135                         return ret;
136                 }
137             }
138         }
139
140         return null;
141     }
142
143
144     /**
145      * Inverse to the previous method - finds name for the give
146      * resource bundle. It is equivalent but more effective to use
147      * this method instead of getExecClassPath(...).getResourceName(...) .
148      */

149     public static String JavaDoc getResourceName(FileObject srcFile, FileObject resFile, char separator, boolean bpar) {
150         // try SOURCE class-path first
151
ClassPath ecp = ClassPath.getClassPath( srcFile, ClassPath.EXECUTE );
152         if (ecp!=null) {
153             String JavaDoc ret = ecp.getResourceName( resFile, separator, bpar);
154             if (ret != null) return ret;
155         }
156
157         ClassPath scp = ClassPath.getClassPath( srcFile, ClassPath.SOURCE );
158         if (scp!= null) {
159             String JavaDoc ret = scp.getResourceName( resFile, separator, bpar);
160             if (ret!=null) return ret;
161         }
162
163         ClassPath rcp = ClassPath.getClassPath( resFile, ClassPath.SOURCE );
164         if (rcp != null) {
165             String JavaDoc ret = rcp.getResourceName( resFile, separator, bpar);
166             if (ret!=null) return ret;
167         }
168         
169         return null;
170         
171     }
172
173     public static boolean isNbBundleAvailable(DataObject srcDataObject) {
174         // is there a good way to recognize that NbBundle is available?
175
// - execution CP may not work if everything is cleaned
176
// - looking for NbBundle.java in sources of execution CP roots is expensive
177
// - checking project impl. class name is ugly
178
// - don't know how to check if there is "org.openide.util" module
179
ClassPath classPath = ClassPath.getClassPath(srcDataObject.getPrimaryFile(), ClassPath.EXECUTE);
180         if (classPath != null && classPath.findResource("org/openide/util/NbBundle.class") != null) // NOI18N
181
return true;
182
183         // hack: check project impl. class name
184
Project p = FileOwnerQuery.getOwner(srcDataObject.getPrimaryFile());
185         if (p != null && p.getClass().getName().startsWith("org.netbeans.modules.apisupport.") // NOI18N
186
&& p.getClass().getName().endsWith("Project")) // NOI18N
187
return true;
188
189         return false;
190     }
191 }
192
Popular Tags