1 19 20 package org.netbeans.modules.web.core.syntax; 21 22 import java.util.logging.Level ; 23 import java.util.logging.Logger ; 24 import javax.swing.text.Document ; 25 import java.net.URLClassLoader ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.StringTokenizer ; 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 TAG_MIME_TYPE = "text/x-tag"; 41 42 45 public static String getContentLanguage() { 46 55 return "text/html"; } 57 58 59 60 63 public static String getScriptingLanguage() { 64 72 return "text/x-java"; } 74 75 public static JSPColoringData getJSPColoringData (Document 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 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 doc, FileObject fo, boolean successfulOnly, boolean preferCurrent) { 90 return getCachedParseResult(doc, fo, successfulOnly, preferCurrent, false); 91 } 92 93 public static URLClassLoader getModuleClassLoader(Document doc, FileObject fo) { 94 return JspContextInfo.getContextInfo (fo).getModuleClassLoader (doc, fo); 95 } 96 97 108 public static FileObject guessWebModuleRoot (Document doc, FileObject fo) { 109 return JspContextInfo.getContextInfo (fo).guessWebModuleRoot (doc, fo); 110 } 111 112 115 public static Map getTaglibMap(Document doc, FileObject fo) { 116 return JspContextInfo.getContextInfo (fo).getTaglibMap (doc, fo); 117 } 118 119 124 public static java.awt.Image getIcon(Document 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 140 public static String resolveRelativeURL(String relativeTo, String url) { 141 String result; 143 if (url.startsWith("/")) { result = "/"; url = url.substring(1); 146 } 147 else { 148 if ((relativeTo == null) || (!relativeTo.startsWith("/"))) throw new IllegalArgumentException (); 151 relativeTo = resolveRelativeURL(null, relativeTo); 152 int lastSlash = relativeTo.lastIndexOf('/'); 153 if (lastSlash == -1) 154 throw new IllegalArgumentException (); 155 result = relativeTo.substring(0, lastSlash + 1); 156 } 157 158 StringTokenizer st = new StringTokenizer (url, "/", true); while(st.hasMoreTokens()) { 161 String tok = st.nextToken(); 162 if (tok.equals("/")) { if (!result.endsWith("/")) result = result + "/"; } 167 else 168 if (tok.equals("")) ; else 171 if (tok.equals(".")) ; else 174 if (tok.equals("..")) { String withoutSlash = result.substring(0, result.length() - 1); 176 int ls = withoutSlash.lastIndexOf("/"); if (ls != -1) 178 result = withoutSlash.substring(0, ls + 1); 179 } 180 else { 181 result = result + tok; 183 } 184 } 186 return result; 188 } 189 190 public static String mapToString(Map m, String indent) { 192 StringBuffer sb = new StringBuffer (); 193 Iterator it = m.keySet().iterator(); 194 while (it.hasNext()) { 195 Object 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 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 218 public static String findRelativePath(FileObject rootFolder, FileObject relativeObject) { 219 String rfp = rootFolder.getPath(); 220 String rop = relativeObject.getPath(); 221 if (!isInSubTree (rootFolder, relativeObject)) { 223 throw new IllegalArgumentException ("" + rootFolder + " / " + relativeObject); } 225 String result = rop.substring(rfp.length()); 227 if (result.startsWith("/")) { result = result.substring(1); 229 } 230 return result; 231 } 232 233 237 238 245 public static String findRelativeContextPath(FileObject rootFolder, FileObject relativeObject) { 246 String result = "/" + findRelativePath(rootFolder, relativeObject); return relativeObject.isFolder() ? (result + "/") : result; } 249 250 256 public static FileObject findRelativeFileObject(FileObject rootFolder, String relativePath) { 257 if (relativePath.startsWith("/")) { relativePath = relativePath.substring(1); 259 } 260 FileObject myObj = rootFolder; 261 StringTokenizer st = new StringTokenizer (relativePath, "/"); while (myObj != null && st.hasMoreTokens()) { 263 myObj = myObj.getFileObject(st.nextToken()); 264 } 265 return myObj; 266 } 267 } 268 | Popular Tags |