KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > JspUtils


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.core.syntax;
21
22 import java.util.logging.Level JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import java.net.URLClassLoader JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
32 import org.netbeans.modules.web.core.syntax.spi.JSPColoringData;
33 import org.netbeans.modules.web.core.syntax.spi.JspContextInfo;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.DataObjectNotFoundException;
36
37 public class JspUtils {
38
39     public static final String JavaDoc TAG_MIME_TYPE = "text/x-tag"; // NOI18N
40

41     
42     /** Returns the MIME type of the content language for this page set in this file's attributes.
43      * If nothing is set, defaults to 'text/html'.
44      */

45     public static String JavaDoc getContentLanguage() {
46         /*try {
47             
48             String contentLanguage = (String)getPrimaryFile ().getAttribute (EA_CONTENT_LANGUAGE);
49             if (contentLanguage != null) {
50                 return contentLanguage;
51             }
52         } catch (Exception ex) {
53             // null pointer or IOException
54         }*/

55         return "text/html"; // NOI18N
56
}
57
58   
59
60     /** Returns the MIME type of the scripting language for this page set in this file's attributes.
61      * If nothing is set, defaults to 'text/x-java'.
62      */

63     public static String JavaDoc getScriptingLanguage() {
64         /*try {
65             String scriptingLanguage = (String)getPrimaryFile ().getAttribute (EA_SCRIPTING_LANGUAGE);
66             if (scriptingLanguage != null) {
67                 return scriptingLanguage;
68             }
69         } catch (Exception ex) {
70             // null pointer or IOException
71         }*/

72         return "text/x-java"; // NOI18N
73
}
74     
75     public static JSPColoringData getJSPColoringData (Document JavaDoc doc, FileObject fo) {
76         JSPColoringData result = null;
77         if (doc != null && fo != null && fo.isValid()){
78             JspContextInfo context = JspContextInfo.getContextInfo (fo);
79             if (context != null)
80                 result = context.getJSPColoringData (doc, fo);
81         }
82         return result;
83     }
84     
85     public static JspParserAPI.ParseResult getCachedParseResult(Document JavaDoc doc, FileObject fo, boolean successfulOnly, boolean preferCurrent, boolean forceParse) {
86         return JspContextInfo.getContextInfo (fo).getCachedParseResult (doc, fo, successfulOnly, preferCurrent);
87     }
88     
89     public static JspParserAPI.ParseResult getCachedParseResult(Document JavaDoc doc, FileObject fo, boolean successfulOnly, boolean preferCurrent) {
90         return getCachedParseResult(doc, fo, successfulOnly, preferCurrent, false);
91     }
92     
93     public static URLClassLoader JavaDoc getModuleClassLoader(Document JavaDoc doc, FileObject fo) {
94         return JspContextInfo.getContextInfo (fo).getModuleClassLoader (doc, fo);
95     }
96     
97     /** Returns the root of the web module containing the given file object.
98      * If the resource belongs to the subtree of the project's web module,
99      * returns this module's document base directory.
100      * Otherwise (or if the project parameter is null), it checks for the WEB-INF directory,
101      * and determines the root accordingly. If WEB-INF is not found, returns null.
102      *
103      * @param fo the resource for which to find the web module root
104      * @param doc document in which is fileobject editted.
105      * @return the root of the web module, or null if a directory containing WEB-INF
106      * is not on the path from resource to the root
107      */

108     public static FileObject guessWebModuleRoot (Document JavaDoc doc, FileObject fo) {
109         return JspContextInfo.getContextInfo (fo).guessWebModuleRoot (doc, fo);
110     }
111     
112     /** Returns the taglib map as returned by the parser, taking data from the editor as parameters.
113      * Returns null in case of a failure (exception, no web module, no parser etc.)
114      */

115     public static Map JavaDoc getTaglibMap(Document JavaDoc doc, FileObject fo) {
116         return JspContextInfo.getContextInfo (fo).getTaglibMap (doc, fo);
117     }
118     
119     /** This method returns an image, which is displayed for the FileObject in the explorer.
120      * @param doc This is the documet, in which the icon will be used (for exmaple for completion).
121      * @param fo file object for which the icon is looking for
122      * @return an Image which is dislayed in the explorer for the file.
123      */

124     public static java.awt.Image JavaDoc getIcon(Document JavaDoc doc, FileObject fo){
125         try {
126             return DataObject.find(fo).getNodeDelegate().getIcon(java.beans.BeanInfo.ICON_COLOR_16x16);
127         }catch(DataObjectNotFoundException e) {
128             Logger.getLogger(JspUtils.class.getName()).log(Level.INFO, "Cannot find icon for " + fo.getNameExt(), e);
129         }
130         return null;
131     }
132     
133     /** Returns an absolute context URL (starting with '/') for a relative URL and base URL.
134     * @param relativeTo url to which the relative URL is related. Treated as directory iff
135     * ends with '/'
136     * @param url the relative URL by RFC 2396
137     * @exception IllegalArgumentException if url is not absolute and relativeTo
138     * can not be related to, or if url is intended to be a directory
139     */

140     public static String JavaDoc resolveRelativeURL(String JavaDoc relativeTo, String JavaDoc url) {
141         //System.out.println("- resolving " + url + " relative to " + relativeTo);
142
String JavaDoc result;
143         if (url.startsWith("/")) { // NOI18N
144
result = "/"; // NOI18N
145
url = url.substring(1);
146         }
147         else {
148             // canonize relativeTo
149
if ((relativeTo == null) || (!relativeTo.startsWith("/"))) // NOI18N
150
throw new IllegalArgumentException JavaDoc();
151             relativeTo = resolveRelativeURL(null, relativeTo);
152             int lastSlash = relativeTo.lastIndexOf('/');
153             if (lastSlash == -1)
154                 throw new IllegalArgumentException JavaDoc();
155             result = relativeTo.substring(0, lastSlash + 1);
156         }
157
158         // now url does not start with '/' and result starts with '/' and ends with '/'
159
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(url, "/", true); // NOI18N
160
while(st.hasMoreTokens()) {
161             String JavaDoc tok = st.nextToken();
162             //System.out.println("token : \"" + tok + "\""); // NOI18N
163
if (tok.equals("/")) { // NOI18N
164
if (!result.endsWith("/")) // NOI18N
165
result = result + "/"; // NOI18N
166
}
167             else
168                 if (tok.equals("")) // NOI18N
169
; // do nohing
170
else
171                     if (tok.equals(".")) // NOI18N
172
; // do nohing
173
else
174                         if (tok.equals("..")) { // NOI18N
175
String JavaDoc withoutSlash = result.substring(0, result.length() - 1);
176                             int ls = withoutSlash.lastIndexOf("/"); // NOI18N
177
if (ls != -1)
178                                 result = withoutSlash.substring(0, ls + 1);
179                         }
180                         else {
181                             // some file
182
result = result + tok;
183                         }
184             //System.out.println("result : " + result); // NOI18N
185
}
186         //System.out.println("- resolved to " + result);
187
return result;
188     }
189     
190     // helper methods for help implement toString()
191
public static String JavaDoc mapToString(Map JavaDoc m, String JavaDoc indent) {
192         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
193         Iterator JavaDoc it = m.keySet().iterator();
194         while (it.hasNext()) {
195             Object JavaDoc key = it.next();
196             sb.append(indent).append(key).append(" -> ").append(m.get(key)).append("\n");
197         }
198         return sb.toString();
199     }
200     
201     
202     /** Decides whether a given file is in the subtree defined by the given folder.
203      * Similar to <code>org.openide.filesystems.FileUtil.isParentOf (FileObject folder, FileObject fo)</code>,
204      * but also accepts the case that <code>fo == folder</code>
205      */

206     public static boolean isInSubTree(FileObject folder, FileObject fo) {
207         if (fo == folder) {
208             return true;
209         }
210         else return FileUtil.isParentOf(folder, fo);
211     }
212
213     /** Finds a relative resource path between rootFolder and relativeObject.
214      * @return relative path between rootFolder and relativeObject. The returned path
215      * never starts with a '/'. It never ends with a '/'.
216      * @exception IllegalArgumentException if relativeObject is not in rootFolder's tree.
217      */

218     public static String JavaDoc findRelativePath(FileObject rootFolder, FileObject relativeObject) {
219         String JavaDoc rfp = rootFolder.getPath();
220         String JavaDoc rop = relativeObject.getPath();
221         // check that they share the start of the path
222
if (!isInSubTree (rootFolder, relativeObject)) {
223             throw new IllegalArgumentException JavaDoc("" + rootFolder + " / " + relativeObject); // NOI18N
224
}
225         // now really return the result
226
String JavaDoc result = rop.substring(rfp.length());
227         if (result.startsWith("/")) { // NOI18N
228
result = result.substring(1);
229         }
230         return result;
231     }
232     
233     /**********************************
234      * Copied over from WebModuleUtils.
235      **********************************
236      */

237     
238     /** Finds a relative context path between rootFolder and relativeObject.
239      * Similar to <code>findRelativePath(FileObject, FileObject)</code>, only
240      * different slash '/' conventions.
241      * @return relative context path between rootFolder and relativeObject. The returned path
242      * always starts with a '/'. It ends with a '/' if the relative object is a directory.
243      * @exception IllegalArgumentException if relativeObject is not in rootFolder's tree.
244      */

245     public static String JavaDoc findRelativeContextPath(FileObject rootFolder, FileObject relativeObject) {
246         String JavaDoc result = "/" + findRelativePath(rootFolder, relativeObject); // NOI18N
247
return relativeObject.isFolder() ? (result + "/") : result; // NOI18N
248
}
249     
250     /** Finds a FileObject relative to a given root folder, with a given relative path.
251      * @param rootFolder the root folder
252      * @relativePath the relative path (not starting with a '/', delimited by '/')
253      * @return fileobject relative to the given root folder or null if not found.
254      * @exception IllegalArgumentException if relativeObject is not in rootFolder's tree.
255      */

256     public static FileObject findRelativeFileObject(FileObject rootFolder, String JavaDoc relativePath) {
257         if (relativePath.startsWith("/")) { // NOI18N
258
relativePath = relativePath.substring(1);
259         }
260         FileObject myObj = rootFolder;
261         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(relativePath, "/"); // NOI18N
262
while (myObj != null && st.hasMoreTokens()) {
263             myObj = myObj.getFileObject(st.nextToken());
264         }
265         return myObj;
266     }
267 }
268
Popular Tags