KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jspparser > ContextUtil


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.jspparser;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileStateInvalidException;
29 import org.openide.filesystems.FileUtil;
30
31 /** Static utilities related to web context stuff - relative paths, relative objects etc.
32  *
33  * @author Petr Jiricka
34  */

35 public class ContextUtil {
36
37     /** Returns a message for a given throwable. Optionally includes the throwable
38      * stack trace in the message.
39      * @param throwable throwable for which to construct the message
40      * @param includeStackTrace whether to include a stack trace of the throwable in the message
41      * @return an appropriate message for the throwable
42      */

43     public static String JavaDoc getThrowableMessage(Throwable JavaDoc throwable,
44             boolean includeStackTrace) {
45         if (includeStackTrace) {
46             StringWriter JavaDoc swriter = new StringWriter JavaDoc();
47             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(swriter);
48             throwable.printStackTrace(pw);
49             pw.close();
50             return swriter.toString();
51         }
52         else {
53             return throwable.getMessage();
54         }
55     }
56     
57     /**********************************
58      * Copied over from WebModuleUtils.
59      **********************************
60      */

61     
62     /** Decides whether a given file is in the subtree defined by the given folder.
63      * Similar to <code>org.openide.filesystems.FileUtil.isParentOf (FileObject folder, FileObject fo)</code>,
64      * but also accepts the case that <code>fo == folder</code>
65      */

66     public static boolean isInSubTree(FileObject folder, FileObject fo) {
67         if (fo == folder) {
68             return true;
69         }
70         else return FileUtil.isParentOf(folder, fo);
71     }
72
73     /** Finds a relative resource path between rootFolder and relativeObject.
74      * @return relative path between rootFolder and relativeObject. The returned path
75      * never starts with a '/'. It never ends with a '/'.
76      * @exception IllegalArgumentException if relativeObject is not in rootFolder's tree.
77      */

78     public static String JavaDoc findRelativePath(FileObject rootFolder, FileObject relativeObject) {
79         String JavaDoc rfp = rootFolder.getPath();
80         String JavaDoc rop = relativeObject.getPath();
81         // check that they share the start of the path
82
if (!isInSubTree (rootFolder, relativeObject)) {
83             throw new IllegalArgumentException JavaDoc("" + rootFolder + " / " + relativeObject); // NOI18N
84
}
85         // now really return the result
86
String JavaDoc result = rop.substring(rfp.length());
87         if (result.startsWith("/")) { // NOI18N
88
result = result.substring(1);
89         }
90         return result;
91     }
92     
93     /** Finds a relative context path between rootFolder and relativeObject.
94      * Similar to <code>findRelativePath(FileObject, FileObject)</code>, only
95      * different slash '/' conventions.
96      * @return relative context path between rootFolder and relativeObject. The returned path
97      * always starts with a '/'. It ends with a '/' if the relative object is a directory.
98      * @exception IllegalArgumentException if relativeObject is not in rootFolder's tree.
99      */

100     public static String JavaDoc findRelativeContextPath(FileObject rootFolder, FileObject relativeObject) {
101         String JavaDoc result = "/" + findRelativePath(rootFolder, relativeObject); // NOI18N
102
return relativeObject.isFolder() ? (result + "/") : result; // NOI18N
103
}
104     
105     /** Finds a FileObject relative to a given root folder, with a given relative path.
106      * @param rootFolder the root folder
107      * @relativePath the relative path (not starting with a '/', delimited by '/')
108      * @return fileobject relative to the given root folder or null if not found.
109      * @exception IllegalArgumentException if relativeObject is not in rootFolder's tree.
110      */

111     public static FileObject findRelativeFileObject(FileObject rootFolder, String JavaDoc relativePath) {
112         if (relativePath.startsWith("/")) { // NOI18N
113
relativePath = relativePath.substring(1);
114         }
115         FileObject myObj = rootFolder;
116         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(relativePath, "/"); // NOI18N
117
while (myObj != null && st.hasMoreTokens()) {
118             myObj = myObj.getFileObject(st.nextToken());
119         }
120         return myObj;
121     }
122 }
123
Popular Tags