1 19 20 package org.netbeans.modules.web.jspparser; 21 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.util.StringTokenizer ; 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 35 public class ContextUtil { 36 37 43 public static String getThrowableMessage(Throwable throwable, 44 boolean includeStackTrace) { 45 if (includeStackTrace) { 46 StringWriter swriter = new StringWriter (); 47 PrintWriter pw = new PrintWriter (swriter); 48 throwable.printStackTrace(pw); 49 pw.close(); 50 return swriter.toString(); 51 } 52 else { 53 return throwable.getMessage(); 54 } 55 } 56 57 61 62 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 78 public static String findRelativePath(FileObject rootFolder, FileObject relativeObject) { 79 String rfp = rootFolder.getPath(); 80 String rop = relativeObject.getPath(); 81 if (!isInSubTree (rootFolder, relativeObject)) { 83 throw new IllegalArgumentException ("" + rootFolder + " / " + relativeObject); } 85 String result = rop.substring(rfp.length()); 87 if (result.startsWith("/")) { result = result.substring(1); 89 } 90 return result; 91 } 92 93 100 public static String findRelativeContextPath(FileObject rootFolder, FileObject relativeObject) { 101 String result = "/" + findRelativePath(rootFolder, relativeObject); return relativeObject.isFolder() ? (result + "/") : result; } 104 105 111 public static FileObject findRelativeFileObject(FileObject rootFolder, String relativePath) { 112 if (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); 114 } 115 FileObject myObj = rootFolder; 116 StringTokenizer st = new StringTokenizer (relativePath, "/"); while (myObj != null && st.hasMoreTokens()) { 118 myObj = myObj.getFileObject(st.nextToken()); 119 } 120 return myObj; 121 } 122 } 123 | Popular Tags |